Search in sources :

Example 11 with ExpressionNode

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

the class ElementPlugin method invoke.

@Override
public PluginInvoke invoke(final Expression expression, final PluginCallInfo callInfo, final CompilerContext compilerContext) {
    return new DefaultPluginInvoke() {

        private final ExpressionNode node = adjustContext(compilerContext, expression, MarkupContext.ELEMENT_NAME, ExpressionContext.ELEMENT).getRoot();

        private String tagVar = compilerContext.generateVariable("tagVar");

        @Override
        public void beforeElement(PushStream stream, String tagName) {
            stream.write(new VariableBinding.Start(tagVar, node));
        }

        @Override
        public void beforeTagOpen(PushStream stream) {
            stream.write(new Conditional.Start(tagVar, true));
            stream.write(new OutText("<"));
            stream.write(new OutputVariable(tagVar));
            stream.write(Conditional.END);
            stream.write(new Conditional.Start(tagVar, false));
        }

        @Override
        public void beforeAttributes(PushStream stream) {
            stream.write(Conditional.END);
        }

        @Override
        public void beforeTagClose(PushStream stream, boolean isSelfClosing) {
            if (!isSelfClosing) {
                stream.write(new Conditional.Start(tagVar, true));
                stream.write(new OutText("</"));
                stream.write(new OutputVariable(tagVar));
                stream.write(new OutText(">"));
                stream.write(Conditional.END);
            }
            stream.write(new Conditional.Start(tagVar, false));
        }

        @Override
        public void afterTagClose(PushStream stream, boolean isSelfClosing) {
            stream.write(Conditional.END);
        }

        @Override
        public void afterElement(PushStream stream) {
            stream.write(VariableBinding.END);
        }
    };
}
Also used : ExpressionNode(org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode) OutText(org.apache.sling.scripting.sightly.compiler.commands.OutText) PushStream(org.apache.sling.scripting.sightly.impl.compiler.PushStream) Conditional(org.apache.sling.scripting.sightly.compiler.commands.Conditional) VariableBinding(org.apache.sling.scripting.sightly.compiler.commands.VariableBinding) OutputVariable(org.apache.sling.scripting.sightly.compiler.commands.OutputVariable)

Example 12 with ExpressionNode

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

the class ListPlugin method invoke.

@Override
public PluginInvoke invoke(final Expression expression, final PluginCallInfo callInfo, final CompilerContext compilerContext) {
    return new DefaultPluginInvoke() {

        private String listVariable = compilerContext.generateVariable("collectionVar");

        private String collectionSizeVar = compilerContext.generateVariable("size");

        @Override
        public void beforeElement(PushStream stream, String tagName) {
            stream.write(new VariableBinding.Start(listVariable, expression.getRoot()));
            stream.write(new VariableBinding.Start(collectionSizeVar, new UnaryOperation(UnaryOperator.LENGTH, new Identifier(listVariable))));
            stream.write(new Conditional.Start(collectionSizeVar, true));
        }

        @Override
        public void beforeChildren(PushStream stream) {
            String itemVariable = decodeItemVariable();
            String loopStatusVar = Syntax.itemLoopStatusVariable(itemVariable);
            String indexVariable = compilerContext.generateVariable("index");
            stream.write(new Loop.Start(listVariable, itemVariable, indexVariable));
            stream.write(new VariableBinding.Start(loopStatusVar, buildStatusObj(indexVariable, collectionSizeVar)));
        }

        @Override
        public void afterChildren(PushStream stream) {
            stream.write(VariableBinding.END);
            stream.write(Loop.END);
        }

        @Override
        public void afterElement(PushStream stream) {
            stream.write(Conditional.END);
            stream.write(VariableBinding.END);
            stream.write(VariableBinding.END);
        }

        private String decodeItemVariable() {
            String[] args = callInfo.getArguments();
            if (args.length > 0) {
                return args[0];
            }
            return Syntax.DEFAULT_LIST_ITEM_VAR_NAME;
        }

        private MapLiteral buildStatusObj(String indexVar, String sizeVar) {
            HashMap<String, ExpressionNode> obj = new HashMap<>();
            Identifier indexId = new Identifier(indexVar);
            BinaryOperation firstExpr = new BinaryOperation(BinaryOperator.EQ, indexId, NumericConstant.ZERO);
            BinaryOperation lastExpr = new BinaryOperation(BinaryOperator.EQ, indexId, new BinaryOperation(BinaryOperator.SUB, new Identifier(sizeVar), NumericConstant.ONE));
            obj.put(INDEX, indexId);
            obj.put(COUNT, new BinaryOperation(BinaryOperator.ADD, indexId, NumericConstant.ONE));
            obj.put(FIRST, firstExpr);
            obj.put(MIDDLE, new UnaryOperation(UnaryOperator.NOT, new BinaryOperation(BinaryOperator.OR, firstExpr, lastExpr)));
            obj.put(LAST, lastExpr);
            obj.put(ODD, parityCheck(indexId, NumericConstant.ZERO));
            obj.put(EVEN, parityCheck(indexId, NumericConstant.ONE));
            return new MapLiteral(obj);
        }

        private ExpressionNode parityCheck(ExpressionNode numericExpression, NumericConstant expected) {
            return new BinaryOperation(BinaryOperator.EQ, new BinaryOperation(BinaryOperator.REM, numericExpression, NumericConstant.TWO), expected);
        }
    };
}
Also used : Loop(org.apache.sling.scripting.sightly.compiler.commands.Loop) UnaryOperation(org.apache.sling.scripting.sightly.compiler.expression.nodes.UnaryOperation) HashMap(java.util.HashMap) BinaryOperation(org.apache.sling.scripting.sightly.compiler.expression.nodes.BinaryOperation) Conditional(org.apache.sling.scripting.sightly.compiler.commands.Conditional) MapLiteral(org.apache.sling.scripting.sightly.compiler.expression.nodes.MapLiteral) Identifier(org.apache.sling.scripting.sightly.compiler.expression.nodes.Identifier) ExpressionNode(org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode) NumericConstant(org.apache.sling.scripting.sightly.compiler.expression.nodes.NumericConstant) PushStream(org.apache.sling.scripting.sightly.impl.compiler.PushStream) VariableBinding(org.apache.sling.scripting.sightly.compiler.commands.VariableBinding)

Example 13 with ExpressionNode

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

the class ExpressionWrapperTest method testFormatOptionsRemoval.

@Test
public void testFormatOptionsRemoval() {
    Interpolation interpolation = new Interpolation();
    Map<String, ExpressionNode> options = new HashMap<>();
    List<ExpressionNode> formatArray = new ArrayList<>();
    formatArray.add(new StringConstant("John"));
    formatArray.add(new StringConstant("Doe"));
    options.put(FormatFilter.FORMAT_OPTION, new ArrayLiteral(formatArray));
    interpolation.addExpression(new Expression(new StringConstant("Hello {0} {1}"), options));
    ExpressionWrapper wrapper = new ExpressionWrapper(filters);
    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());
}
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) ArrayList(java.util.ArrayList) RuntimeCall(org.apache.sling.scripting.sightly.compiler.expression.nodes.RuntimeCall) StringConstant(org.apache.sling.scripting.sightly.compiler.expression.nodes.StringConstant) ArrayLiteral(org.apache.sling.scripting.sightly.compiler.expression.nodes.ArrayLiteral) ExpressionWrapper(org.apache.sling.scripting.sightly.impl.compiler.frontend.ExpressionWrapper) Test(org.junit.Test)

Example 14 with ExpressionNode

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

the class SyntheticMapRemoval method overrideMap.

private MapLiteral overrideMap(String variableName, MapLiteral mapLiteral) {
    Map<String, ExpressionNode> newLiteral = new HashMap<>();
    for (Map.Entry<String, ExpressionNode> entry : mapLiteral.getMap().entrySet()) {
        String property = entry.getKey();
        ExpressionNode valueNode = entry.getValue();
        String valueVariable = valueVariableName(variableName, property);
        newLiteral.put(property, new Identifier(valueVariable));
        outputStream.write(new VariableBinding.Start(valueVariable, valueNode));
    }
    return new MapLiteral(newLiteral);
}
Also used : MapLiteral(org.apache.sling.scripting.sightly.compiler.expression.nodes.MapLiteral) Identifier(org.apache.sling.scripting.sightly.compiler.expression.nodes.Identifier) HashMap(java.util.HashMap) ExpressionNode(org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode) HashMap(java.util.HashMap) Map(java.util.Map) VariableBinding(org.apache.sling.scripting.sightly.compiler.commands.VariableBinding)

Example 15 with ExpressionNode

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

the class ConstantFolding method visit.

@Override
public void visit(VariableBinding.Start variableBindingStart) {
    String variable = variableBindingStart.getVariableName();
    ExpressionNode node = variableBindingStart.getExpression();
    EvalResult result = ExpressionReducer.reduce(node, tracker);
    result = avoidFoldingDataStructures(result);
    tracker.pushVariable(variable, result);
    outStream.write(new VariableBinding.Start(variable, result.getNode()));
}
Also used : ExpressionNode(org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode) VariableBinding(org.apache.sling.scripting.sightly.compiler.commands.VariableBinding)

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