Search in sources :

Example 6 with MapLiteral

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

the class ResourcePlugin method invoke.

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

        private Map<String, ExpressionNode> expressionOptions = new HashMap<>(expression.getOptions());

        @Override
        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))));
            stream.write(new OutputVariable(resourceVar));
            stream.write(VariableBinding.END);
            Patterns.beginStreamIgnore(stream);
        }

        @Override
        public void afterChildren(PushStream stream) {
            Patterns.endStreamIgnore(stream);
        }
    };
}
Also used : MapLiteral(org.apache.sling.scripting.sightly.compiler.expression.nodes.MapLiteral) PushStream(org.apache.sling.scripting.sightly.impl.compiler.PushStream) RuntimeCall(org.apache.sling.scripting.sightly.compiler.expression.nodes.RuntimeCall) Map(java.util.Map) HashMap(java.util.HashMap) VariableBinding(org.apache.sling.scripting.sightly.compiler.commands.VariableBinding) OutputVariable(org.apache.sling.scripting.sightly.compiler.commands.OutputVariable)

Example 7 with MapLiteral

use of org.apache.sling.scripting.sightly.compiler.expression.nodes.MapLiteral 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 8 with MapLiteral

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

the class CallPlugin method invoke.

@Override
public PluginInvoke invoke(final Expression expression, final PluginCallInfo callInfo, final CompilerContext compilerContext) {
    if (callInfo.getArguments().length > 0) {
        throw new SightlyCompilerException("Call plugin should have no arguments.", "data-sly-call." + callInfo.getArguments()[0]);
    }
    return new DefaultPluginInvoke() {

        @Override
        public void beforeChildren(PushStream stream) {
            String templateVar = compilerContext.generateVariable("templateVar");
            String argsVar = compilerContext.generateVariable("templateOptions");
            MapLiteral args = new MapLiteral(expression.getOptions());
            stream.write(new VariableBinding.Start(templateVar, expression.getRoot()));
            stream.write(new VariableBinding.Start(argsVar, args));
            stream.write(new Procedure.Call(templateVar, argsVar));
            stream.write(VariableBinding.END);
            stream.write(VariableBinding.END);
            //ignoring everything else
            Patterns.beginStreamIgnore(stream);
        }

        @Override
        public void afterChildren(PushStream stream) {
            Patterns.endStreamIgnore(stream);
        }
    };
}
Also used : MapLiteral(org.apache.sling.scripting.sightly.compiler.expression.nodes.MapLiteral) SightlyCompilerException(org.apache.sling.scripting.sightly.compiler.SightlyCompilerException) PushStream(org.apache.sling.scripting.sightly.impl.compiler.PushStream) Procedure(org.apache.sling.scripting.sightly.compiler.commands.Procedure) VariableBinding(org.apache.sling.scripting.sightly.compiler.commands.VariableBinding)

Example 9 with MapLiteral

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

the class IncludePlugin method invoke.

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

        @Override
        public void beforeChildren(PushStream stream) {
            String includedContentVar = compilerContext.generateVariable("includedResult");
            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()))));
            stream.write(new OutputVariable(includedContentVar));
            //end includedContentVar
            stream.write(VariableBinding.END);
            //end pathVar
            stream.write(VariableBinding.END);
            Patterns.beginStreamIgnore(stream);
        }

        @Override
        public void afterChildren(PushStream stream) {
            Patterns.endStreamIgnore(stream);
        }
    };
}
Also used : MapLiteral(org.apache.sling.scripting.sightly.compiler.expression.nodes.MapLiteral) Identifier(org.apache.sling.scripting.sightly.compiler.expression.nodes.Identifier) PushStream(org.apache.sling.scripting.sightly.impl.compiler.PushStream) RuntimeCall(org.apache.sling.scripting.sightly.compiler.expression.nodes.RuntimeCall) VariableBinding(org.apache.sling.scripting.sightly.compiler.commands.VariableBinding) OutputVariable(org.apache.sling.scripting.sightly.compiler.commands.OutputVariable)

Example 10 with MapLiteral

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

Aggregations

MapLiteral (org.apache.sling.scripting.sightly.compiler.expression.nodes.MapLiteral)12 VariableBinding (org.apache.sling.scripting.sightly.compiler.commands.VariableBinding)8 HashMap (java.util.HashMap)7 ExpressionNode (org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode)7 PushStream (org.apache.sling.scripting.sightly.impl.compiler.PushStream)6 RuntimeCall (org.apache.sling.scripting.sightly.compiler.expression.nodes.RuntimeCall)5 Map (java.util.Map)4 Identifier (org.apache.sling.scripting.sightly.compiler.expression.nodes.Identifier)4 NumericConstant (org.apache.sling.scripting.sightly.compiler.expression.nodes.NumericConstant)3 Conditional (org.apache.sling.scripting.sightly.compiler.commands.Conditional)2 Loop (org.apache.sling.scripting.sightly.compiler.commands.Loop)2 OutputVariable (org.apache.sling.scripting.sightly.compiler.commands.OutputVariable)2 BinaryOperation (org.apache.sling.scripting.sightly.compiler.expression.nodes.BinaryOperation)2 UnaryOperation (org.apache.sling.scripting.sightly.compiler.expression.nodes.UnaryOperation)2 ArrayList (java.util.ArrayList)1 SightlyCompilerException (org.apache.sling.scripting.sightly.compiler.SightlyCompilerException)1 Procedure (org.apache.sling.scripting.sightly.compiler.commands.Procedure)1 Expression (org.apache.sling.scripting.sightly.compiler.expression.Expression)1 ArrayLiteral (org.apache.sling.scripting.sightly.compiler.expression.nodes.ArrayLiteral)1 StringConstant (org.apache.sling.scripting.sightly.compiler.expression.nodes.StringConstant)1