jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ang...@apache.org
Subject svn commit: r738474 - in /jackrabbit/trunk/jackrabbit-jcr-commons/src: main/java/org/apache/jackrabbit/util/Text.java test/java/org/apache/jackrabbit/util/TextTest.java
Date Wed, 28 Jan 2009 13:06:24 GMT
Author: angela
Date: Wed Jan 28 13:06:23 2009
New Revision: 738474

URL: http://svn.apache.org/viewvc?rev=738474&view=rev
Log:
- JCR-1958: Enhanced JCR remoting (work in progress)
- javadoc issue with Text.escapeIllegalXpathSearchChars

Modified:
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/Text.java
    jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/util/TextTest.java

Modified: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/Text.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/Text.java?rev=738474&r1=738473&r2=738474&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/Text.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/Text.java
Wed Jan 28 13:06:23 2009
@@ -487,7 +487,7 @@
      * A search string like 'test?' will run into a ParseException
      * documented in http://issues.apache.org/jira/browse/JCR-1248
      * 
-     * @param string the string to encode
+     * @param s the string to encode
      * @return the escaped string
      */
     public static String escapeIllegalXpathSearchChars(String s) {
@@ -730,7 +730,7 @@
      *                                  variable is not found
      */
     public static String replaceVariables(Properties variables, String value,
-                                   boolean ignoreMissing)
+                                          boolean ignoreMissing)
             throws IllegalArgumentException {
         StringBuffer result = new StringBuffer();
 
@@ -764,4 +764,66 @@
         return result.toString();
     }
 
+    /**
+     * Generate a valid JSON string from the given <code>str</code>.
+     * 
+     * @param str A String
+     * @return JSON string surrounded by double quotes.
+     * @see <a href="http://tools.ietf.org/html/rfc4627">RFC 4627</a>
+     */
+    public static String getJSONString(String str) {
+        if (str == null || str.length() == 0) {
+            return "\"\"";
+        }
+
+        int len = str.length();
+        StringBuffer sb = new StringBuffer(len + 2);
+        // leading quote
+        sb.append('"');
+        // append passed string escaping characters as required
+        for (int i = 0; i < len; i++) {
+            char c = str.charAt(i);
+            switch (c) {
+                // reverse solidus and double quote
+                case '\\':
+                case '"':
+                    sb.append('\\').append(c);
+                    break;
+                // tab, line breaking chars and backspace
+                case '\b':
+                    sb.append("\\b");
+                    break;
+                case '\f':
+                    sb.append("\\f");
+                    break;
+                case '\n':
+                    sb.append("\\n");
+                    break;
+                case '\r':
+                    sb.append("\\r");
+                    break;
+                case '\t':
+                    sb.append("\\t");
+                    break;
+                // other control characters and 'unescaped'
+                default:
+                    if (c < 32) {
+                        // control characters except those already covered above.
+                        String uc = Integer.toHexString(c);
+                        sb.append("\\u");
+                        int uLen = uc.length();
+                        while (uLen++ < 4) {
+                            sb.append('0');
+                        }
+                        sb.append(uc);
+                    } else {
+                        // unescaped = %x20-21 / %x23-5B / %x5D-10FFFF
+                        sb.append(c);
+                    }
+            }
+        }
+        // trailing quote
+        sb.append('"');
+        return sb.toString();
+    }
 }

Modified: jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/util/TextTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/util/TextTest.java?rev=738474&r1=738473&r2=738474&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/util/TextTest.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/util/TextTest.java
Wed Jan 28 13:06:23 2009
@@ -23,6 +23,8 @@
 import java.util.List;
 import java.util.ArrayList;
 import java.util.Iterator;
+import java.util.Map;
+import java.util.HashMap;
 
 /**
  * Test cases for the Text utility class.
@@ -153,6 +155,33 @@
         assertInvalidUnescape("%%%"); // not a number
     }
 
+    public void testGetJSONString() {
+        Map m = new HashMap();
+        m.put("abc", "\"abc\"");
+        m.put("a \"b\" c", "\"a \\\"b\\\" c\"");
+        m.put("a\tb\rc\nd\fe\b", "\"a\\tb\\rc\\nd\\fe\\b\"");
+        m.put("\\abc", "\"\\\\abc\"");
+        m.put("abc", "\"abc\"");
+
+        // non-printable ascii other than those treated (\t,\r,\n)
+        m.put(String.valueOf((char) 7), "\"\\u0007\"");
+        m.put(String.valueOf((char) 30), "\"\\u001e\"");
+
+        // chinese
+        m.put("\u4e00a\u4e8cb\u4e09c", "\"\u4e00a\u4e8cb\u4e09c\"");
+        /* arabic */
+        m.put("\u062c\u062f\u064a\u062f", "\"\u062c\u062f\u064a\u062f\"");
+        /* Ñaçb?c */
+        m.put("\u00d1a\u00e7b\u0416c", "\"\u00d1a\u00e7b\u0416c\"");
+        // âèøü
+        m.put("âèøü", "\"\u00e2\u00e8\u00f8\u00fc\"");
+
+        for (Iterator it = m.keySet().iterator(); it.hasNext();) {
+            String key = it.next().toString();
+            assertEquals(m.get(key).toString(), Text.getJSONString(key));
+        }
+    }
+    
     private void assertInvalidUnescape(String string) {
         try {
             Text.unescape(string);
@@ -162,4 +191,4 @@
         }
     }
 
-}
+}
\ No newline at end of file



Mime
View raw message