incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r1037644 - in /sling/trunk/bundles/extensions/event/src: main/java/org/apache/sling/event/impl/jobs/Utility.java test/java/org/apache/sling/event/impl/jobs/UtilityTest.java
Date Mon, 22 Nov 2010 09:29:51 GMT
Author: fmeschbe
Date: Mon Nov 22 09:29:50 2010
New Revision: 1037644

URL: http://svn.apache.org/viewvc?rev=1037644&view=rev
Log:
SLING-1878 Fix set of allowed characters (remove [, ], *) and use BitSet instead of String;
add unit tests

Added:
    sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/impl/jobs/UtilityTest.java
  (with props)
Modified:
    sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/Utility.java

Modified: sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/Utility.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/Utility.java?rev=1037644&r1=1037643&r2=1037644&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/Utility.java
(original)
+++ sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/Utility.java
Mon Nov 22 09:29:50 2010
@@ -21,6 +21,7 @@ package org.apache.sling.event.impl.jobs
 import java.io.UnsupportedEncodingException;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
+import java.util.BitSet;
 import java.util.Dictionary;
 import java.util.Hashtable;
 import java.util.UUID;
@@ -34,24 +35,37 @@ import org.osgi.service.event.EventConst
 public abstract class Utility {
 
     /** Allowed characters for a node name */
-    private static final String ALLOWED_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz0123456789_,.-+*#!ยค$%&()=[]?";
+    private static final BitSet ALLOWED_CHARS;
+
     /** Replacement characters for unallowed characters in a node name */
     private static final char REPLACEMENT_CHAR = '_';
 
+    // Prepare the ALLOWED_CHARS bitset with bits indicating the unicode
+    // character index of allowed characters. We deliberately only support
+    // a subset of the actually allowed set of characters for nodes ...
+    static {
+        final String allowed = "ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz0123456789_,.-+#!?$%&()=";
+        final BitSet allowedSet = new BitSet();
+        for (int i = 0; i < allowed.length(); i++) {
+            allowedSet.set(allowed.charAt(i));
+        }
+        ALLOWED_CHARS = allowedSet;
+    }
+
     /**
      * Filter the node name for not allowed characters and replace them.
      * @param nodeName The suggested node name.
      * @return The filtered node name.
      */
     public static String filter(final String nodeName) {
-        final StringBuilder sb  = new StringBuilder();
+        final StringBuilder sb  = new StringBuilder(nodeName.length());
         char lastAdded = 0;
 
         for(int i=0; i < nodeName.length(); i++) {
             final char c = nodeName.charAt(i);
             char toAdd = c;
 
-            if (ALLOWED_CHARS.indexOf(c) < 0) {
+            if (!ALLOWED_CHARS.get(c)) {
                 if (lastAdded == REPLACEMENT_CHAR) {
                     // do not add several _ in a row
                     continue;

Added: sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/impl/jobs/UtilityTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/impl/jobs/UtilityTest.java?rev=1037644&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/impl/jobs/UtilityTest.java
(added)
+++ sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/impl/jobs/UtilityTest.java
Mon Nov 22 09:29:50 2010
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.event.impl.jobs;
+
+import junit.framework.TestCase;
+
+public class UtilityTest extends TestCase {
+
+    public void test_filter_allowed() {
+        final String allowed = "ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz0123456789_,.-+#!?$%&()=";
+        assertEquals("Allowed Characters must not be filtered", allowed,
+            Utility.filter(allowed));
+    }
+
+    public void test_filter_illegal_jcr() {
+        assertEquals("_", Utility.filter("["));
+        assertEquals("_", Utility.filter("]"));
+        assertEquals("_", Utility.filter("*"));
+        assertEquals("_", Utility.filter("/"));
+        assertEquals("_", Utility.filter(":"));
+        assertEquals("_", Utility.filter("'"));
+        assertEquals("_", Utility.filter("\""));
+
+        assertEquals("a_b", Utility.filter("a[b"));
+        assertEquals("a_b", Utility.filter("a]b"));
+        assertEquals("a_b", Utility.filter("a*b"));
+        assertEquals("a_b", Utility.filter("a/b"));
+        assertEquals("a_b", Utility.filter("a:b"));
+        assertEquals("a_b", Utility.filter("a'b"));
+        assertEquals("a_b", Utility.filter("a\"b"));
+
+        assertEquals("_b", Utility.filter("[b"));
+        assertEquals("_b", Utility.filter("]b"));
+        assertEquals("_b", Utility.filter("*b"));
+        assertEquals("_b", Utility.filter("/b"));
+        assertEquals("_b", Utility.filter(":b"));
+        assertEquals("_b", Utility.filter("'b"));
+        assertEquals("_b", Utility.filter("\"b"));
+
+        assertEquals("a_", Utility.filter("a["));
+        assertEquals("a_", Utility.filter("a]"));
+        assertEquals("a_", Utility.filter("a*"));
+        assertEquals("a_", Utility.filter("a/"));
+        assertEquals("a_", Utility.filter("a:"));
+        assertEquals("a_", Utility.filter("a'"));
+        assertEquals("a_", Utility.filter("a\""));
+    }
+
+    public void test_filter_consecutive_replace() {
+        assertEquals("a_b_", Utility.filter("a/[b]"));
+    }
+}

Propchange: sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/impl/jobs/UtilityTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/impl/jobs/UtilityTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev Url



Mime
View raw message