svn commit: r645305 - /xml/xindice/trunk/java/src/org/apache/xindice/util/StringUtilities.java

[email protected]
Newsgroups gmane.text.xml.xindice.devel
Message-ID <[email protected]>
Author: natalia
Date: Sun Apr  6 13:42:36 2008
New Revision: 645305

URL: http://svn.apache.org/viewvc?rev=645305&view=rev
Log:
New method for escaping path in URL

Modified:
    xml/xindice/trunk/java/src/org/apache/xindice/util/StringUtilities.java

Modified: xml/xindice/trunk/java/src/org/apache/xindice/util/StringUtilities.java
URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/src/org/apache/xindice/util/StringUtilities.java?rev=645305&r1=645304&r2=645305&view=diff
==============================================================================
--- xml/xindice/trunk/java/src/org/apache/xindice/util/StringUtilities.java (original)
+++ xml/xindice/trunk/java/src/org/apache/xindice/util/StringUtilities.java Sun Apr  6 13:42:36 2008
@@ -20,6 +20,7 @@
 package org.apache.xindice.util;
 
 import java.util.StringTokenizer;
+import java.io.UnsupportedEncodingException;
 
 /**
  * StringUtilities provides a set of commonly used String parsing and
@@ -28,6 +29,7 @@
  * @version $Revision$, $Date$
  */
 public final class StringUtilities {
+    private static final String ALLOWED_CHARS = "$-_.+!*'(),;/:@&=";
 
     private StringUtilities() {
     }
@@ -225,67 +227,46 @@
         }
     }
 
+    public static boolean isBlank(String str) {
+        for (int i = 0; i < str.length(); i++) {
+            if (!Character.isWhitespace(str.charAt(i))) {
+                return false;
+            }
+        }
+
+        return true;
+    }
+
     /**
-     * Converts input text into its XML representation by escaping all special symbols,
-     * if any are present.
-     *
-     * @param text Input string
-     * @return String with all the special symbols escaped 
+     * Escapes a path in URL by converting all unsafe characters to a sequence
+     * of %xx according to RFC 1738 (Uniform Resource Locators (URL)). It is
+     * similar, but not identical to URLEncoder.
+     * @param path String representation of a URL path to be escaped
+     * @return Escaped path
      */
-    public static String escapeXml(String text) {
-        char[] value = text.toCharArray();
+    public static String escapePath(String path) {
         StringBuffer buf = new StringBuffer();
-        int start = 0;
-        int len = 0;
-
-        for (int i = 0; i < value.length; i++) {
-            String outval = null;
 
-            switch (value[i]) {
-                case '&':
-                    outval = "&amp;";
-                    break;
-                case '\'':
-                    outval = "&apos;";
-                    break;
-                case '\"':
-                    outval = "&quot;";
-                    break;
-                case '<':
-                    outval = "&lt;";
-                    break;
-                case '>':
-                    outval = "&gt;";
-                    break;
-                default :
-                    len++;
-                    break;
-            }
+        for (int i = 0; i < path.length(); i++) {
+            char ch = path.charAt(i);
 
-            if (outval != null) {
-                if (len > 0) {
-                    buf.append(value, start, len);
+            if ((ch < 'A' || ch > 'Z') && (ch < 'a' || ch > 'z') && (ch < '0' || ch > '9') &&
+                ALLOWED_CHARS.indexOf(ch) < 0) {
+                try {
+                    byte[] b = Character.toString(ch).getBytes("utf-8");
+                    for (int j = 0; j < b.length; j++) {
+                        char upper = Character.forDigit(((b[j] >> 4) & 0xF), 16);
+                        char lower = Character.forDigit((b[j] & 0xF), 16);
+                        buf.append('%').append(upper).append(lower);
+                    }
+                } catch (UnsupportedEncodingException e) {
+                    // won't happen
                 }
-                buf.append(outval);
-                start = i + 1;
-                len = 0;
+            } else {
+                buf.append(ch);
             }
         }
 
-        if (len > 0) {
-            buf.append(value, start, len);
-        }
-
         return buf.toString();
-    }
-
-    public static boolean isBlank(String str) {
-        for (int i = 0; i < str.length(); i++) {
-            if (!Character.isWhitespace(str.charAt(i))) {
-                return false;
-            }
-        }
-
-        return true;
     }
 }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.