This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.repoinit.parser-1.0.0
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-repoinit-parser.git

commit 09f97e23862860042021f98a4ce503c09fdd524b
Author: Bertrand Delacretaz <bdelacre...@apache.org>
AuthorDate: Tue May 17 14:12:23 2016 +0000

    SLING-5449 - initial base tree creation language, see test-20.txt for 
examples
    
    git-svn-id: 
https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/repoinit/parser@1744274
 13f79535-47bb-0310-9956-ffa450edef68
---
 .../repoinit/parser/operations/CreatePath.java     | 68 ++++++++++++++++++++++
 .../parser/operations/OperationVisitor.java        |  1 +
 ...tionVisitor.java => PathSegmentDefinition.java} | 33 +++++++++--
 src/main/javacc/RepoInitGrammar.jjt                | 38 ++++++++++--
 .../parser/test/OperationToStringVisitor.java      |  6 ++
 src/test/resources/testcases/test-20-output.txt    |  4 ++
 src/test/resources/testcases/test-20.txt           |  6 ++
 src/test/resources/testcases/test-99-output.txt    |  2 +
 src/test/resources/testcases/test-99.txt           |  3 +
 9 files changed, 152 insertions(+), 9 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/repoinit/parser/operations/CreatePath.java 
b/src/main/java/org/apache/sling/repoinit/parser/operations/CreatePath.java
new file mode 100644
index 0000000..ae1e313
--- /dev/null
+++ b/src/main/java/org/apache/sling/repoinit/parser/operations/CreatePath.java
@@ -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.repoinit.parser.operations;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+public class CreatePath extends Operation {
+    private List<PathSegmentDefinition> pathDef;
+    private final String defaultPrimaryType;
+    
+    public CreatePath(String defaultPrimaryType) {
+        this.pathDef = new ArrayList<PathSegmentDefinition>();
+        this.defaultPrimaryType = defaultPrimaryType;
+    }
+    
+    @Override
+    public String toString() {
+        return getClass().getSimpleName() + " " + pathDef;
+    }
+    
+    @Override
+    protected String getParametersDescription() {
+        return pathDef.toString();
+    }
+
+    @Override
+    public void accept(OperationVisitor v) {
+        v.visitCreatePath(this);
+    }
+    
+    public void addSegment(String path, String primaryType) {
+        // We might get a path like /var/discovery, in which case
+        // the specified primary type applies to the last
+        // segment only
+        final String [] segments = path.split("/");
+        for(int i=0; i < segments.length; i++) {
+            if(segments[i].length() == 0) {
+                continue;
+            }
+            String pt = defaultPrimaryType;
+            if(i == segments.length -1 && primaryType != null) {
+                pt = primaryType;
+            }
+            pathDef.add(new PathSegmentDefinition(segments[i], pt));
+        }
+    }
+    
+    public Iterator<PathSegmentDefinition> getDefinitions() {
+        return pathDef.iterator();
+    }
+}
diff --git 
a/src/main/java/org/apache/sling/repoinit/parser/operations/OperationVisitor.java
 
b/src/main/java/org/apache/sling/repoinit/parser/operations/OperationVisitor.java
index d6e500c..4079386 100644
--- 
a/src/main/java/org/apache/sling/repoinit/parser/operations/OperationVisitor.java
+++ 
b/src/main/java/org/apache/sling/repoinit/parser/operations/OperationVisitor.java
@@ -22,4 +22,5 @@ public interface OperationVisitor {
     void visitDeleteServiceUser(DeleteServiceUser s);
     void visitSetAclPrincipal(SetAclPrincipals s);
     void visitSetAclPaths(SetAclPaths s);
+    void visitCreatePath(CreatePath cp);
 }
diff --git 
a/src/main/java/org/apache/sling/repoinit/parser/operations/OperationVisitor.java
 
b/src/main/java/org/apache/sling/repoinit/parser/operations/PathSegmentDefinition.java
similarity index 53%
copy from 
src/main/java/org/apache/sling/repoinit/parser/operations/OperationVisitor.java
copy to 
src/main/java/org/apache/sling/repoinit/parser/operations/PathSegmentDefinition.java
index d6e500c..62f95b8 100644
--- 
a/src/main/java/org/apache/sling/repoinit/parser/operations/OperationVisitor.java
+++ 
b/src/main/java/org/apache/sling/repoinit/parser/operations/PathSegmentDefinition.java
@@ -17,9 +17,32 @@
 
 package org.apache.sling.repoinit.parser.operations;
 
-public interface OperationVisitor {
-    void visitCreateServiceUser(CreateServiceUser s);
-    void visitDeleteServiceUser(DeleteServiceUser s);
-    void visitSetAclPrincipal(SetAclPrincipals s);
-    void visitSetAclPaths(SetAclPaths s);
+/** Defines a segment of a path to be created, 
+ *  with its name and an optional primary type
+ */
+public class PathSegmentDefinition {
+    private final String segment;
+    private final String primaryType;
+    
+    public PathSegmentDefinition(String segment, String primaryType) {
+        this.segment = segment;
+        this.primaryType = primaryType;
+    }
+
+    public String toString() {
+        final StringBuilder sb = new StringBuilder();
+        sb.append(segment);
+        if(primaryType != null) {
+            sb.append("(").append(primaryType).append(")");
+        }
+        return sb.toString();
+    }
+    
+    public String getSegment() {
+        return segment;
+    }
+
+    public String getPrimaryType() {
+        return primaryType;
+    }
 }
diff --git a/src/main/javacc/RepoInitGrammar.jjt 
b/src/main/javacc/RepoInitGrammar.jjt
index 60e84d8..abc011c 100644
--- a/src/main/javacc/RepoInitGrammar.jjt
+++ b/src/main/javacc/RepoInitGrammar.jjt
@@ -5,8 +5,9 @@
  
 options
 {
-    LOOKAHEAD=3;
     STATIC=false;
+    LOOKAHEAD=3;
+    //FORCE_LA_CHECK=true;
 }
 
 PARSER_BEGIN(RepoInitParserImpl)
@@ -63,6 +64,9 @@ TOKEN:
 |   < END: "end" >
 |   < USER: "user" >
 |   < NODETYPES: "nodetypes" >
+|   < CREATE_PATH: "create_path" >
+|   < LPAREN: "(" >
+|   < RPAREN: ")" >
 |   < PRINCIPAL: (["a"-"z"] | ["A"-"Z"] | ["0"-"9"] | "_" | "-")+ >
 |   < COMMA: "," >
 |   < STAR: "*" >
@@ -79,7 +83,8 @@ List<Operation> parse() :
     ( 
         serviceUserStatement(result) 
         | setAclPaths(result) 
-        | setAclPrincipals(result) 
+        | setAclPrincipals(result)
+        | createPathStatement(result)
         | blankLine() 
     ) * 
     <EOF>
@@ -141,8 +146,7 @@ List<String> namespacedItemsList() :
 List<String> pathsList() :
 {
     Token t = null;
-    List<String> paths = new ArrayList<String>(); 
-    
+    List<String> paths = new ArrayList<String>();
 }
 {
     t = <PATH> { paths.add(t.image); } 
@@ -150,6 +154,32 @@ List<String> pathsList() :
     { return paths; }
 }
 
+void createPathStatement(List<Operation> result) :
+{
+    CreatePath cp = null;
+    String defaultPrimaryType = null;
+    Token t1 = null;
+    Token t2 = null;
+}
+{
+    <CREATE_PATH> 
+    ( <LPAREN> t1 = <NAMESPACED_ITEM> <RPAREN> { defaultPrimaryType = 
t1.image; } ) ?
+    
+    ( t1 = <PATH> ( <LPAREN> t2 = <NAMESPACED_ITEM> <RPAREN> ) ?
+        {
+            if(cp == null) {
+                cp = new CreatePath(defaultPrimaryType);
+            } 
+            cp.addSegment(t1.image, t2 == null ? null : t2.image); 
+            t2 = null; 
+        }
+    ) +
+    
+    (<EOL> | <EOF>)
+    
+    { if(cp != null) result.add(cp); }
+}
+
 void setAclPaths(List<Operation> result) :
 {
     List<String> paths;
diff --git 
a/src/test/java/org/apache/sling/repoinit/parser/test/OperationToStringVisitor.java
 
b/src/test/java/org/apache/sling/repoinit/parser/test/OperationToStringVisitor.java
index b6b704a..a439549 100644
--- 
a/src/test/java/org/apache/sling/repoinit/parser/test/OperationToStringVisitor.java
+++ 
b/src/test/java/org/apache/sling/repoinit/parser/test/OperationToStringVisitor.java
@@ -21,6 +21,7 @@ import java.io.PrintWriter;
 import java.util.Collection;
 
 import org.apache.sling.repoinit.parser.operations.AclLine;
+import org.apache.sling.repoinit.parser.operations.CreatePath;
 import org.apache.sling.repoinit.parser.operations.CreateServiceUser;
 import org.apache.sling.repoinit.parser.operations.DeleteServiceUser;
 import org.apache.sling.repoinit.parser.operations.OperationVisitor;
@@ -72,6 +73,11 @@ class OperationToStringVisitor implements OperationVisitor {
         dumpAclLines(s.getLines());
     }
     
+    @Override
+    public void visitCreatePath(CreatePath cp) {
+        out.println(cp.toString());
+    }
+
     private void dumpAclLines(Collection<AclLine> c) {
         for(AclLine line : c) {
             out.print("  ");
diff --git a/src/test/resources/testcases/test-20-output.txt 
b/src/test/resources/testcases/test-20-output.txt
new file mode 100644
index 0000000..68fa997
--- /dev/null
+++ b/src/test/resources/testcases/test-20-output.txt
@@ -0,0 +1,4 @@
+CreatePath [var(sling:Folder), discovery(nt:unstructured), 
somefolder(sling:Folder)]
+CreatePath [one, two, three]
+CreatePath [three, four(nt:folk), five(nt:jazz), six]
+CreatePath [seven(nt:x), eight(nt:x), nine(nt:x)]
\ No newline at end of file
diff --git a/src/test/resources/testcases/test-20.txt 
b/src/test/resources/testcases/test-20.txt
new file mode 100644
index 0000000..9a67807
--- /dev/null
+++ b/src/test/resources/testcases/test-20.txt
@@ -0,0 +1,6 @@
+# Various create path tests
+# TODO should use "create path" instead of "create_path"
+create_path (sling:Folder) /var/discovery(nt:unstructured)/somefolder
+create_path /one/two/three
+create_path /three/four(nt:folk)/five(nt:jazz)/six
+create_path (nt:x) /seven/eight/nine
diff --git a/src/test/resources/testcases/test-99-output.txt 
b/src/test/resources/testcases/test-99-output.txt
index ec98fc4..2761b1b 100644
--- a/src/test/resources/testcases/test-99-output.txt
+++ b/src/test/resources/testcases/test-99-output.txt
@@ -9,6 +9,8 @@ SetAclPaths on /libs /apps
 CreateServiceUser bob_the_service
 SetAclPaths on /tmp 
   AclLine ALLOW {principals=[bob_the_service], 
privileges=[some:otherPrivilege]}
+CreatePath [content, example.com(sling:Folder)]
+CreatePath [var(nt:unstructured)]
 SetAclPrincipals for alice bob fred 
   AclLine REMOVE_ALL {paths=[/]}
   AclLine ALLOW {paths=[/content, /var], privileges=[jcr:read]}
diff --git a/src/test/resources/testcases/test-99.txt 
b/src/test/resources/testcases/test-99.txt
index 67e0949..6632b03 100644
--- a/src/test/resources/testcases/test-99.txt
+++ b/src/test/resources/testcases/test-99.txt
@@ -17,6 +17,9 @@ set ACL on /tmp
 allow some:otherPrivilege for bob_the_service
 end
 
+create_path /content/example.com(sling:Folder)
+create_path (nt:unstructured) /var
+
 set ACL for alice, bob,fred
     remove * on /
     allow jcr:read on /content,/var

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <commits@sling.apache.org>.

Reply via email to