2 * Copyright (C) 2005 The Mir-coders group
4 * This file is part of Mir.
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.
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.
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
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.
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;
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;
42 import mir.util.UtilFailure;
46 public final class StringUtil {
48 private static TimeZone UTC = TimeZone.getTimeZone("UTC");
50 private StringUtil() { } // this avoids contruction
53 * Formats a number with the specified minimum and maximum number of digits.
55 public static synchronized String zeroPaddingNumber(long value, int minDigits,
58 NumberFormat numberFormat = NumberFormat.getInstance();
59 numberFormat.setMinimumIntegerDigits(minDigits);
60 numberFormat.setMaximumIntegerDigits(maxDigits);
61 return numberFormat.format(value);
65 * Wandelt Datum in einen 8-ziffrigen String um (yyyymmdd)
67 * @return 8-ziffriger String (yyyymmdd)
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)));
76 return webdbDate.toString();
80 * Return a http://www.w3.org/TR/NOTE-datetime formatted date (yyyy-mm-ddThh:mm:ssTZ)
82 * @return w3approved datetime
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));
101 webdbDate.append("-");
104 webdbDate.append("+");
106 webdbDate.append(pad2(Math.abs(offset)));
107 webdbDate.append(":00");
108 return webdbDate.toString();
112 * wandelt Calendar in dd.mm.yyyy / hh.mm um
114 * @return String mit (dd.mm.yyyy / hh.mm um)
116 public static String date2readableDateTime (GregorianCalendar theDate) {
117 String readable = "";
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)
125 readable += " / " + pad2(hour);
126 readable += ":" + pad2(theDate.get(Calendar.MINUTE));
131 * wandelt eine Datum in einen 8-buchstabigen String, der durch <code>/</code>
135 * @return String mit <code>/yyyy/mm/dd</code>
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));
143 //path.append("/").append(webdbDate.substring(6, 8));
144 return path.toString();
148 * L?scht <code>/</code> am Ende des Strings, falls vorhanden
150 * @return String ohne <code>/</code> am Ende
152 public static String removeSlash (String path) {
153 return path.length() > 1 && path.endsWith("/") ? path.substring(0, path.length()
158 * formatiert eine Zahl (0-99) zweistellig (z.B. 5 -> 05)
159 * @return zwistellige Zahl
161 public static String pad2 (int number) {
162 return number < 10 ? "0" + number : String.valueOf(number);
166 * formatiert eine Zahl (0-999) dreistellig (z.B. 7 -> 007)
168 * @return 3-stellige Zahl
170 public static String pad3 (int number) {
171 return number < 10 ? "00" + number : number < 100 ? "0" + number : String.valueOf(number);
175 * Liefert Default-Wert def zur?ck, wenn String <code>s</code>
180 * @return geparster int aus s oder def
182 public static int parseInt(String s, int def) {
183 if (s == null) return def;
185 return Integer.parseInt(s);
186 } catch (NumberFormatException e) {
193 * Converts mir's horrible internal date format (yyyy-MM-dd HH:mm:ss+zz) into a java Date
195 * @param anInternalDate
197 public static Date convertMirInternalDateToDate(String anInternalDate) {
198 Calendar calendar = new GregorianCalendar();
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));
215 timezoneOffset = Integer.parseInt(anInternalDate.substring(20,22));
216 if (anInternalDate.charAt(19) == '-') {
217 timezoneOffset = -timezoneOffset;
220 calendar.setTimeZone(UTC);
221 calendar.set(year, month-1, day, hours, minutes, seconds);
222 calendar.add(Calendar.HOUR, -timezoneOffset);
224 return calendar.getTime();
227 public static String regexpReplace(String anInput, String anExpression, String aReplacement) throws UtilFailure {
228 Perl5Matcher matcher = new Perl5Matcher();
231 return Util.substitute(
232 matcher, new Perl5Compiler().compile(anExpression),
233 new Perl5Substitution(aReplacement), anInput,
234 Util.SUBSTITUTE_ALL);
236 catch (MalformedPatternException e) {
237 throw new UtilFailure(e);