You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:58:21 UTC

[sling-org-apache-sling-repoinit-parser] 04/10: SLING-5842 - optional prefix for CND files to avoid provisioning model parser conflicts

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.4
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-repoinit-parser.git

commit a4ad0c3a143338f15025ac0a9e109216eddfd25c
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Mon Jul 18 16:13:22 2016 +0000

    SLING-5842 - optional prefix for CND files to avoid provisioning model parser conflicts
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/repoinit/parser@1753260 13f79535-47bb-0310-9956-ffa450edef68
---
 .../parser/operations/LinePrefixCleaner.java       | 57 ++++++++++++++++++++
 .../parser/operations/RegisterNodetypes.java       |  9 +++-
 src/main/javacc/RepoInitGrammar.jjt                |  4 +-
 .../parser/test/LinePrefixCleanerTest.java         | 60 ++++++++++++++++++++++
 src/test/resources/testcases/test-50-output.txt    |  8 +--
 src/test/resources/testcases/test-50.txt           | 14 +++--
 src/test/resources/testcases/test-99-output.txt    |  4 +-
 src/test/resources/testcases/test-99.txt           |  6 +--
 8 files changed, 145 insertions(+), 17 deletions(-)

diff --git a/src/main/java/org/apache/sling/repoinit/parser/operations/LinePrefixCleaner.java b/src/main/java/org/apache/sling/repoinit/parser/operations/LinePrefixCleaner.java
new file mode 100644
index 0000000..456e65f
--- /dev/null
+++ b/src/main/java/org/apache/sling/repoinit/parser/operations/LinePrefixCleaner.java
@@ -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();
+    }
+}
diff --git a/src/main/java/org/apache/sling/repoinit/parser/operations/RegisterNodetypes.java b/src/main/java/org/apache/sling/repoinit/parser/operations/RegisterNodetypes.java
index 3266900..54356b5 100644
--- a/src/main/java/org/apache/sling/repoinit/parser/operations/RegisterNodetypes.java
+++ b/src/main/java/org/apache/sling/repoinit/parser/operations/RegisterNodetypes.java
@@ -21,8 +21,15 @@ package org.apache.sling.repoinit.parser.operations;
 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
diff --git a/src/main/javacc/RepoInitGrammar.jjt b/src/main/javacc/RepoInitGrammar.jjt
index f1191eb..9939f81 100644
--- a/src/main/javacc/RepoInitGrammar.jjt
+++ b/src/main/javacc/RepoInitGrammar.jjt
@@ -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() :
diff --git a/src/test/java/org/apache/sling/repoinit/parser/test/LinePrefixCleanerTest.java b/src/test/java/org/apache/sling/repoinit/parser/test/LinePrefixCleanerTest.java
new file mode 100644
index 0000000..4ab5685
--- /dev/null
+++ b/src/test/java/org/apache/sling/repoinit/parser/test/LinePrefixCleanerTest.java
@@ -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));
+    }
+ }
diff --git a/src/test/resources/testcases/test-50-output.txt b/src/test/resources/testcases/test-50-output.txt
index dfa1efd..2c3ca6b 100644
--- a/src/test/resources/testcases/test-50-output.txt
+++ b/src/test/resources/testcases/test-50-output.txt
@@ -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
diff --git a/src/test/resources/testcases/test-50.txt b/src/test/resources/testcases/test-50.txt
index 230a071..b358f05 100644
--- a/src/test/resources/testcases/test-50.txt
+++ b/src/test/resources/testcases/test-50.txt
@@ -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
+===>>
diff --git a/src/test/resources/testcases/test-99-output.txt b/src/test/resources/testcases/test-99-output.txt
index 1fe2f1a..0544b4a 100644
--- a/src/test/resources/testcases/test-99-output.txt
+++ b/src/test/resources/testcases/test-99-output.txt
@@ -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
diff --git a/src/test/resources/testcases/test-99.txt b/src/test/resources/testcases/test-99.txt
index a2f200a..c8b16a0 100644
--- a/src/test/resources/testcases/test-99.txt
+++ b/src/test/resources/testcases/test-99.txt
@@ -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

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