469c1abbee73be4d43a087b90bce3336bef45393
[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 import org.apache.oro.text.regex.Perl5Compiler;
34
35 import java.text.NumberFormat;
36 import java.util.Calendar;
37 import java.util.Date;
38 import java.util.GregorianCalendar;
39 import java.util.TimeZone;
40
41 /**
42  */
43 public final class StringUtil {
44
45   private static TimeZone UTC = TimeZone.getTimeZone("UTC");
46
47   private StringUtil() { }  // this avoids contruction
48
49   /**
50    * Formats a number with the specified minimum and maximum number of digits.
51    **/
52   public static synchronized String zeroPaddingNumber(long value, int minDigits,
53                                                       int maxDigits)
54   {
55     NumberFormat numberFormat = NumberFormat.getInstance();
56     numberFormat.setMinimumIntegerDigits(minDigits);
57     numberFormat.setMaximumIntegerDigits(maxDigits);
58     return numberFormat.format(value);
59   }
60
61   /**
62    * Wandelt Datum in einen 8-ziffrigen String um (yyyymmdd)
63    * @param theDate
64    * @return 8-ziffriger String (yyyymmdd)
65    */
66
67   public static String date2webdbDate (GregorianCalendar theDate) {
68     StringBuffer webdbDate = new StringBuffer();
69     webdbDate.append(String.valueOf(theDate.get(Calendar.YEAR)));
70     webdbDate.append(pad2(theDate.get(Calendar.MONTH) + 1));
71     webdbDate.append(pad2(theDate.get(Calendar.DATE)));
72
73     return  webdbDate.toString();
74   }
75
76   /**
77    * Return a http://www.w3.org/TR/NOTE-datetime formatted date (yyyy-mm-ddThh:mm:ssTZ)
78    * @param theDate
79    * @return w3approved datetime
80    */
81
82   public static String date2w3DateTime (GregorianCalendar theDate) {
83     StringBuffer webdbDate = new StringBuffer();
84     webdbDate.append(String.valueOf(theDate.get(Calendar.YEAR)));
85     webdbDate.append("-");
86     webdbDate.append(pad2(theDate.get(Calendar.MONTH) + 1));
87     webdbDate.append("-");
88     webdbDate.append(pad2(theDate.get(Calendar.DATE)));
89     webdbDate.append("T");
90     webdbDate.append(pad2(theDate.get(Calendar.HOUR_OF_DAY)));
91     webdbDate.append(":");
92     webdbDate.append(pad2(theDate.get(Calendar.MINUTE)));
93     webdbDate.append(":");
94     webdbDate.append(pad2(theDate.get(Calendar.SECOND)));
95     //assumes you are an hour-multiple away from UTC....
96     int offset=(theDate.get(Calendar.ZONE_OFFSET)/(60*60*1000));
97     if (offset < 0){
98       webdbDate.append("-");
99     }
100     else{
101       webdbDate.append("+");
102     }
103     webdbDate.append(pad2(Math.abs(offset)));
104     webdbDate.append(":00");
105     return  webdbDate.toString();
106   }
107
108   /**
109    * wandelt Calendar in dd.mm.yyyy / hh.mm um
110    * @param theDate
111    * @return String mit (dd.mm.yyyy / hh.mm um)
112    */
113   public static String date2readableDateTime (GregorianCalendar theDate) {
114     String readable = "";
115     int hour;
116     readable += pad2(theDate.get(Calendar.DATE));
117     readable += "." + pad2(theDate.get(Calendar.MONTH) + 1);
118     readable += "." + String.valueOf(theDate.get(Calendar.YEAR));
119     hour = theDate.get(Calendar.HOUR);
120     if (theDate.get(Calendar.AM_PM) == Calendar.PM)
121       hour += 12;
122     readable += " / " + pad2(hour);
123     readable += ":" + pad2(theDate.get(Calendar.MINUTE));
124     return  readable;
125   }
126
127   /**
128    * wandelt eine Datum in einen 8-buchstabigen String, der durch <code>/</code>
129    * getrennt ist.
130    *
131    * @param webdbDate
132    * @return String mit <code>/yyyy/mm/dd</code>
133    */
134   public static String webdbDate2path (String webdbDate) {
135     StringBuffer path = new StringBuffer();
136     path.append("/").append(webdbDate.substring(0, 4));
137     path.append("/").append(webdbDate.substring(4, 6));
138     path.append("/");
139     //who did this?
140     //path.append("/").append(webdbDate.substring(6, 8));
141     return  path.toString();
142   }
143
144   /**
145    * L?scht <code>/</code> am Ende des Strings, falls vorhanden
146    * @param path
147    * @return String ohne <code>/</code> am Ende
148    */
149   public static String removeSlash (String path) {
150     return  path.length() > 1 && path.endsWith("/") ? path.substring(0, path.length()
151         - 1) : path;
152   }
153
154   /**
155    * formatiert eine Zahl (0-99) zweistellig (z.B. 5 -> 05)
156    * @return zwistellige Zahl
157    */
158   public static String pad2 (int number) {
159     return  number < 10 ? "0" + number : String.valueOf(number);
160   }
161
162   /**
163    * formatiert eine Zahl (0-999) dreistellig (z.B. 7 -> 007)
164    *
165    * @return 3-stellige Zahl
166    */
167   public static String pad3 (int number) {
168     return  number < 10 ? "00" + number : number < 100 ? "0" + number : String.valueOf(number);
169   }
170
171   /**
172    * Liefert Default-Wert def zur?ck, wenn String <code>s</code>
173    * kein Integer ist.
174    *
175    * @param s
176    * @param def
177    * @return geparster int aus s oder def
178    */
179   public static int parseInt(String s, int def) {
180     if (s == null) return def;
181     try {
182       return Integer.parseInt(s);
183     } catch (NumberFormatException e) {
184       return def;
185     }
186   }
187
188
189   /**
190    * Converts mir's horrible internal date format (yyyy-MM-dd HH:mm:ss+zz) into a java Date
191    *
192    * @param anInternalDate
193    */
194   public static Date convertMirInternalDateToDate(String anInternalDate) {
195     Calendar calendar = new GregorianCalendar();
196
197     int year;
198     int month;
199     int day;
200     int hours;
201     int minutes;
202     int seconds;
203     int timezoneOffset;
204
205     year = Integer.parseInt(anInternalDate.substring(0,4));
206     month = Integer.parseInt(anInternalDate.substring(5,7));
207     day = Integer.parseInt(anInternalDate.substring(8,10));
208     hours = Integer.parseInt(anInternalDate.substring(11,13));
209     minutes = Integer.parseInt(anInternalDate.substring(14,16));
210     seconds = Integer.parseInt(anInternalDate.substring(17,19));
211
212     timezoneOffset = Integer.parseInt(anInternalDate.substring(20,22));
213     if (anInternalDate.charAt(19) == '-') {
214       timezoneOffset = -timezoneOffset;
215     }
216
217     calendar.setTimeZone(UTC);
218     calendar.set(year, month-1, day, hours, minutes, seconds);
219     calendar.add(Calendar.HOUR, -timezoneOffset);
220
221     return calendar.getTime();
222   }
223
224   public static String regexpReplace(String anInput, String anExpression, String aReplacement) throws Exception {
225     Perl5Matcher matcher = new Perl5Matcher();
226
227     return Util.substitute(
228       matcher, new Perl5Compiler().compile(anExpression),
229       new Perl5Substitution(aReplacement), anInput,
230       Util.SUBSTITUTE_ALL);
231   }
232
233
234
235 }
236