SLING-8012 - Extract an HTL runtime bundle from the existing HTL modules
authorRadu Cotescu <radu@apache.org>
Thu, 18 Oct 2018 13:43:15 +0000 (15:43 +0200)
committerRadu Cotescu <radu@apache.org>
Thu, 18 Oct 2018 13:43:15 +0000 (15:43 +0200)
* removed embedded antlr4-runtime; now depending on external dependency for the
ANTLR runtime
* delegated calls to the ObjectModel coming from the org.apache.sling.scripting.sightly.runtime
module and deprecated local org.apache.sling.scripting.sightly.compiler.util.ObjectModel
* deprecated org.apache.sling.scripting.sightly.compiler.RuntimeFunction

23 files changed:
pom.xml
src/main/java/org/apache/sling/scripting/sightly/compiler/RuntimeFunction.java
src/main/java/org/apache/sling/scripting/sightly/compiler/expression/nodes/BinaryOperator.java
src/main/java/org/apache/sling/scripting/sightly/compiler/expression/nodes/RuntimeCall.java
src/main/java/org/apache/sling/scripting/sightly/compiler/expression/nodes/UnaryOperator.java
src/main/java/org/apache/sling/scripting/sightly/compiler/expression/nodes/package-info.java
src/main/java/org/apache/sling/scripting/sightly/compiler/util/ObjectModel.java
src/main/java/org/apache/sling/scripting/sightly/impl/compiler/frontend/ExpressionParser.java
src/main/java/org/apache/sling/scripting/sightly/impl/compiler/optimization/DeadCodeRemoval.java
src/main/java/org/apache/sling/scripting/sightly/impl/compiler/optimization/reduce/ExpressionReducer.java
src/main/java/org/apache/sling/scripting/sightly/impl/filter/FormatFilter.java
src/main/java/org/apache/sling/scripting/sightly/impl/filter/I18nFilter.java
src/main/java/org/apache/sling/scripting/sightly/impl/filter/JoinFilter.java
src/main/java/org/apache/sling/scripting/sightly/impl/filter/URIManipulationFilter.java
src/main/java/org/apache/sling/scripting/sightly/impl/filter/XSSFilter.java
src/main/java/org/apache/sling/scripting/sightly/impl/html/dom/MarkupHandler.java
src/main/java/org/apache/sling/scripting/sightly/impl/plugin/AttributePlugin.java
src/main/java/org/apache/sling/scripting/sightly/impl/plugin/ElementPlugin.java
src/main/java/org/apache/sling/scripting/sightly/impl/plugin/IncludePlugin.java
src/main/java/org/apache/sling/scripting/sightly/impl/plugin/ResourcePlugin.java
src/main/java/org/apache/sling/scripting/sightly/impl/plugin/TextPlugin.java
src/main/java/org/apache/sling/scripting/sightly/impl/plugin/UsePlugin.java
src/test/java/org/apache/sling/scripting/sightly/impl/frontend/ExpressionWrapperTest.java

diff --git a/pom.xml b/pom.xml
index 3e1a15f..157a433 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -37,7 +37,7 @@
         The versioning scheme defined here corresponds to SLING-7406 (<module_version>-<htl_specification_version>). Take care when
         releasing to only increase the first part, unless the module provides support for a newer version of the HTL specification.
     -->
-    <version>1.0.23-1.4.0-SNAPSHOT</version>
+    <version>1.1.0-1.4.0-SNAPSHOT</version>
     <packaging>bundle</packaging>
 
     <name>Apache Sling Scripting HTL Compiler</name>
@@ -55,7 +55,7 @@
   </scm>
 
     <properties>
-        <antlr.version>4.1</antlr.version>
+        <antlr.version>4.7.1</antlr.version>
         <jacoco.maven.plugin.version>0.7.9</jacoco.maven.plugin.version>
     </properties>
 
@@ -79,7 +79,6 @@
                 <configuration>
                     <exportScr>true</exportScr>
                     <instructions>
-                        <Embed-Dependency>antlr4-runtime,org.abego.treelayout.core</Embed-Dependency>
                         <Provide-Capability>
                             io.sightly.compiler; version:Version=1.0,
                             io.sightly.compiler; version:Version=1.1,
             <artifactId>antlr4-runtime</artifactId>
             <version>${antlr.version}</version>
         </dependency>
+
+        <!-- Needed for the ObjectModel -->
         <dependency>
-            <groupId>org.abego.treelayout</groupId>
-            <artifactId>org.abego.treelayout.core</artifactId>
-            <version>1.0.1</version>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.scripting.sightly.runtime</artifactId>
+            <version>1.0.0-1.4.0-SNAPSHOT</version>
+            <scope>provided</scope>
         </dependency>
 
         <!-- Logging -->
index d04458f..19d2a61 100644 (file)
@@ -27,7 +27,11 @@ package org.apache.sling.scripting.sightly.compiler;
  *     that can transpile {@link org.apache.sling.scripting.sightly.compiler.expression.nodes.RuntimeCall}s to specific runtime function
  *     implementations.
  * </p>
+ *
+ * @deprecated constants are available in {@link org.apache.sling.scripting.sightly.compiler.expression.nodes.RuntimeCall}
+ * starting with version 1.2.0 of the {@link org.apache.sling.scripting.sightly.compiler.expression.nodes} API.
  */
+@Deprecated
 public final class RuntimeFunction {
 
     /**
index ff6bb9a..635eb6e 100644 (file)
@@ -21,7 +21,7 @@ package org.apache.sling.scripting.sightly.compiler.expression.nodes;
 import java.util.Collection;
 
 import org.apache.sling.scripting.sightly.compiler.SightlyCompilerException;
-import org.apache.sling.scripting.sightly.compiler.util.ObjectModel;
+import org.apache.sling.scripting.sightly.render.ObjectModel;
 
 /**
  * Binary operators used in expressions.
index f5fb158..76c6ec6 100644 (file)
@@ -21,8 +21,11 @@ package org.apache.sling.scripting.sightly.compiler.expression.nodes;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 
+import org.apache.sling.scripting.sightly.compiler.SightlyCompilerException;
 import org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode;
 import org.apache.sling.scripting.sightly.compiler.expression.NodeVisitor;
 
@@ -31,9 +34,149 @@ import org.apache.sling.scripting.sightly.compiler.expression.NodeVisitor;
  */
 public final class RuntimeCall implements ExpressionNode {
 
+    /**
+     * <p>
+     *     The name of the {@link org.apache.sling.scripting.sightly.compiler.expression.nodes.RuntimeCall} function that will process string
+     *     formatting. The function will receive the following parameters:
+     * </p>
+     * <ol>
+     *     <li>the format String (e.g. 'Hello {0}, welcome to {1}')</li>
+     *     <li>an array of objects that will replace the format placeholders</li>
+     * </ol>
+     * <p>
+     *     For more details check https://github.com/Adobe-Marketing-Cloud/htl-spec/blob/1.2/SPECIFICATION.md#122-format.
+     * </p>
+     */
+    public static final String FORMAT = "format";
+
+    /**
+     * <p>
+     *     The name of the {@link org.apache.sling.scripting.sightly.compiler.expression.nodes.RuntimeCall} function that will process
+     *     i18n. The function will receive the following parameters:
+     * </p>
+     * <ol>
+     *     <li>the String to translate</li>
+     *     <li>optional: locale information</li>
+     *     <li>optional: hint information</li>
+     *     <li>optional (not part of the specification): basename information; for more details see
+     *     {@link java.util.ResourceBundle#getBundle(String, java.util.Locale)}</li>
+     * </ol>
+     * <p>
+     *     For more details check https://github.com/Adobe-Marketing-Cloud/htl-spec/blob/1.2/SPECIFICATION.md#123-i18n.
+     * </p>
+     */
+    public static final String I18N = "i18n";
+
+    /**
+     * <p>
+     *     The name of the {@link org.apache.sling.scripting.sightly.compiler.expression.nodes.RuntimeCall} function that will process
+     *     join operations on arrays. The function will receive the following parameters:
+     * </p>
+     * <ol>
+     *     <li>the array of objects to join (e.g. [1, 2, 3])</li>
+     *     <li>the join string (e.g. ';')</li>
+     * </ol>
+     * <p>
+     *     For more details check https://github.com/Adobe-Marketing-Cloud/htl-spec/blob/1.2/SPECIFICATION.md#124-array-join.
+     * </p>
+     */
+    public static final String JOIN = "join";
+
+    /**
+     * <p>
+     *     The name of the {@link org.apache.sling.scripting.sightly.compiler.expression.nodes.RuntimeCall} function that will provide
+     *     URI manipulation support. The function will receive the following parameters:
+     * </p>
+     * <ol>
+     *     <li>optional: a URI string to process</li>
+     *     <li>optional: a Map containing URI manipulation options</li>
+     * </ol>
+     * <p>
+     *     For more details check https://github.com/Adobe-Marketing-Cloud/htl-spec/blob/1.2/SPECIFICATION.md#125-uri-manipulation.
+     * </p>
+     */
+    public static final String URI_MANIPULATION = "uriManipulation";
+
+    /**
+     * <p>
+     *     The name of the {@link org.apache.sling.scripting.sightly.compiler.expression.nodes.RuntimeCall} function that will provide
+     *     XSS escaping and filtering support. The function will receive the following parameters:
+     * </p>
+     * <ol>
+     *     <li>the original string to escape / filter</li>
+     *     <li>the context to be applied - see {@link org.apache.sling.scripting.sightly.compiler.expression.MarkupContext}</li>
+     * </ol>
+     * <p>
+     *     For more details check https://github.com/Adobe-Marketing-Cloud/htl-spec/blob/1.2/SPECIFICATION.md#121-display-context.
+     * </p>
+     */
+    public static final String XSS = "xss";
+
+    /**
+     * <p>
+     *     The name of the {@link org.apache.sling.scripting.sightly.compiler.expression.nodes.RuntimeCall} function that will perform
+     *     script execution delegation. The function will receive the following parameters:
+     * </p>
+     * <ol>
+     *     <li>optional: the relative or absolute path of the script to execute</li>
+     *     <li>optional: a Map of options to perform script include processing</li>
+     * </ol>
+     * <p>
+     *     For more details about the supported options check
+     *     https://github.com/Adobe-Marketing-Cloud/htl-spec/blob/1.2/SPECIFICATION.md#228-include.
+     * </p>
+     */
+    public static final String INCLUDE = "include";
+
+    /**
+     * <p>
+     *     The name of the {@link org.apache.sling.scripting.sightly.compiler.expression.nodes.RuntimeCall} function that will perform
+     *     resource inclusion in the rendering process. The function will receive the following parameters:
+     * </p>
+     * <ol>
+     *     <li>optional: a relative or absolute path of the resource to be included</li>
+     *     <li>optional: a Map containing the resource processing options</li>
+     * </ol>
+     * <p>
+     *     For more details about the supported options check
+     *     https://github.com/Adobe-Marketing-Cloud/htl-spec/blob/1.2/SPECIFICATION.md#229-resource.
+     * </p>
+     */
+    public static final String RESOURCE = "includeResource";
+
+    /**
+     * <p>
+     *     The name of the {@link org.apache.sling.scripting.sightly.compiler.expression.nodes.RuntimeCall} function that will provide
+     *     the support for loading Use-API objects. The function will receive the following parameters:
+     * </p>
+     * <ol>
+     *     <li>an identifier that allows to discover the Use-API object that needs to be loaded</li>
+     *     <li>optional: a Map of the arguments that are passed to the Use-API object for initialisation or to provide context</li>
+     * </ol>
+     * <p>
+     *     For more details check https://github.com/Adobe-Marketing-Cloud/htl-spec/blob/1.2/SPECIFICATION.md#221-use.
+     * </p>
+     */
+    public static final String USE = "use";
+
+    private static final Set<String> RUNTIME_FUNCTIONS;
+
+    static {
+        RUNTIME_FUNCTIONS = new HashSet<>();
+        RUNTIME_FUNCTIONS.add(USE);
+        RUNTIME_FUNCTIONS.add(RESOURCE);
+        RUNTIME_FUNCTIONS.add(INCLUDE);
+        RUNTIME_FUNCTIONS.add(I18N);
+        RUNTIME_FUNCTIONS.add(XSS);
+        RUNTIME_FUNCTIONS.add(URI_MANIPULATION);
+        RUNTIME_FUNCTIONS.add(JOIN);
+        RUNTIME_FUNCTIONS.add(FORMAT);
+    }
+
     private final String functionName;
     private final List<ExpressionNode> arguments;
 
+
     /**
      * Creates a {@code RuntimeCall} based on a {@code functionName} and an array of {@code arguments}.
      *
@@ -51,6 +194,10 @@ public final class RuntimeCall implements ExpressionNode {
      * @param arguments    the arguments passed to the runtime call
      */
     public RuntimeCall(String functionName, List<ExpressionNode> arguments) {
+        if (!RUNTIME_FUNCTIONS.contains(functionName)) {
+            throw new SightlyCompilerException(
+                    String.format("Function %s is not a recognised runtime function - %s.", functionName, RUNTIME_FUNCTIONS));
+        }
         this.functionName = functionName;
         this.arguments = new ArrayList<>(arguments);
     }
index 61ea5b4..1b944d6 100644 (file)
@@ -19,7 +19,7 @@
 package org.apache.sling.scripting.sightly.compiler.expression.nodes;
 
 import org.apache.commons.lang3.StringUtils;
-import org.apache.sling.scripting.sightly.compiler.util.ObjectModel;
+import org.apache.sling.scripting.sightly.render.ObjectModel;
 
 /**
  * Unary operators used in expressions.
index 104dfe7..e2c21a3 100644 (file)
@@ -19,7 +19,7 @@
  * The {@code org.apache.sling.scripting.sightly.compiler.expression.nodes} package exposes the various
  * {@link org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode} types.
  */
-@Version("1.1.0")
+@Version("1.2.0")
 package org.apache.sling.scripting.sightly.compiler.expression.nodes;
 
 import org.osgi.annotation.versioning.Version;
index f852f03..aa7bd8f 100644 (file)
@@ -38,7 +38,10 @@ import org.slf4j.LoggerFactory;
 
 /**
  * The {@code ObjectModel} class provides various static models for object conversion and object property resolution.
+ *
+ * @deprecated This class has been moved to {@link org.apache.sling.scripting.sightly.render.ObjectModel}.
  */
+@Deprecated
 public final class ObjectModel {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(ObjectModel.class);
@@ -52,7 +55,6 @@ public final class ObjectModel {
     static {
         Set<Class<?>> primitivesBuilder = new HashSet<>();
         primitivesBuilder.add(Boolean.class);
-        primitivesBuilder.add(Boolean.class);
         primitivesBuilder.add(Character.class);
         primitivesBuilder.add(Byte.class);
         primitivesBuilder.add(Short.class);
index a3f24b4..2a6dd4e 100644 (file)
@@ -18,7 +18,7 @@
  ******************************************************************************/
 package org.apache.sling.scripting.sightly.impl.compiler.frontend;
 
-import org.antlr.v4.runtime.ANTLRInputStream;
+import org.antlr.v4.runtime.CharStreams;
 import org.antlr.v4.runtime.CommonTokenStream;
 import org.antlr.v4.runtime.RecognitionException;
 import org.apache.sling.scripting.sightly.compiler.SightlyCompilerException;
@@ -47,7 +47,7 @@ public class ExpressionParser {
     }
 
     private SightlyParser createParser(String string) {
-        SightlyLexer lexer = new SightlyLexer(new ANTLRInputStream(string));
+        SightlyLexer lexer = new SightlyLexer(CharStreams.fromString(string));
         lexer.removeErrorListeners();
         lexer.addErrorListener(new SightlyParserErrorListener());
         CommonTokenStream tokenStream = new CommonTokenStream(lexer);
index f4601df..0ddbc70 100644 (file)
@@ -35,7 +35,7 @@ import org.apache.sling.scripting.sightly.impl.compiler.util.stream.EmitterVisit
 import org.apache.sling.scripting.sightly.impl.compiler.util.stream.Streams;
 import org.apache.sling.scripting.sightly.impl.compiler.visitor.StatefulRangeIgnore;
 import org.apache.sling.scripting.sightly.impl.compiler.visitor.TrackingVisitor;
-import org.apache.sling.scripting.sightly.compiler.util.ObjectModel;
+import org.apache.sling.scripting.sightly.render.ObjectModel;
 
 /**
  * Removes code under conditionals which are proven to fail. It is probably a good idea to run this optimization after running
index a721aef..da317fa 100644 (file)
@@ -40,7 +40,7 @@ import org.apache.sling.scripting.sightly.compiler.expression.nodes.StringConsta
 import org.apache.sling.scripting.sightly.compiler.expression.nodes.TernaryOperator;
 import org.apache.sling.scripting.sightly.compiler.expression.nodes.UnaryOperation;
 import org.apache.sling.scripting.sightly.compiler.util.VariableTracker;
-import org.apache.sling.scripting.sightly.compiler.util.ObjectModel;
+import org.apache.sling.scripting.sightly.render.ObjectModel;
 
 /**
  * Try to evaluate constant parts in expressions
index 52e00f0..1962dc1 100644 (file)
@@ -18,7 +18,6 @@
  ******************************************************************************/
 package org.apache.sling.scripting.sightly.impl.filter;
 
-import org.apache.sling.scripting.sightly.compiler.RuntimeFunction;
 import org.apache.sling.scripting.sightly.compiler.expression.Expression;
 import org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode;
 import org.apache.sling.scripting.sightly.compiler.expression.nodes.MapLiteral;
@@ -39,9 +38,6 @@ public class FormatFilter extends AbstractFilter {
     }
 
     private FormatFilter() {
-        if (FormatFilterLoader.INSTANCE != null) {
-            throw new IllegalStateException("INSTANCE was already defined.");
-        }
     }
 
     public static FormatFilter getInstance() {
@@ -57,7 +53,7 @@ public class FormatFilter extends AbstractFilter {
             return expression;
         }
         ExpressionNode translation =
-                new RuntimeCall(RuntimeFunction.FORMAT, expression.getRoot(),
+                new RuntimeCall(RuntimeCall.FORMAT, expression.getRoot(),
                         new MapLiteral(getFilterOptions(expression,
                                 FORMAT_OPTION,
                                 TYPE_OPTION,
index fdb07cd..5275b13 100644 (file)
@@ -20,7 +20,6 @@ package org.apache.sling.scripting.sightly.impl.filter;
 
 import java.util.Map;
 
-import org.apache.sling.scripting.sightly.compiler.RuntimeFunction;
 import org.apache.sling.scripting.sightly.compiler.expression.Expression;
 import org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode;
 import org.apache.sling.scripting.sightly.compiler.expression.nodes.MapLiteral;
@@ -58,7 +57,7 @@ public final class I18nFilter extends AbstractFilter {
             return expression;
         }
         Map <String, ExpressionNode> options = getFilterOptions(expression, HINT_OPTION, LOCALE_OPTION, BASENAME_OPTION);
-        ExpressionNode translation = new RuntimeCall(RuntimeFunction.I18N, expression.getRoot(), new MapLiteral
+        ExpressionNode translation = new RuntimeCall(RuntimeCall.I18N, expression.getRoot(), new MapLiteral
                 (options));
         expression.removeOption(I18N_OPTION);
         expression.getOptions().put(FormatFilter.FORMAT_LOCALE_OPTION, options.get(LOCALE_OPTION));
index 2c7db74..113b3f5 100644 (file)
@@ -18,7 +18,6 @@
  ******************************************************************************/
 package org.apache.sling.scripting.sightly.impl.filter;
 
-import org.apache.sling.scripting.sightly.compiler.RuntimeFunction;
 import org.apache.sling.scripting.sightly.compiler.expression.Expression;
 import org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode;
 import org.apache.sling.scripting.sightly.compiler.expression.nodes.RuntimeCall;
@@ -51,7 +50,7 @@ public class JoinFilter extends AbstractFilter {
             return expression;
         }
         ExpressionNode translation =
-                new RuntimeCall(RuntimeFunction.JOIN, expression.getRoot(), expression.removeOption(JOIN_OPTION));
+                new RuntimeCall(RuntimeCall.JOIN, expression.getRoot(), expression.removeOption(JOIN_OPTION));
         return expression.withNode(translation);
     }
 }
index 21ae1e0..8ba4b22 100644 (file)
@@ -20,7 +20,6 @@ package org.apache.sling.scripting.sightly.impl.filter;
 
 import java.util.Map;
 
-import org.apache.sling.scripting.sightly.compiler.RuntimeFunction;
 import org.apache.sling.scripting.sightly.compiler.expression.Expression;
 import org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode;
 import org.apache.sling.scripting.sightly.compiler.expression.nodes.MapLiteral;
@@ -80,7 +79,7 @@ public class URIManipulationFilter extends AbstractFilter {
                     ADD_QUERY, REMOVE_QUERY);
             if (uriOptions.size() > 0) {
                 ExpressionNode translation =
-                        new RuntimeCall(RuntimeFunction.URI_MANIPULATION, expression.getRoot(), new MapLiteral(uriOptions));
+                        new RuntimeCall(RuntimeCall.URI_MANIPULATION, expression.getRoot(), new MapLiteral(uriOptions));
                 return expression.withNode(translation);
             }
         }
index 30ac5b2..f693102 100644 (file)
  ******************************************************************************/
 package org.apache.sling.scripting.sightly.impl.filter;
 
-import org.apache.sling.scripting.sightly.compiler.RuntimeFunction;
-import org.apache.sling.scripting.sightly.impl.compiler.Syntax;
 import org.apache.sling.scripting.sightly.compiler.expression.Expression;
 import org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode;
 import org.apache.sling.scripting.sightly.compiler.expression.nodes.RuntimeCall;
+import org.apache.sling.scripting.sightly.impl.compiler.Syntax;
 
 /**
  * XSS filter implementation.
@@ -52,7 +51,7 @@ public class XSSFilter extends AbstractFilter {
         }
         ExpressionNode context = expression.removeOption(Syntax.CONTEXT_OPTION);
         if (context != null) {
-            return expression.withNode(new RuntimeCall(RuntimeFunction.XSS, expression.getRoot(), context));
+            return expression.withNode(new RuntimeCall(RuntimeCall.XSS, expression.getRoot(), context));
         }
         return expression;
     }
index 8c964a0..c8c4966 100644 (file)
@@ -28,7 +28,6 @@ import java.util.Set;
 import java.util.Stack;
 
 import org.apache.commons.lang3.StringUtils;
-import org.apache.sling.scripting.sightly.compiler.RuntimeFunction;
 import org.apache.sling.scripting.sightly.compiler.SightlyCompilerException;
 import org.apache.sling.scripting.sightly.compiler.commands.Conditional;
 import org.apache.sling.scripting.sightly.compiler.commands.OutText;
@@ -222,7 +221,7 @@ public class MarkupHandler {
         boolean alreadyEscaped = false;
         if (valueExpression.getRoot() instanceof RuntimeCall) {
             RuntimeCall rc = (RuntimeCall) valueExpression.getRoot();
-            if (RuntimeFunction.XSS.equals(rc.getFunctionName())) {
+            if (RuntimeCall.XSS.equals(rc.getFunctionName())) {
                 alreadyEscaped = true;
             }
         }
@@ -230,8 +229,7 @@ public class MarkupHandler {
         stream.write(new VariableBinding.Start(attrValue, node)); //attrContent = <expr>
         if (!alreadyEscaped) {
             Expression contentExpression = valueExpression.withNode(new Identifier(attrValue));
-            stream.write(new VariableBinding.Start(attrContent, adjustContext(compilerContext, contentExpression, markupContext,
-                    ExpressionContext.ATTRIBUTE).getRoot()));
+            stream.write(new VariableBinding.Start(attrContent, adjustContext(compilerContext, contentExpression, markupContext).getRoot()));
             stream.write(
                     new VariableBinding.Start(
                             shouldDisplayAttr,
@@ -473,15 +471,14 @@ public class MarkupHandler {
                 ("script".equals(parentElementName) || "style".equals(parentElementName));
     }
 
-    private Expression adjustContext(CompilerContext compilerContext, Expression expression, MarkupContext markupContext,
-                                     ExpressionContext expressionContext) {
+    private Expression adjustContext(CompilerContext compilerContext, Expression expression, MarkupContext markupContext) {
         ExpressionNode root = expression.getRoot();
         if (root instanceof RuntimeCall) {
             RuntimeCall runtimeCall = (RuntimeCall) root;
-            if (runtimeCall.getFunctionName().equals(RuntimeFunction.XSS)) {
+            if (runtimeCall.getFunctionName().equals(RuntimeCall.XSS)) {
                 return expression;
             }
         }
-        return compilerContext.adjustToContext(expression, markupContext, expressionContext);
+        return compilerContext.adjustToContext(expression, markupContext, ExpressionContext.ATTRIBUTE);
     }
 }
index f239571..469f590 100644 (file)
@@ -23,7 +23,6 @@ import java.util.HashSet;
 import java.util.Set;
 
 import org.apache.commons.lang3.StringUtils;
-import org.apache.sling.scripting.sightly.compiler.RuntimeFunction;
 import org.apache.sling.scripting.sightly.compiler.commands.Conditional;
 import org.apache.sling.scripting.sightly.compiler.commands.Loop;
 import org.apache.sling.scripting.sightly.compiler.commands.OutText;
@@ -345,7 +344,7 @@ public class AttributePlugin extends AbstractPlugin {
                                                      ExpressionNode hint) {
         if (hint != null) {
             //todo: this is not the indicated way to escape via XSS. Correct after modifying the compiler context API
-            return new RuntimeCall(RuntimeFunction.XSS, node, new StringConstant(markupContext.getName()), hint);
+            return new RuntimeCall(RuntimeCall.XSS, node, new StringConstant(markupContext.getName()), hint);
         }
         return compilerContext.adjustToContext(new Expression(node), markupContext, ExpressionContext.ATTRIBUTE).getRoot();
     }
index 89de6a2..29439b1 100644 (file)
@@ -18,7 +18,6 @@
  ******************************************************************************/
 package org.apache.sling.scripting.sightly.impl.plugin;
 
-import org.apache.sling.scripting.sightly.compiler.RuntimeFunction;
 import org.apache.sling.scripting.sightly.compiler.commands.Conditional;
 import org.apache.sling.scripting.sightly.compiler.commands.OutText;
 import org.apache.sling.scripting.sightly.compiler.commands.OutputVariable;
@@ -95,7 +94,7 @@ public class ElementPlugin extends AbstractPlugin {
         ExpressionNode root = expression.getRoot();
         if (root instanceof RuntimeCall) {
             RuntimeCall runtimeCall = (RuntimeCall) root;
-            if (runtimeCall.getFunctionName().equals(RuntimeFunction.XSS)) {
+            if (runtimeCall.getFunctionName().equals(RuntimeCall.XSS)) {
                 return expression;
             }
         }
index 0a4e331..b5df5bf 100644 (file)
  ******************************************************************************/
 package org.apache.sling.scripting.sightly.impl.plugin;
 
-import org.apache.sling.scripting.sightly.compiler.RuntimeFunction;
 import org.apache.sling.scripting.sightly.compiler.commands.OutputVariable;
-import org.apache.sling.scripting.sightly.impl.compiler.Patterns;
 import org.apache.sling.scripting.sightly.compiler.commands.VariableBinding;
-import org.apache.sling.scripting.sightly.impl.compiler.PushStream;
 import org.apache.sling.scripting.sightly.compiler.expression.Expression;
 import org.apache.sling.scripting.sightly.compiler.expression.nodes.Identifier;
 import org.apache.sling.scripting.sightly.compiler.expression.nodes.MapLiteral;
 import org.apache.sling.scripting.sightly.compiler.expression.nodes.RuntimeCall;
+import org.apache.sling.scripting.sightly.impl.compiler.Patterns;
+import org.apache.sling.scripting.sightly.impl.compiler.PushStream;
 import org.apache.sling.scripting.sightly.impl.compiler.frontend.CompilerContext;
 
 /**
@@ -48,7 +47,7 @@ public class IncludePlugin extends AbstractPlugin {
                 String pathVar = compilerContext.generateVariable("includePath");
                 stream.write(new VariableBinding.Start(pathVar, expression.getRoot()));
                 stream.write(new VariableBinding.Start(includedContentVar,
-                        new RuntimeCall(RuntimeFunction.INCLUDE, new Identifier(pathVar), new MapLiteral(expression.getOptions()))));
+                        new RuntimeCall(RuntimeCall.INCLUDE, new Identifier(pathVar), new MapLiteral(expression.getOptions()))));
                 stream.write(new OutputVariable(includedContentVar));
                 stream.write(VariableBinding.END); //end includedContentVar
                 stream.write(VariableBinding.END); //end pathVar
index aac78bc..f6e7917 100644 (file)
@@ -21,15 +21,14 @@ package org.apache.sling.scripting.sightly.impl.plugin;
 import java.util.HashMap;
 import java.util.Map;
 
-import org.apache.sling.scripting.sightly.compiler.RuntimeFunction;
 import org.apache.sling.scripting.sightly.compiler.commands.OutputVariable;
-import org.apache.sling.scripting.sightly.impl.compiler.Patterns;
 import org.apache.sling.scripting.sightly.compiler.commands.VariableBinding;
-import org.apache.sling.scripting.sightly.impl.compiler.PushStream;
 import org.apache.sling.scripting.sightly.compiler.expression.Expression;
 import org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode;
 import org.apache.sling.scripting.sightly.compiler.expression.nodes.MapLiteral;
 import org.apache.sling.scripting.sightly.compiler.expression.nodes.RuntimeCall;
+import org.apache.sling.scripting.sightly.impl.compiler.Patterns;
+import org.apache.sling.scripting.sightly.impl.compiler.PushStream;
 import org.apache.sling.scripting.sightly.impl.compiler.frontend.CompilerContext;
 
 /**
@@ -52,7 +51,7 @@ public class ResourcePlugin extends AbstractPlugin {
             public void beforeChildren(PushStream stream) {
                 String resourceVar = compilerContext.generateVariable("resourceContent");
                 stream.write(new VariableBinding.Start(resourceVar,
-                        new RuntimeCall(RuntimeFunction.RESOURCE, expression.getRoot(), new MapLiteral(expressionOptions))));
+                        new RuntimeCall(RuntimeCall.RESOURCE, expression.getRoot(), new MapLiteral(expressionOptions))));
                 stream.write(new OutputVariable(resourceVar));
                 stream.write(VariableBinding.END);
                 Patterns.beginStreamIgnore(stream);
index f7dda86..7fefb4d 100644 (file)
  ******************************************************************************/
 package org.apache.sling.scripting.sightly.impl.plugin;
 
-import org.apache.sling.scripting.sightly.compiler.RuntimeFunction;
 import org.apache.sling.scripting.sightly.compiler.commands.OutputVariable;
+import org.apache.sling.scripting.sightly.compiler.commands.VariableBinding;
+import org.apache.sling.scripting.sightly.compiler.expression.Expression;
 import org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode;
 import org.apache.sling.scripting.sightly.compiler.expression.MarkupContext;
 import org.apache.sling.scripting.sightly.compiler.expression.nodes.RuntimeCall;
 import org.apache.sling.scripting.sightly.impl.compiler.Patterns;
-import org.apache.sling.scripting.sightly.compiler.commands.VariableBinding;
 import org.apache.sling.scripting.sightly.impl.compiler.PushStream;
-import org.apache.sling.scripting.sightly.compiler.expression.Expression;
 import org.apache.sling.scripting.sightly.impl.compiler.frontend.CompilerContext;
 import org.apache.sling.scripting.sightly.impl.filter.ExpressionContext;
 
@@ -66,7 +65,7 @@ public class TextPlugin extends AbstractPlugin {
         ExpressionNode root = expression.getRoot();
         if (root instanceof RuntimeCall) {
             RuntimeCall runtimeCall = (RuntimeCall) root;
-            if (runtimeCall.getFunctionName().equals(RuntimeFunction.XSS)) {
+            if (runtimeCall.getFunctionName().equals(RuntimeCall.XSS)) {
                 return expression;
             }
         }
index ea5bee4..6391db7 100644 (file)
  ******************************************************************************/
 package org.apache.sling.scripting.sightly.impl.plugin;
 
-import org.apache.sling.scripting.sightly.compiler.RuntimeFunction;
 import org.apache.sling.scripting.sightly.compiler.commands.VariableBinding;
-import org.apache.sling.scripting.sightly.impl.compiler.PushStream;
 import org.apache.sling.scripting.sightly.compiler.expression.Expression;
 import org.apache.sling.scripting.sightly.compiler.expression.nodes.MapLiteral;
 import org.apache.sling.scripting.sightly.compiler.expression.nodes.RuntimeCall;
+import org.apache.sling.scripting.sightly.impl.compiler.PushStream;
 import org.apache.sling.scripting.sightly.impl.compiler.frontend.CompilerContext;
 
 public class UsePlugin extends AbstractPlugin {
@@ -45,7 +44,7 @@ public class UsePlugin extends AbstractPlugin {
             public void beforeElement(PushStream stream, String tagName) {
                 String variableName = decodeVariableName();
                 stream.write(new VariableBinding.Global(variableName,
-                        new RuntimeCall(RuntimeFunction.USE, expression.getRoot(), new MapLiteral(expression.getOptions()))));
+                        new RuntimeCall(RuntimeCall.USE, expression.getRoot(), new MapLiteral(expression.getOptions()))));
             }
 
             private String decodeVariableName() {
index 5fffd65..4eea958 100644 (file)
  ******************************************************************************/
 package org.apache.sling.scripting.sightly.impl.frontend;
 
-import static org.junit.Assert.assertEquals;
-
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
-import org.apache.sling.scripting.sightly.compiler.RuntimeFunction;
 import org.apache.sling.scripting.sightly.compiler.expression.Expression;
 import org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode;
 import org.apache.sling.scripting.sightly.compiler.expression.MarkupContext;
@@ -48,6 +45,8 @@ import org.apache.sling.scripting.sightly.impl.filter.XSSFilter;
 import org.junit.BeforeClass;
 import org.junit.Test;
 
+import static org.junit.Assert.assertEquals;
+
 public class ExpressionWrapperTest {
 
     private static List<Filter> filters;
@@ -75,7 +74,7 @@ public class ExpressionWrapperTest {
         Expression result = wrapper.transform(interpolation, MarkupContext.TEXT, ExpressionContext.TEXT);
         List<ExpressionNode> xssArguments = runOptionsAndXSSAssertions(result, 1);
         RuntimeCall i18n = (RuntimeCall) xssArguments.get(0);
-        assertEquals("Expected to I18n runtime function call.", RuntimeFunction.I18N, i18n.getFunctionName());
+        assertEquals("Expected to I18n runtime function call.", RuntimeCall.I18N, i18n.getFunctionName());
     }
 
     @Test
@@ -91,7 +90,7 @@ public class ExpressionWrapperTest {
         Expression result = wrapper.transform(interpolation, MarkupContext.TEXT, ExpressionContext.TEXT);
         List<ExpressionNode> xssArguments = runOptionsAndXSSAssertions(result, 0);
         RuntimeCall format = (RuntimeCall) xssArguments.get(0);
-        assertEquals(RuntimeFunction.FORMAT, format.getFunctionName());
+        assertEquals(RuntimeCall.FORMAT, format.getFunctionName());
     }
 
     @Test
@@ -107,7 +106,7 @@ public class ExpressionWrapperTest {
         Expression result = wrapper.transform(interpolation, MarkupContext.TEXT, ExpressionContext.TEXT);
         List<ExpressionNode> xssArguments = runOptionsAndXSSAssertions(result, 0);
         RuntimeCall join = (RuntimeCall) xssArguments.get(0);
-        assertEquals(RuntimeFunction.JOIN, join.getFunctionName());
+        assertEquals(RuntimeCall.JOIN, join.getFunctionName());
     }
 
     @Test
@@ -143,13 +142,13 @@ public class ExpressionWrapperTest {
         Expression result = wrapper.transform(interpolation, MarkupContext.TEXT, ExpressionContext.TEXT);
         List<ExpressionNode> xssArguments = runOptionsAndXSSAssertions(result, 0);
         RuntimeCall join = (RuntimeCall) xssArguments.get(0);
-        assertEquals(RuntimeFunction.URI_MANIPULATION, join.getFunctionName());
+        assertEquals(RuntimeCall.URI_MANIPULATION, join.getFunctionName());
     }
 
     private List<ExpressionNode> runOptionsAndXSSAssertions(Expression result, int expectedOptions) {
         assertEquals("Options map size for expression after processing is different from expected.", expectedOptions, result.getOptions().size());
         RuntimeCall xss = (RuntimeCall) result.getRoot();
-        assertEquals("Expected XSS escaping applied to expression.", RuntimeFunction.XSS, xss.getFunctionName());
+        assertEquals("Expected XSS escaping applied to expression.", RuntimeCall.XSS, xss.getFunctionName());
         return xss.getArguments();
     }