Author: radu
Date: Wed Sep 30 10:14:07 2015
New Revision: 1705994

URL: http://svn.apache.org/viewvc?rev=1705994&view=rev
Log:
SLING-4971 - "static" node name not allowed as a script ancestor

* defined a set of reserved Java keywords that will get escaped when detected 
in Resource paths

Added:
    
sling/trunk/bundles/scripting/sightly/engine/src/test/java/org/apache/sling/scripting/sightly/impl/engine/compiled/
    
sling/trunk/bundles/scripting/sightly/engine/src/test/java/org/apache/sling/scripting/sightly/impl/engine/compiled/SourceIdentifierTest.java
Modified:
    
sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/compiled/SourceIdentifier.java

Modified: 
sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/compiled/SourceIdentifier.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/compiled/SourceIdentifier.java?rev=1705994&r1=1705993&r2=1705994&view=diff
==============================================================================
--- 
sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/compiled/SourceIdentifier.java
 (original)
+++ 
sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/compiled/SourceIdentifier.java
 Wed Sep 30 10:14:07 2015
@@ -19,6 +19,9 @@
 
 package org.apache.sling.scripting.sightly.impl.engine.compiled;
 
+import java.util.HashSet;
+import java.util.Set;
+
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceUtil;
 import org.apache.sling.commons.classloader.ClassLoaderWriter;
@@ -30,6 +33,59 @@ import org.apache.sling.scripting.sightl
  */
 public class SourceIdentifier {
 
+    private static final Set<String> reservedKeywords = new HashSet<String>(){{
+        add("abstract");
+        add("assert");
+        add("boolean");
+        add("break");
+        add("byte");
+        add("case");
+        add("catch");
+        add("char");
+        add("class");
+        add("const");
+        add("continue");
+        add("default");
+        add("do");
+        add("double");
+        add("else");
+        add("enum");
+        add("extends");
+        add("final");
+        add("finally");
+        add("float");
+        add("for");
+        add("goto");
+        add("if");
+        add("implements");
+        add("import");
+        add("instanceof");
+        add("int");
+        add("interface");
+        add("long");
+        add("native");
+        add("new");
+        add("package");
+        add("private");
+        add("protected");
+        add("public");
+        add("return");
+        add("short");
+        add("static");
+        add("strictfp");
+        add("super");
+        add("switch");
+        add("synchronized");
+        add("this");
+        add("throw");
+        add("throws");
+        add("transient");
+        add("try");
+        add("void");
+        add("volatile");
+        add("while");
+    }};
+
     private final String className;
     private final Resource resource;
     private final String packageName;
@@ -88,10 +144,21 @@ public class SourceIdentifier {
     }
 
     private String buildPackageName(Resource resource) {
-        return ResourceUtil.getParent(resource.getPath())
-                .replaceAll("/", ".")
-                .substring(1)
-                .replaceAll("-", "_");
+        String packageName = 
ResourceUtil.getParent(resource.getPath()).replaceAll("/", 
".").substring(1).replaceAll("-", "_");
+        String[] packageNameElements = packageName.split("\\.");
+        StringBuilder sb = new StringBuilder();
+        for (int i = 0; i < packageNameElements.length; i++) {
+            String subPackage = packageNameElements[i];
+            if (reservedKeywords.contains(subPackage)) {
+                sb.append(subPackage).append("_");
+            } else {
+                sb.append(subPackage);
+            }
+            if (i != packageNameElements.length - 1) {
+                sb.append(".");
+            }
+        }
+        return sb.toString();
     }
 
     private String getExtension(String scriptName) {

Added: 
sling/trunk/bundles/scripting/sightly/engine/src/test/java/org/apache/sling/scripting/sightly/impl/engine/compiled/SourceIdentifierTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/sightly/engine/src/test/java/org/apache/sling/scripting/sightly/impl/engine/compiled/SourceIdentifierTest.java?rev=1705994&view=auto
==============================================================================
--- 
sling/trunk/bundles/scripting/sightly/engine/src/test/java/org/apache/sling/scripting/sightly/impl/engine/compiled/SourceIdentifierTest.java
 (added)
+++ 
sling/trunk/bundles/scripting/sightly/engine/src/test/java/org/apache/sling/scripting/sightly/impl/engine/compiled/SourceIdentifierTest.java
 Wed Sep 30 10:14:07 2015
@@ -0,0 +1,52 @@
+/*******************************************************************************
+ * 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.scripting.sightly.impl.engine.compiled;
+
+import org.apache.sling.api.resource.Resource;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class SourceIdentifierTest {
+
+    private static SourceIdentifier sourceIdentifier;
+
+    @BeforeClass
+    public static void setUp() {
+        final Resource resource = mock(Resource.class);
+        when(resource.getPath()).thenReturn("/apps/blah/static/foo/foo.html");
+        sourceIdentifier = new SourceIdentifier(null, null, null, resource, 
"SightlyJava_");
+    }
+
+    @Test
+    public void testGetClassName() throws Exception {
+        assertEquals("SightlyJava_foo", sourceIdentifier.getClassName());
+    }
+
+    @Test
+    public void testGetPackageName() throws Exception {
+        assertEquals("apps.blah.static_.foo", 
sourceIdentifier.getPackageName());
+    }
+
+    @Test
+    public void testGetFullyQualifiedName() throws Exception {
+        assertEquals("apps.blah.static_.foo.SightlyJava_foo", 
sourceIdentifier.getFullyQualifiedName());
+    }
+}
\ No newline at end of file


Reply via email to