WIP:Ported app to Thymeleaf 3.0.x
authorKai Moritz <kai@juplo.de>
Sat, 15 Feb 2020 16:51:41 +0000 (17:51 +0100)
committerKai Moritz <kai@juplo.de>
Sat, 15 Feb 2020 16:51:41 +0000 (17:51 +0100)
src/main/java/de/juplo/thymeleaf/AbstractSubstituteAttrProcessor.java [deleted file]
src/main/java/de/juplo/thymeleaf/AbstractSubstituteAttributeProcessor.java [new file with mode: 0644]
src/main/java/de/juplo/thymeleaf/ActiveAttrProcessor.java [deleted file]
src/main/java/de/juplo/thymeleaf/ActiveAttributeProcessor.java [new file with mode: 0644]
src/main/java/de/juplo/thymeleaf/InactiveAttrProcessor.java [deleted file]
src/main/java/de/juplo/thymeleaf/InactiveAttributeProcessor.java [new file with mode: 0644]
src/main/java/de/juplo/thymeleaf/JuploDialect.java

diff --git a/src/main/java/de/juplo/thymeleaf/AbstractSubstituteAttrProcessor.java b/src/main/java/de/juplo/thymeleaf/AbstractSubstituteAttrProcessor.java
deleted file mode 100644 (file)
index 6274016..0000000
+++ /dev/null
@@ -1,89 +0,0 @@
-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);
-      // Remove attributes, that are not allowed for <span> or <strong>
-      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");
-      // Also remove the title-attribute, because the mouse-over is confusing
-      substituteElement.removeAttribute("title");
-      substituteElement.removeAttribute("th:title");
-      // Replace the element
-      element.clearChildren();
-      element.addChild(substituteElement);
-      element.getParent().extractChild(element);
-    }
-    return ProcessorResult.OK;
-  }
-
-  @Override
-  public final int getPrecedence()
-  {
-    return ATTR_PRECEDENCE;
-  }
-}
diff --git a/src/main/java/de/juplo/thymeleaf/AbstractSubstituteAttributeProcessor.java b/src/main/java/de/juplo/thymeleaf/AbstractSubstituteAttributeProcessor.java
new file mode 100644 (file)
index 0000000..e10365b
--- /dev/null
@@ -0,0 +1,123 @@
+package de.juplo.thymeleaf;
+
+
+import org.thymeleaf.context.ITemplateContext;
+import org.thymeleaf.engine.AttributeName;
+import org.thymeleaf.engine.EngineEventUtils;
+import org.thymeleaf.exceptions.TemplateProcessingException;
+import org.thymeleaf.model.IProcessableElementTag;
+import org.thymeleaf.processor.element.AbstractAttributeTagProcessor;
+import org.thymeleaf.processor.element.IElementTagStructureHandler;
+import org.thymeleaf.standard.expression.FragmentExpression;
+import org.thymeleaf.standard.expression.FragmentExpression.ExecutedFragmentExpression;
+import org.thymeleaf.standard.expression.IStandardExpression;
+import org.thymeleaf.standard.expression.IStandardExpressionParser;
+import org.thymeleaf.standard.expression.NoOpToken;
+import org.thymeleaf.standard.expression.StandardExpressionExecutionContext;
+import org.thymeleaf.standard.expression.StandardExpressions;
+import org.thymeleaf.templatemode.TemplateMode;
+
+
+/**
+ * Subsitutes the element, that is marked with this attribute-processor.
+ *
+ * @author kai
+ */
+public abstract class AbstractSubstituteAttributeProcessor extends AbstractAttributeTagProcessor
+{
+  public static final int ATTR_PRECEDENCE = 100;
+
+  private final String substituteName;
+
+
+  public AbstractSubstituteAttributeProcessor(String prefix, String attribute, String substitute)
+  {
+    super(TemplateMode.HTML, prefix, null, false, attribute, true, ATTR_PRECEDENCE, true);
+    this.substituteName = substitute;
+  }
+
+
+  @Override
+  protected void doProcess(
+      final ITemplateContext context,
+      final IProcessableElementTag element,
+      final AttributeName name,
+      final String attribute,
+      final IElementTagStructureHandler handler
+      )
+  {
+    if (attribute == null)
+      return;
+
+    final Object result;
+    final IStandardExpression expression =
+       EngineEventUtils.computeAttributeExpression(
+           context,
+           element,
+           name,
+           attribute
+           );
+
+    if (expression != null && expression instanceof FragmentExpression)
+    {
+      final ExecutedFragmentExpression executedFragmentExpression =
+          FragmentExpression.createExecutedFragmentExpression(
+              context,
+              (FragmentExpression) expression,
+              StandardExpressionExecutionContext.NORMAL
+              );
+      result =
+          FragmentExpression.resolveExecutedFragmentExpression(
+              context,
+              executedFragmentExpression,
+              true
+              );
+    }
+    else
+    {
+      result = expression.execute(context);
+    }
+
+    handler.removeAttribute(name);
+
+    // If the result of this expression is NO-OP, there is nothing to execute
+    if (result == NoOpToken.VALUE)
+      return;
+
+    if ((Boolean)result)
+    {
+      // We must not clone the processors, because we remove attributes
+      Element substituteElement =
+          element.cloneElementNodeWithNewName(element, substituteName, false);
+      // Remove attributes, that are not allowed for <span> or <strong>
+      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");
+      // Also remove the title-attribute, because the mouse-over is confusing
+      substituteElement.removeAttribute("title");
+      substituteElement.removeAttribute("th:title");
+      // Replace the element
+      element.clearChildren();
+      element.addChild(substituteElement);
+      element.getParent().extractChild(element);
+    }
+    return ProcessorResult.OK;
+  }
+}
diff --git a/src/main/java/de/juplo/thymeleaf/ActiveAttrProcessor.java b/src/main/java/de/juplo/thymeleaf/ActiveAttrProcessor.java
deleted file mode 100644 (file)
index ef17d00..0000000
+++ /dev/null
@@ -1,15 +0,0 @@
-package de.juplo.thymeleaf;
-
-
-/**
- * Replaces the element by the tag <code>&lt;strong&gt;</code>, if it is
- * marked as active.
- * @author Kai Moritz
- */
-public class ActiveAttrProcessor extends AbstractSubstituteAttrProcessor
-{
-  public ActiveAttrProcessor()
-  {
-    super("active", "strong");
-  }
-}
diff --git a/src/main/java/de/juplo/thymeleaf/ActiveAttributeProcessor.java b/src/main/java/de/juplo/thymeleaf/ActiveAttributeProcessor.java
new file mode 100644 (file)
index 0000000..193a8e7
--- /dev/null
@@ -0,0 +1,15 @@
+package de.juplo.thymeleaf;
+
+
+/**
+ * Replaces the element by the tag <code>&lt;strong&gt;</code>, if it is
+ * marked as active.
+ * @author Kai Moritz
+ */
+public class ActiveAttributeProcessor extends AbstractSubstituteAttributeProcessor
+{
+  public ActiveAttributeProcessor(String prefix)
+  {
+    super(prefix, "active", "strong");
+  }
+}
diff --git a/src/main/java/de/juplo/thymeleaf/InactiveAttrProcessor.java b/src/main/java/de/juplo/thymeleaf/InactiveAttrProcessor.java
deleted file mode 100644 (file)
index f04074c..0000000
+++ /dev/null
@@ -1,15 +0,0 @@
-package de.juplo.thymeleaf;
-
-
-/**
- * Replaces the element by the tag <code>&lt;span&gt;</code>, if it is
- * marked as inactive.
- * @author Kai Moritz
- */
-public class InactiveAttrProcessor extends AbstractSubstituteAttrProcessor
-{
-  public InactiveAttrProcessor()
-  {
-    super("inactive", "span");
-  }
-}
diff --git a/src/main/java/de/juplo/thymeleaf/InactiveAttributeProcessor.java b/src/main/java/de/juplo/thymeleaf/InactiveAttributeProcessor.java
new file mode 100644 (file)
index 0000000..c212aa0
--- /dev/null
@@ -0,0 +1,15 @@
+package de.juplo.thymeleaf;
+
+
+/**
+ * Replaces the element by the tag <code>&lt;span&gt;</code>, if it is
+ * marked as inactive.
+ * @author Kai Moritz
+ */
+public class InactiveAttributeProcessor extends AbstractSubstituteAttributeProcessor
+{
+  public InactiveAttributeProcessor(String prefix)
+  {
+    super(prefix, "inactive", "span");
+  }
+}
index f6e2e96..15810a6 100644 (file)
@@ -39,8 +39,8 @@ public class JuploDialect implements IProcessorDialect
   public Set<IProcessor> getProcessors(final String prefix)
   {
     final Set<IProcessor> processors = new HashSet<>();
-    processors.add(new ActiveAttrProcessor());
-    processors.add(new InactiveAttrProcessor());
+    processors.add(new ActiveAttributeProcessor(prefix));
+    processors.add(new InactiveAttributeProcessor(prefix));
     processors.add(new ImportVariablesAttributeProcessor(prefix));
     return processors;
   }