Search in sources :

Example 6 with ExpressionNode

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

the class SyntheticMapRemoval method visit.

@Override
public void visit(VariableBinding.Start variableBindingStart) {
    ExpressionNode node = variableBindingStart.getExpression();
    String variable = variableBindingStart.getVariableName();
    ExpressionNode transformed = transform(node);
    if (transformed instanceof MapLiteral) {
        MapLiteral newLiteral = overrideMap(variable, (MapLiteral) transformed);
        tracker.pushVariable(variable, newLiteral);
        transformed = newLiteral;
    } else {
        tracker.pushVariable(variable, null);
    }
    outputStream.write(new VariableBinding.Start(variable, transformed));
}
Also used : MapLiteral(org.apache.sling.scripting.sightly.compiler.expression.nodes.MapLiteral) ExpressionNode(org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode) VariableBinding(org.apache.sling.scripting.sightly.compiler.commands.VariableBinding)

Example 7 with ExpressionNode

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

the class ExpressionReducer method evaluate.

@Override
public EvalResult evaluate(ArrayLiteral arrayLiteral) {
    ArrayList<EvalResult> results = new ArrayList<>();
    boolean isConstant = true;
    for (ExpressionNode node : arrayLiteral.getItems()) {
        EvalResult result = eval(node);
        results.add(result);
        isConstant = isConstant && result.isConstant();
    }
    if (isConstant) {
        ArrayList<Object> list = new ArrayList<>();
        for (EvalResult result : results) {
            list.add(result.getValue());
        }
        return EvalResult.constant(list);
    } else {
        ArrayList<ExpressionNode> literal = new ArrayList<>();
        for (EvalResult result : results) {
            literal.add(result.getNode());
        }
        return EvalResult.nonConstant(new ArrayLiteral(literal));
    }
}
Also used : ExpressionNode(org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode) ArrayList(java.util.ArrayList) ArrayLiteral(org.apache.sling.scripting.sightly.compiler.expression.nodes.ArrayLiteral)

Example 8 with ExpressionNode

use of org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode 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 9 with ExpressionNode

use of org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode 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)

Example 10 with ExpressionNode

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

the class ExpressionTranslator method visit.

@Override
public void visit(RuntimeCall runtimeCall) {
    source.startMethodCall(SourceGenConstants.RENDER_CONTEXT_INSTANCE, SourceGenConstants.RUNTIME_CALL_METHOD).stringLiteral(runtimeCall.getFunctionName());
    for (ExpressionNode arg : runtimeCall.getArguments()) {
        source.separateArgument();
        visit(arg);
    }
    source.endCall();
}
Also used : ExpressionNode(org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode)

Aggregations

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