Refactored ActiveAttrProcessor and InactiveAttrProcessor because of DRY
authorKai Moritz <kai@juplo.de>
Wed, 17 Feb 2016 10:36:41 +0000 (11:36 +0100)
committerKai Moritz <kai@juplo.de>
Mon, 18 Jul 2016 14:53:55 +0000 (16:53 +0200)
src/main/java/de/juplo/thymeleaf/AbstractSubstituteAttrProcessor.java [new file with mode: 0644]
src/main/java/de/juplo/thymeleaf/ActiveAttrProcessor.java
src/main/java/de/juplo/thymeleaf/InactiveAttrProcessor.java

diff --git a/src/main/java/de/juplo/thymeleaf/AbstractSubstituteAttrProcessor.java b/src/main/java/de/juplo/thymeleaf/AbstractSubstituteAttrProcessor.java
new file mode 100644 (file)
index 0000000..0348aea
--- /dev/null
@@ -0,0 +1,84 @@
+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;
+
+
+/**
+ * Subsitutes the element, that is marked with this attribute-processor.
+ *
+ * @author kai
+ */
+public abstract class AbstractSubstituteAttrProcessor extends AbstractAttrProcessor
+{
+  public static final int ATTR_PRECEDENCE = 100;
+
+  private final String substituteName;
+
+
+  public AbstractSubstituteAttrProcessor(String attribute, String substitute)
+  {
+    super(attribute);
+    this.substituteName = substitute;
+  }
+
+
+  @Override
+  protected final 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 substituteElement =
+          element.cloneElementNodeWithNewName(element, substituteName, false);
+      substituteElement.removeAttribute("charset");
+      substituteElement.removeAttribute("th:charset");
+      substituteElement.removeAttribute("coords");
+      substituteElement.removeAttribute("href");
+      substituteElement.removeAttribute("th:href");
+      substituteElement.removeAttribute("hreflang");
+      substituteElement.removeAttribute("th:hreflang");
+      substituteElement.removeAttribute("media");
+      substituteElement.removeAttribute("th:media");
+      substituteElement.removeAttribute("name");
+      substituteElement.removeAttribute("th:name");
+      substituteElement.removeAttribute("rel");
+      substituteElement.removeAttribute("th:rel");
+      substituteElement.removeAttribute("ref");
+      substituteElement.removeAttribute("th:ref");
+      substituteElement.removeAttribute("shape");
+      substituteElement.removeAttribute("target");
+      substituteElement.removeAttribute("th:target");
+      substituteElement.removeAttribute("type");
+      substituteElement.removeAttribute("th:type");
+      element.clearChildren();
+      element.addChild(substituteElement);
+      element.getParent().extractChild(element);
+    }
+    return ProcessorResult.OK;
+  }
+
+  @Override
+  public final int getPrecedence()
+  {
+    return ATTR_PRECEDENCE;
+  }
+}
index 7dbe0cd..ef17d00 100644 (file)
@@ -1,81 +1,15 @@
 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 class ActiveAttrProcessor extends AbstractSubstituteAttrProcessor
 {
-  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;
+    super("active", "strong");
   }
 }
index 30db2d5..f04074c 100644 (file)
@@ -1,81 +1,15 @@
 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 class InactiveAttrProcessor extends AbstractSubstituteAttrProcessor
 {
-  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;
+    super("inactive", "span");
   }
 }