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 539cbbd2a702d45d17ed30a3965257d03e8519d8 Author: Bertrand Delacretaz <[email protected]> AuthorDate: Mon Jul 18 08:50:04 2016 +0000 SLING-5843 - add 'register namespace' statement to repoinit parser git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/repoinit/parser@1753177 13f79535-47bb-0310-9956-ffa450edef68 --- .../parser/operations/OperationVisitor.java | 1 + ...perationVisitor.java => RegisterNamespace.java} | 32 +++++++++++++---- .../repoinit/parser/operations/package-info.java | 2 +- src/main/javacc/RepoInitGrammar.jjt | 40 ++++++++++++++++++---- .../parser/test/OperationToStringVisitor.java | 6 ++++ .../repoinit/parser/test/ParsingErrorsTest.java | 21 ++++++++---- src/test/resources/testcases/test-40-output.txt | 3 ++ src/test/resources/testcases/test-40.txt | 4 +++ src/test/resources/testcases/test-99-output.txt | 1 + src/test/resources/testcases/test-99.txt | 2 ++ 10 files changed, 92 insertions(+), 20 deletions(-) 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 4079386..d7d2682 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 @@ -23,4 +23,5 @@ public interface OperationVisitor { void visitSetAclPrincipal(SetAclPrincipals s); void visitSetAclPaths(SetAclPaths s); void visitCreatePath(CreatePath cp); + void visitRegisterNamespace(RegisterNamespace rn); } diff --git a/src/main/java/org/apache/sling/repoinit/parser/operations/OperationVisitor.java b/src/main/java/org/apache/sling/repoinit/parser/operations/RegisterNamespace.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/RegisterNamespace.java index 4079386..b5a947b 100644 --- a/src/main/java/org/apache/sling/repoinit/parser/operations/OperationVisitor.java +++ b/src/main/java/org/apache/sling/repoinit/parser/operations/RegisterNamespace.java @@ -17,10 +17,30 @@ 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); - void visitCreatePath(CreatePath cp); +public class RegisterNamespace extends Operation { + private final String prefix; + private final String uri; + + public RegisterNamespace(String prefix, String uri) { + this.prefix = prefix; + this.uri = uri; + } + + @Override + public String toString() { + return getClass().getSimpleName() + " " + getParametersDescription(); + } + + @Override + protected String getParametersDescription() { + final StringBuilder sb = new StringBuilder(); + sb.append("(").append(prefix == null ? "" : prefix).append(") "); + sb.append(uri); + return sb.toString(); + } + + @Override + public void accept(OperationVisitor v) { + v.visitRegisterNamespace(this); + } } diff --git a/src/main/java/org/apache/sling/repoinit/parser/operations/package-info.java b/src/main/java/org/apache/sling/repoinit/parser/operations/package-info.java index fad0a42..b1ee857 100644 --- a/src/main/java/org/apache/sling/repoinit/parser/operations/package-info.java +++ b/src/main/java/org/apache/sling/repoinit/parser/operations/package-info.java @@ -15,7 +15,7 @@ * limitations under the License. ******************************************************************************/ -@Version("1.0.0") +@Version("2.0.0") package org.apache.sling.repoinit.parser.operations; import aQute.bnd.annotation.Version; diff --git a/src/main/javacc/RepoInitGrammar.jjt b/src/main/javacc/RepoInitGrammar.jjt index 66b52b0..1f887b5 100644 --- a/src/main/javacc/RepoInitGrammar.jjt +++ b/src/main/javacc/RepoInitGrammar.jjt @@ -8,6 +8,8 @@ options STATIC=false; LOOKAHEAD=3; //FORCE_LA_CHECK=true; + //DEBUG_PARSER=true; + //DEBUG_LOOKAHEAD=true; } PARSER_BEGIN(RepoInitParserImpl) @@ -65,13 +67,17 @@ TOKEN: | < END: "end" > | < USER: "user" > | < NODETYPES: "nodetypes" > +| < REGISTER: "register" > +| < NAMESPACE: "namespace" > | < LPAREN: "(" > | < RPAREN: ")" > -| < PRINCIPAL: (["a"-"z"] | ["A"-"Z"] | ["0"-"9"] | "_" | "-")+ > | < COMMA: "," > | < STAR: "*" > + +/* The order of these fuzzy statements is important (first match wins?) */ | < NAMESPACED_ITEM: (["a"-"z"] | ["A"-"Z"])+ ":" (["a"-"z"] | ["A"-"Z"])+ > | < PATH_STRING: "/" (["a"-"z"] | ["A"-"Z"] | ["0"-"9"] | ["-"] | ["_"] | ["."] | ["/"]) * > +| < STRING: (["a"-"z"] | ["A"-"Z"] | ["0"-"9"] | ["-"] | ["_"] | ["."] | ["/"] | [":"]) * > | < EOL: "\n" > } @@ -85,6 +91,7 @@ List<Operation> parse() : | setAclPaths(result) | setAclPrincipals(result) | createPathStatement(result) + | registerNamespaceStatement(result) | blankLine() ) * <EOF> @@ -104,8 +111,8 @@ List<String> principalsList() : List<String> principals = new ArrayList<String>(); } { - t = <PRINCIPAL> { principals.add(t.image); } - ( <COMMA> t = <PRINCIPAL> { principals.add(t.image); } )* + t = <STRING> { principals.add(t.image); } + ( <COMMA> t = <STRING> { principals.add(t.image); } )* { return principals; } } @@ -121,11 +128,11 @@ void serviceUserStatement(List<Operation> result) : (<EOL> | <EOF>) { - for(String PRINCIPAL : principals) { + for(String principal : principals) { if(CREATE == t.kind) { - result.add(new CreateServiceUser(PRINCIPAL)); + result.add(new CreateServiceUser(principal)); } else { - result.add(new DeleteServiceUser(PRINCIPAL)); + result.add(new DeleteServiceUser(principal)); } } } @@ -274,3 +281,24 @@ void setAclPrincipals(List<Operation> result) : result.add(new SetAclPrincipals(principals, lines)); } } + +void registerNamespaceStatement(List<Operation> result) : +{ + Token prefix = null; + Token uri; +} +{ + <REGISTER> <NAMESPACE> + ( <LPAREN> prefix = <STRING> <RPAREN> )? + uri = <STRING> + + { + if(prefix == null) { + result.add(new RegisterNamespace(null, uri.image)); + } else { + result.add(new RegisterNamespace(prefix.image, uri.image)); + } + } + + (<EOL> | <EOF>) +} 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 a439549..a25128a 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 @@ -25,6 +25,7 @@ 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; +import org.apache.sling.repoinit.parser.operations.RegisterNamespace; import org.apache.sling.repoinit.parser.operations.SetAclPaths; import org.apache.sling.repoinit.parser.operations.SetAclPrincipals; @@ -77,6 +78,11 @@ class OperationToStringVisitor implements OperationVisitor { public void visitCreatePath(CreatePath cp) { out.println(cp.toString()); } + + @Override + public void visitRegisterNamespace(RegisterNamespace rn) { + out.println(rn.toString()); + } private void dumpAclLines(Collection<AclLine> c) { for(AclLine line : c) { diff --git a/src/test/java/org/apache/sling/repoinit/parser/test/ParsingErrorsTest.java b/src/test/java/org/apache/sling/repoinit/parser/test/ParsingErrorsTest.java index fb46eef..c902ac1 100644 --- a/src/test/java/org/apache/sling/repoinit/parser/test/ParsingErrorsTest.java +++ b/src/test/java/org/apache/sling/repoinit/parser/test/ParsingErrorsTest.java @@ -56,9 +56,13 @@ public class ParsingErrorsTest { add(new Object[] { "create service user bob, alice, tom21", null }); add(new Object[] { "create service user bob-221_BOB", null }); - add(new Object[] { "create service user bob/221", ParseException.class }); + + // this passes since introducing "register namespace" and loosening + // the PRINCIPAL regexp + add(new Object[] { "create service user bob/221", null }); + add(new Object[] { "create service user bob,/alice, tom21", ParseException.class }); - add(new Object[] { "create service user bob,alice,tom21 # comment not allowed here", TokenMgrError.class }); + add(new Object[] { "create service user bob,alice,tom21 # comment not allowed here", ParseException.class }); add(new Object[] { "CREATE service user bob, alice, tom21", ParseException.class }); add(new Object[] { "create SERVICE user bob, alice, tom21", ParseException.class }); }}; @@ -73,17 +77,20 @@ public class ParsingErrorsTest { @Test public void checkResult() throws ParseException, IOException { final StringReader r = new StringReader(input); + boolean noException = false; try { new RepoInitParserImpl(r).parse(); - if(expected != null) { - fail("Expected a " + expected.getSimpleName() + " for [" + input + "]"); - } + noException = true; } catch(Exception e) { - assertEquals(expected, e.getClass()); + assertEquals("for input " + input, expected, e.getClass()); } catch(Error err) { - assertEquals(expected, err.getClass()); + assertEquals("for input " + input, expected, err.getClass()); } finally { r.close(); } + + if(noException && expected != null) { + fail("Expected a " + expected.getSimpleName() + " for [" + input + "]"); + } } } diff --git a/src/test/resources/testcases/test-40-output.txt b/src/test/resources/testcases/test-40-output.txt new file mode 100644 index 0000000..e80b5c8 --- /dev/null +++ b/src/test/resources/testcases/test-40-output.txt @@ -0,0 +1,3 @@ +RegisterNamespace (foo) uri:some-uri/V/1.0 +RegisterNamespace () uri:without-prefix +RegisterNamespace (prefix_with-other.things) andSimpleURI \ No newline at end of file diff --git a/src/test/resources/testcases/test-40.txt b/src/test/resources/testcases/test-40.txt new file mode 100644 index 0000000..aa77e26 --- /dev/null +++ b/src/test/resources/testcases/test-40.txt @@ -0,0 +1,4 @@ +# Test the register namespace statement +register namespace (foo) uri:some-uri/V/1.0 +register namespace uri:without-prefix +register namespace ( prefix_with-other.things ) andSimpleURI \ No newline at end of file diff --git a/src/test/resources/testcases/test-99-output.txt b/src/test/resources/testcases/test-99-output.txt index 2761b1b..fcbe63c 100644 --- a/src/test/resources/testcases/test-99-output.txt +++ b/src/test/resources/testcases/test-99-output.txt @@ -16,4 +16,5 @@ SetAclPrincipals for alice bob fred AclLine ALLOW {paths=[/content, /var], privileges=[jcr:read]} AclLine DENY {paths=[/content/example.com], privileges=[jcr:write]} AclLine DENY {nodetypes=[example:Page], paths=[/], privileges=[jcr:all]} +RegisterNamespace (NSprefix) uri:someURI/v1.42 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 44de7e9..54927c4 100644 --- a/src/test/resources/testcases/test-99.txt +++ b/src/test/resources/testcases/test-99.txt @@ -31,4 +31,6 @@ set ACL for alice, bob,fred end +register namespace ( NSprefix ) uri:someURI/v1.42 + create service user the-last-one \ No newline at end of file -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
