Author: radu Date: Fri Oct 2 16:01:46 2015 New Revision: 1706440 URL: http://svn.apache.org/viewvc?rev=1706440&view=rev Log: SLING-4971 - "static" node name not allowed as a script ancestor
* reduced duplication of Java reserved keywords definition Added: sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/utils/JavaEscapeUtils.java Modified: sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/VariableAnalyzer.java sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/compiled/SourceIdentifier.java 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/compiled/VariableAnalyzer.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/VariableAnalyzer.java?rev=1706440&r1=1706439&r2=1706440&view=diff ============================================================================== --- sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/VariableAnalyzer.java (original) +++ sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/compiled/VariableAnalyzer.java Fri Oct 2 16:01:46 2015 @@ -23,11 +23,11 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; -import java.util.HashSet; import java.util.List; import org.apache.sling.scripting.sightly.impl.compiler.SightlyParsingException; import org.apache.sling.scripting.sightly.impl.compiler.util.VariableTracker; +import org.apache.sling.scripting.sightly.impl.utils.JavaEscapeUtils; /** * Data structure used in the analysis of variables @@ -35,7 +35,6 @@ import org.apache.sling.scripting.sightl */ public class VariableAnalyzer { - private static final HashSet<String> javaKeywords = new HashSet<String>(); private final VariableTracker<VariableDescriptor> tracker = new VariableTracker<VariableDescriptor>(); private final List<VariableDescriptor> variables = new ArrayList<VariableDescriptor>(); private final HashMap<String, VariableDescriptor> dynamicVariables = new HashMap<String, VariableDescriptor>(); @@ -144,16 +143,16 @@ public class VariableAnalyzer { } private String findDynamicName(String original) { - return DYNAMIC_PREFIX + syntaxSafeName(original); + return DYNAMIC_PREFIX + JavaEscapeUtils.getEscapedToken(original); } private String findGlobalName(String original) { - return GLOBAL_PREFIX + syntaxSafeName(original); + return GLOBAL_PREFIX + JavaEscapeUtils.getEscapedToken(original); } private String findSafeName(String original) { int occurrenceCount = tracker.getOccurrenceCount(original); - String syntaxSafe = syntaxSafeName(original); + String syntaxSafe = JavaEscapeUtils.getEscapedToken(original); if (occurrenceCount == 0) { return syntaxSafe; //no other declarations in scope. Use this very name } else { @@ -161,71 +160,10 @@ public class VariableAnalyzer { } } - private String syntaxSafeName(String original) { - if (javaKeywords.contains(original)) { - return "_" + original; - } - return original.replaceAll("-", "_"); - } - private String validName(String name) { if (name == null || name.contains("-")) { throw new SightlyParsingException("Unsupported identifier name: " + name); } return name.toLowerCase(); } - - static { - javaKeywords.add("abstract"); - javaKeywords.add("continue"); - javaKeywords.add("for"); - javaKeywords.add("new"); - javaKeywords.add("switch"); - javaKeywords.add("assert"); - javaKeywords.add("default"); - javaKeywords.add("goto"); - javaKeywords.add("package"); - javaKeywords.add("synchronized"); - javaKeywords.add("boolean"); - javaKeywords.add("do"); - javaKeywords.add("if"); - javaKeywords.add("private"); - javaKeywords.add("this"); - javaKeywords.add("break"); - javaKeywords.add("double"); - javaKeywords.add("implements"); - javaKeywords.add("protected"); - javaKeywords.add("throw"); - javaKeywords.add("byte"); - javaKeywords.add("else"); - javaKeywords.add("import"); - javaKeywords.add("public"); - javaKeywords.add("throws"); - javaKeywords.add("case"); - javaKeywords.add("enum"); - javaKeywords.add("instanceof"); - javaKeywords.add("return"); - javaKeywords.add("transient"); - javaKeywords.add("catch"); - javaKeywords.add("extends"); - javaKeywords.add("int"); - javaKeywords.add("short"); - javaKeywords.add("try"); - javaKeywords.add("char"); - javaKeywords.add("final"); - javaKeywords.add("interface"); - javaKeywords.add("static"); - javaKeywords.add("void"); - javaKeywords.add("class"); - javaKeywords.add("finally"); - javaKeywords.add("long"); - javaKeywords.add("strictfp"); - javaKeywords.add("volatile"); - javaKeywords.add("const"); - javaKeywords.add("float"); - javaKeywords.add("native"); - javaKeywords.add("super"); - javaKeywords.add("while"); - } - } 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=1706440&r1=1706439&r2=1706440&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 Fri Oct 2 16:01:46 2015 @@ -19,73 +19,18 @@ 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; import org.apache.sling.scripting.sightly.impl.compiler.UnitChangeMonitor; import org.apache.sling.scripting.sightly.impl.engine.SightlyEngineConfiguration; +import org.apache.sling.scripting.sightly.impl.utils.JavaEscapeUtils; /** * Identifies a Java source file in a JCR repository. */ 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; @@ -149,11 +94,7 @@ public class SourceIdentifier { 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); - } + sb.append(JavaEscapeUtils.getEscapedToken(subPackage)); if (i != packageNameElements.length - 1) { sb.append("."); } Added: sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/utils/JavaEscapeUtils.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/utils/JavaEscapeUtils.java?rev=1706440&view=auto ============================================================================== --- sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/utils/JavaEscapeUtils.java (added) +++ sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/utils/JavaEscapeUtils.java Fri Oct 2 16:01:46 2015 @@ -0,0 +1,95 @@ +/******************************************************************************* + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.utils; + +import java.util.HashSet; +import java.util.Set; + +/** + * The {@code JavaEscapeUtils} provides useful methods for escaping or transforming invalid Java tokens to valid ones that could be used in + * generated Java source code. + */ +public class JavaEscapeUtils { + + 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"); + }}; + + /** + * Escapes and / or transforms an invalid token (in case the token represents a reserved Java keyword) to provide a valid token. + * + * @param token the token to be transformed + * @return a valid Java token + */ + public static String getEscapedToken(String token) { + String result = token.toLowerCase(); + if (reservedKeywords.contains(result)) { + result = "_" + result; + } + return result.replaceAll("-", "_"); + } + +} Modified: 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=1706440&r1=1706439&r2=1706440&view=diff ============================================================================== --- sling/trunk/bundles/scripting/sightly/engine/src/test/java/org/apache/sling/scripting/sightly/impl/engine/compiled/SourceIdentifierTest.java (original) +++ sling/trunk/bundles/scripting/sightly/engine/src/test/java/org/apache/sling/scripting/sightly/impl/engine/compiled/SourceIdentifierTest.java Fri Oct 2 16:01:46 2015 @@ -42,11 +42,11 @@ public class SourceIdentifierTest { @Test public void testGetPackageName() throws Exception { - assertEquals("apps.blah.static_.foo", sourceIdentifier.getPackageName()); + assertEquals("apps.blah._static.foo", sourceIdentifier.getPackageName()); } @Test public void testGetFullyQualifiedName() throws Exception { - assertEquals("apps.blah.static_.foo.SightlyJava_foo", sourceIdentifier.getFullyQualifiedName()); + assertEquals("apps.blah._static.foo.SightlyJava_foo", sourceIdentifier.getFullyQualifiedName()); } } \ No newline at end of file