use of org.apache.sling.scripting.sightly.impl.compiler.PushStream 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);
}
};
}
use of org.apache.sling.scripting.sightly.impl.compiler.PushStream 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);
}
};
}
use of org.apache.sling.scripting.sightly.impl.compiler.PushStream 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);
}
};
}
use of org.apache.sling.scripting.sightly.impl.compiler.PushStream 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);
}
};
}
use of org.apache.sling.scripting.sightly.impl.compiler.PushStream in project sling by apache.
the class Streams method map.
/**
* Attach the emitting visitor to the inStream.
*
* @param inStream - the input stream
* @param emitterVisitor - the emitter visitor
* @return - the output stream of the emitter
*/
public static CommandStream map(CommandStream inStream, EmitterVisitor emitterVisitor) {
PushStream outStream = emitterVisitor.getOutputStream();
connect(inStream, outStream, emitterVisitor);
return outStream;
}
Aggregations