incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r906164 - in /sling/trunk/samples/path-based-rtp/src: main/java/org/apache/sling/samples/pathbasedrtp/Mapping.java test/java/org/apache/sling/samples/pathbasedrtp/MappingTest.java
Date Wed, 03 Feb 2010 18:29:49 GMT
Author: bdelacretaz
Date: Wed Feb  3 18:29:49 2010
New Revision: 906164

URL: http://svn.apache.org/viewvc?rev=906164&view=rev
Log:
SLING-1347 - extended mapping syntax implemented

Modified:
    sling/trunk/samples/path-based-rtp/src/main/java/org/apache/sling/samples/pathbasedrtp/Mapping.java
    sling/trunk/samples/path-based-rtp/src/test/java/org/apache/sling/samples/pathbasedrtp/MappingTest.java

Modified: sling/trunk/samples/path-based-rtp/src/main/java/org/apache/sling/samples/pathbasedrtp/Mapping.java
URL: http://svn.apache.org/viewvc/sling/trunk/samples/path-based-rtp/src/main/java/org/apache/sling/samples/pathbasedrtp/Mapping.java?rev=906164&r1=906163&r2=906164&view=diff
==============================================================================
--- sling/trunk/samples/path-based-rtp/src/main/java/org/apache/sling/samples/pathbasedrtp/Mapping.java
(original)
+++ sling/trunk/samples/path-based-rtp/src/main/java/org/apache/sling/samples/pathbasedrtp/Mapping.java
Wed Feb  3 18:29:49 2010
@@ -18,6 +18,8 @@
  */
 package org.apache.sling.samples.pathbasedrtp;
 
+import java.util.regex.Pattern;
+
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -26,30 +28,76 @@
     private final Logger log = LoggerFactory.getLogger(getClass());
     
     String path;
+    final String fixedNodeType;
+    final Pattern nodeTypeExpr;
     int resourceTypeIndex;
+    public static final String DEFAULT_NODE_TYPE = "nt:unstructured";
     
     Mapping(String definition) {
-        final String [] parts = definition.split(":");
-        if(parts.length != 2) {
-            log.debug("Invalid Mapping definition ignored: {}", definition);
-        } else {
-            path = parts[0];
-            try {
-                resourceTypeIndex = Integer.parseInt(parts[1]);
-            } catch(Exception e) {
-                log.warn("Invalid path index in Mapping {}", definition);
+        final String [] parts = definition.trim().split(":");
+        if(parts.length < 2) {
+            throw new IllegalArgumentException("Illegal definition'" + definition + "'");
+        }
+        
+        path = parts[0];
+        try {
+            resourceTypeIndex = Integer.parseInt(parts[1]);
+        } catch(Exception e) {
+            log.warn("Invalid path index in Mapping {}", definition);
+        }
+
+        // Parse the definition: two parts means default nodetype,
+        // more than two means fixed nodetype or regexp.
+        // Nodetypes can contain : which is our separator - not fun.
+        if(parts.length > 2) {
+            final StringBuilder sb = new StringBuilder();
+            for(int i=2; i < parts.length; i++) {
+                sb.append(parts[i]);
+                if(i < parts.length - 1) {
+                    sb.append(":");
+                }
+            }
+            
+            String expr = sb.toString();
+            if(expr.startsWith("(") && expr.endsWith(")")) {
+                // Definition in brackets -> regexp
+                fixedNodeType = null;
+                expr = expr.substring(1);
+                expr = expr.substring(0, expr.length() - 1);
+                nodeTypeExpr = Pattern.compile(expr);
+            } else {
+                // No brackets -> fixed node type
+                fixedNodeType = expr;
+                nodeTypeExpr = null;
             }
+        } else {
+            fixedNodeType = DEFAULT_NODE_TYPE;
+            nodeTypeExpr = null;
         }
+        
+        log.debug(toString());
     }
     
     @Override
     public String toString() {
-       return "Mapping: path=" + path + ", resource type index=" + resourceTypeIndex; 
+        final StringBuilder sb = new StringBuilder();
+        sb.append("Mapping: path=");
+        sb.append(path);
+        sb.append(", resource type index=");
+        sb.append(resourceTypeIndex);
+        if(fixedNodeType != null) {
+            sb.append(", node type=");
+            sb.append(fixedNodeType);
+        } else {
+            sb.append(", node type regexp=");
+            sb.append(nodeTypeExpr);
+        }
+        return sb.toString();
     }
     
     String getResourceType(String nodePath, String nodeType) {
         String result = null;
-        if(path!=null && nodePath.startsWith(path) && "nt:unstructured".equals(nodeType))
{
+        if(path!=null && nodePath.startsWith(path) && nodeTypeMatches(nodeType))
{
             final String [] paths = nodePath.split("/");
             if(paths.length >= resourceTypeIndex+1) {
                 result = paths[resourceTypeIndex];
@@ -57,4 +105,14 @@
         }
         return result;
     }
+    
+    private boolean nodeTypeMatches(String nodeType) {
+        if(fixedNodeType != null) {
+            return fixedNodeType.equals(nodeType);
+        } else if(nodeTypeExpr == null) {
+            throw new IllegalStateException("Neither fixedNodeType nor nodeTypeExpr supplied
in " + this);
+        }
+        return nodeTypeExpr.matcher(nodeType).matches();
+        
+    }
 }
\ No newline at end of file

Modified: sling/trunk/samples/path-based-rtp/src/test/java/org/apache/sling/samples/pathbasedrtp/MappingTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/samples/path-based-rtp/src/test/java/org/apache/sling/samples/pathbasedrtp/MappingTest.java?rev=906164&r1=906163&r2=906164&view=diff
==============================================================================
--- sling/trunk/samples/path-based-rtp/src/test/java/org/apache/sling/samples/pathbasedrtp/MappingTest.java
(original)
+++ sling/trunk/samples/path-based-rtp/src/test/java/org/apache/sling/samples/pathbasedrtp/MappingTest.java
Wed Feb  3 18:29:49 2010
@@ -20,6 +20,7 @@
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertNotNull;
 import org.junit.Test;
 
 public class MappingTest {
@@ -65,10 +66,10 @@
         assertNull(m.getResourceType("/content/foo", "nt:unstructured"));
     }
     
-    /*
     @Test
     public void testNodetypeString() {
         final Mapping m = new Mapping("/content:2:nt:file");
+        assertNotNull("nt:file must match", m.getResourceType("/content/foo1", "nt:file"));
         assertEquals("foo1", m.getResourceType("/content/foo1", "nt:file"));
         assertNull(m.getResourceType("/content/foo1", "nt:unstructured"));
         assertNull(m.getResourceType("/content/foo1", "some:type"));
@@ -77,10 +78,11 @@
     @Test
     public void testNodetypeRegexp() {
         final Mapping m = new Mapping("/content:2:(nt:(file|test))");
+        assertNotNull("nt:file must match", m.getResourceType("/content/foo1", "nt:file"));
         assertEquals("foo1", m.getResourceType("/content/foo1", "nt:file"));
+        assertNotNull("nt:test must match", m.getResourceType("/content/foo1", "nt:test"));
         assertEquals("foo2", m.getResourceType("/content/foo2", "nt:test"));
         assertNull(m.getResourceType("/content/foo1", "nt:unstructured"));
         assertNull(m.getResourceType("/content/foo1", "some:type"));
     }
-    */
 }
\ No newline at end of file



Mime
View raw message