use of org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode 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);
}
use of org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode 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());
}
use of org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode in project sling by apache.
the class DeadCodeRemoval method visit.
@Override
public void visit(VariableBinding.Start variableBindingStart) {
Boolean truthValue = null;
ExpressionNode node = variableBindingStart.getExpression();
if (node instanceof StringConstant) {
truthValue = CompileTimeObjectModel.toBoolean(((StringConstant) node).getText());
}
if (node instanceof BooleanConstant) {
truthValue = ((BooleanConstant) node).getValue();
}
if (node instanceof NumericConstant) {
truthValue = CompileTimeObjectModel.toBoolean(((NumericConstant) node).getValue());
}
if (node instanceof NullLiteral) {
truthValue = CompileTimeObjectModel.toBoolean(null);
}
tracker.pushVariable(variableBindingStart.getVariableName(), truthValue);
outStream.write(variableBindingStart);
}
use of org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode in project sling by apache.
the class RepeatPlugin 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));
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 afterTagClose(PushStream stream, boolean isSelfClosing) {
stream.write(NEW_LINE);
}
@Override
public void afterElement(PushStream stream) {
stream.write(VariableBinding.END);
stream.write(Loop.END);
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);
}
};
}
use of org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode 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());
}
Aggregations