jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r1170635 - in /jackrabbit/sandbox/jackrabbit-mk: jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/ jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/util/ jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/sp...
Date Wed, 14 Sep 2011 14:06:34 GMT
Author: mduerig
Date: Wed Sep 14 14:06:33 2011
New Revision: 1170635

URL: http://svn.apache.org/viewvc?rev=1170635&view=rev
Log:
Microkernel based Jackrabbit prototype (WIP) 
refactor: consolidate utils

Modified:
    jackrabbit/sandbox/jackrabbit-mk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/Text.java
    jackrabbit/sandbox/jackrabbit-mk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/util/TextTest.java
    jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/identifier/AbstractIdFactory.java
    jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/CommitMessage.java
    jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/util/

Modified: jackrabbit/sandbox/jackrabbit-mk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/Text.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-mk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/Text.java?rev=1170635&r1=1170634&r2=1170635&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-mk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/Text.java
(original)
+++ jackrabbit/sandbox/jackrabbit-mk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/util/Text.java
Wed Sep 14 14:06:33 2011
@@ -109,6 +109,64 @@ public class Text {
     }
 
     /**
+     * Escape all {@code target} characters of string {@code s} with the given
+     * {@code escape} character.
+     *
+     * @param s  string to escape
+     * @param target  characters to escape
+     * @param escape  escape character prepended to all target characters
+     * @return  escaped string
+     */
+    public static String escape(String s, char target, char escape) {
+        if (s == null) {
+            return null;
+        }
+
+        StringBuilder sb = new StringBuilder();
+        for (int k = 0; k < s.length(); k++) {
+            char c = s.charAt(k);
+            if (c == target || c == escape) {
+                sb.append(escape);
+            }
+            sb.append(c);
+        }
+
+        return sb.toString();
+    }
+
+    /**
+     * Unescape all characters in string {@code s} which are escaped with the
+     * given {@code escape} character.
+     *
+     * @param s  string to unescape
+     * @param escape  escape character prepended to all target characters
+     * @return  unescaped string
+     */
+    public static String unescape(String s, char escape) {
+        if (s == null) {
+            return null;
+        }
+
+        boolean escaping = false;
+        StringBuilder sb = new StringBuilder();
+        for (int k = 0; k < s.length(); k++) {
+            char c = s.charAt(k);
+            if (escaping) {
+                sb.append(c);
+                escaping = false;
+            }
+            else if (c == escape) {
+                escaping = true;
+            }
+            else {
+                sb.append(c);
+            }
+        }
+
+        return sb.toString();
+    }
+
+    /**
      * returns an array of strings decomposed of the original string, split at
      * every occurrence of 'ch'. if 2 'ch' follow each other with no intermediate
      * characters, empty "" entries are avoided.
@@ -314,8 +372,8 @@ public class Text {
      * @return the escaped string
      * @throws NullPointerException if {@code string} is {@code null}.
      */
-    public static String escape(String string, char escape) {
-        return escape(string, escape, false);
+    public static String urlEncode(String string, char escape) {
+        return urlEncode(string, escape, false);
     }
 
     /**
@@ -331,7 +389,7 @@ public class Text {
      * @return the escaped string
      * @throws NullPointerException if {@code string} is {@code null}.
      */
-    public static String escape(String string, char escape, boolean isPath) {
+    public static String urlEncode(String string, char escape, boolean isPath) {
         try {
             BitSet validChars = isPath ? URISaveEx : URISave;
             byte[] bytes = string.getBytes("utf-8");
@@ -361,23 +419,23 @@ public class Text {
      * @return the escaped string
      * @throws NullPointerException if {@code string} is {@code null}.
      */
-    public static String escape(String string) {
-        return escape(string, '%');
+    public static String urlEncode(String string) {
+        return urlEncode(string, '%');
     }
 
     /**
      * Does a URL encoding of the {@code path}. The characters that
      * don't need encoding are those defined 'unreserved' in section 2.3 of
      * the 'URI generic syntax' RFC 2396. In contrast to the
-     * {@link #escape(String)} method, not the entire path string is escaped,
+     * {@link #urlEncode(String)} method, not the entire path string is escaped,
      * but every individual part (i.e. the slashes are not escaped).
      *
      * @param path the path to encode
      * @return the escaped path
      * @throws NullPointerException if {@code path} is {@code null}.
      */
-    public static String escapePath(String path) {
-        return escape(path, '%', true);
+    public static String urlEncodePath(String path) {
+        return urlEncode(path, '%', true);
     }
 
     /**
@@ -394,7 +452,7 @@ public class Text {
      *                                        or if not enough characters follow an
      *                                        escape character
      */
-    public static String unescape(String string, char escape)  {
+    public static String urlDecode(String string, char escape)  {
         try {
             byte[] utf8 = string.getBytes("utf-8");
 
@@ -435,8 +493,8 @@ public class Text {
      * @throws IllegalArgumentException       if the 2 characters following the escape
      *                                        character do not represent a hex-number.
      */
-    public static String unescape(String string) {
-        return unescape(string, '%');
+    public static String urlDecode(String string) {
+        return urlDecode(string, '%');
     }
 
     /**
@@ -505,7 +563,7 @@ public class Text {
      * Unescapes previously escaped jcr chars.
      * <p/>
      * Please note, that this does not exactly the same as the url related
-     * {@link #unescape(String)}, since it handles the byte-encoding
+     * {@link #urlDecode(String)}, since it handles the byte-encoding
      * differently.
      *
      * @param name the name to unescape

Modified: jackrabbit/sandbox/jackrabbit-mk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/util/TextTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-mk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/util/TextTest.java?rev=1170635&r1=1170634&r2=1170635&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-mk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/util/TextTest.java
(original)
+++ jackrabbit/sandbox/jackrabbit-mk/jackrabbit-jcr-commons/src/test/java/org/apache/jackrabbit/util/TextTest.java
Wed Sep 14 14:06:33 2011
@@ -16,12 +16,12 @@
  */
 package org.apache.jackrabbit.util;
 
+import junit.framework.TestCase;
+
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
 
-import junit.framework.TestCase;
-
 /**
  * Test cases for the Text utility class.
  */
@@ -143,16 +143,16 @@ public class TextTest extends TestCase {
         String escaped = testString
             .replaceAll("%", "%25")
             .replaceAll(" ", "%20");
-        String unescaped = Text.unescape(escaped);
+        String unescaped = Text.urlDecode(escaped);
         assertEquals(testString, unescaped);
 
-        escaped = Text.escape(testString);
-        unescaped = Text.unescape(escaped);
+        escaped = Text.urlEncode(testString);
+        unescaped = Text.urlDecode(escaped);
         assertEquals(testString, unescaped);
 
-        assertEquals("%", Text.unescape("%25"));
-        assertEquals("", Text.unescape(""));
-        assertEquals("\u4e2d\u56fd\u7684\u7f51\u9875", Text.unescape("\u4e2d\u56fd\u7684\u7f51\u9875"));
+        assertEquals("%", Text.urlDecode("%25"));
+        assertEquals("", Text.urlDecode(""));
+        assertEquals("\u4e2d\u56fd\u7684\u7f51\u9875", Text.urlDecode("\u4e2d\u56fd\u7684\u7f51\u9875"));
     }
 
     /**
@@ -175,7 +175,7 @@ public class TextTest extends TestCase {
 
     private void assertInvalidUnescape(String string) {
         try {
-            Text.unescape(string);
+            Text.urlDecode(string);
             fail("Text.unescape(" + string + ") should throw IllegalArgumentException");
         } catch (IllegalArgumentException expected) {
         } catch (RuntimeException unexpected) {

Modified: jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/identifier/AbstractIdFactory.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/identifier/AbstractIdFactory.java?rev=1170635&r1=1170634&r2=1170635&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/identifier/AbstractIdFactory.java
(original)
+++ jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi-commons/src/main/java/org/apache/jackrabbit/spi/commons/identifier/AbstractIdFactory.java
Wed Sep 14 14:06:33 2011
@@ -78,7 +78,7 @@ public abstract class AbstractIdFactory 
             return DELIMITER + path.toString();
         } else {
             StringBuilder bf = new StringBuilder();
-            bf.append(Text.escape(uniqueId, DELIMITER));
+            bf.append(Text.urlEncode(uniqueId, DELIMITER));
             bf.append(DELIMITER);
             bf.append(path.toString());
             return bf.toString();
@@ -98,7 +98,7 @@ public abstract class AbstractIdFactory 
             case 0:
                 return createNodeId((String) null, getPathFactory().create(jcrIdentifier.substring(1)));
             default:
-                String uniqueId = Text.unescape(jcrIdentifier.substring(0, pos), DELIMITER);
+                String uniqueId = Text.urlDecode(jcrIdentifier.substring(0, pos), DELIMITER);
                 Path path = getPathFactory().create(jcrIdentifier.substring(pos+1));
                 return createNodeId(uniqueId, path);
         }        

Modified: jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/CommitMessage.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/CommitMessage.java?rev=1170635&r1=1170634&r2=1170635&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/CommitMessage.java
(original)
+++ jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/CommitMessage.java
Wed Sep 14 14:06:33 2011
@@ -1,6 +1,7 @@
 package org.apache.jackrabbit.spi2microkernel;
 
-import org.apache.jackrabbit.spi2microkernel.util.Text;
+
+import org.apache.jackrabbit.util.Text;
 
 public class CommitMessage {
     private final String userId;



Mime
View raw message