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 [4/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/compiled/operator/IsWhiteSpaceGen.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/IsWhiteSpaceGen.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/IsWhiteSpaceGen.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/IsWhiteSpaceGen.java Fri Nov 14 14:04:56 2014
@@ -0,0 +1,55 @@
+/*******************************************************************************
+ * 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.compiled.operator;
+
+import org.apache.sling.scripting.sightly.compiled.ExpressionTranslator;
+import org.apache.sling.scripting.sightly.compiled.GenHelper;
+import org.apache.sling.scripting.sightly.compiled.JavaSource;
+import org.apache.sling.scripting.sightly.compiled.SourceGenConstants;
+import org.apache.sling.scripting.sightly.compiled.Type;
+import org.apache.sling.scripting.sightly.compiled.ExpressionTranslator;
+import org.apache.sling.scripting.sightly.compiled.JavaSource;
+import org.apache.sling.scripting.sightly.compiled.SourceGenConstants;
+import org.apache.sling.scripting.sightly.compiled.Type;
+
+/**
+ * Generator for IS_WHITESPACE operator
+ */
+public final class IsWhiteSpaceGen implements UnaryOpGen {
+
+ public static final IsWhiteSpaceGen INSTANCE = new IsWhiteSpaceGen();
+
+ private IsWhiteSpaceGen() {
+ }
+
+ @Override
+ public Type returnType(Type operandType) {
+ return Type.BOOLEAN;
+ }
+
+ @Override
+ public void generate(JavaSource source, ExpressionTranslator visitor, TypedNode typedNode) {
+ GenHelper.typeCoercion(source, visitor, typedNode, Type.STRING);
+ source.startCall(SourceGenConstants.TRIM_METHOD, true)
+ .endCall()
+ .startCall(SourceGenConstants.STRING_EMPTY, true)
+ .endCall();
+ }
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/LengthOpGen.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/LengthOpGen.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/LengthOpGen.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/LengthOpGen.java Fri Nov 14 14:04:56 2014
@@ -0,0 +1,51 @@
+/*******************************************************************************
+ * 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.compiled.operator;
+
+import org.apache.sling.scripting.sightly.compiled.ExpressionTranslator;
+import org.apache.sling.scripting.sightly.compiled.GenHelper;
+import org.apache.sling.scripting.sightly.compiled.JavaSource;
+import org.apache.sling.scripting.sightly.compiled.SourceGenConstants;
+import org.apache.sling.scripting.sightly.compiled.Type;
+import org.apache.sling.scripting.sightly.compiled.JavaSource;
+import org.apache.sling.scripting.sightly.compiled.SourceGenConstants;
+import org.apache.sling.scripting.sightly.compiled.Type;
+
+/**
+ * Generator for the collection length operator
+ */
+public final class LengthOpGen implements UnaryOpGen {
+
+ public static final LengthOpGen INSTANCE = new LengthOpGen();
+
+ private LengthOpGen() {
+ }
+
+ @Override
+ public Type returnType(Type operandType) {
+ return Type.LONG;
+ }
+
+ @Override
+ public void generate(JavaSource source, ExpressionTranslator visitor, TypedNode typedNode) {
+ GenHelper.listCoercion(source, visitor, typedNode);
+ source.startCall(SourceGenConstants.COLLECTION_LENGTH_METHOD, true).endCall();
+ }
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/LogicalOpGen.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/LogicalOpGen.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/LogicalOpGen.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/LogicalOpGen.java Fri Nov 14 14:04:56 2014
@@ -0,0 +1,83 @@
+/*******************************************************************************
+ * 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.compiled.operator;
+
+import org.apache.sling.scripting.sightly.compiled.ExpressionTranslator;
+import org.apache.sling.scripting.sightly.compiled.GenHelper;
+import org.apache.sling.scripting.sightly.compiled.JavaSource;
+import org.apache.sling.scripting.sightly.compiled.Type;
+import org.apache.sling.scripting.sightly.compiler.api.expression.ExpressionNode;
+import org.apache.sling.scripting.sightly.compiler.util.expression.SideEffectVisitor;
+import org.apache.sling.scripting.sightly.compiled.JavaSource;
+import org.apache.sling.scripting.sightly.compiled.Type;
+import org.apache.sling.scripting.sightly.compiler.util.expression.SideEffectVisitor;
+
+/**
+ * Generator for logical operators
+ */
+public abstract class LogicalOpGen implements BinaryOpGen {
+
+ private final String javaOperator;
+
+ protected LogicalOpGen(String javaOperator) {
+ this.javaOperator = javaOperator;
+ }
+
+ public static final LogicalOpGen AND = new LogicalOpGen("&&") {
+ @Override
+ protected void generateGeneric(JavaSource source, SideEffectVisitor visitor, TypedNode left, TypedNode right) {
+ GenHelper.generateTernary(source, visitor, left, right, left);
+ }
+ };
+
+ public static final LogicalOpGen OR = new LogicalOpGen("||") {
+ @Override
+ protected void generateGeneric(JavaSource source, SideEffectVisitor visitor, TypedNode left, TypedNode right) {
+ GenHelper.generateTernary(source, visitor, left, left, right);
+ }
+ };
+
+ @Override
+ public Type returnType(Type leftType, Type rightType) {
+ if (leftType == rightType) {
+ return leftType;
+ }
+ return Type.UNKNOWN;
+ }
+
+ @Override
+ public void generate(JavaSource source, ExpressionTranslator visitor, TypedNode left, TypedNode right) {
+ if (OpHelper.sameType(left, right) == Type.BOOLEAN) {
+ generateWithOperator(source, visitor, left.getNode(), right.getNode());
+ } else {
+ generateGeneric(source, visitor, left, right);
+ }
+ }
+
+ protected abstract void generateGeneric(JavaSource source, SideEffectVisitor visitor,
+ TypedNode left, TypedNode right);
+
+ private void generateWithOperator(JavaSource source, SideEffectVisitor visitor,
+ ExpressionNode leftNode, ExpressionNode rightNode) {
+ leftNode.accept(visitor);
+ source.append(javaOperator);
+ rightNode.accept(visitor);
+ }
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/LongOpGen.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/LongOpGen.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/LongOpGen.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/LongOpGen.java Fri Nov 14 14:04:56 2014
@@ -0,0 +1,38 @@
+/*******************************************************************************
+ * 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.compiled.operator;
+
+import org.apache.sling.scripting.sightly.compiled.Type;
+import org.apache.sling.scripting.sightly.compiled.Type;
+
+/**
+ * Generator for long numeric operators
+ */
+public class LongOpGen extends NumericOpGen {
+
+ public LongOpGen(String javaOperator) {
+ super(javaOperator);
+ }
+
+ @Override
+ protected Type commonType(Type leftType, Type rightType) {
+ return Type.LONG;
+ }
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/NotOpGen.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/NotOpGen.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/NotOpGen.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/NotOpGen.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.compiled.operator;
+
+import org.apache.sling.scripting.sightly.compiled.ExpressionTranslator;
+import org.apache.sling.scripting.sightly.compiled.GenHelper;
+import org.apache.sling.scripting.sightly.compiled.JavaSource;
+import org.apache.sling.scripting.sightly.compiled.Type;
+
+/**
+ * Generator for the not operator
+ */
+public final class NotOpGen implements UnaryOpGen {
+
+ public static final NotOpGen INSTANCE = new NotOpGen();
+
+ private NotOpGen() {
+ }
+
+ @Override
+ public Type returnType(Type operandType) {
+ return Type.BOOLEAN;
+ }
+
+ @Override
+ public void generate(JavaSource source, ExpressionTranslator visitor, TypedNode typedNode) {
+ source.negation();
+ GenHelper.typeCoercion(source, visitor, typedNode, Type.BOOLEAN);
+ }
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/NumericOpGen.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/NumericOpGen.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/NumericOpGen.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/NumericOpGen.java Fri Nov 14 14:04:56 2014
@@ -0,0 +1,58 @@
+/*******************************************************************************
+ * 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.compiled.operator;
+
+import org.apache.sling.scripting.sightly.compiled.ExpressionTranslator;
+import org.apache.sling.scripting.sightly.compiled.GenHelper;
+import org.apache.sling.scripting.sightly.compiled.JavaSource;
+import org.apache.sling.scripting.sightly.compiled.Type;
+import org.apache.sling.scripting.sightly.compiled.Type;
+
+/**
+ * Generator for numeric value
+ */
+public class NumericOpGen implements BinaryOpGen {
+
+ private final String javaOperator;
+
+ public NumericOpGen(String javaOperator) {
+ this.javaOperator = javaOperator;
+ }
+
+ @Override
+ public Type returnType(Type leftType, Type rightType) {
+ return commonType(leftType, rightType);
+ }
+
+ @Override
+ public void generate(JavaSource source, ExpressionTranslator visitor, TypedNode left, TypedNode right) {
+ Type commonType = commonType(left.getType(), right.getType());
+ GenHelper.typeCoercion(source, visitor, left, commonType);
+ source.append(javaOperator);
+ GenHelper.typeCoercion(source, visitor, right, commonType);
+ }
+
+ protected Type commonType(Type leftType, Type rightType) {
+ if (leftType == rightType && leftType == Type.LONG) {
+ return Type.LONG;
+ }
+ return Type.DOUBLE;
+ }
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/OpHelper.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/OpHelper.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/OpHelper.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/OpHelper.java Fri Nov 14 14:04:56 2014
@@ -0,0 +1,40 @@
+/*******************************************************************************
+ * 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.compiled.operator;
+
+import org.apache.sling.scripting.sightly.compiled.Type;
+import org.apache.sling.scripting.sightly.compiled.Type;
+
+/**
+ * Helper class for operator generation
+ */
+public class OpHelper {
+
+ public static Type sameType(TypedNode left, TypedNode right) {
+ if (left.getType().equals(right.getType())) {
+ return left.getType();
+ }
+ return null;
+ }
+
+ public static boolean isNumericType(Type type) {
+ return type == Type.LONG || type == Type.DOUBLE;
+ }
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/Operators.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/Operators.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/Operators.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/Operators.java Fri Nov 14 14:04:56 2014
@@ -0,0 +1,88 @@
+/*******************************************************************************
+ * 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.compiled.operator;
+
+import java.util.EnumMap;
+import java.util.Map;
+
+import org.apache.sling.scripting.sightly.compiler.api.expression.node.BinaryOperator;
+import org.apache.sling.scripting.sightly.compiler.api.expression.node.UnaryOperator;
+
+/**
+ * Provides mappings from operators to generators
+ */
+public class Operators {
+ private static final Map<BinaryOperator, BinaryOpGen> representationMap =
+ new EnumMap<BinaryOperator, BinaryOpGen>(BinaryOperator.class);
+
+ private static final Map<UnaryOperator, UnaryOpGen> unaryMapping =
+ new EnumMap<UnaryOperator, UnaryOpGen>(UnaryOperator.class);
+
+ static {
+ representationMap.put(BinaryOperator.AND, LogicalOpGen.AND);
+ representationMap.put(BinaryOperator.OR, LogicalOpGen.OR);
+ representationMap.put(BinaryOperator.CONCATENATE, ConcatenateOpGen.INSTANCE);
+ representationMap.put(BinaryOperator.ADD, new NumericOpGen("+"));
+ representationMap.put(BinaryOperator.SUB, new NumericOpGen("-"));
+ representationMap.put(BinaryOperator.MUL, new NumericOpGen("*"));
+ representationMap.put(BinaryOperator.I_DIV, new LongOpGen("/"));
+ representationMap.put(BinaryOperator.REM, new LongOpGen("%"));
+ representationMap.put(BinaryOperator.DIV, new NumericOpGen("/"));
+ representationMap.put(BinaryOperator.EQ, new EquivalenceOpGen(false));
+ representationMap.put(BinaryOperator.NEQ, new EquivalenceOpGen(true));
+ representationMap.put(BinaryOperator.LT, new ComparisonOpGen(BinaryOperator.LT));
+ representationMap.put(BinaryOperator.LEQ, new ComparisonOpGen(BinaryOperator.LEQ));
+ representationMap.put(BinaryOperator.GT, new ComparisonOpGen(BinaryOperator.GT));
+ representationMap.put(BinaryOperator.GEQ, new ComparisonOpGen(BinaryOperator.GEQ));
+ representationMap.put(BinaryOperator.STRICT_EQ, new StrictEqGenOp(false));
+ representationMap.put(BinaryOperator.STRICT_NEQ, new StrictEqGenOp(true));
+
+ unaryMapping.put(UnaryOperator.LENGTH, LengthOpGen.INSTANCE);
+ unaryMapping.put(UnaryOperator.IS_WHITESPACE, IsWhiteSpaceGen.INSTANCE);
+ unaryMapping.put(UnaryOperator.NOT, NotOpGen.INSTANCE);
+ }
+
+
+ /**
+ * Provide the signature of the given operator
+ * @param operator - the operator
+ * @return - the signature for the operator
+ */
+ public static BinaryOpGen generatorFor(BinaryOperator operator) {
+ return provide(representationMap, operator);
+ }
+
+ /**
+ * Provide the signature of the given operator
+ * @param operator - the operator
+ * @return - the signature for the operator
+ */
+ public static UnaryOpGen generatorFor(UnaryOperator operator) {
+ return provide(unaryMapping, operator);
+ }
+
+ private static <K, V> V provide(Map<K, V> map, K key) {
+ V v = map.get(key);
+ if (v == null) {
+ throw new UnsupportedOperationException("Cannot find generator for operator: " + key);
+ }
+ return v;
+ }
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/StrictEqGenOp.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/StrictEqGenOp.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/StrictEqGenOp.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/StrictEqGenOp.java Fri Nov 14 14:04:56 2014
@@ -0,0 +1,57 @@
+/*******************************************************************************
+ * 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.compiled.operator;
+
+import org.apache.sling.scripting.sightly.api.ObjectModel;
+import org.apache.sling.scripting.sightly.compiled.ExpressionTranslator;
+import org.apache.sling.scripting.sightly.compiled.JavaSource;
+import org.apache.sling.scripting.sightly.compiled.SourceGenConstants;
+import org.apache.sling.scripting.sightly.compiled.Type;
+import org.apache.sling.scripting.sightly.compiled.SourceGenConstants;
+import org.apache.sling.scripting.sightly.compiled.Type;
+
+/**
+ * Generator for strict equality
+ */
+public class StrictEqGenOp implements BinaryOpGen {
+
+ private final boolean negated;
+
+ public StrictEqGenOp(boolean negated) {
+ this.negated = negated;
+ }
+
+ @Override
+ public Type returnType(Type leftType, Type rightType) {
+ return Type.BOOLEAN;
+ }
+
+ @Override
+ public void generate(JavaSource source, ExpressionTranslator visitor, TypedNode left, TypedNode right) {
+ if (negated) {
+ source.negation();
+ }
+ source.startMethodCall(SourceGenConstants.OBJ_MODEL_INSTANCE, ObjectModel.STRICT_EQ);
+ left.getNode().accept(visitor);
+ source.separateArgument();
+ right.getNode().accept(visitor);
+ source.endCall();
+ }
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/TypedNode.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/TypedNode.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/TypedNode.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/TypedNode.java Fri Nov 14 14:04:56 2014
@@ -0,0 +1,45 @@
+/*******************************************************************************
+ * 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.compiled.operator;
+
+import org.apache.sling.scripting.sightly.compiled.Type;
+import org.apache.sling.scripting.sightly.compiler.api.expression.ExpressionNode;
+import org.apache.sling.scripting.sightly.compiled.Type;
+
+/**
+ * Expression node with type information
+ */
+public class TypedNode {
+ private final ExpressionNode node;
+ private final Type type;
+
+ public TypedNode(ExpressionNode node, Type type) {
+ this.node = node;
+ this.type = type;
+ }
+
+ public ExpressionNode getNode() {
+ return node;
+ }
+
+ public Type getType() {
+ return type;
+ }
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/UnaryOpGen.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/UnaryOpGen.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/UnaryOpGen.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiled/operator/UnaryOpGen.java Fri Nov 14 14:04:56 2014
@@ -0,0 +1,35 @@
+/*******************************************************************************
+ * 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.compiled.operator;
+
+import org.apache.sling.scripting.sightly.compiled.ExpressionTranslator;
+import org.apache.sling.scripting.sightly.compiled.JavaSource;
+import org.apache.sling.scripting.sightly.compiled.Type;
+
+/**
+ * Interface for generators of unary operators
+ */
+public interface UnaryOpGen {
+
+ Type returnType(Type operandType);
+
+ void generate(JavaSource source, ExpressionTranslator visitor, TypedNode typedNode);
+
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/BaseCompiler.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/BaseCompiler.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/BaseCompiler.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/BaseCompiler.java Fri Nov 14 14:04:56 2014
@@ -0,0 +1,61 @@
+/*******************************************************************************
+ * 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;
+
+import org.apache.sling.scripting.sightly.compiler.api.SightlyCompiler;
+import org.apache.sling.scripting.sightly.compiler.api.ris.CommandStream;
+import org.apache.sling.scripting.sightly.compiler.debug.SanityChecker;
+import org.apache.sling.scripting.sightly.compiler.optimization.StreamTransformer;
+import org.apache.sling.scripting.sightly.compiler.util.stream.PushStream;
+import org.apache.sling.scripting.sightly.compiler.api.SightlyCompiler;
+import org.apache.sling.scripting.sightly.compiler.api.ris.CommandStream;
+import org.apache.sling.scripting.sightly.compiler.optimization.StreamTransformer;
+import org.apache.sling.scripting.sightly.compiler.util.stream.PushStream;
+
+/**
+ * A base implementation of a compiler
+ */
+public abstract class BaseCompiler implements SightlyCompiler {
+
+ @Override
+ public void compile(String source, CompilerBackend backend) {
+ PushStream stream = new PushStream();
+ SanityChecker.attachChecker(stream);
+ CommandStream optimizedStream = optimizedStream(stream);
+ //optimizedStream.addHandler(LoggingHandler.INSTANCE);
+ backend.handle(optimizedStream);
+ getFrontend().compile(stream, source);
+ }
+
+ /**
+ * Get the stream optimizer for this compiler
+ * @return - a stream transformer that will optimize the command stream
+ */
+ protected abstract StreamTransformer getOptimizer();
+
+ /**
+ * Get the front-end for this compiler
+ * @return a compiler front end
+ */
+ protected abstract CompilerFrontend getFrontend();
+
+ private CommandStream optimizedStream(CommandStream stream) {
+ return getOptimizer().transform(stream);
+ }
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/CompilerBackend.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/CompilerBackend.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/CompilerBackend.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/CompilerBackend.java Fri Nov 14 14:04:56 2014
@@ -0,0 +1,35 @@
+/*******************************************************************************
+ * 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;
+
+import org.apache.sling.scripting.sightly.compiler.api.ris.CommandStream;
+import org.apache.sling.scripting.sightly.compiler.api.ris.CommandStream;
+
+/**
+ * A compiler backend
+ */
+public interface CompilerBackend {
+
+ /**
+ * Process a stream of commands
+ * @param stream - the stream of commands
+ */
+ void handle(CommandStream stream);
+
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/CompilerException.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/CompilerException.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/CompilerException.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/CompilerException.java Fri Nov 14 14:04:56 2014
@@ -0,0 +1,38 @@
+/*******************************************************************************
+ * 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;
+
+public class CompilerException extends RuntimeException {
+
+ public CompilerException() {
+ super();
+ }
+
+ public CompilerException(String message) {
+ super(message);
+ }
+
+ public CompilerException(String message, Throwable throwable) {
+ super(message, throwable);
+ }
+
+ public CompilerException(Throwable throwable) {
+ super(throwable);
+ }
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/CompilerFrontend.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/CompilerFrontend.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/CompilerFrontend.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/CompilerFrontend.java Fri Nov 14 14:04:56 2014
@@ -0,0 +1,35 @@
+/*******************************************************************************
+ * 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;
+
+import org.apache.sling.scripting.sightly.compiler.util.stream.PushStream;
+
+/**
+ * Sightly compiler
+ */
+public interface CompilerFrontend {
+
+ /**
+ * Compile the source code to a stream of commands
+ * @param stream the output stream
+ * @param source the source code
+ */
+ void compile(PushStream stream, String source);
+
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/SightlyCompileService.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/SightlyCompileService.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/SightlyCompileService.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/SightlyCompileService.java Fri Nov 14 14:04:56 2014
@@ -0,0 +1,55 @@
+/*******************************************************************************
+ * 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;
+
+import org.apache.sling.api.resource.Resource;
+
+/**
+ * The {@code SightlyCompileService} allows for simple instantiation of arbitrary classes that are either stored in the repository
+ * or in regular OSGi bundles. It also compiles Java sources on-the-fly and can discover class' source files based on
+ * Resources (typically Sling components). It supports Sling Resource type inheritance.
+ *
+ * The Java compiler only recompiles a class in case the source's timestamp is older than the compiled class if already
+ * existing. Otherwise it will load the class directly through the Sling ClassLoaderWriter service.
+ */
+public interface SightlyCompileService {
+
+ /**
+ * This method returns an Object instance based on a class that is either found through regular classloading mechanisms or on-the-fly
+ * compilation. In case the requested class does not denote a fully qualified classname, this service will try to find the class through
+ * Sling's servlet resolution mechanism and compile the class on-the-fly if required.
+ *
+ * @param resource the lookup will be performed based on this resource
+ * @param className name of class to use for object instantiation
+ * @param attemptLoad attempt to load the object from the class loader's cache
+ * @return object instance of the requested class
+ * @throws CompilerException in case of any runtime exception
+ */
+ public Object getInstance(Resource resource, String className, boolean attemptLoad);
+
+ /**
+ * Compiles a class using the passed fully qualified classname and based on the resource that represents the class' source.
+ *
+ * @param scriptResource resource that constitutes the class' source
+ * @param className Fully qualified name of the class to compile
+ * @return object instance of the class to compile
+ * @throws CompilerException in case of any runtime exception
+ */
+ public Object compileSource(Resource scriptResource, String className);
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/SightlyCompileServiceImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/SightlyCompileServiceImpl.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/SightlyCompileServiceImpl.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/SightlyCompileServiceImpl.java Fri Nov 14 14:04:56 2014
@@ -0,0 +1,298 @@
+/*******************************************************************************
+ * 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;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.util.List;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.Service;
+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.commons.classloader.ClassLoaderWriter;
+import org.apache.sling.commons.compiler.CompilationResult;
+import org.apache.sling.commons.compiler.CompilationUnit;
+import org.apache.sling.commons.compiler.CompilerMessage;
+import org.apache.sling.commons.compiler.Options;
+import org.apache.sling.jcr.compiler.JcrJavaCompiler;
+import org.apache.sling.scripting.sightly.api.ResourceResolution;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Component
+@Service
+public class SightlyCompileServiceImpl implements SightlyCompileService {
+
+ private static final Logger LOG = LoggerFactory.getLogger(SightlyCompileServiceImpl.class);
+
+ @Reference
+ private ClassLoaderWriter classLoaderWriter = null;
+
+ @Reference
+ private JcrJavaCompiler jcrJavaCompiler = null;
+
+ @Reference
+ private ResourceResolverFactory rrf = null;
+
+ private Options options;
+
+ private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
+ private Lock readLock = lock.readLock();
+ private Lock writeLock = lock.writeLock();
+
+ @Override
+ public Object getInstance(Resource resource, String className, boolean forceLoad) {
+
+ LOG.debug("Attempting to obtain bean instance of resource '{}' and class '{}'", resource.getPath(), className);
+
+ // assume fully qualified class name
+ if (className.contains(".")) {
+ Resource pojoResource = checkIfPojoIsInRepo(className);
+ if (pojoResource != null) {
+ return compileSource(pojoResource, className);
+ } else {
+ LOG.debug("fully qualified classname provided, loading object directly");
+ return loadObject(className);
+ }
+ }
+
+ LOG.debug("trying to find Java source based on resource: {}", resource.getPath());
+
+ // try to find Java source in JCR
+ // use the servlet resolver to do all the magic lookup (resource type hierarchy and search path) for us
+
+ Resource scriptResource = ResourceResolution.resolveComponentRelative(resource.getResourceResolver(), resource, className + ".java");
+ if (scriptResource != null) {
+ LOG.debug("found Java bean script resource: " + scriptResource.getPath());
+ try {
+ return compileJavaResource(new SlingResourceCompilationUnit(scriptResource), scriptResource.getPath());
+ } catch (Exception e) {
+ throw new CompilerException(e);
+ }
+ } else {
+ // not found in JCR, try to load from bundle using current resource type package
+ // /apps/project/components/foo => apps.project.components.foo.<scriptName>
+ Resource resourceType = resource.getResourceResolver().getResource(resource.getResourceType());
+
+ if (resourceType == null) {
+ resourceType = resource;
+ }
+
+ String resourceTypeDir = resourceType.getPath();
+ className = getJavaNameFromPath(resourceTypeDir) + "." + className;
+
+ LOG.debug("Java bean source not found, trying to locate using" + " component directory as packagename: {}", resourceTypeDir);
+
+ LOG.debug("loading Java class: " + className);
+ return loadObject(className);
+ }
+ }
+
+ @Override
+ public Object compileSource(Resource javaResource, String fullQualifiedClassName) {
+ LOG.debug("Compiling Sightly based Java class from resource: " + javaResource.getPath());
+ try {
+ CompilationUnit compilationUnit = new SightlyCompilationUnit(javaResource, fullQualifiedClassName);
+ return compileJavaResource(compilationUnit, javaResource.getPath());
+ } catch (Exception e) {
+ throw new CompilerException(e);
+ }
+ }
+
+ /**
+ * Instantiate and return an instance of a class.
+ *
+ * @param className class to instantiate
+ * @return instance of class
+ */
+ private Object loadObject(String className) {
+ try {
+ return loadClass(className).newInstance();
+ } catch (Exception e) {
+ throw new CompilerException(e);
+ }
+ }
+
+ /**
+ * Retrieve a class from the ClassLoaderWriter service.
+ *
+ * @param name name of class to load
+ * @return Class
+ * @throws ClassNotFoundException
+ */
+ private Class loadClass(String name) throws ClassNotFoundException {
+ readLock.lock();
+ try {
+ if (classLoaderWriter != null) {
+ return classLoaderWriter.getClassLoader().loadClass(name);
+ }
+ } finally {
+ readLock.unlock();
+ }
+ return Class.forName(name);
+ }
+
+ /**
+ * Compiles a class stored in the repository and returns an instance of the compiled class.
+ *
+ * @param compilationUnit a compilation unit
+ * @param scriptPath the path of the script to compile
+ * @return instance of compiled class
+ * @throws Exception
+ */
+ private Object compileJavaResource(CompilationUnit compilationUnit, String scriptPath) throws Exception {
+ writeLock.lock();
+ try {
+ long start = System.currentTimeMillis();
+ CompilationResult compilationResult = jcrJavaCompiler.compile(new String[]{scriptPath}, options);
+ long end = System.currentTimeMillis();
+ List<CompilerMessage> errors = compilationResult.getErrors();
+ if (errors != null && errors.size() > 0) {
+ throw new CompilerException(createErrorMsg(errors));
+ }
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("script compiled: {}", compilationResult.didCompile());
+ LOG.debug("compilation took {}ms", end - start);
+ }
+ return compilationResult.loadCompiledClass(compilationUnit.getMainClassName()).newInstance();
+ } finally {
+ writeLock.unlock();
+ }
+ }
+
+ @Activate
+ @SuppressWarnings("unused")
+ protected void activate() {
+ LOG.info("Activating {}", getClass().getName());
+
+ String version = System.getProperty("java.specification.version");
+ options = new Options();
+ options.put(Options.KEY_GENERATE_DEBUG_INFO, true);
+ options.put(Options.KEY_SOURCE_VERSION, version);
+ options.put(Options.KEY_TARGET_VERSION, version);
+ options.put(Options.KEY_CLASS_LOADER_WRITER, classLoaderWriter);
+ }
+
+ //---------------------------------- private -----------------------------------
+ /**
+ * Checks is a POJO class name is represented by a resource from the repository.
+ *
+ * @param className the class name
+ * @return the Resource in which the class is defined, {@code null} if the POJO was not found in the repository
+ */
+ private Resource checkIfPojoIsInRepo(String className) {
+ // POJOs will always be loaded through the compiler (prevents stale class loader issues)
+ String pojoPath = "/" + className.replaceAll("\\.", "/") + ".java";
+ ResourceResolver rr = null;
+ try {
+ rr = rrf.getAdministrativeResourceResolver(null);
+ Resource pojoResource = rr.getResource(pojoPath);
+ if (pojoResource != null) {
+ for (String s : rr.getSearchPath()) {
+ if (pojoPath.startsWith(s)) {
+ return pojoResource;
+ }
+ }
+ }
+ } catch (LoginException le) {
+ LOG.error("Cannot search repository for POJO.", le);
+ } finally {
+ if (rr != null) {
+ rr.close();
+ }
+ }
+ return null;
+ }
+
+ private String getJavaNameFromPath(String path) {
+ if (path.endsWith(".java")) {
+ path = path.substring(0, path.length() - 5);
+ }
+ return path.substring(1).replace("/", ".").replace("-", "_");
+ }
+
+ private String createErrorMsg(List<CompilerMessage> errors) {
+ final StringBuilder buffer = new StringBuilder();
+ buffer.append("Compilation errors in ");
+ buffer.append(errors.get(0).getFile());
+ buffer.append(":");
+ StringBuilder errorsBuffer = new StringBuilder();
+ boolean duplicateVariable = false;
+ for (final CompilerMessage e : errors) {
+ if (!duplicateVariable) {
+ if (e.getMessage().contains("Duplicate local variable")) {
+ duplicateVariable = true;
+ buffer.append(" Maybe you defined more than one identical block elements without defining a different variable for "
+ + "each one?");
+ }
+ }
+ errorsBuffer.append("\nLine ");
+ errorsBuffer.append(e.getLine());
+ errorsBuffer.append(", column ");
+ errorsBuffer.append(e.getColumn());
+ errorsBuffer.append(" : ");
+ errorsBuffer.append(e.getMessage());
+ }
+ buffer.append(errorsBuffer);
+ return buffer.toString();
+ }
+
+ class SlingResourceCompilationUnit implements CompilationUnit {
+ private final Resource resource;
+
+ public SlingResourceCompilationUnit(Resource resource) {
+ this.resource = resource;
+ }
+
+ public Reader getSource() throws IOException {
+ return new InputStreamReader(resource.adaptTo(InputStream.class), "UTF-8");
+ }
+
+ public String getMainClassName() {
+ return getJavaNameFromPath(resource.getPath());
+ }
+
+ public long getLastModified() {
+ return resource.getResourceMetadata().getModificationTime();
+ }
+ }
+
+ class SightlyCompilationUnit extends SlingResourceCompilationUnit {
+ private String fqcn;
+
+ public SightlyCompilationUnit(Resource resource, String fqcn) {
+ super(resource);
+ this.fqcn = fqcn;
+ }
+
+ public String getMainClassName() {
+ return fqcn;
+ }
+ }
+
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/SightlyCompilerImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/SightlyCompilerImpl.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/SightlyCompilerImpl.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/SightlyCompilerImpl.java Fri Nov 14 14:04:56 2014
@@ -0,0 +1,210 @@
+/*******************************************************************************
+ * 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;
+
+import java.util.ArrayList;
+import java.util.Dictionary;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.felix.scr.annotations.Activate;
+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.ReferenceCardinality;
+import org.apache.felix.scr.annotations.ReferencePolicy;
+import org.apache.felix.scr.annotations.References;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.commons.osgi.PropertiesUtil;
+import org.osgi.service.component.ComponentContext;
+
+import org.apache.sling.scripting.sightly.compiler.api.Filter;
+import org.apache.sling.scripting.sightly.compiler.api.MarkupParser;
+import org.apache.sling.scripting.sightly.api.ObjectModel;
+import org.apache.sling.scripting.sightly.compiler.api.SightlyCompiler;
+import org.apache.sling.scripting.sightly.compiler.api.plugin.Plugin;
+import org.apache.sling.scripting.sightly.compiler.frontend.SimpleFrontend;
+import org.apache.sling.scripting.sightly.compiler.optimization.CoalescingWrites;
+import org.apache.sling.scripting.sightly.compiler.optimization.DeadCodeRemoval;
+import org.apache.sling.scripting.sightly.compiler.optimization.SequenceStreamTransformer;
+import org.apache.sling.scripting.sightly.compiler.optimization.StreamTransformer;
+import org.apache.sling.scripting.sightly.compiler.optimization.SyntheticMapRemoval;
+import org.apache.sling.scripting.sightly.compiler.optimization.UnusedVariableRemoval;
+import org.apache.sling.scripting.sightly.compiler.optimization.reduce.ConstantFolding;
+import org.apache.sling.scripting.sightly.common.Dynamic;
+
+/**
+ * Implementation for the Sightly compiler
+ */
+@Component(metatype = true, label = "Apache Sling Scripting Sightly Java Compiler", description = "The Apache Sling Sightly Java Compiler" +
+ " is responsible for translating Sightly scripts into Java source code.")
+@Service(SightlyCompiler.class)
+@References({
+ @Reference(
+ policy = ReferencePolicy.DYNAMIC,
+ referenceInterface = Filter.class,
+ name = "filterService",
+ cardinality = ReferenceCardinality.OPTIONAL_MULTIPLE
+ ),
+ @Reference(
+ policy = ReferencePolicy.DYNAMIC,
+ referenceInterface = Plugin.class,
+ name = "pluginService",
+ cardinality = ReferenceCardinality.OPTIONAL_MULTIPLE
+ )
+})
+@Properties({
+ @Property(
+ name = SightlyCompilerImpl.CONSTANT_FOLDING_OPT,
+ boolValue = true,
+ label = "Constant Folding",
+ description = "Optimises expressions by evaluating static parts in advance."
+ ),
+ @Property(
+ name = SightlyCompilerImpl.DEAD_CODE_OPT,
+ boolValue = true,
+ label = "Dead Code Removal",
+ description = "Optimises expressions evaluations by removing code that's under the false branch of an if."
+ ),
+ @Property(
+ name = SightlyCompilerImpl.SYNTHETIC_MAP_OPT,
+ boolValue = true,
+ label = "Synthetic Map Removal",
+ description = "Optimises expressions by replacing calls to map.get with the actual object from the map if the only usages" +
+ " of that map are value retrievals."
+ ),
+ @Property(
+ name = SightlyCompilerImpl.UNUSED_VAR_OPT,
+ boolValue = true,
+ label = "Unused Variables Removal",
+ description = "Optimises expression evaluations by removing unused variables from the generated Java source code."
+ ),
+ @Property(
+ name = SightlyCompilerImpl.COALESCING_WRITES_OPT,
+ boolValue = true,
+ label = "Coalescing Writes",
+ description = "Optimises expression evaluations by merging together consecutive writes to the Java source code files."
+ )
+})
+public class SightlyCompilerImpl extends BaseCompiler {
+
+ public static final String CONSTANT_FOLDING_OPT = "org.apache.sling.scripting.sightly.compiler.constantFolding";
+ public static final String DEAD_CODE_OPT = "org.apache.sling.scripting.sightly.compiler.deadCodeRemoval";
+ public static final String SYNTHETIC_MAP_OPT = "org.apache.sling.scripting.sightly.compiler.syntheticMapRemoval";
+ public static final String UNUSED_VAR_OPT = "org.apache.sling.scripting.sightly.compiler.unusedVarRemoval";
+ public static final String COALESCING_WRITES_OPT = "org.apache.sling.scripting.sightly.compiler.coalescingWrites";
+
+ private List<Filter> filters = new ArrayList<Filter>();
+ private List<Plugin> plugins = new ArrayList<Plugin>();
+
+ private volatile StreamTransformer optimizer;
+ private volatile CompilerFrontend frontend;
+
+ @Reference
+ protected MarkupParser markupParser;
+
+ @Reference
+ protected ObjectModel objectModel;
+
+ @Override
+ protected StreamTransformer getOptimizer() {
+ return optimizer;
+ }
+
+ @Override
+ protected CompilerFrontend getFrontend() {
+ return frontend;
+ }
+
+ @Activate
+ protected void activate(ComponentContext context) {
+ Dictionary properties = context.getProperties();
+ reloadOptimizations(properties);
+ reloadFrontend();
+ }
+
+ @SuppressWarnings("UnusedDeclaration")
+ protected void bindFilterService(Filter filter, Map<String, Object> properties) {
+ synchronized(filters) {
+ filters = add(filters, filter);
+ reloadFrontend();
+ }
+ }
+
+ @SuppressWarnings("UnusedDeclaration")
+ protected void unbindFilterService(Filter filter, Map<String, Object> properties) {
+ synchronized (filters) {
+ filters = remove(filters, filter);
+ reloadFrontend();
+ }
+ }
+
+ @SuppressWarnings("UnusedDeclaration")
+ protected void bindPluginService(Plugin plugin, Map<String, Object> properties) {
+ synchronized (plugins) {
+ plugins = add(plugins, plugin);
+ reloadFrontend();
+ }
+ }
+
+ @SuppressWarnings("UnusedDeclaration")
+ protected void unbindPluginService(Plugin plugin, Map<String, Object> properties) {
+ synchronized (plugins) {
+ plugins = remove(plugins, plugin);
+ reloadFrontend();
+ }
+ }
+
+ private void reloadOptimizations(Dictionary properties) {
+ ArrayList<StreamTransformer> transformers = new ArrayList<StreamTransformer>();
+ Dynamic dynamic = new Dynamic(objectModel);
+ activateOptimization(CONSTANT_FOLDING_OPT, transformers, properties, ConstantFolding.transformer(dynamic));
+ activateOptimization(DEAD_CODE_OPT, transformers, properties, DeadCodeRemoval.transformer(dynamic));
+ activateOptimization(SYNTHETIC_MAP_OPT, transformers, properties, SyntheticMapRemoval.TRANSFORMER);
+ activateOptimization(UNUSED_VAR_OPT, transformers, properties, UnusedVariableRemoval.TRANSFORMER);
+ activateOptimization(COALESCING_WRITES_OPT, transformers, properties, CoalescingWrites.TRANSFORMER);
+ optimizer = new SequenceStreamTransformer(transformers);
+ }
+
+ private void activateOptimization(String option, ArrayList<StreamTransformer> transformers,
+ Dictionary dictionary, StreamTransformer transformer) {
+ boolean activate = PropertiesUtil.toBoolean(dictionary.get(option), true);
+ if (activate) {
+ transformers.add(transformer);
+ }
+ }
+
+ private void reloadFrontend() {
+ frontend = new SimpleFrontend(markupParser, plugins, filters);
+ }
+
+ private static <T> List<T> add(List<T> list, T item) {
+ ArrayList<T> result = new ArrayList<T>(list);
+ result.add(item);
+ return result;
+ }
+
+ private static <T> List<T> remove(List<T> list, T item) {
+ ArrayList<T> result = new ArrayList<T>(list);
+ result.remove(item);
+ return result;
+ }
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/Syntax.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/Syntax.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/Syntax.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/Syntax.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;
+
+import java.util.Arrays;
+
+import org.apache.sling.scripting.sightly.compiler.api.plugin.PluginCallInfo;
+import org.apache.sling.scripting.sightly.compiler.api.plugin.PluginCallInfo;
+
+/**
+ * Sightly Syntax specific elements
+ */
+public class Syntax {
+
+ public static final String SLY_COMMENT_PREFIX = "<!--/*";
+
+ public static final String SLY_COMMENT_SUFFIX = "*/-->";
+
+ public static final String PLUGIN_ATTRIBUTE_PREFIX = "data-sly-";
+
+ public static final String DEFAULT_LIST_ITEM_VAR_NAME = "item";
+
+ public static final String ITEM_LOOP_STATUS_SUFFIX = "List";
+
+ public static final String CONTEXT_OPTION = "context";
+
+ public static final String MAP_ITEM_KEY_PROPERTY = "key";
+
+ public static final String MAP_ITEM_VALUE_PROPERTY = "value";
+
+ public static final String LENGTH_PROPERTY = "size";
+
+ /**
+ * Checks whether a piece of text represents a Sightly comment
+ * @param text - the text
+ * @return - true if it is a Sightly comment, false otherwise
+ */
+ public static boolean isSightlyComment(String text) {
+ //todo: performance concern
+ String trimmed = text.trim();
+ return trimmed.startsWith(SLY_COMMENT_PREFIX) && trimmed.endsWith(SLY_COMMENT_SUFFIX);
+ }
+
+ public static boolean isPluginAttribute(String attributeName) {
+ return attributeName.startsWith(PLUGIN_ATTRIBUTE_PREFIX);
+ }
+
+ public static PluginCallInfo parsePluginAttribute(String attributeName) {
+ if (!isPluginAttribute(attributeName)) {
+ return null;
+ }
+ String fragment = attributeName.substring(PLUGIN_ATTRIBUTE_PREFIX.length());
+ String[] parts = fragment.split("\\.");
+ if (parts.length == 0) {
+ return null;
+ }
+ return new PluginCallInfo(parts[0], Arrays.copyOfRange(parts, 1, parts.length));
+ }
+
+ public static String itemLoopStatusVariable(String itemVariable) {
+ return itemVariable + ITEM_LOOP_STATUS_SUFFIX;
+ }
+
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/api/CompilerException.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/api/CompilerException.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/api/CompilerException.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/api/CompilerException.java Fri Nov 14 14:04:56 2014
@@ -0,0 +1,41 @@
+/*******************************************************************************
+ * 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.api;
+
+/**
+ * Compile-specific exception
+ */
+public class CompilerException extends RuntimeException {
+
+ public CompilerException() {
+ super();
+ }
+
+ public CompilerException(String message) {
+ super(message);
+ }
+
+ public CompilerException(String message, Throwable throwable) {
+ super(message, throwable);
+ }
+
+ public CompilerException(Throwable throwable) {
+ super(throwable);
+ }
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/api/Filter.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/api/Filter.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/api/Filter.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/api/Filter.java Fri Nov 14 14:04:56 2014
@@ -0,0 +1,46 @@
+/*******************************************************************************
+ * 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.api;
+
+import org.apache.sling.scripting.sightly.compiler.api.expression.Expression;
+import org.apache.sling.scripting.sightly.compiler.api.expression.Expression;
+
+/**
+ * A filter is a transformation which performs modifications on expressions. Unlike plugins, filters
+ * are always applied on an expression. Whether the filter transformation is actually necessary is
+ * decided by the filter. The application order of filters is given by filter priority.
+ */
+public interface Filter extends Comparable<Filter> {
+
+ /**
+ * Transform the given expression
+ * @param expression the original expression
+ * @return a transformed expression. If the filter is not applicable
+ * to the given expression, then the original expression shall be returned
+ */
+ Expression apply(Expression expression);
+
+ /**
+ * The priority with which filters are applied. This establishes order between filters. Filters with
+ * lower priority are applied first.
+ * @return an integer representing the filter's priority
+ */
+ int priority();
+
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/api/MarkupHandler.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/api/MarkupHandler.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/api/MarkupHandler.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/api/MarkupHandler.java Fri Nov 14 14:04:56 2014
@@ -0,0 +1,82 @@
+/*******************************************************************************
+ * 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.api;
+
+/**
+ * Interface used to communicate between HTML parsers and the compiler
+ * front-end.
+ */
+public interface MarkupHandler {
+
+ /**
+ * Signal the start of a new element
+ * @param markup - the markup of the open tag (Something like \< div)
+ * @param tagName - the tag of the element
+ */
+ void onOpenTagStart(String markup, String tagName);
+
+ /**
+ * Signal a element attribute
+ * @param name - the name of the attribute
+ * @param value - the value of the attribute. If the attribute has no value, null should be passed
+ */
+ void onAttribute(String name, String value);
+
+ /**
+ * Signal that the start tag has ended
+ * @param markup the markup for this event
+ */
+ void onOpenTagEnd(String markup);
+
+ /**
+ * @param markup the HTML markup for this event
+ * Signal that a tag was closed
+ */
+ void onCloseTag(String markup);
+
+ /**
+ * Signal a text node
+ * @param text the raw text content
+ */
+ void onText(String text);
+
+ /**
+ * Signal a comment node
+ * @param markup - the markup for the comment
+ */
+ void onComment(String markup);
+
+ /**
+ * Signal a data node
+ * @param markup - the markup for this event
+ */
+ void onDataNode(String markup);
+
+ /**
+ * Signal a document type declaration
+ * @param markup - the markup for this event
+ */
+ void onDocType(String markup);
+
+ /**
+ * Signal that the document was fully processed
+ */
+ void onDocumentFinished();
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/api/MarkupParser.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/api/MarkupParser.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/api/MarkupParser.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/api/MarkupParser.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.api;
+
+/**
+ * Interface for markup parsers that are meant to be used in the
+ * Sightly compiler front-end
+ */
+public interface MarkupParser {
+
+ /**
+ * Parse the given document and use the handler to process
+ * the markup events
+ * @param document - the parsed document
+ * @param handler - a markup handler
+ */
+ void parse(String document, MarkupHandler handler);
+
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/api/SightlyCompiler.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/api/SightlyCompiler.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/api/SightlyCompiler.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/api/SightlyCompiler.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.api;
+
+import org.apache.sling.scripting.sightly.compiler.CompilerBackend;
+
+/**
+ * Created by mdanila on 2/3/14.
+ */
+public interface SightlyCompiler {
+
+ /**
+ * Compile the given markup source and feed it to the given backend
+ * @param source the HTML source code
+ * @param backend the backend that will process the command stream from the source
+ */
+ void compile(String source, CompilerBackend backend);
+
+}
Added: sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/api/expression/Expression.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/api/expression/Expression.java?rev=1639641&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/api/expression/Expression.java (added)
+++ sling/trunk/contrib/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/compiler/api/expression/Expression.java Fri Nov 14 14:04:56 2014
@@ -0,0 +1,106 @@
+/*******************************************************************************
+ * 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.api.expression;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * This class represent a Sightly Expression.
+ */
+public class Expression {
+
+ private final Map<String, ExpressionNode> options;
+ private final ExpressionNode root;
+
+ public Expression(ExpressionNode root, Map<String, ExpressionNode> options) {
+ this.root = root;
+ this.options = new HashMap<String, ExpressionNode>(options);
+ }
+
+ public Expression(ExpressionNode root) {
+ this(root, Collections.<String, ExpressionNode>emptyMap());
+ }
+
+ /**
+ * Get the options for this expression
+ * @return - the expression options
+ */
+ public Map<String, ExpressionNode> getOptions() {
+ return Collections.unmodifiableMap(options);
+ }
+
+ /**
+ * Get the root node of this expression
+ * @return - the root expression node
+ */
+ public ExpressionNode getRoot() {
+ return root;
+ }
+
+ /**
+ * Get the option with the specified name
+ * @param name the name of the option
+ * @return the expression node for the option value, or null if the
+ * option is not in the expression
+ */
+ public ExpressionNode getOption(String name) {
+ return options.get(name);
+ }
+
+ /**
+ * Return an expression where the given options are no longer present
+ * @param removedOptions the options to be removed
+ * @return a copy where the mention options are no longer present
+ */
+ public Expression removeOptions(String ... removedOptions) {
+ HashMap<String, ExpressionNode> newOptions = new HashMap<String, ExpressionNode>(options);
+ for (String option : removedOptions) {
+ newOptions.remove(option);
+ }
+ return new Expression(root, newOptions);
+ }
+
+ /**
+ * Return a copy, but with the specified node as root
+ * @param node the new root
+ * @return a copy with a new root
+ */
+ public Expression withNode(ExpressionNode node) {
+ return new Expression(node, options);
+ }
+
+ /**
+ * Checks whether the expression has the specified option
+ * @param name the name of the option
+ * @return true if the option is present, false otherwise
+ */
+ public boolean containsOption(String name) {
+ return options.containsKey(name);
+ }
+
+ @Override
+ public String toString() {
+ return "Expression{" +
+ "options=" + getOptions() +
+ ", root=" + root +
+ '}';
+ }
+}