Implemented juplo:active, that replaces active <a>-tags by the tag <strong>
authorKai Moritz <kai@juplo.de>
Sun, 10 Jan 2016 17:48:10 +0000 (18:48 +0100)
committerKai Moritz <kai@juplo.de>
Mon, 18 Jul 2016 14:49:48 +0000 (16:49 +0200)
src/main/java/de/juplo/thymeleaf/ActiveElementProcessor.java [new file with mode: 0644]
src/main/java/de/juplo/thymeleaf/JuploDialect.java [new file with mode: 0644]

diff --git a/src/main/java/de/juplo/thymeleaf/ActiveElementProcessor.java b/src/main/java/de/juplo/thymeleaf/ActiveElementProcessor.java
new file mode 100644 (file)
index 0000000..9f714a0
--- /dev/null
@@ -0,0 +1,77 @@
+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))
+        {
+          Element strong =
+              element.cloneElementNodeWithNewName(element, "strong", true);
+          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/JuploDialect.java b/src/main/java/de/juplo/thymeleaf/JuploDialect.java
new file mode 100644 (file)
index 0000000..40f2bb1
--- /dev/null
@@ -0,0 +1,35 @@
+package de.juplo.thymeleaf;
+
+
+import java.util.HashSet;
+import java.util.Set;
+import org.thymeleaf.dialect.AbstractDialect;
+import org.thymeleaf.processor.IProcessor;
+
+
+/**
+ * A collection of usefull tools.
+ * @author Kai Moritz
+ */
+public class JuploDialect extends AbstractDialect
+{
+  public JuploDialect()
+  {
+    super();
+  }
+
+
+  @Override
+  public String getPrefix()
+  {
+    return "juplo";
+  }
+
+  @Override
+  public Set<IProcessor> getProcessors()
+  {
+    final Set<IProcessor> processors = new HashSet<>();
+    processors.add(new ActiveElementProcessor());
+    return processors;
+  }
+}