re-introduce leaner/simpler encodeHTML(String aText) method, we need it for the admin...
authormh <mh>
Tue, 10 Dec 2002 09:07:50 +0000 (09:07 +0000)
committermh <mh>
Tue, 10 Dec 2002 09:07:50 +0000 (09:07 +0000)
source/mir/misc/StringUtil.java

index 4315a45..0e96f4c 100755 (executable)
@@ -40,7 +40,7 @@ import  gnu.regexp.*;
 /**
  * Statische Hilfsmethoden zur Stringbehandlung
  *
- * @version $Id: StringUtil.java,v 1.23.2.3 2002/12/09 06:23:38 mh Exp $
+ * @version $Id: StringUtil.java,v 1.23.2.4 2002/12/10 09:07:50 mh Exp $
  * @author rk, mir-coders group
  *
  */
@@ -413,6 +413,41 @@ public final class StringUtil {
                return buf.toString();
        }
 
+  /*
+   * replaces characters that cannot appera in HTML with escaped equivalents.
+   */
+  public static String encodeHTML(String aText) {
+    final char[] CHARACTERS_TO_ESCAPE = { '&', '<', '>', '"', '\'' };
+    final String[] ESCAPE_CODES = { "&amp;", "&lt;", "&gt;", "&quot;", "&apos;" };
+
+    int position, nextPosition;
+    int i;
+    StringBuffer result = new StringBuffer();
+
+    position=0;
+
+    do {
+      nextPosition = indexOfCharacters(aText, CHARACTERS_TO_ESCAPE, position);
+
+      if (nextPosition<0)
+        nextPosition = aText.length();
+
+      result.append(aText.substring(position, nextPosition));
+
+      if (nextPosition<aText.length())
+        for (i=0; i<CHARACTERS_TO_ESCAPE.length; i++) {
+          if (CHARACTERS_TO_ESCAPE[i] == aText.charAt(nextPosition)) {
+            result.append(ESCAPE_CODES[i]);
+            break;
+          }
+        }
+      position=nextPosition+1;
+    }
+    while (nextPosition<aText.length()) ;
+
+    return result.toString();
+  }
+
        /**
         * Wandelet String in byte[] um.
         * @param s
@@ -618,11 +653,11 @@ public final class StringUtil {
                return new String(a);
        }
 
-               /**
-        * Findet <code>element</code> im String-Array <code>array</code>
+        /**
+        * Finds <code>element</code> in String-Array <code>array</code>
         * @param array
         * @param element
-        * @return Fundstelle als int oder -1
+        * @return Position where the element was found or -1
         */
        public static int indexOf(String[] array, String element) {
                if (array != null)
@@ -632,6 +667,32 @@ public final class StringUtil {
                return -1;
        }
 
+        /**
+   * Finds position of first in <code>aCharacters</code> array in String
+   * <code>aString</code> starting from position <code>aFrom</code>
+   *
+        * @param aString
+        * @param aCharacters
+   * @param aFrom
+        * @return Position where the element was found or -1
+        */
+  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;
+  }
+
+
        /**
         * Testet auf Vorkommen von <code>element</code> in <code>array</code>
         * @param array String-Array
@@ -677,6 +738,21 @@ public final class StringUtil {
                }
        }
 
+  public static String interpretAsString(Object aValue) throws Exception {
+    if (aValue instanceof String)
+      return (String) aValue;
+
+    if (aValue instanceof Integer)
+      return ((Integer) aValue).toString();
+
+    if (aValue == null)
+      return "";
+
+    throw new Exception("String expected, "+aValue+" found");
+  }
+
+
+
        /**
         * Liefert Defaultwert def zurück, wenn s nicht zu einem float geparsed werden kann.
         * @param s
@@ -823,6 +899,20 @@ public final class StringUtil {
                }
        }
 
+        /**
+        *  deleteHTMLTableTags
+        *  this method deletes all <table>, <tr> and <td>-tags
+        */
+       public static final String deleteHTMLTableTags(String haystack) {
+               try {
+                       RE regex = new RE("</?(table|td|tr)>",RE.REG_ICASE);
+                       haystack = regex.substituteAll(haystack,"");
+                       return haystack;
+               } catch(REException ex){
+                       return null;
+               }
+       }
+
        /**
         * this method deletes all html tags
         */