SLING-7709 - [HTL] Implement the data-sly-set block element
authorRadu Cotescu <radu@apache.org>
Wed, 6 Jun 2018 13:33:42 +0000 (15:33 +0200)
committerRadu Cotescu <radu@apache.org>
Wed, 6 Jun 2018 13:33:42 +0000 (15:33 +0200)
src/main/java/org/apache/sling/scripting/sightly/compiler/SightlyCompiler.java
src/main/java/org/apache/sling/scripting/sightly/impl/filter/ExpressionContext.java
src/main/java/org/apache/sling/scripting/sightly/impl/plugin/SetPlugin.java [new file with mode: 0644]

index 7c14e0e..6aeecde 100644 (file)
@@ -54,6 +54,7 @@ import org.apache.sling.scripting.sightly.impl.plugin.ListPlugin;
 import org.apache.sling.scripting.sightly.impl.plugin.Plugin;
 import org.apache.sling.scripting.sightly.impl.plugin.RepeatPlugin;
 import org.apache.sling.scripting.sightly.impl.plugin.ResourcePlugin;
+import org.apache.sling.scripting.sightly.impl.plugin.SetPlugin;
 import org.apache.sling.scripting.sightly.impl.plugin.TemplatePlugin;
 import org.apache.sling.scripting.sightly.impl.plugin.TestPlugin;
 import org.apache.sling.scripting.sightly.impl.plugin.TextPlugin;
@@ -96,6 +97,7 @@ public final class SightlyCompiler {
         plugins.add(new ResourcePlugin());
         plugins.add(new TemplatePlugin());
         plugins.add(new TestPlugin());
+        plugins.add(new SetPlugin());
         plugins.add(new TextPlugin());
         plugins.add(new UnwrapPlugin());
         plugins.add(new UsePlugin());
index b9cc06c..531e813 100644 (file)
@@ -30,6 +30,7 @@ public enum ExpressionContext {
     PLUGIN_DATA_SLY_ATTRIBUTE,
     PLUGIN_DATA_SLY_ELEMENT,
     PLUGIN_DATA_SLY_TEST,
+    PLUGIN_DATA_SLY_SET,
     PLUGIN_DATA_SLY_LIST,
     PLUGIN_DATA_SLY_REPEAT,
     PLUGIN_DATA_SLY_INCLUDE,
diff --git a/src/main/java/org/apache/sling/scripting/sightly/impl/plugin/SetPlugin.java b/src/main/java/org/apache/sling/scripting/sightly/impl/plugin/SetPlugin.java
new file mode 100644 (file)
index 0000000..010918b
--- /dev/null
@@ -0,0 +1,62 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ 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.impl.plugin;
+
+import org.apache.sling.scripting.sightly.compiler.SightlyCompilerException;
+import org.apache.sling.scripting.sightly.compiler.commands.VariableBinding;
+import org.apache.sling.scripting.sightly.compiler.expression.Expression;
+import org.apache.sling.scripting.sightly.impl.compiler.PushStream;
+import org.apache.sling.scripting.sightly.impl.compiler.frontend.CompilerContext;
+
+/**
+ * Implementation for the {@code data-sly-set} plugin
+ */
+public class SetPlugin extends AbstractPlugin {
+
+    public SetPlugin() {
+        name = "set";
+        priority = 1;
+    }
+
+    @Override
+    public PluginInvoke invoke(final Expression expressionNode, final PluginCallInfo callInfo, final CompilerContext compilerContext) {
+
+        final String variableName = decodeVariableName(callInfo);
+
+        return new DefaultPluginInvoke() {
+
+            @Override
+            public void beforeElement(PushStream stream, String tagName) {
+                stream.write(new VariableBinding.Global(variableName, expressionNode.getRoot()));
+            }
+
+
+        };
+    }
+
+    private String decodeVariableName(PluginCallInfo callInfo) {
+        String[] arguments = callInfo.getArguments();
+        if (arguments.length == 0) {
+            throw new SightlyCompilerException("Identifier name was not provided.");
+        }
+        return arguments[0];
+    }
+
+
+}