Search in sources :

Example 1 with RuntimeCall

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

the class I18nFilter method apply.

@Override
public Expression apply(Expression expression, ExpressionContext expressionContext) {
    if (!expression.containsOption(I18N_OPTION) || expressionContext == ExpressionContext.PLUGIN_DATA_SLY_USE || expressionContext == ExpressionContext.PLUGIN_DATA_SLY_TEMPLATE || expressionContext == ExpressionContext.PLUGIN_DATA_SLY_CALL) {
        return expression;
    }
    Map<String, ExpressionNode> options = getFilterOptions(expression, HINT_OPTION, LOCALE_OPTION, BASENAME_OPTION);
    ExpressionNode translation = new RuntimeCall(RuntimeFunction.I18N, expression.getRoot(), new MapLiteral(options));
    expression.removeOption(I18N_OPTION);
    expression.getOptions().put(FormatFilter.FORMAT_LOCALE_OPTION, options.get(LOCALE_OPTION));
    return expression.withNode(translation);
}
Also used : MapLiteral(org.apache.sling.scripting.sightly.compiler.expression.nodes.MapLiteral) ExpressionNode(org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode) RuntimeCall(org.apache.sling.scripting.sightly.compiler.expression.nodes.RuntimeCall)

Example 2 with RuntimeCall

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

the class UsePlugin method invoke.

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

        @Override
        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()))));
        }

        private String decodeVariableName() {
            String[] arguments = callInfo.getArguments();
            if (arguments.length > 0) {
                return arguments[0];
            }
            return DEFAULT_VARIABLE_NAME;
        }
    };
}
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) VariableBinding(org.apache.sling.scripting.sightly.compiler.commands.VariableBinding)

Example 3 with RuntimeCall

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

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

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

Aggregations

RuntimeCall (org.apache.sling.scripting.sightly.compiler.expression.nodes.RuntimeCall)11 ExpressionNode (org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode)7 HashMap (java.util.HashMap)5 Expression (org.apache.sling.scripting.sightly.compiler.expression.Expression)5 MapLiteral (org.apache.sling.scripting.sightly.compiler.expression.nodes.MapLiteral)5 StringConstant (org.apache.sling.scripting.sightly.compiler.expression.nodes.StringConstant)5 ArrayList (java.util.ArrayList)4 VariableBinding (org.apache.sling.scripting.sightly.compiler.commands.VariableBinding)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 OutputVariable (org.apache.sling.scripting.sightly.compiler.commands.OutputVariable)3 ArrayLiteral (org.apache.sling.scripting.sightly.compiler.expression.nodes.ArrayLiteral)3 PushStream (org.apache.sling.scripting.sightly.impl.compiler.PushStream)3 Identifier (org.apache.sling.scripting.sightly.compiler.expression.nodes.Identifier)2 NumericConstant (org.apache.sling.scripting.sightly.compiler.expression.nodes.NumericConstant)2 Map (java.util.Map)1 Conditional (org.apache.sling.scripting.sightly.compiler.commands.Conditional)1 MarkupContext (org.apache.sling.scripting.sightly.compiler.expression.MarkupContext)1 BinaryOperation (org.apache.sling.scripting.sightly.compiler.expression.nodes.BinaryOperation)1