X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=source%2Fmir%2Futil%2FStringRoutines.java;h=b79ef109dba709dc38b64b581961528552ee6208;hb=95c2e3a74f492f6f61d4dbe8bb47996f43af0627;hp=8d7257cc22f3976f0a0b48c31ed8dd8ea0424042;hpb=55b409cfb4c95a18ee3183c99a7133e51395d454;p=mir.git diff --git a/source/mir/util/StringRoutines.java b/source/mir/util/StringRoutines.java index 8d7257cc..b79ef109 100755 --- a/source/mir/util/StringRoutines.java +++ b/source/mir/util/StringRoutines.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2001, 2002 The Mir-coders group + * Copyright (C) 2001, 2002 The Mir-coders group * * This file is part of Mir. * @@ -18,31 +18,28 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * In addition, as a special exception, The Mir-coders gives permission to link - * the code of this program with the com.oreilly.servlet library, any library - * licensed under the Apache Software License, The Sun (tm) Java Advanced - * Imaging library (JAI), The Sun JIMI library (or with modified versions of - * the above that use the same license as the above), and distribute linked - * combinations including the two. You must obey the GNU General Public - * License in all respects for all of the code used other than the above - * mentioned libraries. If you modify this file, you may extend this exception - * to your version of the file, but you are not obligated to do so. If you do - * not wish to do so, delete this exception statement from your version. + * the code of this program with any library licensed under the Apache Software License, + * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library + * (or with modified versions of the above that use the same license as the above), + * and distribute linked combinations including the two. You must obey the + * GNU General Public License in all respects for all of the code used other than + * the above mentioned libraries. If you modify this file, you may extend this + * exception to your version of the file, but you are not obligated to do so. + * If you do not wish to do so, delete this exception statement from your version. */ - package mir.util; import gnu.regexp.RE; -import gnu.regexp.REException; +import java.util.ArrayList; import java.util.List; -import java.util.Vector; public class StringRoutines { private StringRoutines() { } - static int indexOfCharacters(String aString, char[] aCharacters, int aFrom) { + public static int indexOfCharacters(String aString, char[] aCharacters, int aFrom) { int i; int result=-1; int position; @@ -58,13 +55,15 @@ public class StringRoutines { return result; } - static String replaceStringCharacters(String aText, char[] aCharactersToReplace, String[] aStringsToSubstitute) { + public static String replaceStringCharacters(String aText, char[] aCharactersToReplace, String[] aStringsToSubstitute) { + if (aText==null) + return null; + int position, nextPosition; int i; StringBuffer result = new StringBuffer(); position=0; - do { nextPosition = StringRoutines.indexOfCharacters(aText, aCharactersToReplace, position); @@ -86,14 +85,55 @@ public class StringRoutines { return result.toString(); } + /** + */ + + public static String replaceEscapedStringCharacters(String aText, char anEscapeCharacter, char[] aCharactersToReplace, String[] aStringsToSubstitute) { + if (aText==null) + return null; + + int position, nextPosition; + int i; + StringBuffer result = new StringBuffer(); + position=0; + do { + nextPosition = aText.indexOf(anEscapeCharacter, position); + + if (nextPosition<0) + nextPosition = aText.length(); + + result.append(aText.substring(position, nextPosition)); + + if (nextPosition+1=0) { - result.add(aString.substring(previousPosition, position)); - previousPosition = position + aSeparator.length(); + if (aString!=null) { + while ( (position = aString.indexOf(aSeparator, previousPosition)) >= 0) { + result.add(aString.substring(previousPosition, position)); + previousPosition = position + aSeparator.length(); + } + result.add(aString.substring(previousPosition, aString.length())); } - result.add(aString.substring(previousPosition, aString.length())); - return result; } @@ -193,10 +238,9 @@ public class StringRoutines { * @return */ public static List separateString(String aString, String aSeparator) { - List result= new Vector(); + List result= new ArrayList(); int previousPosition = 0; int position; - int endOfNamePosition; if((position = aString.indexOf(aSeparator, previousPosition))>=0) { result.add(aString.substring(previousPosition, position)); @@ -207,4 +251,98 @@ public class StringRoutines { return result; } -} \ No newline at end of file + + /** + * Separates a string based on a separator, taking into account an escape character: + * seperateString("a:/::b", ":", "/"); will lead to + * a List with 3 Strings: "a", ":" and "b" + * + * @param aString The string to split + * @param aSeparator + * @return + */ + + public static List splitStringWithEscape(String aString, char aSeparator, char anEscape) { + List result= new ArrayList(); + int previousPosition = 0; + int position; + + StringBuffer currentItem = new StringBuffer(); + + if (aString!=null && aString.length()>0) { + while ((position = indexOfCharacters(aString, new char[] {aSeparator, anEscape}, previousPosition))>=0) { + currentItem.append(aString.substring(previousPosition, position)); + + if (aString.charAt(position)==aSeparator) { + result.add(currentItem.toString()); + currentItem.delete(0, currentItem.length()); + } + else { + if (aString.length()>position+1) { + position=position+1; + currentItem.append(aString.charAt(position)); + } + else { + currentItem.append(aString.charAt(position)); + } + } + previousPosition = position + 1; + } + currentItem.append(aString.substring(previousPosition, aString.length())); + result.add(currentItem.toString()); + } + + return result; + } + + public static String replicateString(String aString, int aCount) { + StringBuffer result = new StringBuffer(); + + for (int i=0; i=0; digit--) { + int value = (int) (aData >> (digit*4)) & 0xf; + result.append(HEX_CHARACTERS[value]); + } + + return result.toString(); + } + + public static String convertToHex(byte[] aData) { + StringBuffer result = new StringBuffer(); + + for (int i = 0; i