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 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
32 import gnu.regexp.RE;
\r
33 import gnu.regexp.REException;
\r
35 import java.util.List;
\r
36 import java.util.Vector;
\r
38 public class StringRoutines {
\r
40 private StringRoutines() {
\r
43 public static int indexOfCharacters(String aString, char[] aCharacters, int aFrom) {
\r
48 for (i=0; i<aCharacters.length ; i++) {
\r
49 position = aString.indexOf(aCharacters[i], aFrom);
\r
51 if (position != -1 && ( result == -1 || position < result )) {
\r
59 public static String replaceStringCharacters(String aText, char[] aCharactersToReplace, String[] aStringsToSubstitute) {
\r
63 int position, nextPosition;
\r
65 StringBuffer result = new StringBuffer();
\r
69 nextPosition = StringRoutines.indexOfCharacters(aText, aCharactersToReplace, position);
\r
72 nextPosition = aText.length();
\r
74 result.append(aText.substring(position, nextPosition));
\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
83 position=nextPosition+1;
\r
85 while (nextPosition<aText.length()) ;
\r
87 return result.toString();
\r
91 public static String interpretAsString(Object aValue) throws Exception {
\r
92 if (aValue instanceof String)
\r
93 return (String) aValue;
\r
95 if (aValue instanceof Integer)
\r
96 return ((Integer) aValue).toString();
\r
101 throw new Exception("String expected, "+aValue+" found");
\r
104 public static int interpretAsInteger(Object aValue) throws Exception {
\r
105 if (aValue instanceof Integer)
\r
106 return ((Integer) aValue).intValue();
\r
108 if (aValue instanceof String)
\r
110 return Integer.parseInt((String) aValue);
\r
112 catch (Throwable t) {
\r
113 throw new Exception("Integer expected, "+aValue+" found");
\r
116 throw new Exception("Integer expected, "+aValue+" found");
\r
122 * @param aSearchExpression
\r
123 * @param aReplacement
\r
125 * @throws Exception
\r
127 public static String performRegularExpressionReplacement(String aSource,
\r
128 String aSearchExpression, String aReplacement) throws Exception {
\r
130 RE regularExpression;
\r
132 regularExpression = new RE(aSearchExpression);
\r
134 return regularExpression.substituteAll(aSource, aReplacement);
\r
140 * @param aSearchExpression
\r
142 * @throws REException
\r
144 public static boolean performRegularExpressionSearch(String aSource,
\r
145 String aSearchExpression) throws REException {
\r
146 RE regularExpression;
\r
148 regularExpression = new RE(aSearchExpression);
\r
150 return regularExpression.isMatch(aSource);
\r
154 * Separates a string based on a separator:
\r
155 * <code>seperateString("a:b:c", ":");</code> will lead to
\r
156 * a List with 3 Strings: <code>"a"</code>, <code>"b"</code> and <code>"c"</code>
\r
158 * @param aString The string to split
\r
159 * @param aSeparator
\r
163 public static List splitString(String aString, String aSeparator) {
\r
164 List result= new Vector();
\r
165 int previousPosition = 0;
\r
167 int endOfNamePosition;
\r
169 if (aString!=null) {
\r
170 while ( (position = aString.indexOf(aSeparator, previousPosition)) >= 0) {
\r
171 result.add(aString.substring(previousPosition, position));
\r
172 previousPosition = position + aSeparator.length();
\r
174 result.add(aString.substring(previousPosition, aString.length()));
\r
181 * Separates a String into at most 2 parts based on a separator:
\r
184 * <code>seperateString("a:b:c", ":");</code> will lead to
\r
185 * a List with 2 Strings: <code>"a"</code> and <code>"b:c"</code>
\r
187 * <code>seperateString("abc", ":");</code> will lead to
\r
188 * a List with a single String: <code>"abc"</code>
\r
193 * @param aSeparator
\r
196 public static List separateString(String aString, String aSeparator) {
\r
197 List result= new Vector();
\r
198 int previousPosition = 0;
\r
201 if((position = aString.indexOf(aSeparator, previousPosition))>=0) {
\r
202 result.add(aString.substring(previousPosition, position));
\r
203 previousPosition = position + aSeparator.length();
\r
206 result.add(aString.substring(previousPosition, aString.length()));
\r
211 public static List splitStringWithEscape(String aString, char aSeparator, char anEscape) {
\r
212 List result= new Vector();
\r
213 int previousPosition = 0;
\r
215 int endOfNamePosition;
\r
216 StringBuffer currentItem = new StringBuffer();
\r
218 if (aString!=null) {
\r
219 while ((position = indexOfCharacters(aString, new char[] {aSeparator, anEscape}, previousPosition))>=0) {
\r
220 currentItem.append(aString.substring(previousPosition, position));
\r
222 if (aString.charAt(position)==aSeparator) {
\r
223 result.add(currentItem.toString());
\r
224 currentItem.delete(0, currentItem.length());
\r
227 if (aString.length()>position+1) {
\r
228 position=position+1;
\r
229 currentItem.append(aString.charAt(position));
\r
232 previousPosition = position + 1;
\r
234 currentItem.append(aString.substring(previousPosition, aString.length()));
\r
235 result.add(currentItem.toString());
\r
241 public static String replicateString(String aString, int aCount) {
\r
242 StringBuffer result = new StringBuffer();
\r
244 for (int i=0; i<aCount; i++)
\r
245 result.append(aString);
\r
247 return result.toString();
\r
250 public static String replicateChar(char aCharacter, int aCount) {
\r
251 char result[] = new char[aCount];
\r
253 for (int i=0; i<aCount; i++)
\r
254 result[i]= aCharacter;
\r
256 return new String(result);
\r
259 public static String padStringLeft(String aString, int aLength, char aPadCharacter) {
\r
260 if (aString.length()<aLength)
\r
261 return replicateChar(aPadCharacter, aLength-aString.length()) + aString;
\r
266 private static final char HEX_CHARACTERS[] = {
\r
267 '0', '1', '2', '3', '4', '5', '6', '7',
\r
268 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
\r
271 public static String convertToHex(long aData, int aNumberOfDigits) {
\r
272 StringBuffer result = new StringBuffer();
\r
274 for (int digit = aNumberOfDigits-1; digit>=0; digit--) {
\r
275 int value = (int) (aData >> (digit*4)) & 0xf;
\r
276 result.append(HEX_CHARACTERS[value]);
\r
279 return result.toString();
\r
282 public static String convertToHex(byte[] aData) {
\r
283 StringBuffer result = new StringBuffer();
\r
285 for (int i = 0; i<aData.length; i++) {
\r
286 result.append(convertToHex(aData[i], 2));
\r
290 return result.toString();
\r