From d221272c806efad874dafac1a9197f717ce3984f Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Mon, 11 Jan 2016 13:44:09 +0100 Subject: [PATCH] Implemented juplo:inactive, that replaces inactive -tags by the tag --- .../thymeleaf/InactiveElementProcessor.java | 77 +++++++++++++++++++ .../java/de/juplo/thymeleaf/JuploDialect.java | 1 + 2 files changed, 78 insertions(+) create mode 100644 src/main/java/de/juplo/thymeleaf/InactiveElementProcessor.java diff --git a/src/main/java/de/juplo/thymeleaf/InactiveElementProcessor.java b/src/main/java/de/juplo/thymeleaf/InactiveElementProcessor.java new file mode 100644 index 0000000..621a764 --- /dev/null +++ b/src/main/java/de/juplo/thymeleaf/InactiveElementProcessor.java @@ -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 <span>, if it is + * marked as inactive. + * @author Kai Moritz + */ +public class InactiveElementProcessor extends AbstractProcessor +{ + private final AttributeNameProcessorMatcher matcher = + new AttributeNameProcessorMatcher("inactive"); + + + @Override + public IProcessorMatcher 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, "span", 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 index 40f2bb1..0aeb165 100644 --- a/src/main/java/de/juplo/thymeleaf/JuploDialect.java +++ b/src/main/java/de/juplo/thymeleaf/JuploDialect.java @@ -30,6 +30,7 @@ public class JuploDialect extends AbstractDialect { final Set processors = new HashSet<>(); processors.add(new ActiveElementProcessor()); + processors.add(new InactiveElementProcessor()); return processors; } } -- 2.20.1