You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2014/11/14 15:05:03 UTC
svn commit: r1639641 [8/15] - in /sling/trunk/contrib/scripting/sightly: ./
engine/ engine/src/ engine/src/main/ engine/src/main/antlr4/
engine/src/main/antlr4/org/ engine/src/main/antlr4/org/apache/
engine/src/main/antlr4/org/apache/sling/ engine/src/...
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/util/expression/NodeTransformer.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/util/expression/NodeTransformer.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/util/expression/NodeTransformer.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/util/expression/NodeTransformer.java Fri Nov 14 14:04:56 2014
@@ -0,0 +1,129 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+
+package org.apache.sling.scripting.sightly.compiler.util.expression;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.sling.scripting.sightly.compiler.api.expression.ExpressionNode;
+import org.apache.sling.scripting.sightly.compiler.api.expression.NodeVisitor;
+import org.apache.sling.scripting.sightly.compiler.api.expression.node.ArrayLiteral;
+import org.apache.sling.scripting.sightly.compiler.api.expression.node.BinaryOperation;
+import org.apache.sling.scripting.sightly.compiler.api.expression.node.BooleanConstant;
+import org.apache.sling.scripting.sightly.compiler.api.expression.node.Identifier;
+import org.apache.sling.scripting.sightly.compiler.api.expression.node.MapLiteral;
+import org.apache.sling.scripting.sightly.compiler.api.expression.node.NullLiteral;
+import org.apache.sling.scripting.sightly.compiler.api.expression.node.NumericConstant;
+import org.apache.sling.scripting.sightly.compiler.api.expression.node.PropertyAccess;
+import org.apache.sling.scripting.sightly.compiler.api.expression.node.RuntimeCall;
+import org.apache.sling.scripting.sightly.compiler.api.expression.node.StringConstant;
+import org.apache.sling.scripting.sightly.compiler.api.expression.node.TernaryOperator;
+import org.apache.sling.scripting.sightly.compiler.api.expression.node.UnaryOperation;
+import org.apache.sling.scripting.sightly.compiler.api.expression.node.UnaryOperation;
+
+/**
+ * Created by mdanila on 2/2/14.
+ */
+public class NodeTransformer implements NodeVisitor<ExpressionNode> {
+
+ public final ExpressionNode transform(ExpressionNode node) {
+ return node.accept(this);
+ }
+
+ @Override
+ public ExpressionNode evaluate(PropertyAccess propertyAccess) {
+ return new PropertyAccess(transform(propertyAccess.getTarget()), transform(propertyAccess.getProperty()));
+ }
+
+ @Override
+ public ExpressionNode evaluate(Identifier identifier) {
+ return identifier;
+ }
+
+ @Override
+ public ExpressionNode evaluate(StringConstant text) {
+ return text;
+ }
+
+ @Override
+ public ExpressionNode evaluate(BinaryOperation binaryOperation) {
+ return new BinaryOperation(binaryOperation.getOperator(),
+ transform(binaryOperation.getLeftOperand()),
+ transform(binaryOperation.getRightOperand()));
+ }
+
+ @Override
+ public ExpressionNode evaluate(BooleanConstant booleanConstant) {
+ return booleanConstant;
+ }
+
+ @Override
+ public ExpressionNode evaluate(NumericConstant numericConstant) {
+ return numericConstant;
+ }
+
+ @Override
+ public ExpressionNode evaluate(UnaryOperation unaryOperation) {
+ return new UnaryOperation(unaryOperation.getOperator(), transform(unaryOperation.getTarget()));
+ }
+
+ @Override
+ public ExpressionNode evaluate(TernaryOperator ternaryOperator) {
+ return new TernaryOperator(
+ transform(ternaryOperator.getCondition()),
+ transform(ternaryOperator.getThenBranch()),
+ transform(ternaryOperator.getElseBranch()));
+ }
+
+ @Override
+ public ExpressionNode evaluate(RuntimeCall runtimeCall) {
+ List<ExpressionNode> children = transformList(runtimeCall.getArguments());
+ return new RuntimeCall(runtimeCall.getFunctionName(), children);
+ }
+
+ @Override
+ public ExpressionNode evaluate(MapLiteral mapLiteral) {
+ HashMap<String, ExpressionNode> map = new HashMap<String, ExpressionNode>();
+ for (Map.Entry<String, ExpressionNode> entry : mapLiteral.getMap().entrySet()) {
+ map.put(entry.getKey(), transform(entry.getValue()));
+ }
+ return new MapLiteral(map);
+ }
+
+ @Override
+ public ExpressionNode evaluate(ArrayLiteral arrayLiteral) {
+ return new ArrayLiteral(transformList(arrayLiteral.getItems()));
+ }
+
+ @Override
+ public ExpressionNode evaluate(NullLiteral nullLiteral) {
+ return nullLiteral;
+ }
+
+ private List<ExpressionNode> transformList(List<ExpressionNode> nodes) {
+ ArrayList<ExpressionNode> result = new ArrayList<ExpressionNode>();
+ for (ExpressionNode node : nodes) {
+ result.add(transform(node));
+ }
+ return result;
+ }
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/util/expression/SideEffectVisitor.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/util/expression/SideEffectVisitor.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/util/expression/SideEffectVisitor.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/util/expression/SideEffectVisitor.java Fri Nov 14 14:04:56 2014
@@ -0,0 +1,138 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+package org.apache.sling.scripting.sightly.compiler.util.expression;
+
+import org.apache.sling.scripting.sightly.compiler.api.expression.NodeVisitor;
+import org.apache.sling.scripting.sightly.compiler.api.expression.node.ArrayLiteral;
+import org.apache.sling.scripting.sightly.compiler.api.expression.node.BinaryOperation;
+import org.apache.sling.scripting.sightly.compiler.api.expression.node.BooleanConstant;
+import org.apache.sling.scripting.sightly.compiler.api.expression.node.Identifier;
+import org.apache.sling.scripting.sightly.compiler.api.expression.node.MapLiteral;
+import org.apache.sling.scripting.sightly.compiler.api.expression.node.NullLiteral;
+import org.apache.sling.scripting.sightly.compiler.api.expression.node.NumericConstant;
+import org.apache.sling.scripting.sightly.compiler.api.expression.node.PropertyAccess;
+import org.apache.sling.scripting.sightly.compiler.api.expression.node.RuntimeCall;
+import org.apache.sling.scripting.sightly.compiler.api.expression.node.StringConstant;
+import org.apache.sling.scripting.sightly.compiler.api.expression.node.TernaryOperator;
+import org.apache.sling.scripting.sightly.compiler.api.expression.node.UnaryOperation;
+import org.apache.sling.scripting.sightly.compiler.api.expression.node.*;
+
+/**
+ * Expression visitor which does not return anything for a node. Instead
+ * it performs specific side-effects depending on the expression node type.
+ */
+public abstract class SideEffectVisitor implements NodeVisitor<Object> {
+
+ public abstract void visit(PropertyAccess propertyAccess);
+
+ public abstract void visit(Identifier identifier);
+
+ public abstract void visit(StringConstant text);
+
+ public abstract void visit(BinaryOperation binaryOperation);
+
+ public abstract void visit(BooleanConstant booleanConstant);
+
+ public abstract void visit(NumericConstant numericConstant);
+
+ public abstract void visit(UnaryOperation unaryOperation);
+
+ public abstract void visit(TernaryOperator ternaryOperator);
+
+ public abstract void visit(RuntimeCall runtimeCall);
+
+ public abstract void visit(MapLiteral mapLiteral);
+
+ public abstract void visit(ArrayLiteral arrayLiteral);
+
+ public abstract void visit(NullLiteral nullLiteral);
+
+ @Override
+ public Object evaluate(PropertyAccess propertyAccess) {
+ visit(propertyAccess);
+ return null;
+ }
+
+ @Override
+ public Object evaluate(Identifier identifier) {
+ visit(identifier);
+ return null;
+ }
+
+ @Override
+ public Object evaluate(StringConstant text) {
+ visit(text);
+ return null;
+ }
+
+ @Override
+ public Object evaluate(BinaryOperation binaryOperation) {
+ visit(binaryOperation);
+ return null;
+ }
+
+ @Override
+ public Object evaluate(BooleanConstant booleanConstant) {
+ visit(booleanConstant);
+ return null;
+ }
+
+ @Override
+ public Object evaluate(NumericConstant numericConstant) {
+ visit(numericConstant);
+ return null;
+ }
+
+ @Override
+ public Object evaluate(UnaryOperation unaryOperation) {
+ visit(unaryOperation);
+ return null;
+ }
+
+ @Override
+ public Object evaluate(TernaryOperator ternaryOperator) {
+ visit(ternaryOperator);
+ return null;
+ }
+
+ @Override
+ public Object evaluate(RuntimeCall runtimeCall) {
+ visit(runtimeCall);
+ return null;
+ }
+
+ @Override
+ public Object evaluate(MapLiteral mapLiteral) {
+ visit(mapLiteral);
+ return null;
+ }
+
+ @Override
+ public Object evaluate(ArrayLiteral arrayLiteral) {
+ visit(arrayLiteral);
+ return null;
+ }
+
+ @Override
+ public Object evaluate(NullLiteral nullLiteral) {
+ visit(nullLiteral);
+ return null;
+ }
+}
+
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/util/stream/BroadcastHandler.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/util/stream/BroadcastHandler.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/util/stream/BroadcastHandler.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/util/stream/BroadcastHandler.java Fri Nov 14 14:04:56 2014
@@ -0,0 +1,64 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+package org.apache.sling.scripting.sightly.compiler.util.stream;
+
+import org.apache.sling.scripting.sightly.compiler.api.ris.Command;
+import org.apache.sling.scripting.sightly.compiler.api.ris.CommandHandler;
+import org.apache.sling.scripting.sightly.compiler.api.ris.Command;
+import org.apache.sling.scripting.sightly.compiler.api.ris.CommandHandler;
+
+import java.util.ArrayList;
+
+/**
+ * Broadcasts commands to other handlers
+ */
+public class BroadcastHandler implements CommandHandler {
+
+ private ArrayList<CommandHandler> handlers = new ArrayList<CommandHandler>();
+
+ /**
+ * Add a new child handler
+ * @param handler - the listening handler
+ */
+ public void addHandler(CommandHandler handler) {
+ handlers.add(handler);
+ }
+
+
+ @Override
+ public void onEmit(Command command) {
+ for (CommandHandler handler : handlers) {
+ handler.onEmit(command);
+ }
+ }
+
+ @Override
+ public void onError(String errorMessage) {
+ for (CommandHandler handler : handlers) {
+ handler.onError(errorMessage);
+ }
+ }
+
+ @Override
+ public void onDone() {
+ for (CommandHandler handler : handlers) {
+ handler.onDone();
+ }
+ }
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/util/stream/EmitterVisitor.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/util/stream/EmitterVisitor.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/util/stream/EmitterVisitor.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/util/stream/EmitterVisitor.java Fri Nov 14 14:04:56 2014
@@ -0,0 +1,36 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+
+package org.apache.sling.scripting.sightly.compiler.util.stream;
+
+import org.apache.sling.scripting.sightly.compiler.api.ris.CommandVisitor;
+import org.apache.sling.scripting.sightly.compiler.api.ris.CommandVisitor;
+
+/**
+ * Command visitor which writes to an output stream
+ */
+public interface EmitterVisitor extends CommandVisitor {
+
+ /**
+ * Get the output stream
+ * @return - the output stream of this visitor
+ */
+ PushStream getOutputStream();
+
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/util/stream/PushStream.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/util/stream/PushStream.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/util/stream/PushStream.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/util/stream/PushStream.java Fri Nov 14 14:04:56 2014
@@ -0,0 +1,80 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+package org.apache.sling.scripting.sightly.compiler.util.stream;
+
+import org.apache.sling.scripting.sightly.compiler.api.ris.Command;
+import org.apache.sling.scripting.sightly.compiler.api.ris.CommandHandler;
+import org.apache.sling.scripting.sightly.compiler.api.ris.CommandStream;
+import org.apache.sling.scripting.sightly.compiler.api.ris.Command;
+import org.apache.sling.scripting.sightly.compiler.api.ris.CommandHandler;
+
+/**
+ * A stream that can be written into
+ */
+public class PushStream implements CommandStream {
+
+ private BroadcastHandler handler = new BroadcastHandler();
+ private boolean closed;
+
+ /**
+ * Add a command handler
+ * @param handler - a command handler
+ */
+ @Override
+ public void addHandler(CommandHandler handler) {
+ this.handler.addHandler(handler);
+ }
+
+ /**
+ * Emit the specified command
+ * @param command - the emitted command
+ * @throws UnsupportedOperationException - if the stream is closed
+ */
+ public void emit(Command command) {
+ if (closed) {
+ throw new UnsupportedOperationException("Stream is closed");
+ }
+ this.handler.onEmit(command);
+ }
+
+ /**
+ * Signal an error
+ * @param message - the error message
+ * @throws UnsupportedOperationException - if the stream is already closed
+ */
+ public void signalError(String message) {
+ if (closed) {
+ throw new UnsupportedOperationException("Stream is closed");
+ }
+ closed = true;
+ this.handler.onError(message);
+ }
+
+ /**
+ * Close the stream
+ * @throws UnsupportedOperationException - if the stream is already closed
+ */
+ public void signalDone() {
+ if (closed) {
+ throw new UnsupportedOperationException("Stream is already closed");
+ }
+ closed = true;
+ this.handler.onDone();
+ }
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/util/stream/ReplayStream.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/util/stream/ReplayStream.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/util/stream/ReplayStream.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/util/stream/ReplayStream.java Fri Nov 14 14:04:56 2014
@@ -0,0 +1,47 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+package org.apache.sling.scripting.sightly.compiler.util.stream;
+
+import java.util.List;
+
+import org.apache.sling.scripting.sightly.compiler.api.ris.Command;
+import org.apache.sling.scripting.sightly.compiler.api.ris.CommandHandler;
+import org.apache.sling.scripting.sightly.compiler.api.ris.CommandStream;
+import org.apache.sling.scripting.sightly.compiler.api.ris.Command;
+import org.apache.sling.scripting.sightly.compiler.api.ris.CommandHandler;
+
+/**
+ * A stream which replays the same commands for all handlers
+ */
+public class ReplayStream implements CommandStream {
+
+ private final List<Command> commands;
+
+ public ReplayStream(List<Command> commands) {
+ this.commands = commands;
+ }
+
+ @Override
+ public void addHandler(CommandHandler handler) {
+ for (Command command : commands) {
+ handler.onEmit(command);
+ }
+ handler.onDone();
+ }
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/util/stream/Streams.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/util/stream/Streams.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/util/stream/Streams.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/util/stream/Streams.java Fri Nov 14 14:04:56 2014
@@ -0,0 +1,59 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+
+package org.apache.sling.scripting.sightly.compiler.util.stream;
+
+import org.apache.sling.scripting.sightly.compiler.api.ris.CommandStream;
+import org.apache.sling.scripting.sightly.compiler.api.ris.CommandVisitor;
+import org.apache.sling.scripting.sightly.compiler.api.ris.CommandVisitor;
+
+/**
+ * Utility functions for streams
+ */
+public class Streams {
+
+ /**
+ * Attach the visitor as a handle to the inStream and propagate
+ * the done signal from the inStream to the outStream
+ * @param inStream - the input stream
+ * @param outStream - the output stream
+ * @param visitor - a command visitor
+ */
+ public static void connect(CommandStream inStream, final PushStream outStream, CommandVisitor visitor) {
+ inStream.addHandler(new VisitorHandler(visitor) {
+ @Override
+ public void onDone() {
+ outStream.signalDone();
+ }
+ });
+ }
+
+ /**
+ * 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;
+ }
+
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/util/stream/VisitorHandler.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/util/stream/VisitorHandler.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/util/stream/VisitorHandler.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/util/stream/VisitorHandler.java Fri Nov 14 14:04:56 2014
@@ -0,0 +1,53 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+package org.apache.sling.scripting.sightly.compiler.util.stream;
+
+import org.apache.sling.scripting.sightly.compiler.api.ris.Command;
+import org.apache.sling.scripting.sightly.compiler.api.ris.CommandHandler;
+import org.apache.sling.scripting.sightly.compiler.api.ris.CommandVisitor;
+import org.apache.sling.scripting.sightly.compiler.api.ris.Command;
+import org.apache.sling.scripting.sightly.compiler.api.ris.CommandHandler;
+import org.apache.sling.scripting.sightly.compiler.api.ris.CommandVisitor;
+
+/**
+ * Delegates commands to a visitor
+ */
+public class VisitorHandler implements CommandHandler {
+
+ private CommandVisitor visitor;
+
+ public VisitorHandler(CommandVisitor visitor) {
+ this.visitor = visitor;
+ }
+
+ @Override
+ public void onEmit(Command command) {
+ command.accept(visitor);
+ }
+
+ @Override
+ public void onError(String errorMessage) {
+ throw new RuntimeException(errorMessage);
+ }
+
+ @Override
+ public void onDone() {
+
+ }
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/visitor/IgnoreRange.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/visitor/IgnoreRange.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/visitor/IgnoreRange.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/visitor/IgnoreRange.java Fri Nov 14 14:04:56 2014
@@ -0,0 +1,53 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+package org.apache.sling.scripting.sightly.compiler.visitor;
+
+import org.apache.sling.scripting.sightly.compiler.api.ris.Command;
+import org.apache.sling.scripting.sightly.compiler.api.ris.Command;
+
+/**
+ * Ignore a nested range of commands
+ */
+public abstract class IgnoreRange extends UniformVisitor {
+
+ private final Class<? extends Command> rangeStart;
+ private final Class<? extends Command> rangeEnd;
+
+ private int skipCount = 1;
+
+ public IgnoreRange(Class<? extends Command> rangeStart, Class<? extends Command> rangeEnd) {
+ this.rangeStart = rangeStart;
+ this.rangeEnd = rangeEnd;
+ }
+
+ @Override
+ public void onCommand(Command command) {
+ Class<? extends Command> commandClass = command.getClass();
+ if (commandClass.equals(rangeStart)) {
+ skipCount++;
+ } else if (commandClass.equals(rangeEnd)) {
+ skipCount--;
+ }
+ if (skipCount == 0) {
+ onCompleted();
+ }
+ }
+
+ protected abstract void onCompleted();
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/visitor/StateControl.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/visitor/StateControl.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/visitor/StateControl.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/visitor/StateControl.java Fri Nov 14 14:04:56 2014
@@ -0,0 +1,49 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+
+package org.apache.sling.scripting.sightly.compiler.visitor;
+
+import org.apache.sling.scripting.sightly.compiler.api.ris.CommandVisitor;
+import org.apache.sling.scripting.sightly.compiler.api.ris.CommandVisitor;
+
+/**
+ * Interface to control stateful visitors
+ */
+public interface StateControl {
+
+ /**
+ * Push a new visitor as the current state
+ * @param visitor - the new active visitor
+ */
+ void push(CommandVisitor visitor);
+
+ /**
+ * Pop the current visitor and set the previous visitor active
+ * @return - the previously active visitor
+ */
+ CommandVisitor pop();
+
+ /**
+ * Replace the current active visitor with another
+ * @param visitor the new active visitor
+ * @return the previously active visitor
+ */
+ CommandVisitor replace(CommandVisitor visitor);
+
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/visitor/StatefulRangeIgnore.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/visitor/StatefulRangeIgnore.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/visitor/StatefulRangeIgnore.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/visitor/StatefulRangeIgnore.java Fri Nov 14 14:04:56 2014
@@ -0,0 +1,42 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+
+package org.apache.sling.scripting.sightly.compiler.visitor;
+
+import org.apache.sling.scripting.sightly.compiler.api.ris.Command;
+import org.apache.sling.scripting.sightly.compiler.api.ris.Command;
+
+/**
+ * Variant of {@link IgnoreRange} to be used
+ * in stateful visitors.
+ */
+public class StatefulRangeIgnore extends IgnoreRange {
+
+ private final StateControl stateControl;
+
+ public StatefulRangeIgnore(StateControl stateControl, Class<? extends Command> rangeStart, Class<? extends Command> rangeEnd) {
+ super(rangeStart, rangeEnd);
+ this.stateControl = stateControl;
+ }
+
+ @Override
+ protected void onCompleted() {
+ stateControl.pop();
+ }
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/visitor/StatefulVisitor.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/visitor/StatefulVisitor.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/visitor/StatefulVisitor.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/visitor/StatefulVisitor.java Fri Nov 14 14:04:56 2014
@@ -0,0 +1,173 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+package org.apache.sling.scripting.sightly.compiler.visitor;
+
+import java.util.Stack;
+
+import org.apache.sling.scripting.sightly.compiler.api.ris.Command;
+import org.apache.sling.scripting.sightly.compiler.api.ris.CommandVisitor;
+import org.apache.sling.scripting.sightly.compiler.api.ris.command.BufferControl;
+import org.apache.sling.scripting.sightly.compiler.api.ris.command.Conditional;
+import org.apache.sling.scripting.sightly.compiler.api.ris.command.Loop;
+import org.apache.sling.scripting.sightly.compiler.api.ris.command.OutText;
+import org.apache.sling.scripting.sightly.compiler.api.ris.command.OutVariable;
+import org.apache.sling.scripting.sightly.compiler.api.ris.command.Procedure;
+import org.apache.sling.scripting.sightly.compiler.api.ris.command.VariableBinding;
+import org.apache.sling.scripting.sightly.compiler.api.ris.Command;
+import org.apache.sling.scripting.sightly.compiler.api.ris.CommandVisitor;
+import org.apache.sling.scripting.sightly.compiler.api.ris.command.OutText;
+
+/**
+ * Implements the state pattern for command visitors
+ */
+public class StatefulVisitor implements CommandVisitor {
+
+ private final Stack<CommandVisitor> stack = new Stack<CommandVisitor>();
+ private CommandVisitor visitor;
+ private Control control = new Control();
+
+ public StatefulVisitor() {
+ this.visitor = InvalidState.INSTANCE;
+ }
+
+ public StateControl getControl() {
+ return control;
+ }
+
+ public void initializeWith(CommandVisitor initialState) {
+ if (this.visitor != InvalidState.INSTANCE) {
+ throw new IllegalStateException("Initial state is already set");
+ }
+ this.visitor = initialState;
+ }
+
+ @Override
+ public void visit(Conditional.Start conditionalStart) {
+ visitor.visit(conditionalStart);
+ }
+
+ @Override
+ public void visit(Conditional.End conditionalEnd) {
+ visitor.visit(conditionalEnd);
+ }
+
+ @Override
+ public void visit(VariableBinding.Start variableBindingStart) {
+ visitor.visit(variableBindingStart);
+ }
+
+ @Override
+ public void visit(VariableBinding.End variableBindingEnd) {
+ visitor.visit(variableBindingEnd);
+ }
+
+ @Override
+ public void visit(VariableBinding.Global globalAssignment) {
+ visitor.visit(globalAssignment);
+ }
+
+ @Override
+ public void visit(OutVariable outVariable) {
+ visitor.visit(outVariable);
+ }
+
+ @Override
+ public void visit(OutText outText) {
+ visitor.visit(outText);
+ }
+
+ @Override
+ public void visit(Loop.Start loopStart) {
+ visitor.visit(loopStart);
+ }
+
+ @Override
+ public void visit(Loop.End loopEnd) {
+ visitor.visit(loopEnd);
+ }
+
+ @Override
+ public void visit(BufferControl.Push bufferPush) {
+ visitor.visit(bufferPush);
+ }
+
+ @Override
+ public void visit(BufferControl.Pop bufferPop) {
+ visitor.visit(bufferPop);
+ }
+
+ @Override
+ public void visit(Procedure.Start startProcedure) {
+ visitor.visit(startProcedure);
+ }
+
+ @Override
+ public void visit(Procedure.End endProcedure) {
+ visitor.visit(endProcedure);
+ }
+
+ @Override
+ public void visit(Procedure.Call procedureCall) {
+ visitor.visit(procedureCall);
+ }
+
+ private void pushVisitor(CommandVisitor visitor) {
+ stack.push(this.visitor);
+ this.visitor = visitor;
+ }
+
+ private CommandVisitor popVisitor() {
+ CommandVisitor top = this.visitor;
+ this.visitor = this.stack.pop();
+ return top;
+ }
+
+ private final class Control implements StateControl {
+
+ @Override
+ public void push(CommandVisitor visitor) {
+ pushVisitor(visitor);
+ }
+
+ @Override
+ public CommandVisitor pop() {
+ return popVisitor();
+ }
+
+ @Override
+ public CommandVisitor replace(CommandVisitor visitor) {
+ CommandVisitor current = pop();
+ push(visitor);
+ return current;
+ }
+ }
+
+ private static final class InvalidState extends UniformVisitor {
+
+ public static final InvalidState INSTANCE = new InvalidState();
+
+ private InvalidState() {
+ }
+
+ @Override
+ public void onCommand(Command command) {
+ throw new IllegalStateException("StatefulVisitor has not been initialized");
+ }
+ }
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/visitor/TrackingVisitor.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/visitor/TrackingVisitor.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/visitor/TrackingVisitor.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/visitor/TrackingVisitor.java Fri Nov 14 14:04:56 2014
@@ -0,0 +1,71 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+
+package org.apache.sling.scripting.sightly.compiler.visitor;
+
+import org.apache.sling.scripting.sightly.compiler.api.ris.Command;
+import org.apache.sling.scripting.sightly.compiler.api.ris.command.BufferControl;
+import org.apache.sling.scripting.sightly.compiler.api.ris.command.Loop;
+import org.apache.sling.scripting.sightly.compiler.api.ris.command.VariableBinding;
+import org.apache.sling.scripting.sightly.compiler.util.VariableTracker;
+import org.apache.sling.scripting.sightly.compiler.api.ris.Command;
+import org.apache.sling.scripting.sightly.compiler.api.ris.command.Loop;
+import org.apache.sling.scripting.sightly.compiler.util.VariableTracker;
+
+/**
+ * Command visitor which tracks variables in commands
+ */
+public abstract class TrackingVisitor<T> extends UniformVisitor {
+
+ protected final VariableTracker<T> tracker = new VariableTracker<T>();
+
+ @Override
+ public void visit(VariableBinding.Start variableBindingStart) {
+ super.visit(variableBindingStart);
+ tracker.pushVariable(variableBindingStart.getVariableName(), assignDefault(variableBindingStart));
+ }
+
+ @Override
+ public void visit(VariableBinding.End variableBindingEnd) {
+ super.visit(variableBindingEnd);
+ tracker.popVariable();
+ }
+
+ @Override
+ public void visit(Loop.Start loopStart) {
+ super.visit(loopStart);
+ tracker.pushVariable(loopStart.getIndexVariable(), assignDefault(loopStart));
+ tracker.pushVariable(loopStart.getItemVariable(), assignDefault(loopStart));
+ }
+
+ @Override
+ public void visit(Loop.End loopEnd) {
+ super.visit(loopEnd);
+ tracker.popVariable();
+ tracker.popVariable();
+ }
+
+ @Override
+ public void visit(BufferControl.Pop bufferPop) {
+ super.visit(bufferPop);
+ tracker.pushVariable(bufferPop.getVariableName(), assignDefault(bufferPop));
+ }
+
+ protected abstract T assignDefault(Command command);
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/visitor/UniformVisitor.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/visitor/UniformVisitor.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/visitor/UniformVisitor.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/visitor/UniformVisitor.java Fri Nov 14 14:04:56 2014
@@ -0,0 +1,109 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+package org.apache.sling.scripting.sightly.compiler.visitor;
+
+import org.apache.sling.scripting.sightly.compiler.api.ris.Command;
+import org.apache.sling.scripting.sightly.compiler.api.ris.CommandVisitor;
+import org.apache.sling.scripting.sightly.compiler.api.ris.command.BufferControl;
+import org.apache.sling.scripting.sightly.compiler.api.ris.command.Conditional;
+import org.apache.sling.scripting.sightly.compiler.api.ris.command.Loop;
+import org.apache.sling.scripting.sightly.compiler.api.ris.command.OutText;
+import org.apache.sling.scripting.sightly.compiler.api.ris.command.OutVariable;
+import org.apache.sling.scripting.sightly.compiler.api.ris.command.Procedure;
+import org.apache.sling.scripting.sightly.compiler.api.ris.command.VariableBinding;
+import org.apache.sling.scripting.sightly.compiler.api.ris.Command;
+import org.apache.sling.scripting.sightly.compiler.api.ris.CommandVisitor;
+
+/**
+ * Do not dispatch for different command types
+ */
+public abstract class UniformVisitor implements CommandVisitor {
+
+ protected abstract void onCommand(Command command);
+
+ @Override
+ public void visit(Conditional.Start conditionalStart) {
+ onCommand(conditionalStart);
+ }
+
+ @Override
+ public void visit(Conditional.End conditionalEnd) {
+ onCommand(conditionalEnd);
+ }
+
+ @Override
+ public void visit(VariableBinding.Start variableBindingStart) {
+ onCommand(variableBindingStart);
+ }
+
+ @Override
+ public void visit(VariableBinding.End variableBindingEnd) {
+ onCommand(variableBindingEnd);
+ }
+
+ @Override
+ public void visit(VariableBinding.Global globalAssignment) {
+ onCommand(globalAssignment);
+ }
+
+ @Override
+ public void visit(OutVariable outVariable) {
+ onCommand(outVariable);
+ }
+
+ @Override
+ public void visit(OutText outText) {
+ onCommand(outText);
+ }
+
+ @Override
+ public void visit(Loop.Start loopStart) {
+ onCommand(loopStart);
+ }
+
+ @Override
+ public void visit(Loop.End loopEnd) {
+ onCommand(loopEnd);
+ }
+
+ @Override
+ public void visit(BufferControl.Push bufferPush) {
+ onCommand(bufferPush);
+ }
+
+ @Override
+ public void visit(BufferControl.Pop bufferPop) {
+ onCommand(bufferPop);
+ }
+
+ @Override
+ public void visit(Procedure.Start startProcedure) {
+ onCommand(startProcedure);
+ }
+
+ @Override
+ public void visit(Procedure.End endProcedure) {
+ onCommand(endProcedure);
+ }
+
+ @Override
+ public void visit(Procedure.Call procedureCall) {
+ onCommand(procedureCall);
+ }
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/engine/ExtensionRegistryService.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/engine/ExtensionRegistryService.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/engine/ExtensionRegistryService.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/engine/ExtensionRegistryService.java Fri Nov 14 14:04:56 2014
@@ -0,0 +1,36 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+package org.apache.sling.scripting.sightly.engine;
+
+import java.util.Map;
+
+import org.apache.sling.scripting.sightly.api.RuntimeExtension;
+
+/**
+ * Aggregator for all runtime extensions.
+ */
+public interface ExtensionRegistryService {
+
+ /**
+ * A map from extension names to extensions
+ * @return a name-extension map
+ */
+ Map<String, RuntimeExtension> extensions();
+
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/engine/ExtensionRegistryServiceImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/engine/ExtensionRegistryServiceImpl.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/engine/ExtensionRegistryServiceImpl.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/engine/ExtensionRegistryServiceImpl.java Fri Nov 14 14:04:56 2014
@@ -0,0 +1,89 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+package org.apache.sling.scripting.sightly.engine;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.apache.felix.scr.annotations.ReferencePolicy;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.commons.osgi.PropertiesUtil;
+import org.apache.sling.scripting.sightly.api.RuntimeExtension;
+import org.osgi.framework.Constants;
+
+/**
+ * Implementation for {@link ExtensionRegistryService}.
+ */
+@Component
+@Service(ExtensionRegistryService.class)
+@Reference(
+ policy = ReferencePolicy.DYNAMIC,
+ referenceInterface = RuntimeExtension.class,
+ name = "extensionService",
+ cardinality = ReferenceCardinality.OPTIONAL_MULTIPLE
+)
+@SuppressWarnings("unused")
+public class ExtensionRegistryServiceImpl implements ExtensionRegistryService {
+
+ private volatile Map<String, RuntimeExtension> mapping = new HashMap<String, RuntimeExtension>();
+ private Map<String, Integer> mappingPriorities = new HashMap<String, Integer>(10, 0.9f);
+
+ @Override
+ public Map<String, RuntimeExtension> extensions() {
+ return mapping;
+ }
+
+ @SuppressWarnings("UnusedDeclaration")
+ protected synchronized void bindExtensionService(RuntimeExtension extension, Map<String, Object> properties) {
+ Integer newPriority = PropertiesUtil.toInteger(properties.get(Constants.SERVICE_RANKING), 0);
+ Integer priority = PropertiesUtil.toInteger(mappingPriorities.get(extension.name()), 0);
+ if (newPriority > priority) {
+ mapping = Collections.unmodifiableMap(add(mapping, extension));
+ mappingPriorities.put(extension.name(), newPriority);
+ } else {
+ if (!mapping.containsKey(extension.name())) {
+ mapping = Collections.unmodifiableMap(add(mapping, extension));
+ mappingPriorities.put(extension.name(), newPriority);
+ }
+ }
+
+ }
+
+ @SuppressWarnings("UnusedDeclaration")
+ protected synchronized void unbindExtensionService(RuntimeExtension extension, Map<String, Object> properties) {
+ mappingPriorities.remove(extension.name());
+ mapping = Collections.unmodifiableMap(remove(mapping, extension));
+ }
+
+ private Map<String, RuntimeExtension> add(Map<String, RuntimeExtension> oldMap, RuntimeExtension extension) {
+ HashMap<String, RuntimeExtension> newMap = new HashMap<String, RuntimeExtension>(oldMap);
+ newMap.put(extension.name(), extension);
+ return newMap;
+ }
+
+ private Map<String, RuntimeExtension> remove(Map<String, RuntimeExtension> oldMap, RuntimeExtension extension) {
+ HashMap<String, RuntimeExtension> newMap = new HashMap<String, RuntimeExtension>(oldMap);
+ newMap.remove(extension.name());
+ return newMap;
+ }
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/engine/SightlyBindingsValuesProvider.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/engine/SightlyBindingsValuesProvider.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/engine/SightlyBindingsValuesProvider.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/engine/SightlyBindingsValuesProvider.java Fri Nov 14 14:04:56 2014
@@ -0,0 +1,53 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+package org.apache.sling.scripting.sightly.engine;
+
+import javax.script.Bindings;
+
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Properties;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ValueMap;
+import org.apache.sling.api.scripting.SlingBindings;
+import org.apache.sling.scripting.api.BindingsValuesProvider;
+
+/**
+ * Sightly specific {@code BindingsValuesProvider}.
+ */
+@Component()
+@Service(BindingsValuesProvider.class)
+@Properties({
+ @Property(name = "javax.script.name", value = "sightly", propertyPrivate = true)
+})
+public class SightlyBindingsValuesProvider implements BindingsValuesProvider {
+
+ public static final String PROPERTIES = "properties";
+
+ @Override
+ public void addBindings(Bindings bindings) {
+ if (!bindings.containsKey(PROPERTIES)) {
+ Resource currentResource = (Resource) bindings.get(SlingBindings.RESOURCE);
+ if (currentResource != null) {
+ bindings.put(PROPERTIES, currentResource.adaptTo(ValueMap.class));
+ }
+ }
+ }
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/engine/SightlyEngineConfiguration.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/engine/SightlyEngineConfiguration.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/engine/SightlyEngineConfiguration.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/engine/SightlyEngineConfiguration.java Fri Nov 14 14:04:56 2014
@@ -0,0 +1,108 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+
+package org.apache.sling.scripting.sightly.engine;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Dictionary;
+import java.util.jar.Attributes;
+import java.util.jar.Manifest;
+
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Properties;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.commons.osgi.PropertiesUtil;
+import org.osgi.service.component.ComponentContext;
+
+/**
+ * Holds various Sightly engine global configurations.
+ */
+@Component(
+ metatype = true,
+ label = "Apache Sling Scripting Sightly Engine Configuration",
+ description = "Sightly Engine Configuration Options"
+)
+@Service(SightlyEngineConfiguration.class)
+@Properties({
+ @Property(
+ name = SightlyEngineConfiguration.SCR_PROP_NAME_DEVMODE,
+ boolValue = SightlyEngineConfiguration.SCR_PROP_DEFAULT_DEVMODE,
+ label = "Development Mode",
+ description = "If enabled, Sightly components will be recompiled at every request instead of loading objects from memory."
+ ),
+ @Property(
+ name = SightlyEngineConfiguration.SCR_PROP_NAME_ENCODING,
+ value = SightlyEngineConfiguration.SCR_PROP_DEFAULT_ENCODING,
+ label = "Template Files Default Encoding",
+ description = "The default encoding used for reading Sightly template files (this directly affects how Sightly templates" +
+ "are rendered)."
+ )
+})
+public class SightlyEngineConfiguration {
+
+ public static final String SCR_PROP_NAME_DEVMODE = "org.apache.sling.scripting.sightly.devmode";
+ public static final boolean SCR_PROP_DEFAULT_DEVMODE = false;
+
+ public static final String SCR_PROP_NAME_ENCODING = "org.apache.sling.scripting.sightly.encoding";
+ public static final String SCR_PROP_DEFAULT_ENCODING = "UTF-8";
+
+ private String engineVersion = "0";
+ private boolean devMode = false;
+ private String encoding = SCR_PROP_DEFAULT_ENCODING;
+
+ public String getEngineVersion() {
+ return engineVersion;
+ }
+
+ public boolean isDevMode() {
+ return devMode;
+ }
+
+ public String getEncoding() {
+ return encoding;
+ }
+
+ protected void activate(ComponentContext componentContext) {
+ InputStream ins = null;
+ try {
+ ins = getClass().getResourceAsStream("/META-INF/MANIFEST.MF");
+ if (ins != null) {
+ Manifest manifest = new Manifest(ins);
+ Attributes attrs = manifest.getMainAttributes();
+ String version = attrs.getValue("ScriptEngine-Version");
+ if (version != null) {
+ engineVersion = version;
+ }
+ }
+ } catch (IOException ioe) {
+ } finally {
+ if (ins != null) {
+ try {
+ ins.close();
+ } catch (IOException ignore) {
+ }
+ }
+ }
+ Dictionary properties = componentContext.getProperties();
+ devMode = PropertiesUtil.toBoolean(properties.get(SCR_PROP_NAME_DEVMODE), SCR_PROP_DEFAULT_DEVMODE);
+ encoding = PropertiesUtil.toString(properties.get(SCR_PROP_NAME_ENCODING), SCR_PROP_DEFAULT_ENCODING);
+ }
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/engine/SightlyScriptEngine.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/engine/SightlyScriptEngine.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/engine/SightlyScriptEngine.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/engine/SightlyScriptEngine.java Fri Nov 14 14:04:56 2014
@@ -0,0 +1,183 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+
+package org.apache.sling.scripting.sightly.engine;
+
+import java.io.PrintWriter;
+import java.io.Reader;
+import java.util.Collections;
+
+import javax.script.Bindings;
+import javax.script.ScriptContext;
+import javax.script.ScriptEngineFactory;
+import javax.script.ScriptException;
+import javax.script.SimpleBindings;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.resource.LoginException;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.ResourceResolverFactory;
+import org.apache.sling.api.scripting.SlingBindings;
+import org.apache.sling.api.scripting.SlingScriptHelper;
+import org.apache.sling.commons.classloader.DynamicClassLoader;
+import org.apache.sling.scripting.api.AbstractSlingScriptEngine;
+import org.apache.sling.scripting.sightly.api.*;
+import org.apache.sling.scripting.sightly.common.Dynamic;
+import org.apache.sling.scripting.sightly.common.SightlyRuntimeImpl;
+import org.apache.sling.scripting.sightly.engine.runtime.UnitLocatorImpl;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.sling.scripting.sightly.api.SightlyEngineException;
+import org.apache.sling.scripting.sightly.engine.runtime.UnitLocatorImpl;
+import org.apache.sling.scripting.sightly.api.RenderContext;
+import org.apache.sling.scripting.sightly.api.RenderUnit;
+import org.apache.sling.scripting.sightly.common.Dynamic;
+import org.apache.sling.scripting.sightly.common.SightlyRuntimeImpl;
+import org.apache.sling.scripting.sightly.api.StackedWriter;
+import org.apache.sling.scripting.sightly.api.UnitLocator;
+
+/**
+ * The Sightly Script engine
+ */
+public class SightlyScriptEngine extends AbstractSlingScriptEngine {
+
+ private static final Logger LOG = LoggerFactory.getLogger(SightlyScriptEngine.class);
+ private static final Bindings EMPTY_BINDINGS = new SimpleBindings(Collections.<String, Object>emptyMap());
+ private static final int MAX_CLASSLOADER_RETRIES = 5;
+
+ private final UnitLoader unitLoader;
+ private final Dynamic dynamic;
+ private final ExtensionRegistryService extensionRegistryService;
+
+ public SightlyScriptEngine(ScriptEngineFactory scriptEngineFactory,
+ UnitLoader unitLoader,
+ Dynamic dynamic,
+ ExtensionRegistryService extensionRegistryService) {
+ super(scriptEngineFactory);
+ this.unitLoader = unitLoader;
+ this.dynamic = dynamic;
+ this.extensionRegistryService = extensionRegistryService;
+ }
+
+ @Override
+ public Object eval(Reader reader, ScriptContext scriptContext) throws ScriptException {
+ ClassLoader old = Thread.currentThread().getContextClassLoader();
+ Thread.currentThread().setContextClassLoader(((SightlyScriptEngineFactory) getFactory()).getClassLoader());
+ checkArguments(reader, scriptContext);
+ Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
+ SlingScriptHelper slingScriptHelper = (SlingScriptHelper) bindings.get(SlingBindings.SLING);
+ Resource scriptResource = slingScriptHelper.getScript().getScriptResource();
+
+ final SlingBindings slingBindings = new SlingBindings();
+ slingBindings.putAll(bindings);
+
+ Bindings globalBindings = new SimpleBindings(slingBindings);
+
+ final SlingHttpServletRequest request = slingBindings.getRequest();
+ final Object oldValue = request.getAttribute(SlingBindings.class.getName());
+ try {
+ request.setAttribute(SlingBindings.class.getName(), slingBindings);
+ evaluateScript(scriptResource, globalBindings);
+ } finally {
+ request.setAttribute(SlingBindings.class.getName(), oldValue);
+ Thread.currentThread().setContextClassLoader(old);
+ }
+
+ return null;
+ }
+
+ private void evaluateScript(Resource scriptResource, Bindings bindings) {
+ RenderUnit renderUnit = unitLoader.createUnit(scriptResource, bindings);
+ ResourceResolver resourceResolver = null;
+ RenderContext context = null;
+ try {
+ resourceResolver = getAdminResourceResolver(bindings);
+ context = provideContext(scriptResource, bindings, resourceResolver);
+ renderUnit.render(context, EMPTY_BINDINGS);
+ } catch (NoClassDefFoundError defFoundError) {
+ if (context != null) {
+ ClassLoader dcl = renderUnit.getClass().getClassLoader().getParent();
+ if (dcl instanceof DynamicClassLoader && !((DynamicClassLoader) dcl).isLive()) {
+ boolean defError = true;
+ int retries = 0;
+ while (defError) {
+ try {
+ renderUnit = unitLoader.createUnit(scriptResource, bindings);
+ renderUnit.render(context, EMPTY_BINDINGS);
+ defError = false;
+ } catch (Throwable t) {
+ if (!(t instanceof NoClassDefFoundError)) {
+ // break immediately if there's a different error than a classloader one
+ if (t instanceof Error) {
+ throw (Error) t;
+ }
+ throw (RuntimeException) t;
+ }
+ retries++;
+ if (retries > MAX_CLASSLOADER_RETRIES) {
+ LOG.error("Max number of retries (" + MAX_CLASSLOADER_RETRIES +
+ ") for obtaining a valid RenderUnit was exceeded.");
+ throw defFoundError;
+ }
+ }
+ }
+ } else {
+ // if we can't recover from this just throw the original exception
+ throw defFoundError;
+ }
+ }
+ }
+ finally {
+ if (resourceResolver != null) {
+ resourceResolver.close();
+ }
+ }
+ }
+
+ private RenderContext provideContext(Resource scriptResource, Bindings bindings, ResourceResolver resourceResolver) {
+ PrintWriter printWriter = (PrintWriter) bindings.get(SlingBindings.OUT);
+ StackedWriter writer = new StackedWriter(printWriter);
+ UnitLocator unitLocator = new UnitLocatorImpl(unitLoader, resourceResolver, bindings, scriptResource);
+ SightlyRuntimeImpl runtime = new SightlyRuntimeImpl(extensionRegistryService.extensions());
+ RenderContext renderContext = new RenderContext(writer, bindings, dynamic, runtime, unitLocator);
+ runtime.setRenderContext(renderContext);
+ return renderContext;
+ }
+
+ private void checkArguments(Reader reader, ScriptContext scriptContext) {
+ if (reader == null) {
+ throw new NullPointerException("Reader cannot be null");
+ }
+ if (scriptContext == null) {
+ throw new NullPointerException("ScriptContext cannot be null");
+ }
+ }
+
+ private ResourceResolver getAdminResourceResolver(Bindings bindings) {
+ SlingScriptHelper sling = (SlingScriptHelper) bindings.get(SlingBindings.SLING);
+ ResourceResolverFactory rrf = sling.getService(ResourceResolverFactory.class);
+ try {
+ return rrf.getAdministrativeResourceResolver(null);
+ } catch (LoginException e) {
+ throw new SightlyEngineException(e);
+ }
+ }
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/engine/SightlyScriptEngineFactory.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/engine/SightlyScriptEngineFactory.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/engine/SightlyScriptEngineFactory.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/engine/SightlyScriptEngineFactory.java Fri Nov 14 14:04:56 2014
@@ -0,0 +1,116 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+
+package org.apache.sling.scripting.sightly.engine;
+
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineFactory;
+
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Properties;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.commons.classloader.DynamicClassLoaderManager;
+import org.apache.sling.scripting.api.AbstractScriptEngineFactory;
+import org.apache.sling.scripting.sightly.common.Dynamic;
+import org.osgi.service.component.ComponentContext;
+
+import org.apache.sling.scripting.sightly.api.ObjectModel;
+import org.apache.sling.scripting.sightly.common.Dynamic;
+
+/**
+ * Sightly template engine factory
+ */
+@Component()
+@Service(ScriptEngineFactory.class)
+@Properties({
+ @Property(name = "service.description", value = "Sightly Templating Engine"),
+ @Property(name = "compatible.javax.script.name", value = "sly")
+})
+public class SightlyScriptEngineFactory extends AbstractScriptEngineFactory {
+
+ @Reference
+ private UnitLoader unitLoader = null;
+
+ @Reference
+ private ExtensionRegistryService extensionRegistryService = null;
+
+ @Reference
+ private ObjectModel objectModel = null;
+
+ @Reference()
+ private DynamicClassLoaderManager dynamicClassLoaderManager;
+
+ private ClassLoader dynamicClassLoader;
+
+ protected Dynamic dynamic;
+
+ public final static String SHORT_NAME = "sightly";
+
+ public final static String LANGUAGE_NAME = "The Sightly Templating Language";
+
+ public final static String LANGUAGE_VERSION = "1.0";
+
+ public final static String EXTENSION = "html";
+
+ public SightlyScriptEngineFactory() {
+ setNames(SHORT_NAME);
+ setExtensions(EXTENSION);
+ }
+
+ @Override
+ public String getLanguageName() {
+ return LANGUAGE_NAME;
+ }
+
+ @Override
+ public String getLanguageVersion() {
+ return LANGUAGE_VERSION;
+ }
+
+ @Override
+ public ScriptEngine getScriptEngine() {
+ return new SightlyScriptEngine(this, unitLoader, dynamic, extensionRegistryService);
+ }
+
+ protected void activate(ComponentContext componentContext) {
+ dynamic = new Dynamic(objectModel);
+ }
+
+ protected void bindDynamicClassLoaderManager(final DynamicClassLoaderManager dclm) {
+ if (this.dynamicClassLoader != null) {
+ this.dynamicClassLoader = null;
+ this.dynamicClassLoaderManager = null;
+ }
+ this.dynamicClassLoaderManager = dclm;
+ dynamicClassLoader = dclm.getDynamicClassLoader();
+ }
+
+ protected void unbindDynamicClassLoaderManager(final DynamicClassLoaderManager dclm) {
+ if (this.dynamicClassLoaderManager == dclm) {
+ this.dynamicClassLoader = null;
+ this.dynamicClassLoaderManager = null;
+ }
+ }
+
+ protected ClassLoader getClassLoader() {
+ return dynamicClassLoader;
+ }
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/engine/UnitLoader.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/engine/UnitLoader.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/engine/UnitLoader.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/engine/UnitLoader.java Fri Nov 14 14:04:56 2014
@@ -0,0 +1,42 @@
+/*******************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ ******************************************************************************/
+
+package org.apache.sling.scripting.sightly.engine;
+
+import javax.script.Bindings;
+
+import org.apache.sling.api.resource.Resource;
+
+import org.apache.sling.scripting.sightly.api.RenderUnit;
+
+/**
+ * Create rendering units from resources
+ */
+public interface UnitLoader {
+
+ /**
+ * Create a render unit from the given resource
+ *
+ * @param resource the resource
+ * @param bindings the bindings
+ * @return the render unit
+ */
+ RenderUnit createUnit(Resource resource, Bindings bindings);
+
+}