6bdeac7bfbc73375deddd8194953af08062d5db3
[mir.git] / source / mir / misc / StringUtil.java
1 /*
2  * Copyright (C) 2005 The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
6  * Mir is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Mir is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Mir; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with  any library licensed under the Apache Software License.
22  * You must obey the GNU General Public License in all respects for all of the code used
23  * other than the above mentioned libraries.  If you modify this file, you may extend this
24  * exception to your version of the file, but you are not obligated to do so.
25  * If you do not wish to do so, delete this exception statement from your version.
26  */
27 package  mir.misc;
28
29 import org.apache.oro.text.regex.Pattern;
30 import org.apache.oro.text.regex.Perl5Matcher;
31 import org.apache.oro.text.regex.Perl5Substitution;
32 import org.apache.oro.text.regex.Util;
33
34 import java.text.NumberFormat;
35 import java.util.Calendar;
36 import java.util.Date;
37 import java.util.GregorianCalendar;
38 import java.util.TimeZone;
39
40 /**
41  */
42 public final class StringUtil {
43
44   private static TimeZone UTC = TimeZone.getTimeZone("UTC");
45
46   private StringUtil() { }  // this avoids contruction
47
48   /**
49    * Formats a number with the specified minimum and maximum number of digits.
50    **/
51   public static synchronized String zeroPaddingNumber(long value, int minDigits,
52                                                       int maxDigits)
53   {
54     NumberFormat numberFormat = NumberFormat.getInstance();
55     numberFormat.setMinimumIntegerDigits(minDigits);
56     numberFormat.setMaximumIntegerDigits(maxDigits);
57     return numberFormat.format(value);
58   }
59
60   /**
61    * Wandelt Datum in einen 8-ziffrigen String um (yyyymmdd)
62    * @param theDate
63    * @return 8-ziffriger String (yyyymmdd)
64    */
65
66   public static String date2webdbDate (GregorianCalendar theDate) {
67     StringBuffer webdbDate = new StringBuffer();
68     webdbDate.append(String.valueOf(theDate.get(Calendar.YEAR)));
69     webdbDate.append(pad2(theDate.get(Calendar.MONTH) + 1));
70     webdbDate.append(pad2(theDate.get(Calendar.DATE)));
71
72     return  webdbDate.toString();
73   }
74
75   /**
76    * Return a http://www.w3.org/TR/NOTE-datetime formatted date (yyyy-mm-ddThh:mm:ssTZ)
77    * @param theDate
78    * @return w3approved datetime
79    */
80
81   public static String date2w3DateTime (GregorianCalendar theDate) {
82     StringBuffer webdbDate = new StringBuffer();
83     webdbDate.append(String.valueOf(theDate.get(Calendar.YEAR)));
84     webdbDate.append("-");
85     webdbDate.append(pad2(theDate.get(Calendar.MONTH) + 1));
86     webdbDate.append("-");
87     webdbDate.append(pad2(theDate.get(Calendar.DATE)));
88     webdbDate.append("T");
89     webdbDate.append(pad2(theDate.get(Calendar.HOUR_OF_DAY)));
90     webdbDate.append(":");
91     webdbDate.append(pad2(theDate.get(Calendar.MINUTE)));
92     webdbDate.append(":");
93     webdbDate.append(pad2(theDate.get(Calendar.SECOND)));
94     //assumes you are an hour-multiple away from UTC....
95     int offset=(theDate.get(Calendar.ZONE_OFFSET)/(60*60*1000));
96     if (offset < 0){
97       webdbDate.append("-");
98     }
99     else{
100       webdbDate.append("+");
101     }
102     webdbDate.append(pad2(Math.abs(offset)));
103     webdbDate.append(":00");
104     return  webdbDate.toString();
105   }
106
107   /**
108    * wandelt Calendar in dd.mm.yyyy / hh.mm um
109    * @param theDate
110    * @return String mit (dd.mm.yyyy / hh.mm um)
111    */
112   public static String date2readableDateTime (GregorianCalendar theDate) {
113     String readable = "";
114     int hour;
115     readable += pad2(theDate.get(Calendar.DATE));
116     readable += "." + pad2(theDate.get(Calendar.MONTH) + 1);
117     readable += "." + String.valueOf(theDate.get(Calendar.YEAR));
118     hour = theDate.get(Calendar.HOUR);
119     if (theDate.get(Calendar.AM_PM) == Calendar.PM)
120       hour += 12;
121     readable += " / " + pad2(hour);
122     readable += ":" + pad2(theDate.get(Calendar.MINUTE));
123     return  readable;
124   }
125
126   /**
127    * wandelt eine Datum in einen 8-buchstabigen String, der durch <code>/</code>
128    * getrennt ist.
129    *
130    * @param webdbDate
131    * @return String mit <code>/yyyy/mm/dd</code>
132    */
133   public static String webdbDate2path (String webdbDate) {
134     StringBuffer path = new StringBuffer();
135     path.append("/").append(webdbDate.substring(0, 4));
136     path.append("/").append(webdbDate.substring(4, 6));
137     path.append("/");
138     //who did this?
139     //path.append("/").append(webdbDate.substring(6, 8));
140     return  path.toString();
141   }
142
143   /**
144    * L?scht <code>/</code> am Ende des Strings, falls vorhanden
145    * @param path
146    * @return String ohne <code>/</code> am Ende
147    */
148   public static String removeSlash (String path) {
149     return  path.length() > 1 && path.endsWith("/") ? path.substring(0, path.length()
150         - 1) : path;
151   }
152
153   /**
154    * formatiert eine Zahl (0-99) zweistellig (z.B. 5 -> 05)
155    * @return zwistellige Zahl
156    */
157   public static String pad2 (int number) {
158     return  number < 10 ? "0" + number : String.valueOf(number);
159   }
160
161   /**
162    * formatiert eine Zahl (0-999) dreistellig (z.B. 7 -> 007)
163    *
164    * @return 3-stellige Zahl
165    */
166   public static String pad3 (int number) {
167     return  number < 10 ? "00" + number : number < 100 ? "0" + number : String.valueOf(number);
168   }
169
170   /**
171    * Liefert Default-Wert def zur?ck, wenn String <code>s</code>
172    * kein Integer ist.
173    *
174    * @param s
175    * @param def
176    * @return geparster int aus s oder def
177    */
178   public static int parseInt(String s, int def) {
179     if (s == null) return def;
180     try {
181       return Integer.parseInt(s);
182     } catch (NumberFormatException e) {
183       return def;
184     }
185   }
186
187
188   /**
189    * Converts mir's horrible internal date format (yyyy-MM-dd HH:mm:ss+zz) into a java Date
190    *
191    * @param anInternalDate
192    */
193   public static Date convertMirInternalDateToDate(String anInternalDate) {
194     Calendar calendar = new GregorianCalendar();
195
196     int year;
197     int month;
198     int day;
199     int hours;
200     int minutes;
201     int seconds;
202     int timezoneOffset;
203
204     year = Integer.parseInt(anInternalDate.substring(0,4));
205     month = Integer.parseInt(anInternalDate.substring(5,7));
206     day = Integer.parseInt(anInternalDate.substring(8,10));
207     hours = Integer.parseInt(anInternalDate.substring(11,13));
208     minutes = Integer.parseInt(anInternalDate.substring(14,16));
209     seconds = Integer.parseInt(anInternalDate.substring(17,19));
210
211     timezoneOffset = Integer.parseInt(anInternalDate.substring(20,22));
212     if (anInternalDate.charAt(19) == '-') {
213       timezoneOffset = -timezoneOffset;
214     }
215
216     calendar.setTimeZone(UTC);
217     calendar.set(year, month-1, day, hours, minutes, seconds);
218     calendar.add(Calendar.HOUR, -timezoneOffset);
219
220     return calendar.getTime();
221   }
222
223   public String regexpReplace(String anInput, Pattern anExpression, String aReplacement) {
224     Perl5Matcher matcher = new Perl5Matcher();
225
226     return Util.substitute(
227       matcher, anExpression,
228       new Perl5Substitution(aReplacement), anInput,
229       Util.SUBSTITUTE_ALL);
230   }
231
232
233
234 }
235