SLING-7713 - [HTL] Add support for data-sly-unwrap identifiers
authorRadu Cotescu <radu@apache.org>
Thu, 7 Jun 2018 12:16:50 +0000 (14:16 +0200)
committerRadu Cotescu <radu@apache.org>
Thu, 7 Jun 2018 12:16:50 +0000 (14:16 +0200)
src/main/java/org/apache/sling/scripting/sightly/impl/plugin/AbstractPlugin.java
src/main/java/org/apache/sling/scripting/sightly/impl/plugin/SetPlugin.java
src/main/java/org/apache/sling/scripting/sightly/impl/plugin/TestPlugin.java
src/main/java/org/apache/sling/scripting/sightly/impl/plugin/UnwrapPlugin.java

index cd09ef7..5a204c1 100644 (file)
@@ -37,4 +37,12 @@ public abstract class AbstractPlugin implements Plugin {
     public int compareTo(Plugin o) {
         return this.priority - o.priority();
     }
+
+    public String decodeVariableName(PluginCallInfo pluginCallInfo) {
+        String[] args = pluginCallInfo.getArguments();
+        if (args.length > 0) {
+            return args[0];
+        }
+        return null;
+    }
 }
index 010918b..f4fe975 100644 (file)
@@ -18,6 +18,7 @@
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
 package org.apache.sling.scripting.sightly.impl.plugin;
 
+import org.apache.commons.lang3.StringUtils;
 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;
@@ -38,6 +39,9 @@ public class SetPlugin extends AbstractPlugin {
     public PluginInvoke invoke(final Expression expressionNode, final PluginCallInfo callInfo, final CompilerContext compilerContext) {
 
         final String variableName = decodeVariableName(callInfo);
+        if (StringUtils.isEmpty(variableName)) {
+            throw new SightlyCompilerException("Identifier name was not provided.");
+        }
 
         return new DefaultPluginInvoke() {
 
@@ -50,13 +54,4 @@ public class SetPlugin extends AbstractPlugin {
         };
     }
 
-    private String decodeVariableName(PluginCallInfo callInfo) {
-        String[] arguments = callInfo.getArguments();
-        if (arguments.length == 0) {
-            throw new SightlyCompilerException("Identifier name was not provided.");
-        }
-        return arguments[0];
-    }
-
-
 }
index 66570d4..202bfb4 100644 (file)
@@ -43,7 +43,7 @@ public class TestPlugin extends AbstractPlugin {
 
             @Override
             public void beforeElement(PushStream stream, String tagName) {
-                String variableName = decodeVariableName();
+                String variableName = decodeVariableName(callInfo);
                 globalBinding = variableName != null;
                 if (variableName == null) {
                     variableName = compilerContext.generateVariable("testVariable");
@@ -63,15 +63,6 @@ public class TestPlugin extends AbstractPlugin {
                     stream.write(VariableBinding.END);
                 }
             }
-
-            private String decodeVariableName() {
-                String[] args = callInfo.getArguments();
-                if (args.length > 0) {
-                    return args[0];
-                }
-                return null;
-            }
-
         };
     }
 
index 104ecf7..51e2594 100644 (file)
@@ -40,17 +40,28 @@ public class UnwrapPlugin extends AbstractPlugin {
     }
 
     @Override
-    public PluginInvoke invoke(final Expression expression, PluginCallInfo callInfo, final CompilerContext compilerContext) {
+    public PluginInvoke invoke(final Expression expression, final PluginCallInfo callInfo, final CompilerContext compilerContext) {
+
         return new DefaultPluginInvoke() {
 
-            private final String variable = compilerContext.generateVariable("unwrapCondition");
-            private final Command unwrapTest = new Conditional.Start(variable, false);
+            private Command unwrapTest;
             private boolean isSlyTag = false;
+            private String identifierName = decodeVariableName(callInfo);
+            private boolean globalBinding;
 
             @Override
             public void beforeElement(PushStream stream, String tagName) {
+                globalBinding = identifierName != null;
+                if (identifierName == null) {
+                    identifierName = compilerContext.generateVariable("unwrapCondition");
+                }
+                if (globalBinding) {
+                    stream.write(new VariableBinding.Global(identifierName, expression.getRoot()));
+                } else {
+                    stream.write(new VariableBinding.Start(identifierName, testNode()));
+                }
                 isSlyTag = "sly".equals(tagName.toLowerCase());
-                stream.write(new VariableBinding.Start(variable, testNode()));
+                unwrapTest = new Conditional.Start(identifierName, false);
             }
 
             @Override
@@ -87,7 +98,9 @@ public class UnwrapPlugin extends AbstractPlugin {
 
             @Override
             public void afterElement(PushStream stream) {
-                stream.write(VariableBinding.END);
+                if (!globalBinding) {
+                    stream.write(VariableBinding.END);
+                }
             }
 
             private ExpressionNode testNode() {