- experimental opensessions
[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 the com.oreilly.servlet library, any library\r
22  * licensed under the Apache Software License, The Sun (tm) Java Advanced\r
23  * Imaging library (JAI), The Sun JIMI library (or with modified versions of\r
24  * the above that use the same license as the above), and distribute linked\r
25  * combinations including the two.  You must obey the GNU General Public\r
26  * License in all respects for all of the code used other than the above\r
27  * mentioned libraries.  If you modify this file, you may extend this exception\r
28  * to your version of the file, but you are not obligated to do so.  If you do\r
29  * not wish to do so, delete this exception statement from your version.\r
30  */\r
31 \r
32 package mir.util;\r
33 \r
34 import gnu.regexp.RE;\r
35 import gnu.regexp.REException;\r
36 \r
37 import java.util.List;\r
38 import java.util.Vector;\r
39 \r
40 public class StringRoutines {\r
41 \r
42   private StringRoutines() {\r
43   }\r
44 \r
45   static int indexOfCharacters(String aString, char[] aCharacters, int aFrom) {\r
46     int i;\r
47     int result=-1;\r
48     int position;\r
49 \r
50     for (i=0; i<aCharacters.length ; i++) {\r
51       position = aString.indexOf(aCharacters[i], aFrom);\r
52 \r
53       if (position != -1 && ( result == -1 || position < result )) {\r
54         result = position;\r
55       }\r
56     }\r
57 \r
58     return result;\r
59   }\r
60 \r
61   static String replaceStringCharacters(String aText, char[] aCharactersToReplace, String[] aStringsToSubstitute) {\r
62     if (aText==null)\r
63       return null;\r
64 \r
65     int position, nextPosition;\r
66     int i;\r
67     StringBuffer result = new StringBuffer();\r
68 \r
69     position=0;\r
70     do {\r
71       nextPosition = StringRoutines.indexOfCharacters(aText, aCharactersToReplace, position);\r
72 \r
73       if (nextPosition<0)\r
74         nextPosition = aText.length();\r
75 \r
76       result.append(aText.substring(position, nextPosition));\r
77 \r
78       if (nextPosition<aText.length())\r
79         for (i=0; i<aCharactersToReplace.length; i++) {\r
80           if (aCharactersToReplace[i] == aText.charAt(nextPosition)) {\r
81             result.append(aStringsToSubstitute[i]);\r
82             break;\r
83           }\r
84         }\r
85       position=nextPosition+1;\r
86     }\r
87     while (nextPosition<aText.length()) ;\r
88 \r
89     return result.toString();\r
90   }\r
91 \r
92 \r
93   public static String interpretAsString(Object aValue) throws Exception {\r
94     if (aValue instanceof String)\r
95       return (String) aValue;\r
96 \r
97     if (aValue instanceof Integer)\r
98       return ((Integer) aValue).toString();\r
99 \r
100     if (aValue == null)\r
101       return "";\r
102 \r
103     throw new Exception("String expected, "+aValue+" found");\r
104   }\r
105 \r
106   public static int interpretAsInteger(Object aValue) throws Exception {\r
107     if (aValue instanceof Integer)\r
108       return ((Integer) aValue).intValue();\r
109 \r
110     if (aValue instanceof String)\r
111       try {\r
112         return Integer.parseInt((String) aValue);\r
113       }\r
114       catch (Throwable t) {\r
115         throw new Exception("Integer expected, "+aValue+" found");\r
116       }\r
117 \r
118     throw new Exception("Integer expected, "+aValue+" found");\r
119   }\r
120 \r
121   /**\r
122    *\r
123    * @param aSource\r
124    * @param aSearchExpression\r
125    * @param aReplacement\r
126    * @return\r
127    * @throws Exception\r
128    */\r
129   public static String performRegularExpressionReplacement(String aSource,\r
130       String aSearchExpression, String aReplacement) throws Exception {\r
131 \r
132     RE regularExpression;\r
133 \r
134     regularExpression = new RE(aSearchExpression);\r
135 \r
136     return regularExpression.substituteAll(aSource, aReplacement);\r
137   }\r
138 \r
139   /**\r
140    *\r
141    * @param aSource\r
142    * @param aSearchExpression\r
143    * @return\r
144    * @throws REException\r
145    */\r
146   public static boolean performRegularExpressionSearch(String aSource,\r
147       String aSearchExpression) throws REException {\r
148     RE regularExpression;\r
149 \r
150     regularExpression = new RE(aSearchExpression);\r
151 \r
152     return regularExpression.isMatch(aSource);\r
153   }\r
154 \r
155   /**\r
156    * Separates a string based on a separator:\r
157    *     <code>seperateString("a:b:c", ":");</code> will lead to\r
158    *     a List with 3 Strings: <code>"a"</code>, <code>"b"</code> and <code>"c"</code>\r
159    *\r
160    * @param aString     The string to split\r
161    * @param aSeparator\r
162    * @return\r
163    */\r
164 \r
165   public static List splitString(String aString, String aSeparator) {\r
166     List result= new Vector();\r
167     int previousPosition = 0;\r
168     int position;\r
169     int endOfNamePosition;\r
170 \r
171     if (aString!=null) {\r
172       while ( (position = aString.indexOf(aSeparator, previousPosition)) >= 0) {\r
173         result.add(aString.substring(previousPosition, position));\r
174         previousPosition = position + aSeparator.length();\r
175       }\r
176       result.add(aString.substring(previousPosition, aString.length()));\r
177     }\r
178 \r
179     return result;\r
180   }\r
181 \r
182   /**\r
183    * Separates a String into at most 2 parts based on a separator:\r
184    * <ul>\r
185    *   <li>\r
186    *     <code>seperateString("a:b:c", ":");</code> will lead to\r
187    *     a List with 2 Strings: <code>"a"</code> and <code>"b:c"</code>\r
188    *   <li>\r
189    *     <code>seperateString("abc", ":");</code> will lead to\r
190    *     a List with a single String: <code>"abc"</code>\r
191    * </ul>\r
192    *\r
193    *\r
194    * @param aString\r
195    * @param aSeparator\r
196    * @return\r
197    */\r
198   public static List separateString(String aString, String aSeparator) {\r
199     List result= new Vector();\r
200     int previousPosition = 0;\r
201     int position;\r
202     int endOfNamePosition;\r
203 \r
204     if((position = aString.indexOf(aSeparator, previousPosition))>=0) {\r
205       result.add(aString.substring(previousPosition, position));\r
206       previousPosition = position + aSeparator.length();\r
207     }\r
208 \r
209     result.add(aString.substring(previousPosition, aString.length()));\r
210 \r
211     return result;\r
212   }\r
213 }