sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1753260 - in /sling/trunk/bundles/extensions/repoinit/parser/src: main/java/org/apache/sling/repoinit/parser/operations/ main/javacc/ test/java/org/apache/sling/repoinit/parser/test/ test/resources/testcases/
Date Mon, 18 Jul 2016 16:13:22 GMT
Author: bdelacretaz
Date: Mon Jul 18 16:13:22 2016
New Revision: 1753260

URL: http://svn.apache.org/viewvc?rev=1753260&view=rev
Log:
SLING-5842 - optional prefix for CND files to avoid provisioning model parser conflicts

Added:
    sling/trunk/bundles/extensions/repoinit/parser/src/main/java/org/apache/sling/repoinit/parser/operations/LinePrefixCleaner.java
    sling/trunk/bundles/extensions/repoinit/parser/src/test/java/org/apache/sling/repoinit/parser/test/LinePrefixCleanerTest.java
Modified:
    sling/trunk/bundles/extensions/repoinit/parser/src/main/java/org/apache/sling/repoinit/parser/operations/RegisterNodetypes.java
    sling/trunk/bundles/extensions/repoinit/parser/src/main/javacc/RepoInitGrammar.jjt
    sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-50-output.txt
    sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-50.txt
    sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-99-output.txt
    sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-99.txt

Added: sling/trunk/bundles/extensions/repoinit/parser/src/main/java/org/apache/sling/repoinit/parser/operations/LinePrefixCleaner.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/repoinit/parser/src/main/java/org/apache/sling/repoinit/parser/operations/LinePrefixCleaner.java?rev=1753260&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/repoinit/parser/src/main/java/org/apache/sling/repoinit/parser/operations/LinePrefixCleaner.java
(added)
+++ sling/trunk/bundles/extensions/repoinit/parser/src/main/java/org/apache/sling/repoinit/parser/operations/LinePrefixCleaner.java
Mon Jul 18 16:13:22 2016
@@ -0,0 +1,57 @@
+/*
+ * 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.io.BufferedReader;
+import java.io.IOException;
+import java.io.StringReader;
+
+/** Removes an prefix at the beginning of
+ *  each line in a String. 
+ *  Used for embedding CND files in repoinit
+ *  statements and hiding them from the Sling
+ *  provisioning model parser which fails on
+ *  statements like [sling:someNodetype] which
+ *  are similar to provisioning model sections. 
+ */
+public class LinePrefixCleaner {
+    public String removePrefix(String prefix, String textBlock) {
+        final StringBuilder result = new StringBuilder();
+        try {
+            final BufferedReader r = new BufferedReader(new StringReader(textBlock));
+            try {
+                String line = null;
+                while( (line = r.readLine()) != null) {
+                    if(result.length() > 0) {
+                        result.append("\n");
+                    }
+                    if(line.startsWith(prefix)) {
+                        result.append(line.substring(prefix.length()));
+                    } else {
+                        result.append(line);
+                    }
+                }
+            } finally {
+                r.close();
+            }
+        } catch(IOException ioe) {
+            throw new RuntimeException("Unexpected IOException", ioe);
+        }
+        return result.toString();
+    }
+}

Modified: sling/trunk/bundles/extensions/repoinit/parser/src/main/java/org/apache/sling/repoinit/parser/operations/RegisterNodetypes.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/repoinit/parser/src/main/java/org/apache/sling/repoinit/parser/operations/RegisterNodetypes.java?rev=1753260&r1=1753259&r2=1753260&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/repoinit/parser/src/main/java/org/apache/sling/repoinit/parser/operations/RegisterNodetypes.java
(original)
+++ sling/trunk/bundles/extensions/repoinit/parser/src/main/java/org/apache/sling/repoinit/parser/operations/RegisterNodetypes.java
Mon Jul 18 16:13:22 2016
@@ -21,8 +21,15 @@ package org.apache.sling.repoinit.parser
 public class RegisterNodetypes extends Operation {
     private final String cndStatements;
     
+    /** Optional prefix used at the beginning of CND lines,
+     *  to avoid conflicts with Sling provisioning
+     *  model parser. If present at the beginning of CND lines,
+     *  this string is removed.
+     */
+    public static final String CND_OPTIONAL_PREFIX = "<< ";
+    
     public RegisterNodetypes(String cndStatements) {
-        this.cndStatements = cndStatements;
+        this.cndStatements = new LinePrefixCleaner().removePrefix(CND_OPTIONAL_PREFIX, cndStatements);
     }
     
     @Override

Modified: sling/trunk/bundles/extensions/repoinit/parser/src/main/javacc/RepoInitGrammar.jjt
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/repoinit/parser/src/main/javacc/RepoInitGrammar.jjt?rev=1753260&r1=1753259&r2=1753260&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/repoinit/parser/src/main/javacc/RepoInitGrammar.jjt (original)
+++ sling/trunk/bundles/extensions/repoinit/parser/src/main/javacc/RepoInitGrammar.jjt Mon
Jul 18 16:13:22 2016
@@ -69,7 +69,7 @@ TOKEN:
 |   < NODETYPES: "nodetypes" >
 |   < REGISTER: "register" >
 |   < NAMESPACE: "namespace" >
-|   < START_TEXTBLOCK: "<<<===" > : TEXTBLOCK
+|   < START_TEXTBLOCK: "<<===" > : TEXTBLOCK
 |   < LPAREN: "(" >
 |   < RPAREN: ")" >
 |   < COMMA: "," >
@@ -90,7 +90,7 @@ TOKEN:
 <TEXTBLOCK> TOKEN :
 {
     < TEXT : ~[] > 
-|   < END_TEXTBLOCK: "===>>>" > : DEFAULT
+|   < END_TEXTBLOCK: "===>>" > : DEFAULT
 } 
 
 List<Operation> parse() :

Added: sling/trunk/bundles/extensions/repoinit/parser/src/test/java/org/apache/sling/repoinit/parser/test/LinePrefixCleanerTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/repoinit/parser/src/test/java/org/apache/sling/repoinit/parser/test/LinePrefixCleanerTest.java?rev=1753260&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/repoinit/parser/src/test/java/org/apache/sling/repoinit/parser/test/LinePrefixCleanerTest.java
(added)
+++ sling/trunk/bundles/extensions/repoinit/parser/src/test/java/org/apache/sling/repoinit/parser/test/LinePrefixCleanerTest.java
Mon Jul 18 16:13:22 2016
@@ -0,0 +1,60 @@
+/*
+ * 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.test;
+
+import static org.junit.Assert.assertEquals;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import org.apache.sling.repoinit.parser.operations.LinePrefixCleaner;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+
+public class LinePrefixCleanerTest {
+    private final String prefix;
+    private final String input;
+    private final String expected;
+    
+    @Parameters(name="{0}")
+    public static Collection<Object[]> data() {
+        final List<Object []> result = new ArrayList<Object[]>();
+        result.add(new Object[] { "", "", ""});
+        result.add(new Object[] { "<< ", "", ""});
+        result.add(new Object[] { "", "One\ntwo", "One\ntwo"});
+        result.add(new Object[] { "<< ", "<< Three\n<< four", "Three\nfour"});
+        result.add(new Object[] { "<", "<Five\nsix\n< seven", "Five\nsix\n seven"});
+        return result;
+        
+    }
+
+    public LinePrefixCleanerTest(String prefix, String input, String expected) {
+        this.prefix = prefix;
+        this.input = input;
+        this.expected = expected;
+    }
+    
+    @Test
+    public void cleanup() {
+        final LinePrefixCleaner c = new LinePrefixCleaner();
+        assertEquals(expected, c.removePrefix(prefix, input));
+    }
+ }

Modified: sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-50-output.txt
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-50-output.txt?rev=1753260&r1=1753259&r2=1753260&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-50-output.txt
(original)
+++ sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-50-output.txt
Mon Jul 18 16:13:22 2016
@@ -1,5 +1,4 @@
 RegisterNodetypes:
-
     <slingevent='http://sling.apache.org/jcr/event/1.0'>
     <nt='http://www.jcp.org/jcr/nt/1.0'>
     <mix='http://www.jcp.org/jcr/mix/1.0'>
@@ -21,7 +20,8 @@ RegisterNodetypes:
       - slingevent:expression (string)
       - slingevent:date (date)
       - slingevent:period (long)
-
 RegisterNodetypes:
-
-Just one line, not indented
\ No newline at end of file
+Just one line, not indented
+RegisterNodetypes:
+Using line prefixes
+to avoid conflicts with Sling provisioning model parser
\ No newline at end of file

Modified: sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-50.txt
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-50.txt?rev=1753260&r1=1753259&r2=1753260&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-50.txt
(original)
+++ sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-50.txt
Mon Jul 18 16:13:22 2016
@@ -1,7 +1,7 @@
 # Test parsing embedded CNDs
 
 register nodetypes
-<<<===
+<<===
     <slingevent='http://sling.apache.org/jcr/event/1.0'>
     <nt='http://www.jcp.org/jcr/nt/1.0'>
     <mix='http://www.jcp.org/jcr/mix/1.0'>
@@ -23,9 +23,15 @@ register nodetypes
       - slingevent:expression (string)
       - slingevent:date (date)
       - slingevent:period (long)
-===>>>
+===>>
 
 register nodetypes
-<<<===
+<<===
 Just one line, not indented
-===>>>
+===>>
+
+register nodetypes
+<<===
+<< Using line prefixes
+<< to avoid conflicts with Sling provisioning model parser
+===>>

Modified: sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-99-output.txt
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-99-output.txt?rev=1753260&r1=1753259&r2=1753260&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-99-output.txt
(original)
+++ sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-99-output.txt
Mon Jul 18 16:13:22 2016
@@ -18,11 +18,9 @@ SetAclPrincipals for alice bob fred
   AclLine DENY {nodetypes=[example:Page], paths=[/], privileges=[jcr:all]}
 RegisterNamespace (NSprefix) uri:someURI/v1.42
 RegisterNodetypes:
-
     <slingevent='http://sling.apache.org/jcr/event/1.0'>
     
-    [slingevent:Event] > nt:unstructured, nt:hierarchyNode
+ [slingevent:Event] > nt:unstructured, nt:hierarchyNode
       - slingevent:topic (string)
       - slingevent:properties (binary)
-
 CreateServiceUser the-last-one
\ No newline at end of file

Modified: sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-99.txt
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-99.txt?rev=1753260&r1=1753259&r2=1753260&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-99.txt
(original)
+++ sling/trunk/bundles/extensions/repoinit/parser/src/test/resources/testcases/test-99.txt
Mon Jul 18 16:13:22 2016
@@ -34,12 +34,12 @@ end
 register namespace ( NSprefix ) uri:someURI/v1.42
 
 register nodetypes
-<<<===
+<<===
     <slingevent='http://sling.apache.org/jcr/event/1.0'>
     
-    [slingevent:Event] > nt:unstructured, nt:hierarchyNode
+<<  [slingevent:Event] > nt:unstructured, nt:hierarchyNode
       - slingevent:topic (string)
       - slingevent:properties (binary)
-===>>>
+===>>
 
 create service user the-last-one
\ No newline at end of file



Mime
View raw message