admin activity logger added
[mir.git] / source / mir / util / StringRoutines.java
1 /*\r
2  * Copyright (C) 2001, 2002 The Mir-coders group\r
3  *\r
4  * This file is part of Mir.\r
5  *\r
6  * Mir is free software; you can redistribute it and/or modify\r
7  * it under the terms of the GNU General Public License as published by\r
8  * the Free Software Foundation; either version 2 of the License, or\r
9  * (at your option) any later version.\r
10  *\r
11  * Mir is distributed in the hope that it will be useful,\r
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14  * GNU General Public License for more details.\r
15  *\r
16  * You should have received a copy of the GNU General Public License\r
17  * along with Mir; if not, write to the Free Software\r
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19  *\r
20  * In addition, as a special exception, The Mir-coders gives permission to link\r
21  * the code of this program with  any library licensed under the Apache Software License,\r
22  * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library\r
23  * (or with modified versions of the above that use the same license as the above),\r
24  * and distribute linked combinations including the two.  You must obey the\r
25  * GNU General Public License in all respects for all of the code used other than\r
26  * the above mentioned libraries.  If you modify this file, you may extend this\r
27  * exception to your version of the file, but you are not obligated to do so.\r
28  * If you do not wish to do so, delete this exception statement from your version.\r
29  */\r
30 package mir.util;\r
31 \r
32 import gnu.regexp.RE;\r
33 import gnu.regexp.REException;\r
34 \r
35 import java.util.List;\r
36 import java.util.Vector;\r
37 \r
38 public class StringRoutines {\r
39 \r
40   private StringRoutines() {\r
41   }\r
42 \r
43   public static int indexOfCharacters(String aString, char[] aCharacters, int aFrom) {\r
44     int i;\r
45     int result=-1;\r
46     int position;\r
47 \r
48     for (i=0; i<aCharacters.length ; i++) {\r
49       position = aString.indexOf(aCharacters[i], aFrom);\r
50 \r
51       if (position != -1 && ( result == -1 || position < result )) {\r
52         result = position;\r
53       }\r
54     }\r
55 \r
56     return result;\r
57   }\r
58 \r
59   public static String replaceStringCharacters(String aText, char[] aCharactersToReplace, String[] aStringsToSubstitute) {\r
60     if (aText==null)\r
61       return null;\r
62 \r
63     int position, nextPosition;\r
64     int i;\r
65     StringBuffer result = new StringBuffer();\r
66 \r
67     position=0;\r
68     do {\r
69       nextPosition = StringRoutines.indexOfCharacters(aText, aCharactersToReplace, position);\r
70 \r
71       if (nextPosition<0)\r
72         nextPosition = aText.length();\r
73 \r
74       result.append(aText.substring(position, nextPosition));\r
75 \r
76       if (nextPosition<aText.length())\r
77         for (i=0; i<aCharactersToReplace.length; i++) {\r
78           if (aCharactersToReplace[i] == aText.charAt(nextPosition)) {\r
79             result.append(aStringsToSubstitute[i]);\r
80             break;\r
81           }\r
82         }\r
83       position=nextPosition+1;\r
84     }\r
85     while (nextPosition<aText.length()) ;\r
86 \r
87     return result.toString();\r
88   }\r
89   /**\r
90    *\r
91    * @param aText\r
92    * @param anEscapeCharacater\r
93    * @param aCharactersToReplace\r
94    * @param aStringsToSubstitute\r
95    * @return\r
96    */\r
97 \r
98   public static String replaceEscapedStringCharacters(String aText, char anEscapeCharacter, char[] aCharactersToReplace, String[] aStringsToSubstitute) {\r
99     if (aText==null)\r
100       return null;\r
101 \r
102     int position, nextPosition;\r
103     int i;\r
104     StringBuffer result = new StringBuffer();\r
105 \r
106     position=0;\r
107     do {\r
108       nextPosition = aText.indexOf(anEscapeCharacter, position);\r
109 \r
110       if (nextPosition<0)\r
111         nextPosition = aText.length();\r
112 \r
113       result.append(aText.substring(position, nextPosition));\r
114 \r
115       if (nextPosition+1<aText.length()) {\r
116         nextPosition = nextPosition+1;\r
117 \r
118         boolean found = false;\r
119         for (i = 0; i < aCharactersToReplace.length; i++) {\r
120           if (aCharactersToReplace[i] == aText.charAt(nextPosition)) {\r
121             result.append(aStringsToSubstitute[i]);\r
122             found=true;\r
123             break;\r
124           }\r
125         }\r
126 \r
127         if (!found) {\r
128           result.append(aText.charAt(nextPosition));\r
129         }\r
130       }\r
131       position=nextPosition+1;\r
132     }\r
133     while (nextPosition<aText.length()) ;\r
134 \r
135     return result.toString();\r
136   }\r
137 \r
138   public static String interpretAsString(Object aValue) throws Exception {\r
139     if (aValue instanceof String)\r
140       return (String) aValue;\r
141 \r
142     if (aValue instanceof Integer)\r
143       return ((Integer) aValue).toString();\r
144 \r
145     if (aValue == null)\r
146       return "";\r
147 \r
148     throw new Exception("String expected, "+aValue+" found");\r
149   }\r
150 \r
151   public static int interpretAsInteger(Object aValue) throws Exception {\r
152     if (aValue instanceof Integer)\r
153       return ((Integer) aValue).intValue();\r
154 \r
155     if (aValue instanceof String)\r
156       try {\r
157         return Integer.parseInt((String) aValue);\r
158       }\r
159       catch (Throwable t) {\r
160         throw new Exception("Integer expected, "+aValue+" found");\r
161       }\r
162 \r
163     throw new Exception("Integer expected, "+aValue+" found");\r
164   }\r
165 \r
166   /**\r
167    *\r
168    * @param aSource\r
169    * @param aSearchExpression\r
170    * @param aReplacement\r
171    * @return\r
172    * @throws Exception\r
173    */\r
174   public static String performRegularExpressionReplacement(String aSource,\r
175       String aSearchExpression, String aReplacement) throws UtilExc {\r
176     try {\r
177       RE regularExpression;\r
178 \r
179       regularExpression = new RE(aSearchExpression);\r
180 \r
181       return regularExpression.substituteAll(aSource, aReplacement);\r
182     }\r
183     catch (Throwable t) {\r
184       throw new UtilFailure("StringRoutines.performRegularExpressionReplacement: " + t.toString(), t);\r
185     }\r
186   }\r
187 \r
188   public static String performCaseInsensitiveRegularExpressionReplacement(String aSource,\r
189       String aSearchExpression, String aReplacement) throws UtilExc {\r
190     try {\r
191       RE regularExpression;\r
192 \r
193       regularExpression = new RE(aSearchExpression, RE.REG_ICASE);\r
194 \r
195       return regularExpression.substituteAll(aSource, aReplacement);\r
196     }\r
197     catch (Throwable t) {\r
198       throw new UtilFailure("StringRoutines.performRegularExpressionReplacement: " + t.toString(), t);\r
199     }\r
200   }\r
201 \r
202   /**\r
203    *\r
204    * @param aSource\r
205    * @param aSearchExpression\r
206    * @return\r
207    * @throws REException\r
208    */\r
209   public static boolean performRegularExpressionSearch(String aSource,\r
210       String aSearchExpression) throws UtilExc {\r
211     try {\r
212       RE regularExpression;\r
213 \r
214       regularExpression = new RE(aSearchExpression);\r
215 \r
216       return regularExpression.isMatch(aSource);\r
217     }\r
218     catch (Throwable t) {\r
219       throw new UtilFailure("StringRoutines.performRegularExpressionSearch: " + t.toString(), t);\r
220     }\r
221   }\r
222 \r
223   /**\r
224    * Separates a string based on a separator:\r
225    *     <code>seperateString("a:b:c", ":");</code> will lead to\r
226    *     a List with 3 Strings: <code>"a"</code>, <code>"b"</code> and <code>"c"</code>\r
227    *\r
228    * @param aString     The string to split\r
229    * @param aSeparator\r
230    * @return\r
231    */\r
232 \r
233   public static List splitString(String aString, String aSeparator) {\r
234     List result= new Vector();\r
235     int previousPosition = 0;\r
236     int position;\r
237     int endOfNamePosition;\r
238 \r
239     if (aString!=null) {\r
240       while ( (position = aString.indexOf(aSeparator, previousPosition)) >= 0) {\r
241         result.add(aString.substring(previousPosition, position));\r
242         previousPosition = position + aSeparator.length();\r
243       }\r
244       result.add(aString.substring(previousPosition, aString.length()));\r
245     }\r
246 \r
247     return result;\r
248   }\r
249 \r
250   /**\r
251    * Separates a String into at most 2 parts based on a separator:\r
252    * <ul>\r
253    *   <li>\r
254    *     <code>seperateString("a:b:c", ":");</code> will lead to\r
255    *     a List with 2 Strings: <code>"a"</code> and <code>"b:c"</code>\r
256    *   <li>\r
257    *     <code>seperateString("abc", ":");</code> will lead to\r
258    *     a List with a single String: <code>"abc"</code>\r
259    * </ul>\r
260    *\r
261    *\r
262    * @param aString\r
263    * @param aSeparator\r
264    * @return\r
265    */\r
266   public static List separateString(String aString, String aSeparator) {\r
267     List result= new Vector();\r
268     int previousPosition = 0;\r
269     int position;\r
270 \r
271     if((position = aString.indexOf(aSeparator, previousPosition))>=0) {\r
272       result.add(aString.substring(previousPosition, position));\r
273       previousPosition = position + aSeparator.length();\r
274     }\r
275 \r
276     result.add(aString.substring(previousPosition, aString.length()));\r
277 \r
278     return result;\r
279   }\r
280 \r
281   public static List splitStringWithEscape(String aString, char aSeparator, char anEscape) {\r
282     List result= new Vector();\r
283     int previousPosition = 0;\r
284     int position;\r
285     int endOfNamePosition;\r
286     StringBuffer currentItem = new StringBuffer();\r
287 \r
288     if (aString!=null) {\r
289       while ((position = indexOfCharacters(aString, new char[] {aSeparator, anEscape}, previousPosition))>=0) {\r
290         currentItem.append(aString.substring(previousPosition, position));\r
291 \r
292         if (aString.charAt(position)==aSeparator) {\r
293           result.add(currentItem.toString());\r
294           currentItem.delete(0, currentItem.length());\r
295         }\r
296         else {\r
297           currentItem.append(aString.charAt(position));\r
298           if (aString.length()>position+1) {\r
299             position=position+1;\r
300             currentItem.append(aString.charAt(position));\r
301           }\r
302         }\r
303         previousPosition = position + 1;\r
304       }\r
305       currentItem.append(aString.substring(previousPosition, aString.length()));\r
306       result.add(currentItem.toString());\r
307     }\r
308 \r
309     return result;\r
310   }\r
311 \r
312   public static String replicateString(String aString, int aCount) {\r
313     StringBuffer result = new StringBuffer();\r
314 \r
315     for (int i=0; i<aCount; i++)\r
316       result.append(aString);\r
317 \r
318     return result.toString();\r
319   }\r
320 \r
321   public static String replicateChar(char aCharacter, int aCount) {\r
322     char result[] = new char[aCount];\r
323 \r
324     for (int i=0; i<aCount; i++)\r
325       result[i]= aCharacter;\r
326 \r
327     return new String(result);\r
328   }\r
329 \r
330   public static String padStringLeft(String aString, int aLength, char aPadCharacter) {\r
331     if (aString.length()<aLength)\r
332       return replicateChar(aPadCharacter, aLength-aString.length()) + aString;\r
333     else\r
334       return aString;\r
335   }\r
336 \r
337   private static final char HEX_CHARACTERS[] = {\r
338       '0', '1', '2', '3', '4', '5', '6', '7',\r
339       '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'\r
340   };\r
341 \r
342   public static String convertToHex(long aData, int aNumberOfDigits) {\r
343     StringBuffer result = new StringBuffer();\r
344 \r
345     for (int digit = aNumberOfDigits-1; digit>=0; digit--) {\r
346       int value = (int) (aData >> (digit*4)) & 0xf;\r
347       result.append(HEX_CHARACTERS[value]);\r
348     }\r
349 \r
350     return result.toString();\r
351   }\r
352 \r
353   public static String convertToHex(byte[] aData) {\r
354     StringBuffer result = new StringBuffer();\r
355 \r
356     for (int i = 0; i<aData.length; i++) {\r
357       result.append(convertToHex(aData[i], 2));\r
358 \r
359     }\r
360 \r
361     return result.toString();\r
362   }\r
363 }