*** empty log message ***
[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   public static String interpretAsString(Object aValue) throws Exception {\r
92     if (aValue instanceof String)\r
93       return (String) aValue;\r
94 \r
95     if (aValue instanceof Integer)\r
96       return ((Integer) aValue).toString();\r
97 \r
98     if (aValue == null)\r
99       return "";\r
100 \r
101     throw new Exception("String expected, "+aValue+" found");\r
102   }\r
103 \r
104   public static int interpretAsInteger(Object aValue) throws Exception {\r
105     if (aValue instanceof Integer)\r
106       return ((Integer) aValue).intValue();\r
107 \r
108     if (aValue instanceof String)\r
109       try {\r
110         return Integer.parseInt((String) aValue);\r
111       }\r
112       catch (Throwable t) {\r
113         throw new Exception("Integer expected, "+aValue+" found");\r
114       }\r
115 \r
116     throw new Exception("Integer expected, "+aValue+" found");\r
117   }\r
118 \r
119   /**\r
120    *\r
121    * @param aSource\r
122    * @param aSearchExpression\r
123    * @param aReplacement\r
124    * @return\r
125    * @throws Exception\r
126    */\r
127   public static String performRegularExpressionReplacement(String aSource,\r
128       String aSearchExpression, String aReplacement) throws UtilExc {\r
129     try {\r
130       RE regularExpression;\r
131 \r
132       regularExpression = new RE(aSearchExpression);\r
133 \r
134       return regularExpression.substituteAll(aSource, aReplacement);\r
135     }\r
136     catch (Throwable t) {\r
137       throw new UtilFailure("StringRoutines.performRegularExpressionReplacement: " + t.toString(), t);\r
138     }\r
139   }\r
140 \r
141   public static String performCaseInsensitiveRegularExpressionReplacement(String aSource,\r
142       String aSearchExpression, String aReplacement) throws UtilExc {\r
143     try {\r
144       RE regularExpression;\r
145 \r
146       regularExpression = new RE(aSearchExpression, RE.REG_ICASE);\r
147 \r
148       return regularExpression.substituteAll(aSource, aReplacement);\r
149     }\r
150     catch (Throwable t) {\r
151       throw new UtilFailure("StringRoutines.performRegularExpressionReplacement: " + t.toString(), t);\r
152     }\r
153   }\r
154 \r
155   /**\r
156    *\r
157    * @param aSource\r
158    * @param aSearchExpression\r
159    * @return\r
160    * @throws REException\r
161    */\r
162   public static boolean performRegularExpressionSearch(String aSource,\r
163       String aSearchExpression) throws UtilExc {\r
164     try {\r
165       RE regularExpression;\r
166 \r
167       regularExpression = new RE(aSearchExpression);\r
168 \r
169       return regularExpression.isMatch(aSource);\r
170     }\r
171     catch (Throwable t) {\r
172       throw new UtilFailure("StringRoutines.performRegularExpressionSearch: " + t.toString(), t);\r
173     }\r
174   }\r
175 \r
176   /**\r
177    * Separates a string based on a separator:\r
178    *     <code>seperateString("a:b:c", ":");</code> will lead to\r
179    *     a List with 3 Strings: <code>"a"</code>, <code>"b"</code> and <code>"c"</code>\r
180    *\r
181    * @param aString     The string to split\r
182    * @param aSeparator\r
183    * @return\r
184    */\r
185 \r
186   public static List splitString(String aString, String aSeparator) {\r
187     List result= new Vector();\r
188     int previousPosition = 0;\r
189     int position;\r
190     int endOfNamePosition;\r
191 \r
192     if (aString!=null) {\r
193       while ( (position = aString.indexOf(aSeparator, previousPosition)) >= 0) {\r
194         result.add(aString.substring(previousPosition, position));\r
195         previousPosition = position + aSeparator.length();\r
196       }\r
197       result.add(aString.substring(previousPosition, aString.length()));\r
198     }\r
199 \r
200     return result;\r
201   }\r
202 \r
203   /**\r
204    * Separates a String into at most 2 parts based on a separator:\r
205    * <ul>\r
206    *   <li>\r
207    *     <code>seperateString("a:b:c", ":");</code> will lead to\r
208    *     a List with 2 Strings: <code>"a"</code> and <code>"b:c"</code>\r
209    *   <li>\r
210    *     <code>seperateString("abc", ":");</code> will lead to\r
211    *     a List with a single String: <code>"abc"</code>\r
212    * </ul>\r
213    *\r
214    *\r
215    * @param aString\r
216    * @param aSeparator\r
217    * @return\r
218    */\r
219   public static List separateString(String aString, String aSeparator) {\r
220     List result= new Vector();\r
221     int previousPosition = 0;\r
222     int position;\r
223 \r
224     if((position = aString.indexOf(aSeparator, previousPosition))>=0) {\r
225       result.add(aString.substring(previousPosition, position));\r
226       previousPosition = position + aSeparator.length();\r
227     }\r
228 \r
229     result.add(aString.substring(previousPosition, aString.length()));\r
230 \r
231     return result;\r
232   }\r
233 \r
234   public static List splitStringWithEscape(String aString, char aSeparator, char anEscape) {\r
235     List result= new Vector();\r
236     int previousPosition = 0;\r
237     int position;\r
238     int endOfNamePosition;\r
239     StringBuffer currentItem = new StringBuffer();\r
240 \r
241     if (aString!=null) {\r
242       while ((position = indexOfCharacters(aString, new char[] {aSeparator, anEscape}, previousPosition))>=0) {\r
243         currentItem.append(aString.substring(previousPosition, position));\r
244 \r
245         if (aString.charAt(position)==aSeparator) {\r
246           result.add(currentItem.toString());\r
247           currentItem.delete(0, currentItem.length());\r
248         }\r
249         else {\r
250           if (aString.length()>position+1) {\r
251             position=position+1;\r
252             currentItem.append(aString.charAt(position));\r
253           }\r
254         }\r
255         previousPosition = position + 1;\r
256       }\r
257       currentItem.append(aString.substring(previousPosition, aString.length()));\r
258       result.add(currentItem.toString());\r
259     }\r
260 \r
261     return result;\r
262   }\r
263 \r
264   public static String replicateString(String aString, int aCount) {\r
265     StringBuffer result = new StringBuffer();\r
266 \r
267     for (int i=0; i<aCount; i++)\r
268       result.append(aString);\r
269 \r
270     return result.toString();\r
271   }\r
272 \r
273   public static String replicateChar(char aCharacter, int aCount) {\r
274     char result[] = new char[aCount];\r
275 \r
276     for (int i=0; i<aCount; i++)\r
277       result[i]= aCharacter;\r
278 \r
279     return new String(result);\r
280   }\r
281 \r
282   public static String padStringLeft(String aString, int aLength, char aPadCharacter) {\r
283     if (aString.length()<aLength)\r
284       return replicateChar(aPadCharacter, aLength-aString.length()) + aString;\r
285     else\r
286       return aString;\r
287   }\r
288 \r
289   private static final char HEX_CHARACTERS[] = {\r
290       '0', '1', '2', '3', '4', '5', '6', '7',\r
291       '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'\r
292   };\r
293 \r
294   public static String convertToHex(long aData, int aNumberOfDigits) {\r
295     StringBuffer result = new StringBuffer();\r
296 \r
297     for (int digit = aNumberOfDigits-1; digit>=0; digit--) {\r
298       int value = (int) (aData >> (digit*4)) & 0xf;\r
299       result.append(HEX_CHARACTERS[value]);\r
300     }\r
301 \r
302     return result.toString();\r
303   }\r
304 \r
305   public static String convertToHex(byte[] aData) {\r
306     StringBuffer result = new StringBuffer();\r
307 \r
308     for (int i = 0; i<aData.length; i++) {\r
309       result.append(convertToHex(aData[i], 2));\r
310 \r
311     }\r
312 \r
313     return result.toString();\r
314   }\r
315 }