support for CAPTCHAs
[mir.git] / source / mir / util / StringRoutines.java
index 16e9846..55d5edf 100755 (executable)
-/*\r
- * Copyright (C) 2001, 2002 The Mir-coders group\r
- *\r
- * This file is part of Mir.\r
- *\r
- * Mir is free software; you can redistribute it and/or modify\r
- * it under the terms of the GNU General Public License as published by\r
- * the Free Software Foundation; either version 2 of the License, or\r
- * (at your option) any later version.\r
- *\r
- * Mir is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
- * GNU General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU General Public License\r
- * along with Mir; if not, write to the Free Software\r
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
- *\r
- * In addition, as a special exception, The Mir-coders gives permission to link\r
- * the code of this program with  any library licensed under the Apache Software License,\r
- * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library\r
- * (or with modified versions of the above that use the same license as the above),\r
- * and distribute linked combinations including the two.  You must obey the\r
- * GNU General Public License in all respects for all of the code used other than\r
- * the above mentioned libraries.  If you modify this file, you may extend this\r
- * exception to your version of the file, but you are not obligated to do so.\r
- * If you do not wish to do so, delete this exception statement from your version.\r
- */\r
-package mir.util;\r
-\r
-import gnu.regexp.RE;\r
-import gnu.regexp.REException;\r
-\r
-import java.util.List;\r
-import java.util.Vector;\r
-\r
-public class StringRoutines {\r
-\r
-  private StringRoutines() {\r
-  }\r
-\r
-  public static int indexOfCharacters(String aString, char[] aCharacters, int aFrom) {\r
-    int i;\r
-    int result=-1;\r
-    int position;\r
-\r
-    for (i=0; i<aCharacters.length ; i++) {\r
-      position = aString.indexOf(aCharacters[i], aFrom);\r
-\r
-      if (position != -1 && ( result == -1 || position < result )) {\r
-        result = position;\r
-      }\r
-    }\r
-\r
-    return result;\r
-  }\r
-\r
-  public static String replaceStringCharacters(String aText, char[] aCharactersToReplace, String[] aStringsToSubstitute) {\r
-    if (aText==null)\r
-      return null;\r
-\r
-    int position, nextPosition;\r
-    int i;\r
-    StringBuffer result = new StringBuffer();\r
-\r
-    position=0;\r
-    do {\r
-      nextPosition = StringRoutines.indexOfCharacters(aText, aCharactersToReplace, position);\r
-\r
-      if (nextPosition<0)\r
-        nextPosition = aText.length();\r
-\r
-      result.append(aText.substring(position, nextPosition));\r
-\r
-      if (nextPosition<aText.length())\r
-        for (i=0; i<aCharactersToReplace.length; i++) {\r
-          if (aCharactersToReplace[i] == aText.charAt(nextPosition)) {\r
-            result.append(aStringsToSubstitute[i]);\r
-            break;\r
-          }\r
-        }\r
-      position=nextPosition+1;\r
-    }\r
-    while (nextPosition<aText.length()) ;\r
-\r
-    return result.toString();\r
-  }\r
-  /**\r
-   *\r
-   * @param aText\r
-   * @param anEscapeCharacater\r
-   * @param aCharactersToReplace\r
-   * @param aStringsToSubstitute\r
-   * @return\r
-   */\r
-\r
-  public static String replaceEscapedStringCharacters(String aText, char anEscapeCharacter, char[] aCharactersToReplace, String[] aStringsToSubstitute) {\r
-    if (aText==null)\r
-      return null;\r
-\r
-    int position, nextPosition;\r
-    int i;\r
-    StringBuffer result = new StringBuffer();\r
-\r
-    position=0;\r
-    do {\r
-      nextPosition = aText.indexOf(anEscapeCharacter, position);\r
-\r
-      if (nextPosition<0)\r
-        nextPosition = aText.length();\r
-\r
-      result.append(aText.substring(position, nextPosition));\r
-\r
-      if (nextPosition+1<aText.length()) {\r
-        nextPosition = nextPosition+1;\r
-\r
-        boolean found = false;\r
-        for (i = 0; i < aCharactersToReplace.length; i++) {\r
-          if (aCharactersToReplace[i] == aText.charAt(nextPosition)) {\r
-            result.append(aStringsToSubstitute[i]);\r
-            found=true;\r
-            break;\r
-          }\r
-        }\r
-\r
-        if (!found) {\r
-          result.append(aText.charAt(nextPosition));\r
-        }\r
-      }\r
-      position=nextPosition+1;\r
-    }\r
-    while (nextPosition<aText.length()) ;\r
-\r
-    return result.toString();\r
-  }\r
-\r
-  public static String interpretAsString(Object aValue) throws Exception {\r
-    if (aValue instanceof String)\r
-      return (String) aValue;\r
-\r
-    if (aValue instanceof Integer)\r
-      return ((Integer) aValue).toString();\r
-\r
-    if (aValue == null)\r
-      return "";\r
-\r
-    throw new Exception("String expected, "+aValue+" found");\r
-  }\r
-\r
-  public static int interpretAsInteger(Object aValue) throws Exception {\r
-    if (aValue instanceof Integer)\r
-      return ((Integer) aValue).intValue();\r
-\r
-    if (aValue instanceof String)\r
-      try {\r
-        return Integer.parseInt((String) aValue);\r
-      }\r
-      catch (Throwable t) {\r
-        throw new Exception("Integer expected, "+aValue+" found");\r
-      }\r
-\r
-    throw new Exception("Integer expected, "+aValue+" found");\r
-  }\r
-\r
-  /**\r
-   *\r
-   * @param aSource\r
-   * @param aSearchExpression\r
-   * @param aReplacement\r
-   * @return\r
-   * @throws Exception\r
-   */\r
-  public static String performRegularExpressionReplacement(String aSource,\r
-      String aSearchExpression, String aReplacement) throws UtilExc {\r
-    try {\r
-      RE regularExpression;\r
-\r
-      regularExpression = new RE(aSearchExpression);\r
-\r
-      return regularExpression.substituteAll(aSource, aReplacement);\r
-    }\r
-    catch (Throwable t) {\r
-      throw new UtilFailure("StringRoutines.performRegularExpressionReplacement: " + t.toString(), t);\r
-    }\r
-  }\r
-\r
-  public static String performCaseInsensitiveRegularExpressionReplacement(String aSource,\r
-      String aSearchExpression, String aReplacement) throws UtilExc {\r
-    try {\r
-      RE regularExpression;\r
-\r
-      regularExpression = new RE(aSearchExpression, RE.REG_ICASE);\r
-\r
-      return regularExpression.substituteAll(aSource, aReplacement);\r
-    }\r
-    catch (Throwable t) {\r
-      throw new UtilFailure("StringRoutines.performRegularExpressionReplacement: " + t.toString(), t);\r
-    }\r
-  }\r
-\r
-  /**\r
-   *\r
-   * @param aSource\r
-   * @param aSearchExpression\r
-   * @return\r
-   * @throws REException\r
-   */\r
-  public static boolean performRegularExpressionSearch(String aSource,\r
-      String aSearchExpression) throws UtilExc {\r
-    try {\r
-      RE regularExpression;\r
-\r
-      regularExpression = new RE(aSearchExpression);\r
-\r
-      return regularExpression.isMatch(aSource);\r
-    }\r
-    catch (Throwable t) {\r
-      throw new UtilFailure("StringRoutines.performRegularExpressionSearch: " + t.toString(), t);\r
-    }\r
-  }\r
-\r
-  /**\r
-   * Separates a string based on a separator:\r
-   *     <code>seperateString("a:b:c", ":");</code> will lead to\r
-   *     a List with 3 Strings: <code>"a"</code>, <code>"b"</code> and <code>"c"</code>\r
-   *\r
-   * @param aString     The string to split\r
-   * @param aSeparator\r
-   * @return\r
-   */\r
-\r
-  public static List splitString(String aString, String aSeparator) {\r
-    List result= new Vector();\r
-    int previousPosition = 0;\r
-    int position;\r
-    int endOfNamePosition;\r
-\r
-    if (aString!=null) {\r
-      while ( (position = aString.indexOf(aSeparator, previousPosition)) >= 0) {\r
-        result.add(aString.substring(previousPosition, position));\r
-        previousPosition = position + aSeparator.length();\r
-      }\r
-      result.add(aString.substring(previousPosition, aString.length()));\r
-    }\r
-\r
-    return result;\r
-  }\r
-\r
-  /**\r
-   * Separates a String into at most 2 parts based on a separator:\r
-   * <ul>\r
-   *   <li>\r
-   *     <code>seperateString("a:b:c", ":");</code> will lead to\r
-   *     a List with 2 Strings: <code>"a"</code> and <code>"b:c"</code>\r
-   *   <li>\r
-   *     <code>seperateString("abc", ":");</code> will lead to\r
-   *     a List with a single String: <code>"abc"</code>\r
-   * </ul>\r
-   *\r
-   *\r
-   * @param aString\r
-   * @param aSeparator\r
-   * @return\r
-   */\r
-  public static List separateString(String aString, String aSeparator) {\r
-    List result= new Vector();\r
-    int previousPosition = 0;\r
-    int position;\r
-\r
-    if((position = aString.indexOf(aSeparator, previousPosition))>=0) {\r
-      result.add(aString.substring(previousPosition, position));\r
-      previousPosition = position + aSeparator.length();\r
-    }\r
-\r
-    result.add(aString.substring(previousPosition, aString.length()));\r
-\r
-    return result;\r
-  }\r
-\r
-  public static List splitStringWithEscape(String aString, char aSeparator, char anEscape) {\r
-    List result= new Vector();\r
-    int previousPosition = 0;\r
-    int position;\r
-    int endOfNamePosition;\r
-    StringBuffer currentItem = new StringBuffer();\r
-\r
-    if (aString!=null) {\r
-      while ((position = indexOfCharacters(aString, new char[] {aSeparator, anEscape}, previousPosition))>=0) {\r
-        currentItem.append(aString.substring(previousPosition, position));\r
-\r
-        if (aString.charAt(position)==aSeparator) {\r
-          result.add(currentItem.toString());\r
-          currentItem.delete(0, currentItem.length());\r
-        }\r
-        else {\r
-          currentItem.append(aString.charAt(position));\r
-          if (aString.length()>position+1) {\r
-            position=position+1;\r
-            currentItem.append(aString.charAt(position));\r
-          }\r
-        }\r
-        previousPosition = position + 1;\r
-      }\r
-      currentItem.append(aString.substring(previousPosition, aString.length()));\r
-      result.add(currentItem.toString());\r
-    }\r
-\r
-    return result;\r
-  }\r
-\r
-  public static String replicateString(String aString, int aCount) {\r
-    StringBuffer result = new StringBuffer();\r
-\r
-    for (int i=0; i<aCount; i++)\r
-      result.append(aString);\r
-\r
-    return result.toString();\r
-  }\r
-\r
-  public static String replicateChar(char aCharacter, int aCount) {\r
-    char result[] = new char[aCount];\r
-\r
-    for (int i=0; i<aCount; i++)\r
-      result[i]= aCharacter;\r
-\r
-    return new String(result);\r
-  }\r
-\r
-  public static String padStringLeft(String aString, int aLength, char aPadCharacter) {\r
-    if (aString.length()<aLength)\r
-      return replicateChar(aPadCharacter, aLength-aString.length()) + aString;\r
-    else\r
-      return aString;\r
-  }\r
-\r
-  private static final char HEX_CHARACTERS[] = {\r
-      '0', '1', '2', '3', '4', '5', '6', '7',\r
-      '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'\r
-  };\r
-\r
-  public static String convertToHex(long aData, int aNumberOfDigits) {\r
-    StringBuffer result = new StringBuffer();\r
-\r
-    for (int digit = aNumberOfDigits-1; digit>=0; digit--) {\r
-      int value = (int) (aData >> (digit*4)) & 0xf;\r
-      result.append(HEX_CHARACTERS[value]);\r
-    }\r
-\r
-    return result.toString();\r
-  }\r
-\r
-  public static String convertToHex(byte[] aData) {\r
-    StringBuffer result = new StringBuffer();\r
-\r
-    for (int i = 0; i<aData.length; i++) {\r
-      result.append(convertToHex(aData[i], 2));\r
-\r
-    }\r
-\r
-    return result.toString();\r
-  }\r
-}\r
+/*
+ * Copyright (C) 2001, 2002 The Mir-coders group
+ *
+ * This file is part of Mir.
+ *
+ * Mir is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Mir is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Mir; if not, write to the Free Software
+ * 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  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 org.apache.oro.text.regex.*;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class StringRoutines {
+
+  private StringRoutines() {
+  }
+
+  public static int indexOfCharacters(String aString, char[] aCharacters, int aFrom) {
+    int i;
+    int result=-1;
+    int position;
+
+    for (i=0; i<aCharacters.length ; i++) {
+      position = aString.indexOf(aCharacters[i], aFrom);
+
+      if (position != -1 && ( result == -1 || position < result )) {
+        result = position;
+      }
+    }
+
+    return result;
+  }
+
+  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);
+
+      if (nextPosition<0)
+        nextPosition = aText.length();
+
+      result.append(aText.substring(position, nextPosition));
+
+      if (nextPosition<aText.length())
+        for (i=0; i<aCharactersToReplace.length; i++) {
+          if (aCharactersToReplace[i] == aText.charAt(nextPosition)) {
+            result.append(aStringsToSubstitute[i]);
+            break;
+          }
+        }
+      position=nextPosition+1;
+    }
+    while (nextPosition<aText.length()) ;
+
+    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<aText.length()) {
+        nextPosition = nextPosition+1;
+
+        boolean found = false;
+        for (i = 0; i < aCharactersToReplace.length; i++) {
+          if (aCharactersToReplace[i] == aText.charAt(nextPosition)) {
+            result.append(aStringsToSubstitute[i]);
+            found=true;
+            break;
+          }
+        }
+
+        if (!found) {
+          result.append(aText.charAt(nextPosition));
+        }
+      }
+      position=nextPosition+1;
+    }
+    while (nextPosition<aText.length()) ;
+
+    return result.toString();
+  }
+
+  public static String interpretAsString(Object aValue) throws Exception {
+    if (aValue instanceof String)
+      return (String) aValue;
+
+    if (aValue instanceof Integer)
+      return aValue.toString();
+
+    if (aValue == null)
+      return "";
+
+    throw new Exception("String expected, "+aValue+" found");
+  }
+
+  public static int interpretAsInteger(Object aValue) throws Exception {
+    if (aValue instanceof Integer)
+      return ((Integer) aValue).intValue();
+
+    if (aValue instanceof String)
+      try {
+        return Integer.parseInt((String) aValue);
+      }
+      catch (Throwable t) {
+        throw new Exception("Integer expected, "+aValue+" found");
+      }
+
+    throw new Exception("Integer expected, "+aValue+" found");
+  }
+
+  public static String performRegularExpressionReplacement(String aSource, String aSearchExpression, String aReplacement) {
+    try {
+      Pattern pattern = new Perl5Compiler().compile(aSearchExpression);
+
+      return Util.substitute(
+              new Perl5Matcher(), pattern, new Perl5Substitution(aReplacement), aSource, Util.SUBSTITUTE_ALL);
+    }
+    catch (MalformedPatternException t) {
+      throw new UtilFailure("Invalid regular expression", t);
+    }
+  }
+
+  /**
+   * Separates a string based on a separator:
+   *     <code>seperateString("a:b:c", ":");</code> will lead to
+   *     a List with 3 Strings: <code>"a"</code>, <code>"b"</code> and <code>"c"</code>
+   *
+   * @param aString     The string to split
+   * @param aSeparator
+   * @return
+   */
+
+  public static List splitString(String aString, String aSeparator) {
+    List result= new ArrayList();
+    int previousPosition = 0;
+    int position;
+
+    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()));
+    }
+
+    return result;
+  }
+
+  /**
+   * Separates a String into at most 2 parts based on a separator:
+   * <ul>
+   *   <li>
+   *     <code>seperateString("a:b:c", ":");</code> will lead to
+   *     a List with 2 Strings: <code>"a"</code> and <code>"b:c"</code>
+   *   <li>
+   *     <code>seperateString("abc", ":");</code> will lead to
+   *     a List with a single String: <code>"abc"</code>
+   * </ul>
+   *
+   *
+   * @param aString
+   * @param aSeparator
+   * @return
+   */
+  public static List separateString(String aString, String aSeparator) {
+    List result= new ArrayList();
+    int previousPosition = 0;
+    int position;
+
+    if((position = aString.indexOf(aSeparator, previousPosition))>=0) {
+      result.add(aString.substring(previousPosition, position));
+      previousPosition = position + aSeparator.length();
+    }
+
+    result.add(aString.substring(previousPosition, aString.length()));
+
+    return result;
+  }
+
+  /**
+   * Separates a string based on a separator, taking into account an escape character:
+   *     <code>seperateString("a:/::b", ":", "/");</code> will lead to
+   *     a List with 3 Strings: <code>"a"</code>, <code>":"</code> and <code>"b"</code>
+   *
+   * @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<aCount; i++)
+      result.append(aString);
+
+    return result.toString();
+  }
+
+  public static String replicateChar(char aCharacter, int aCount) {
+    char result[] = new char[aCount];
+
+    for (int i=0; i<aCount; i++)
+      result[i]= aCharacter;
+
+    return new String(result);
+  }
+
+  public static String padStringLeft(String aString, int aLength, char aPadCharacter) {
+    if (aString.length()<aLength)
+      return replicateChar(aPadCharacter, aLength-aString.length()) + aString;
+               return aString;
+  }
+
+  private static final char HEX_CHARACTERS[] = {
+      '0', '1', '2', '3', '4', '5', '6', '7',
+      '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
+  };
+
+  public static String convertToHex(long aData, int aNumberOfDigits) {
+    StringBuffer result = new StringBuffer();
+
+    for (int digit = aNumberOfDigits-1; digit>=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<aData.length; i++) {
+      result.append(convertToHex(aData[i], 2));
+
+    }
+
+    return result.toString();
+  }
+}