2 * Copyright (C) 2001, 2002 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 * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23 * (or with modified versions of the above that use the same license as the above),
24 * and distribute linked combinations including the two. You must obey the
25 * GNU General Public License in all respects for all of the code used other than
26 * the above mentioned libraries. If you modify this file, you may extend this
27 * exception to your version of the file, but you are not obligated to do so.
28 * If you do not wish to do so, delete this exception statement from your version.
34 import java.util.ArrayList;
35 import java.util.List;
37 public class StringRoutines {
39 private StringRoutines() {
42 public static int indexOfCharacters(String aString, char[] aCharacters, int aFrom) {
47 for (i=0; i<aCharacters.length ; i++) {
48 position = aString.indexOf(aCharacters[i], aFrom);
50 if (position != -1 && ( result == -1 || position < result )) {
58 public static String replaceStringCharacters(String aText, char[] aCharactersToReplace, String[] aStringsToSubstitute) {
62 int position, nextPosition;
64 StringBuffer result = new StringBuffer();
68 nextPosition = StringRoutines.indexOfCharacters(aText, aCharactersToReplace, position);
71 nextPosition = aText.length();
73 result.append(aText.substring(position, nextPosition));
75 if (nextPosition<aText.length())
76 for (i=0; i<aCharactersToReplace.length; i++) {
77 if (aCharactersToReplace[i] == aText.charAt(nextPosition)) {
78 result.append(aStringsToSubstitute[i]);
82 position=nextPosition+1;
84 while (nextPosition<aText.length()) ;
86 return result.toString();
91 public static String replaceEscapedStringCharacters(String aText, char anEscapeCharacter, char[] aCharactersToReplace, String[] aStringsToSubstitute) {
95 int position, nextPosition;
97 StringBuffer result = new StringBuffer();
101 nextPosition = aText.indexOf(anEscapeCharacter, position);
104 nextPosition = aText.length();
106 result.append(aText.substring(position, nextPosition));
108 if (nextPosition+1<aText.length()) {
109 nextPosition = nextPosition+1;
111 boolean found = false;
112 for (i = 0; i < aCharactersToReplace.length; i++) {
113 if (aCharactersToReplace[i] == aText.charAt(nextPosition)) {
114 result.append(aStringsToSubstitute[i]);
121 result.append(aText.charAt(nextPosition));
124 position=nextPosition+1;
126 while (nextPosition<aText.length()) ;
128 return result.toString();
131 public static String interpretAsString(Object aValue) throws Exception {
132 if (aValue instanceof String)
133 return (String) aValue;
135 if (aValue instanceof Integer)
136 return aValue.toString();
141 throw new Exception("String expected, "+aValue+" found");
144 public static int interpretAsInteger(Object aValue) throws Exception {
145 if (aValue instanceof Integer)
146 return ((Integer) aValue).intValue();
148 if (aValue instanceof String)
150 return Integer.parseInt((String) aValue);
152 catch (Throwable t) {
153 throw new Exception("Integer expected, "+aValue+" found");
156 throw new Exception("Integer expected, "+aValue+" found");
159 public static String performRegularExpressionReplacement(String aSource, String aSearchExpression, String aReplacement) {
161 RE regularExpression;
163 regularExpression = new RE(aSearchExpression);
165 return regularExpression.substituteAll(aSource, aReplacement);
167 catch (Throwable t) {
168 throw new UtilFailure("StringRoutines.performRegularExpressionReplacement: " + t.toString(), t);
173 * Separates a string based on a separator:
174 * <code>seperateString("a:b:c", ":");</code> will lead to
175 * a List with 3 Strings: <code>"a"</code>, <code>"b"</code> and <code>"c"</code>
177 * @param aString The string to split
182 public static List splitString(String aString, String aSeparator) {
183 List result= new ArrayList();
184 int previousPosition = 0;
188 while ( (position = aString.indexOf(aSeparator, previousPosition)) >= 0) {
189 result.add(aString.substring(previousPosition, position));
190 previousPosition = position + aSeparator.length();
192 result.add(aString.substring(previousPosition, aString.length()));
199 * Separates a String into at most 2 parts based on a separator:
202 * <code>seperateString("a:b:c", ":");</code> will lead to
203 * a List with 2 Strings: <code>"a"</code> and <code>"b:c"</code>
205 * <code>seperateString("abc", ":");</code> will lead to
206 * a List with a single String: <code>"abc"</code>
214 public static List separateString(String aString, String aSeparator) {
215 List result= new ArrayList();
216 int previousPosition = 0;
219 if((position = aString.indexOf(aSeparator, previousPosition))>=0) {
220 result.add(aString.substring(previousPosition, position));
221 previousPosition = position + aSeparator.length();
224 result.add(aString.substring(previousPosition, aString.length()));
230 * Separates a string based on a separator, taking into account an escape character:
231 * <code>seperateString("a:/::b", ":", "/");</code> will lead to
232 * a List with 3 Strings: <code>"a"</code>, <code>":"</code> and <code>"b"</code>
234 * @param aString The string to split
239 public static List splitStringWithEscape(String aString, char aSeparator, char anEscape) {
240 List result= new ArrayList();
241 int previousPosition = 0;
244 StringBuffer currentItem = new StringBuffer();
246 if (aString!=null && aString.length()>0) {
247 while ((position = indexOfCharacters(aString, new char[] {aSeparator, anEscape}, previousPosition))>=0) {
248 currentItem.append(aString.substring(previousPosition, position));
250 if (aString.charAt(position)==aSeparator) {
251 result.add(currentItem.toString());
252 currentItem.delete(0, currentItem.length());
255 if (aString.length()>position+1) {
257 currentItem.append(aString.charAt(position));
260 currentItem.append(aString.charAt(position));
263 previousPosition = position + 1;
265 currentItem.append(aString.substring(previousPosition, aString.length()));
266 result.add(currentItem.toString());
272 public static String replicateString(String aString, int aCount) {
273 StringBuffer result = new StringBuffer();
275 for (int i=0; i<aCount; i++)
276 result.append(aString);
278 return result.toString();
281 public static String replicateChar(char aCharacter, int aCount) {
282 char result[] = new char[aCount];
284 for (int i=0; i<aCount; i++)
285 result[i]= aCharacter;
287 return new String(result);
290 public static String padStringLeft(String aString, int aLength, char aPadCharacter) {
291 if (aString.length()<aLength)
292 return replicateChar(aPadCharacter, aLength-aString.length()) + aString;
296 private static final char HEX_CHARACTERS[] = {
297 '0', '1', '2', '3', '4', '5', '6', '7',
298 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
301 public static String convertToHex(long aData, int aNumberOfDigits) {
302 StringBuffer result = new StringBuffer();
304 for (int digit = aNumberOfDigits-1; digit>=0; digit--) {
305 int value = (int) (aData >> (digit*4)) & 0xf;
306 result.append(HEX_CHARACTERS[value]);
309 return result.toString();
312 public static String convertToHex(byte[] aData) {
313 StringBuffer result = new StringBuffer();
315 for (int i = 0; i<aData.length; i++) {
316 result.append(convertToHex(aData[i], 2));
320 return result.toString();