Search in sources :

Example 1 with Expression

use of org.apache.sling.scripting.sightly.compiler.expression.Expression in project sling by apache.

the class ExpressionWrapper method transform.

public Expression transform(Interpolation interpolation, MarkupContext markupContext, ExpressionContext expressionContext) {
    ArrayList<ExpressionNode> nodes = new ArrayList<>();
    HashMap<String, ExpressionNode> options = new HashMap<>();
    for (Fragment fragment : interpolation.getFragments()) {
        if (fragment.isString()) {
            nodes.add(new StringConstant(fragment.getText()));
        } else {
            Expression expression = fragment.getExpression();
            Expression transformed = adjustToContext(expression, markupContext, expressionContext);
            nodes.add(transformed.getRoot());
            options.putAll(transformed.getOptions());
        }
    }
    ExpressionNode root = join(nodes);
    if (interpolation.size() > 1 && options.containsKey(Syntax.CONTEXT_OPTION)) {
        //context must not be calculated by merging
        options.remove(Syntax.CONTEXT_OPTION);
    }
    return new Expression(root, options, interpolation.getContent());
}
Also used : HashMap(java.util.HashMap) Expression(org.apache.sling.scripting.sightly.compiler.expression.Expression) ExpressionNode(org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode) ArrayList(java.util.ArrayList) StringConstant(org.apache.sling.scripting.sightly.compiler.expression.nodes.StringConstant)

Example 2 with Expression

use of org.apache.sling.scripting.sightly.compiler.expression.Expression in project sling by apache.

the class ExpressionWrapperTest method testI18nOptionsRemoval.

@Test
public void testI18nOptionsRemoval() {
    Interpolation interpolation = new Interpolation();
    Map<String, ExpressionNode> options = new HashMap<>();
    options.put(I18nFilter.HINT_OPTION, new StringConstant("hint"));
    options.put(I18nFilter.LOCALE_OPTION, new StringConstant("de"));
    options.put(I18nFilter.I18N_OPTION, NullLiteral.INSTANCE);
    interpolation.addExpression(new Expression(new StringConstant("hello"), options));
    ExpressionWrapper wrapper = new ExpressionWrapper(filters);
    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());
}
Also used : Interpolation(org.apache.sling.scripting.sightly.impl.compiler.frontend.Interpolation) HashMap(java.util.HashMap) Expression(org.apache.sling.scripting.sightly.compiler.expression.Expression) ExpressionNode(org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode) RuntimeCall(org.apache.sling.scripting.sightly.compiler.expression.nodes.RuntimeCall) StringConstant(org.apache.sling.scripting.sightly.compiler.expression.nodes.StringConstant) ExpressionWrapper(org.apache.sling.scripting.sightly.impl.compiler.frontend.ExpressionWrapper) Test(org.junit.Test)

Example 3 with Expression

use of org.apache.sling.scripting.sightly.compiler.expression.Expression in project sling by apache.

the class MarkupHandler method handlePlugin.

private void handlePlugin(String name, String value, ElementContext context) {
    PluginCallInfo callInfo = Syntax.parsePluginAttribute(name);
    if (callInfo != null) {
        Plugin plugin = obtainPlugin(callInfo.getName());
        ExpressionContext expressionContext = ExpressionContext.getContextForPlugin(plugin.name());
        Expression expr = expressionWrapper.transform(expressionParser.parseInterpolation(value), null, expressionContext);
        PluginInvoke invoke = plugin.invoke(expr, callInfo, compilerContext);
        context.addPlugin(invoke, plugin.priority());
        context.addPluginCall(name, callInfo, expr);
    }
}
Also used : PluginCallInfo(org.apache.sling.scripting.sightly.impl.plugin.PluginCallInfo) PluginInvoke(org.apache.sling.scripting.sightly.impl.plugin.PluginInvoke) ExpressionContext(org.apache.sling.scripting.sightly.impl.filter.ExpressionContext) Expression(org.apache.sling.scripting.sightly.compiler.expression.Expression) Plugin(org.apache.sling.scripting.sightly.impl.plugin.Plugin)

Example 4 with Expression

use of org.apache.sling.scripting.sightly.compiler.expression.Expression in project sling by apache.

the class ExpressionWrapperTest method testURIOptionsRemoval.

@Test
public void testURIOptionsRemoval() {
    Interpolation interpolation = new Interpolation();
    Map<String, ExpressionNode> options = new HashMap<>();
    options.put(URIManipulationFilter.SCHEME, new StringConstant("https"));
    options.put(URIManipulationFilter.DOMAIN, new StringConstant("www.example.org"));
    options.put(URIManipulationFilter.PREPEND_PATH, new StringConstant("/before"));
    options.put(URIManipulationFilter.PATH, new StringConstant("/path"));
    options.put(URIManipulationFilter.APPEND_PATH, new StringConstant("/after"));
    List<ExpressionNode> selectors = new ArrayList<>();
    selectors.add(new StringConstant("a"));
    selectors.add(new StringConstant("b"));
    options.put(URIManipulationFilter.SELECTORS, new ArrayLiteral(selectors));
    options.put(URIManipulationFilter.EXTENSION, new StringConstant("html"));
    options.put(URIManipulationFilter.PREPEND_SUFFIX, new StringConstant("/pre"));
    options.put(URIManipulationFilter.APPEND_SUFFIX, new StringConstant("/after"));
    options.put(URIManipulationFilter.FRAGMENT, new StringConstant("rewrite"));
    Map<String, ExpressionNode> query = new HashMap<>();
    query.put("q", new StringConstant("sightly"));
    query.put("array", new ArrayLiteral(new ArrayList<ExpressionNode>() {

        {
            add(new NumericConstant(1));
            add(new NumericConstant(2));
            add(new NumericConstant(3));
        }
    }));
    options.put(URIManipulationFilter.QUERY, new MapLiteral(query));
    options.put(URIManipulationFilter.REMOVE_QUERY, new StringConstant("array"));
    interpolation.addExpression(new Expression(new StringConstant("http://www.example.com/resource.selector.extension/suffix#fragment?param=value"), options));
    ExpressionWrapper wrapper = new ExpressionWrapper(filters);
    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());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RuntimeCall(org.apache.sling.scripting.sightly.compiler.expression.nodes.RuntimeCall) ArrayLiteral(org.apache.sling.scripting.sightly.compiler.expression.nodes.ArrayLiteral) Interpolation(org.apache.sling.scripting.sightly.impl.compiler.frontend.Interpolation) MapLiteral(org.apache.sling.scripting.sightly.compiler.expression.nodes.MapLiteral) Expression(org.apache.sling.scripting.sightly.compiler.expression.Expression) ExpressionNode(org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode) NumericConstant(org.apache.sling.scripting.sightly.compiler.expression.nodes.NumericConstant) StringConstant(org.apache.sling.scripting.sightly.compiler.expression.nodes.StringConstant) ExpressionWrapper(org.apache.sling.scripting.sightly.impl.compiler.frontend.ExpressionWrapper) Test(org.junit.Test)

Example 5 with Expression

use of org.apache.sling.scripting.sightly.compiler.expression.Expression in project sling by apache.

the class ExpressionWrapperTest method testJoinOptionsRemoval.

@Test
public void testJoinOptionsRemoval() {
    Interpolation interpolation = new Interpolation();
    Map<String, ExpressionNode> options = new HashMap<>();
    options.put(JoinFilter.JOIN_OPTION, new StringConstant(";"));
    List<ExpressionNode> array = new ArrayList<>();
    array.add(new NumericConstant(0));
    array.add(new NumericConstant(1));
    interpolation.addExpression(new Expression(new ArrayLiteral(array), options));
    ExpressionWrapper wrapper = new ExpressionWrapper(filters);
    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());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RuntimeCall(org.apache.sling.scripting.sightly.compiler.expression.nodes.RuntimeCall) ArrayLiteral(org.apache.sling.scripting.sightly.compiler.expression.nodes.ArrayLiteral) Interpolation(org.apache.sling.scripting.sightly.impl.compiler.frontend.Interpolation) Expression(org.apache.sling.scripting.sightly.compiler.expression.Expression) ExpressionNode(org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode) NumericConstant(org.apache.sling.scripting.sightly.compiler.expression.nodes.NumericConstant) StringConstant(org.apache.sling.scripting.sightly.compiler.expression.nodes.StringConstant) ExpressionWrapper(org.apache.sling.scripting.sightly.impl.compiler.frontend.ExpressionWrapper) Test(org.junit.Test)

Aggregations

Expression (org.apache.sling.scripting.sightly.compiler.expression.Expression)10 StringConstant (org.apache.sling.scripting.sightly.compiler.expression.nodes.StringConstant)7 ExpressionNode (org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode)6 Interpolation (org.apache.sling.scripting.sightly.impl.compiler.frontend.Interpolation)6 HashMap (java.util.HashMap)5 RuntimeCall (org.apache.sling.scripting.sightly.compiler.expression.nodes.RuntimeCall)5 ArrayList (java.util.ArrayList)4 ExpressionWrapper (org.apache.sling.scripting.sightly.impl.compiler.frontend.ExpressionWrapper)4 Test (org.junit.Test)4 ArrayLiteral (org.apache.sling.scripting.sightly.compiler.expression.nodes.ArrayLiteral)3 Conditional (org.apache.sling.scripting.sightly.compiler.commands.Conditional)2 OutputVariable (org.apache.sling.scripting.sightly.compiler.commands.OutputVariable)2 VariableBinding (org.apache.sling.scripting.sightly.compiler.commands.VariableBinding)2 BinaryOperation (org.apache.sling.scripting.sightly.compiler.expression.nodes.BinaryOperation)2 Identifier (org.apache.sling.scripting.sightly.compiler.expression.nodes.Identifier)2 NumericConstant (org.apache.sling.scripting.sightly.compiler.expression.nodes.NumericConstant)2 PushStream (org.apache.sling.scripting.sightly.impl.compiler.PushStream)2 Fragment (org.apache.sling.scripting.sightly.impl.compiler.frontend.Fragment)2 MarkupContext (org.apache.sling.scripting.sightly.compiler.expression.MarkupContext)1 MapLiteral (org.apache.sling.scripting.sightly.compiler.expression.nodes.MapLiteral)1