2 * Copyright (C) 2001, 2002 The Mir-coders group
\r
4 * This file is part of Mir.
\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
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
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
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
34 import gnu.regexp.RE;
\r
35 import gnu.regexp.REException;
\r
37 import java.util.List;
\r
38 import java.util.Vector;
\r
40 public class StringRoutines {
\r
42 private StringRoutines() {
\r
45 static int indexOfCharacters(String aString, char[] aCharacters, int aFrom) {
\r
50 for (i=0; i<aCharacters.length ; i++) {
\r
51 position = aString.indexOf(aCharacters[i], aFrom);
\r
53 if (position != -1 && ( result == -1 || position < result )) {
\r
61 static String replaceStringCharacters(String aText, char[] aCharactersToReplace, String[] aStringsToSubstitute) {
\r
65 int position, nextPosition;
\r
67 StringBuffer result = new StringBuffer();
\r
71 nextPosition = StringRoutines.indexOfCharacters(aText, aCharactersToReplace, position);
\r
74 nextPosition = aText.length();
\r
76 result.append(aText.substring(position, nextPosition));
\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
85 position=nextPosition+1;
\r
87 while (nextPosition<aText.length()) ;
\r
89 return result.toString();
\r
93 public static String interpretAsString(Object aValue) throws Exception {
\r
94 if (aValue instanceof String)
\r
95 return (String) aValue;
\r
97 if (aValue instanceof Integer)
\r
98 return ((Integer) aValue).toString();
\r
100 if (aValue == null)
\r
103 throw new Exception("String expected, "+aValue+" found");
\r
106 public static int interpretAsInteger(Object aValue) throws Exception {
\r
107 if (aValue instanceof Integer)
\r
108 return ((Integer) aValue).intValue();
\r
110 if (aValue instanceof String)
\r
112 return Integer.parseInt((String) aValue);
\r
114 catch (Throwable t) {
\r
115 throw new Exception("Integer expected, "+aValue+" found");
\r
118 throw new Exception("Integer expected, "+aValue+" found");
\r
124 * @param aSearchExpression
\r
125 * @param aReplacement
\r
127 * @throws Exception
\r
129 public static String performRegularExpressionReplacement(String aSource,
\r
130 String aSearchExpression, String aReplacement) throws Exception {
\r
132 RE regularExpression;
\r
134 regularExpression = new RE(aSearchExpression);
\r
136 return regularExpression.substituteAll(aSource, aReplacement);
\r
142 * @param aSearchExpression
\r
144 * @throws REException
\r
146 public static boolean performRegularExpressionSearch(String aSource,
\r
147 String aSearchExpression) throws REException {
\r
148 RE regularExpression;
\r
150 regularExpression = new RE(aSearchExpression);
\r
152 return regularExpression.isMatch(aSource);
\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
160 * @param aString The string to split
\r
161 * @param aSeparator
\r
165 public static List splitString(String aString, String aSeparator) {
\r
166 List result= new Vector();
\r
167 int previousPosition = 0;
\r
169 int endOfNamePosition;
\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
176 result.add(aString.substring(previousPosition, aString.length()));
\r
183 * Separates a String into at most 2 parts based on a separator:
\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
189 * <code>seperateString("abc", ":");</code> will lead to
\r
190 * a List with a single String: <code>"abc"</code>
\r
195 * @param aSeparator
\r
198 public static List separateString(String aString, String aSeparator) {
\r
199 List result= new Vector();
\r
200 int previousPosition = 0;
\r
202 int endOfNamePosition;
\r
204 if((position = aString.indexOf(aSeparator, previousPosition))>=0) {
\r
205 result.add(aString.substring(previousPosition, position));
\r
206 previousPosition = position + aSeparator.length();
\r
209 result.add(aString.substring(previousPosition, aString.length()));
\r