You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ra...@apache.org on 2019/05/23 19:24:22 UTC

[sling-org-apache-sling-scripting-sightly-compiler] branch issue/SLING-6779 created (now 9b790d2)

This is an automated email from the ASF dual-hosted git repository.

radu pushed a change to branch issue/SLING-6779
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-scripting-sightly-compiler.git.


      at 9b790d2  SLING-6779 - [htl-maven-plugin] No warning / error on wrong options

This branch includes the following new commits:

     new 9b790d2  SLING-6779 - [htl-maven-plugin] No warning / error on wrong options

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[sling-org-apache-sling-scripting-sightly-compiler] 01/01: SLING-6779 - [htl-maven-plugin] No warning / error on wrong options

Posted by ra...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

radu pushed a commit to branch issue/SLING-6779
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-scripting-sightly-compiler.git

commit 9b790d2e1bf81d1dcaf3cef60299fc080654aa55
Author: Radu Cotescu <ra...@apache.org>
AuthorDate: Thu May 23 21:23:40 2019 +0200

    SLING-6779 - [htl-maven-plugin] No warning / error on wrong options
    
    * started adapting code to report unknown options
---
 .../sightly/impl/filter/AbstractFilter.java        | 22 ++++++++++++++++++++-
 .../scripting/sightly/impl/filter/Filter.java      | 12 +++++++++++
 .../sightly/impl/filter/FormatFilter.java          | 23 ++++++++++++++++++++--
 .../scripting/sightly/impl/filter/I18nFilter.java  | 20 +++++++++++--------
 4 files changed, 66 insertions(+), 11 deletions(-)

diff --git a/src/main/java/org/apache/sling/scripting/sightly/impl/filter/AbstractFilter.java b/src/main/java/org/apache/sling/scripting/sightly/impl/filter/AbstractFilter.java
index f72ded7..9f2f931 100644
--- a/src/main/java/org/apache/sling/scripting/sightly/impl/filter/AbstractFilter.java
+++ b/src/main/java/org/apache/sling/scripting/sightly/impl/filter/AbstractFilter.java
@@ -20,6 +20,9 @@ package org.apache.sling.scripting.sightly.impl.filter;
 
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Set;
+import java.util.function.Function;
+import java.util.function.Supplier;
 
 import org.apache.sling.scripting.sightly.compiler.expression.Expression;
 import org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode;
@@ -43,6 +46,21 @@ public abstract class AbstractFilter implements Filter {
         return 1;
     }
 
+    @Override
+    public Expression apply(Expression expression, ExpressionContext expressionContext) {
+        boolean filter = false;
+        for (String option : getOptions()) {
+            if (expression.containsOption(option)) {
+                filter = true;
+                break;
+            }
+        }
+        if (filter) {
+            return apply(expression, getFilterOptions(expression, getOptions()));
+        }
+        return expression;
+    }
+
     /**
      * Collects the options passed in the {@code options} array into a new map while removing them from the original expression.
      *
@@ -50,7 +68,7 @@ public abstract class AbstractFilter implements Filter {
      * @param options    the options of interest for the {@link Filter}
      * @return a map with the retrieved options; the map can be empty if none of the options were found
      */
-    protected Map<String, ExpressionNode> getFilterOptions(Expression expression, String... options) {
+    protected Map<String, ExpressionNode> getFilterOptions(Expression expression, Set<String> options) {
         Map<String, ExpressionNode> collector = new HashMap<>();
         for (String option : options) {
             ExpressionNode optionNode = expression.removeOption(option);
@@ -61,6 +79,8 @@ public abstract class AbstractFilter implements Filter {
         return collector;
     }
 
+    protected abstract Expression apply(Expression expression, Map<String, ExpressionNode> options);
+
     @Override
     public int hashCode() {
         return super.hashCode();
diff --git a/src/main/java/org/apache/sling/scripting/sightly/impl/filter/Filter.java b/src/main/java/org/apache/sling/scripting/sightly/impl/filter/Filter.java
index be239ef..44d7288 100644
--- a/src/main/java/org/apache/sling/scripting/sightly/impl/filter/Filter.java
+++ b/src/main/java/org/apache/sling/scripting/sightly/impl/filter/Filter.java
@@ -18,6 +18,9 @@
  ******************************************************************************/
 package org.apache.sling.scripting.sightly.impl.filter;
 
+import java.util.Collections;
+import java.util.Set;
+
 import org.apache.sling.scripting.sightly.compiler.expression.Expression;
 
 /**
@@ -45,4 +48,13 @@ public interface Filter extends Comparable<Filter> {
      */
     int priority();
 
+    /**
+     * Provides the option names this {@code Filter} will process.
+     *
+     * @return a set of option names
+     */
+    default Set<String> getOptions() {
+        return Collections.emptySet();
+    }
+
 }
diff --git a/src/main/java/org/apache/sling/scripting/sightly/impl/filter/FormatFilter.java b/src/main/java/org/apache/sling/scripting/sightly/impl/filter/FormatFilter.java
index 1962dc1..0af52dd 100644
--- a/src/main/java/org/apache/sling/scripting/sightly/impl/filter/FormatFilter.java
+++ b/src/main/java/org/apache/sling/scripting/sightly/impl/filter/FormatFilter.java
@@ -18,6 +18,11 @@
  ******************************************************************************/
 package org.apache.sling.scripting.sightly.impl.filter;
 
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Optional;
+import java.util.Set;
+
 import org.apache.sling.scripting.sightly.compiler.expression.Expression;
 import org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode;
 import org.apache.sling.scripting.sightly.compiler.expression.nodes.MapLiteral;
@@ -33,6 +38,9 @@ public class FormatFilter extends AbstractFilter {
     public static final String FORMAT_LOCALE_OPTION = "formatLocale";
     public static final String TIMEZONE_OPTION = "timezone";
 
+    private static final Set<String> OPTIONS =
+            new HashSet<>(Arrays.asList(FORMAT_OPTION, TYPE_OPTION, FORMAT_LOCALE_OPTION, TIMEZONE_OPTION));
+
     private static final class FormatFilterLoader {
         private static final FormatFilter INSTANCE = new FormatFilter();
     }
@@ -48,8 +56,14 @@ public class FormatFilter extends AbstractFilter {
     public Expression apply(Expression expression, ExpressionContext expressionContext) {
         //todo: if the expression is a string constant, we can produce the transformation at
         //compile time, with no need of a runtime function
-        if (!expression.containsOption(FORMAT_OPTION) || expressionContext == ExpressionContext.PLUGIN_DATA_SLY_USE || expressionContext
-                == ExpressionContext.PLUGIN_DATA_SLY_TEMPLATE || expressionContext == ExpressionContext.PLUGIN_DATA_SLY_CALL) {
+        boolean filter = false;
+        for (String option : OPTIONS) {
+            if (expression.containsOption(option)) {
+                filter = true;
+                break;
+            }
+        }
+        if (!filter) {
             return expression;
         }
         ExpressionNode translation =
@@ -62,4 +76,9 @@ public class FormatFilter extends AbstractFilter {
                                 TIMEZONE_OPTION)));
         return expression.withNode(translation);
     }
+
+    @Override
+    public Set<String> getOptions() {
+        return OPTIONS;
+    }
 }
diff --git a/src/main/java/org/apache/sling/scripting/sightly/impl/filter/I18nFilter.java b/src/main/java/org/apache/sling/scripting/sightly/impl/filter/I18nFilter.java
index 5275b13..16b9228 100644
--- a/src/main/java/org/apache/sling/scripting/sightly/impl/filter/I18nFilter.java
+++ b/src/main/java/org/apache/sling/scripting/sightly/impl/filter/I18nFilter.java
@@ -18,7 +18,10 @@
  ******************************************************************************/
 package org.apache.sling.scripting.sightly.impl.filter;
 
+import java.util.Arrays;
+import java.util.HashSet;
 import java.util.Map;
+import java.util.Set;
 
 import org.apache.sling.scripting.sightly.compiler.expression.Expression;
 import org.apache.sling.scripting.sightly.compiler.expression.ExpressionNode;
@@ -35,6 +38,8 @@ public final class I18nFilter extends AbstractFilter {
     public static final String LOCALE_OPTION = "locale";
     public static final String BASENAME_OPTION = "basename";
 
+    private static final Set<String> OPTIONS = new HashSet<>(Arrays.asList(I18N_OPTION, HINT_OPTION, LOCALE_OPTION, BASENAME_OPTION));
+
     private static final class I18nFilterLoader {
         private static final I18nFilter INSTANCE = new I18nFilter();
     }
@@ -51,16 +56,15 @@ public final class I18nFilter extends AbstractFilter {
     }
 
     @Override
-    public Expression apply(Expression expression, ExpressionContext expressionContext) {
-        if (!expression.containsOption(I18N_OPTION) || expressionContext == ExpressionContext.PLUGIN_DATA_SLY_USE || expressionContext
-                == ExpressionContext.PLUGIN_DATA_SLY_TEMPLATE || expressionContext == ExpressionContext.PLUGIN_DATA_SLY_CALL) {
-            return expression;
-        }
-        Map <String, ExpressionNode> options = getFilterOptions(expression, HINT_OPTION, LOCALE_OPTION, BASENAME_OPTION);
-        ExpressionNode translation = new RuntimeCall(RuntimeCall.I18N, expression.getRoot(), new MapLiteral
-                (options));
+    protected Expression apply(Expression expression, Map<String, ExpressionNode> options) {
+        ExpressionNode translation = new RuntimeCall(RuntimeCall.I18N, expression.getRoot(), new MapLiteral(options));
         expression.removeOption(I18N_OPTION);
         expression.getOptions().put(FormatFilter.FORMAT_LOCALE_OPTION, options.get(LOCALE_OPTION));
         return expression.withNode(translation);
     }
+
+    @Override
+    public Set<String> getOptions() {
+        return OPTIONS;
+    }
 }