You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ra...@apache.org on 2016/07/21 12:39:28 UTC

svn commit: r1753673 - in /sling/trunk/bundles/api/src: main/java/org/apache/sling/api/resource/observation/ResourceChangeListener.java main/java/org/apache/sling/api/resource/path/Path.java test/java/org/apache/sling/api/resource/path/PathTest.java

Author: radu
Date: Thu Jul 21 12:39:28 2016
New Revision: 1753673

URL: http://svn.apache.org/viewvc?rev=1753673&view=rev
Log:
SLING-5837 - Allow ResourceChangeListeners to define glob patterns for resource matching

* made Javadoc clearer
* added more complex tests for glob pattern matching

Modified:
    sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/observation/ResourceChangeListener.java
    sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/path/Path.java
    sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/path/PathTest.java

Modified: sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/observation/ResourceChangeListener.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/observation/ResourceChangeListener.java?rev=1753673&r1=1753672&r2=1753673&view=diff
==============================================================================
--- sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/observation/ResourceChangeListener.java (original)
+++ sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/observation/ResourceChangeListener.java Thu Jul 21 12:39:28 2016
@@ -60,7 +60,7 @@ public interface ResourceChangeListener
      *
      * <p>If the whole tree of all search paths should be observed, the special value {@code .} should be used.</p>
      *
-     * <p>A glob pattern should start with the {@code glob:} prefix (e.g. <code>glob:**&#47;*.html</code>). The following rules are used
+     * <p>A glob pattern must start with the {@code glob:} prefix (e.g. <code>glob:**&#47;*.html</code>). The following rules are used
      * to interpret glob patterns:</p>
      * <ul>
      *     <li>The {@code *} character matches zero or more characters of a name component without crossing directory boundaries.</li>

Modified: sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/path/Path.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/path/Path.java?rev=1753673&r1=1753672&r2=1753673&view=diff
==============================================================================
--- sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/path/Path.java (original)
+++ sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/path/Path.java Thu Jul 21 12:39:28 2016
@@ -38,7 +38,7 @@ public class Path implements Comparable<
     /**
      * <p>Create a new path object either from a concrete path or from a glob pattern.</p>
      *
-     * <p>A glob pattern should start with the {@code glob:} prefix (e.g. <code>glob:**&#47;*.html</code>). The following rules are used
+     * <p>A glob pattern must start with the {@code glob:} prefix (e.g. <code>glob:**&#47;*.html</code>). The following rules are used
      * to interpret glob patterns:</p>
      * <ul>
      *     <li>The {@code *} character matches zero or more characters of a name component without crossing directory boundaries.</li>
@@ -57,7 +57,6 @@ public class Path implements Comparable<
             isPattern = false;
             regexPattern = null;
         }
-
     }
 
     /**
@@ -120,10 +119,10 @@ public class Path implements Comparable<
                     break;
                 default:
                     if (isRegexMeta(currentChar)) {
-                        stringBuilder.append('\\');
+                        stringBuilder.append(Pattern.quote(Character.toString(currentChar)));
+                    } else {
+                        stringBuilder.append(currentChar);
                     }
-
-                    stringBuilder.append(currentChar);
             }
         }
         return stringBuilder.append('$').toString();
@@ -134,7 +133,7 @@ public class Path implements Comparable<
     }
 
     private static boolean isRegexMeta(char character) {
-        return ".^$+{[]|()".indexOf(character) != -1;
+        return "<([{\\^-=$!|]})?*+.>".indexOf(character) != -1;
     }
 
 }

Modified: sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/path/PathTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/path/PathTest.java?rev=1753673&r1=1753672&r2=1753673&view=diff
==============================================================================
--- sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/path/PathTest.java (original)
+++ sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/path/PathTest.java Thu Jul 21 12:39:28 2016
@@ -18,12 +18,11 @@
  */
 package org.apache.sling.api.resource.path;
 
+import org.junit.Test;
+
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
-import org.apache.sling.api.resource.path.Path;
-import org.junit.Test;
-
 public class PathTest {
 
     @Test public void testRootMatching() {
@@ -59,5 +58,13 @@ public class PathTest {
         final Path path_2 = new Path("glob:/apps/*.html");
         assertTrue(path_2.matches("/apps/a.html"));
         assertFalse(path_2.matches("/apps/a/a.html"));
+
+        final Path path_3 = new Path("glob:/a/m-p/$structure/**/[cmp]/*.html");
+        assertTrue(path_3.matches("/a/m-p/$structure/1/2/3/[cmp]/test.html"));
+        assertTrue(path_3.matches("/a/m-p/$structure/1/[cmp]/test.html"));
+        assertTrue(path_3.matches("/a/m-p/$structure/1/[cmp]/te-[s]t$.html"));
+        assertTrue(path_3.matches("/a/m-p/$structure/1/[cmp]/.html"));
+        assertFalse(path_3.matches("/a/m-p/$structure/1/[cmp]/html"));
+        assertFalse(path_3.matches("/a/m-p/$structure/[cmp]/test.html"));
     }
 }