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