This is an automated email from the ASF dual-hosted git repository.

radu pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-scripting-bundle-tracker.git


The following commit(s) were added to refs/heads/master by this push:
     new 3a873fb  SLING-9158 - Switch from JavaEscapeUtils to JavaEscapeHelper
3a873fb is described below

commit 3a873fb7cf3b70717820fb40af60ed499908124b
Author: Radu Cotescu <[email protected]>
AuthorDate: Wed Feb 26 17:58:19 2020 +0100

    SLING-9158 - Switch from JavaEscapeUtils to JavaEscapeHelper
    
    * removed JavaEscapeUtils and refactored callers to use JavaEscapeHelper
    * updated IT setup
---
 pom.xml                                            |   6 +
 .../tracker/internal/BundledScriptFinder.java      |  16 +-
 .../bundle/tracker/internal/JavaEscapeUtils.java   | 161 ---------------------
 .../tracker/internal/JavaEscapeUtilsTest.java      |  35 -----
 4 files changed, 8 insertions(+), 210 deletions(-)

diff --git a/pom.xml b/pom.xml
index b6ed7ee..e9b8583 100644
--- a/pom.xml
+++ b/pom.xml
@@ -178,6 +178,12 @@
             <version>2.0.54</version>
             <scope>provided</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.commons.compiler</artifactId>
+            <version>2.3.7-SNAPSHOT</version>
+            <scope>provided</scope>
+        </dependency>
         <!-- TESTING -->
         <dependency>
             <groupId>junit</groupId>
diff --git 
a/src/main/java/org/apache/sling/scripting/bundle/tracker/internal/BundledScriptFinder.java
 
b/src/main/java/org/apache/sling/scripting/bundle/tracker/internal/BundledScriptFinder.java
index aad44f8..f967a4a 100644
--- 
a/src/main/java/org/apache/sling/scripting/bundle/tracker/internal/BundledScriptFinder.java
+++ 
b/src/main/java/org/apache/sling/scripting/bundle/tracker/internal/BundledScriptFinder.java
@@ -32,6 +32,7 @@ import javax.script.ScriptEngineManager;
 
 import org.apache.commons.lang3.StringUtils;
 import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.commons.compiler.source.JavaEscapeHelper;
 import org.osgi.service.component.annotations.Component;
 import org.osgi.service.component.annotations.Reference;
 
@@ -56,7 +57,7 @@ public class BundledScriptFinder {
                 for (String extension : getScriptEngineExtensions()) {
                     URL bundledScriptURL;
                     if (precompiledScripts) {
-                        String className = fromScriptPathToClassName(match);
+                        String className = 
JavaEscapeHelper.makeJavaPackage(match);
                         try {
                             Class clazz = 
provider.getBundle().loadClass(className);
                             return new PrecompiledScript(provider.getBundle(), 
scriptEngineManager.getEngineByExtension(extension),
@@ -133,17 +134,4 @@ public class BundledScriptFinder {
         Collections.reverse(_scriptEngineExtensions);
         return Collections.unmodifiableList(_scriptEngineExtensions);
     }
-
-    private String fromScriptPathToClassName(String scriptPath) {
-        String[] parts = scriptPath.split("/");
-        StringBuilder stringBuilder = new StringBuilder();
-        for (String part : parts) {
-            if (stringBuilder.length() > 0) {
-                stringBuilder.append(".");
-            }
-            stringBuilder.append(JavaEscapeUtils.makeJavaIdentifier(part));
-        }
-        return stringBuilder.toString();
-    }
-
 }
diff --git 
a/src/main/java/org/apache/sling/scripting/bundle/tracker/internal/JavaEscapeUtils.java
 
b/src/main/java/org/apache/sling/scripting/bundle/tracker/internal/JavaEscapeUtils.java
deleted file mode 100644
index 989f916..0000000
--- 
a/src/main/java/org/apache/sling/scripting/bundle/tracker/internal/JavaEscapeUtils.java
+++ /dev/null
@@ -1,161 +0,0 @@
-/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- ~ 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.bundle.tracker.internal;
-
-import java.util.HashSet;
-import java.util.Set;
-
-import org.apache.commons.lang3.StringUtils;
-
-/**
- * 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> javaKeywords = 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");
-    }};
-
-    /**
-     * Converts the given identifier to a legal Java identifier
-     *
-     * @param identifier the identifier to convert
-     * @return legal Java identifier corresponding to the given identifier
-     */
-    public static String makeJavaIdentifier(String identifier) {
-        StringBuilder modifiedIdentifier = new 
StringBuilder(identifier.length());
-        if (!Character.isJavaIdentifierStart(identifier.charAt(0))) {
-            modifiedIdentifier.append('_');
-        }
-        for (int i = 0; i < identifier.length(); i++) {
-            char ch = identifier.charAt(i);
-            if (Character.isJavaIdentifierPart(ch)) {
-                modifiedIdentifier.append(ch);
-            } else if (ch == '.') {
-                modifiedIdentifier.append('_');
-            } else {
-                modifiedIdentifier.append(mangleChar(ch));
-            }
-        }
-        if (isJavaKeyword(modifiedIdentifier.toString())) {
-            modifiedIdentifier.append('_');
-        }
-        return modifiedIdentifier.toString();
-    }
-
-    /**
-     * Mangle the specified character to create a legal Java class name.
-     *
-     * @param ch the character to mangle
-     * @return the mangled
-     */
-    public static String mangleChar(char ch) {
-        return String.format("__%04x__", (int) ch);
-    }
-
-    /**
-     * Provided a mangled string (obtained by calling {@link 
#mangleChar(char)}) it will will return the character that was mangled.
-     *
-     * @param mangled the mangled string
-     * @return the original character
-     */
-    public static char unmangle(String mangled) {
-        String toProcess = mangled.replaceAll("__", "");
-        return (char) Integer.parseInt(toProcess, 16);
-    }
-
-    /**
-     * Converts the given scriptName to a Java package or fully-qualified 
class name
-     *
-     * @param scriptName the scriptName to convert
-     * @return Java package corresponding to the given scriptName
-     */
-    public static String makeJavaPackage(String scriptName) {
-        String classNameComponents[] = StringUtils.split(scriptName, "/\\");
-        StringBuilder legalClassNames = new StringBuilder();
-        for (int i = 0; i < classNameComponents.length; i++) {
-            legalClassNames.append(makeJavaIdentifier(classNameComponents[i]));
-            if (i < classNameComponents.length - 1) {
-                legalClassNames.append('.');
-            }
-        }
-        return legalClassNames.toString();
-    }
-
-    /**
-     * Test whether the argument is a Java keyword.
-     *
-     * @param key the String to test
-     * @return {@code true} if the String is a Java keyword, {@code false} 
otherwise
-     */
-    public static boolean isJavaKeyword(String key) {
-        return javaKeywords.contains(key);
-    }
-
-}
diff --git 
a/src/test/java/org/apache/sling/scripting/bundle/tracker/internal/JavaEscapeUtilsTest.java
 
b/src/test/java/org/apache/sling/scripting/bundle/tracker/internal/JavaEscapeUtilsTest.java
deleted file mode 100644
index f193a8c..0000000
--- 
a/src/test/java/org/apache/sling/scripting/bundle/tracker/internal/JavaEscapeUtilsTest.java
+++ /dev/null
@@ -1,35 +0,0 @@
-/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- ~ 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.bundle.tracker.internal;
-
-import org.junit.Test;
-
-import static org.junit.Assert.assertEquals;
-
-public class JavaEscapeUtilsTest {
-
-    @Test
-    public void testMakeJavaPackage() {
-        assertEquals("apps.projects.script_html", 
JavaEscapeUtils.makeJavaPackage("/apps/projects/script.html"));
-        // test also with windows path names
-        assertEquals("apps.projects.script_html", 
JavaEscapeUtils.makeJavaPackage("\\apps\\projects\\script.html"));
-        assertEquals("apps.projects.my__002d__script_html", 
JavaEscapeUtils.makeJavaPackage("/apps/projects/my-script.html"));
-    }
-
-}

Reply via email to