Keep as close to the implementations in thymeleaf, as possible
authorKai Moritz <kai@juplo.de>
Tue, 12 Jan 2016 08:12:33 +0000 (09:12 +0100)
committerKai Moritz <kai@juplo.de>
Mon, 18 Jul 2016 14:53:49 +0000 (16:53 +0200)
src/main/java/de/juplo/thymeleaf/ActiveAttrProcessor.java [new file with mode: 0644]
src/main/java/de/juplo/thymeleaf/ActiveElementProcessor.java [deleted file]
src/main/java/de/juplo/thymeleaf/InactiveAttrProcessor.java [new file with mode: 0644]
src/main/java/de/juplo/thymeleaf/InactiveElementProcessor.java [deleted file]
src/main/java/de/juplo/thymeleaf/JuploDialect.java

diff --git a/src/main/java/de/juplo/thymeleaf/ActiveAttrProcessor.java b/src/main/java/de/juplo/thymeleaf/ActiveAttrProcessor.java
new file mode 100644 (file)
index 0000000..7dbe0cd
--- /dev/null
@@ -0,0 +1,81 @@
+package de.juplo.thymeleaf;
+
+
+import org.thymeleaf.Arguments;
+import org.thymeleaf.Configuration;
+import org.thymeleaf.dom.Element;
+import org.thymeleaf.processor.ProcessorResult;
+import org.thymeleaf.processor.attr.AbstractAttrProcessor;
+import org.thymeleaf.standard.expression.IStandardExpression;
+import org.thymeleaf.standard.expression.IStandardExpressionParser;
+import org.thymeleaf.standard.expression.StandardExpressions;
+
+
+/**
+ * Replaces the element by the tag <code>&lt;strong&gt;</code>, if it is
+ * marked as active.
+ * @author Kai Moritz
+ */
+public class ActiveAttrProcessor extends AbstractAttrProcessor
+{
+  public static final int ATTR_PRECEDENCE = 100;
+
+
+  public ActiveAttrProcessor()
+  {
+    super("active");
+  }
+
+
+  @Override
+  protected ProcessorResult processAttribute(
+      Arguments arguments,
+      Element element,
+      String name
+      )
+  {
+    Configuration configuration = arguments.getConfiguration();
+    IStandardExpressionParser parser =
+        StandardExpressions.getExpressionParser(configuration);
+    String value = element.getAttributeValue(name);
+    IStandardExpression expression =
+        parser.parseExpression(configuration, arguments, value);
+    element.removeAttribute(name);
+    if ((Boolean)expression.execute(configuration, arguments))
+    {
+      // We must not clone the processors, because we remove attributes
+      Element strong =
+          element.cloneElementNodeWithNewName(element, "strong", false);
+      strong.removeAttribute("charset");
+      strong.removeAttribute("th:charset");
+      strong.removeAttribute("coords");
+      strong.removeAttribute("href");
+      strong.removeAttribute("th:href");
+      strong.removeAttribute("hreflang");
+      strong.removeAttribute("th:hreflang");
+      strong.removeAttribute("media");
+      strong.removeAttribute("th:media");
+      strong.removeAttribute("name");
+      strong.removeAttribute("th:name");
+      strong.removeAttribute("rel");
+      strong.removeAttribute("th:rel");
+      strong.removeAttribute("ref");
+      strong.removeAttribute("th:ref");
+      strong.removeAttribute("shape");
+      strong.removeAttribute("target");
+      strong.removeAttribute("th:target");
+      strong.removeAttribute("type");
+      strong.removeAttribute("th:type");
+      element.clearChildren();
+      element.addChild(strong);
+      element.getParent().extractChild(element);
+    }
+    return ProcessorResult.OK;
+  }
+
+  @Override
+  public int getPrecedence()
+  {
+    return ATTR_PRECEDENCE;
+  }
+}
diff --git a/src/main/java/de/juplo/thymeleaf/ActiveElementProcessor.java b/src/main/java/de/juplo/thymeleaf/ActiveElementProcessor.java
deleted file mode 100644 (file)
index 46725e1..0000000
+++ /dev/null
@@ -1,98 +0,0 @@
-package de.juplo.thymeleaf;
-
-
-import org.thymeleaf.Arguments;
-import org.thymeleaf.Configuration;
-import org.thymeleaf.dom.Element;
-import org.thymeleaf.dom.Node;
-import org.thymeleaf.processor.AbstractProcessor;
-import org.thymeleaf.processor.IProcessorMatcher;
-import org.thymeleaf.processor.ProcessorMatchingContext;
-import org.thymeleaf.processor.ProcessorResult;
-import org.thymeleaf.processor.AttributeNameProcessorMatcher;
-import org.thymeleaf.standard.expression.IStandardExpression;
-import org.thymeleaf.standard.expression.IStandardExpressionParser;
-import org.thymeleaf.standard.expression.StandardExpressions;
-
-
-/**
- * Replaces the element by the tag <code>&lt;strong&gt;</code>, if it is
- * marked as active.
- * @author Kai Moritz
- */
-public class ActiveElementProcessor extends AbstractProcessor
-{
-  private final AttributeNameProcessorMatcher matcher =
-      new AttributeNameProcessorMatcher("active");
-
-
-  @Override
-  public IProcessorMatcher<? extends Node> getMatcher()
-  {
-    return matcher;
-  }
-
-  @Override
-  protected ProcessorResult doProcess(
-      Arguments arguments,
-      ProcessorMatchingContext context,
-      Node node
-      )
-  {
-    // Because of the type of applicability being used, casts to Element here will not fail
-    final Element element = (Element) node;
-    final String[] names = this.matcher.getAttributeNames(context);
-
-    for (final String name : names)
-    {
-      if (element.hasNormalizedAttribute(name))
-      {
-        Configuration configuration = arguments.getConfiguration();
-        IStandardExpressionParser parser =
-            StandardExpressions.getExpressionParser(configuration);
-        String value = element.getAttributeValue(name);
-        IStandardExpression expression =
-            parser.parseExpression(configuration, arguments, value);
-        element.removeAttribute(name);
-        if ((Boolean)expression.execute(configuration, arguments))
-        {
-          // We must not clone the processors, because we remove attributes
-          Element strong =
-              element.cloneElementNodeWithNewName(element, "strong", false);
-          strong.removeAttribute("charset");
-          strong.removeAttribute("th:charset");
-          strong.removeAttribute("coords");
-          strong.removeAttribute("href");
-          strong.removeAttribute("th:href");
-          strong.removeAttribute("hreflang");
-          strong.removeAttribute("th:hreflang");
-          strong.removeAttribute("media");
-          strong.removeAttribute("th:media");
-          strong.removeAttribute("name");
-          strong.removeAttribute("th:name");
-          strong.removeAttribute("rel");
-          strong.removeAttribute("th:rel");
-          strong.removeAttribute("ref");
-          strong.removeAttribute("th:ref");
-          strong.removeAttribute("shape");
-          strong.removeAttribute("target");
-          strong.removeAttribute("th:target");
-          strong.removeAttribute("type");
-          strong.removeAttribute("th:type");
-          element.clearChildren();
-          element.addChild(strong);
-          element.getParent().extractChild(element);
-        }
-        break;
-      }
-    }
-    return ProcessorResult.OK;
-  }
-
-  @Override
-  public int getPrecedence()
-  {
-    // Be sure to be executed first
-    return 0;
-  }
-}
diff --git a/src/main/java/de/juplo/thymeleaf/InactiveAttrProcessor.java b/src/main/java/de/juplo/thymeleaf/InactiveAttrProcessor.java
new file mode 100644 (file)
index 0000000..30db2d5
--- /dev/null
@@ -0,0 +1,81 @@
+package de.juplo.thymeleaf;
+
+
+import org.thymeleaf.Arguments;
+import org.thymeleaf.Configuration;
+import org.thymeleaf.dom.Element;
+import org.thymeleaf.processor.ProcessorResult;
+import org.thymeleaf.processor.attr.AbstractAttrProcessor;
+import org.thymeleaf.standard.expression.IStandardExpression;
+import org.thymeleaf.standard.expression.IStandardExpressionParser;
+import org.thymeleaf.standard.expression.StandardExpressions;
+
+
+/**
+ * Replaces the element by the tag <code>&lt;span&gt;</code>, if it is
+ * marked as inactive.
+ * @author Kai Moritz
+ */
+public class InactiveAttrProcessor extends AbstractAttrProcessor
+{
+  public static final int ATTR_PRECEDENCE = 100;
+
+
+  public InactiveAttrProcessor()
+  {
+    super("inactive");
+  }
+
+
+  @Override
+  protected ProcessorResult processAttribute(
+      Arguments arguments,
+      Element element,
+      String name
+      )
+  {
+    Configuration configuration = arguments.getConfiguration();
+    IStandardExpressionParser parser =
+        StandardExpressions.getExpressionParser(configuration);
+    String value = element.getAttributeValue(name);
+    IStandardExpression expression =
+        parser.parseExpression(configuration, arguments, value);
+    element.removeAttribute(name);
+    if ((Boolean)expression.execute(configuration, arguments))
+    {
+      // We must not clone the processors, because we remove attributes
+      Element strong =
+          element.cloneElementNodeWithNewName(element, "span", false);
+      strong.removeAttribute("charset");
+      strong.removeAttribute("th:charset");
+      strong.removeAttribute("coords");
+      strong.removeAttribute("href");
+      strong.removeAttribute("th:href");
+      strong.removeAttribute("hreflang");
+      strong.removeAttribute("th:hreflang");
+      strong.removeAttribute("media");
+      strong.removeAttribute("th:media");
+      strong.removeAttribute("name");
+      strong.removeAttribute("th:name");
+      strong.removeAttribute("rel");
+      strong.removeAttribute("th:rel");
+      strong.removeAttribute("ref");
+      strong.removeAttribute("th:ref");
+      strong.removeAttribute("shape");
+      strong.removeAttribute("target");
+      strong.removeAttribute("th:target");
+      strong.removeAttribute("type");
+      strong.removeAttribute("th:type");
+      element.clearChildren();
+      element.addChild(strong);
+      element.getParent().extractChild(element);
+    }
+    return ProcessorResult.OK;
+  }
+
+  @Override
+  public int getPrecedence()
+  {
+    return ATTR_PRECEDENCE;
+  }
+}
diff --git a/src/main/java/de/juplo/thymeleaf/InactiveElementProcessor.java b/src/main/java/de/juplo/thymeleaf/InactiveElementProcessor.java
deleted file mode 100644 (file)
index cd05854..0000000
+++ /dev/null
@@ -1,98 +0,0 @@
-package de.juplo.thymeleaf;
-
-
-import org.thymeleaf.Arguments;
-import org.thymeleaf.Configuration;
-import org.thymeleaf.dom.Element;
-import org.thymeleaf.dom.Node;
-import org.thymeleaf.processor.AbstractProcessor;
-import org.thymeleaf.processor.IProcessorMatcher;
-import org.thymeleaf.processor.ProcessorMatchingContext;
-import org.thymeleaf.processor.ProcessorResult;
-import org.thymeleaf.processor.AttributeNameProcessorMatcher;
-import org.thymeleaf.standard.expression.IStandardExpression;
-import org.thymeleaf.standard.expression.IStandardExpressionParser;
-import org.thymeleaf.standard.expression.StandardExpressions;
-
-
-/**
- * Replaces the element by the tag <code>&lt;span&gt;</code>, if it is
- * marked as inactive.
- * @author Kai Moritz
- */
-public class InactiveElementProcessor extends AbstractProcessor
-{
-  private final AttributeNameProcessorMatcher matcher =
-      new AttributeNameProcessorMatcher("inactive");
-
-
-  @Override
-  public IProcessorMatcher<? extends Node> getMatcher()
-  {
-    return matcher;
-  }
-
-  @Override
-  protected ProcessorResult doProcess(
-      Arguments arguments,
-      ProcessorMatchingContext context,
-      Node node
-      )
-  {
-    // Because of the type of applicability being used, casts to Element here will not fail
-    final Element element = (Element) node;
-    final String[] names = this.matcher.getAttributeNames(context);
-
-    for (final String name : names)
-    {
-      if (element.hasNormalizedAttribute(name))
-      {
-        Configuration configuration = arguments.getConfiguration();
-        IStandardExpressionParser parser =
-            StandardExpressions.getExpressionParser(configuration);
-        String value = element.getAttributeValue(name);
-        IStandardExpression expression =
-            parser.parseExpression(configuration, arguments, value);
-        element.removeAttribute(name);
-        if ((Boolean)expression.execute(configuration, arguments))
-        {
-          // We must not clone the processors, because we remove attributes
-          Element strong =
-              element.cloneElementNodeWithNewName(element, "span", false);
-          strong.removeAttribute("charset");
-          strong.removeAttribute("th:charset");
-          strong.removeAttribute("coords");
-          strong.removeAttribute("href");
-          strong.removeAttribute("th:href");
-          strong.removeAttribute("hreflang");
-          strong.removeAttribute("th:hreflang");
-          strong.removeAttribute("media");
-          strong.removeAttribute("th:media");
-          strong.removeAttribute("name");
-          strong.removeAttribute("th:name");
-          strong.removeAttribute("rel");
-          strong.removeAttribute("th:rel");
-          strong.removeAttribute("ref");
-          strong.removeAttribute("th:ref");
-          strong.removeAttribute("shape");
-          strong.removeAttribute("target");
-          strong.removeAttribute("th:target");
-          strong.removeAttribute("type");
-          strong.removeAttribute("th:type");
-          element.clearChildren();
-          element.addChild(strong);
-          element.getParent().extractChild(element);
-        }
-        break;
-      }
-    }
-    return ProcessorResult.OK;
-  }
-
-  @Override
-  public int getPrecedence()
-  {
-    // Be sure to be executed first
-    return 0;
-  }
-}
index 0aeb165..599791b 100644 (file)
@@ -29,8 +29,8 @@ public class JuploDialect extends AbstractDialect
   public Set<IProcessor> getProcessors()
   {
     final Set<IProcessor> processors = new HashSet<>();
-    processors.add(new ActiveElementProcessor());
-    processors.add(new InactiveElementProcessor());
+    processors.add(new ActiveAttrProcessor());
+    processors.add(new InactiveAttrProcessor());
     return processors;
   }
 }