X-Git-Url: https://juplo.de/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fthymeleaf%2FActiveAttrProcessor.java;fp=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fthymeleaf%2FActiveAttrProcessor.java;h=7dbe0cdcd1457725c5f9cdb5343f4632b8abfe50;hb=815df4f032e0cfdef9857f711420c772f9e2c0d5;hp=0000000000000000000000000000000000000000;hpb=8f5433b0625ba08b380ba757ee6b090945f8cf2c;p=juplo-dialect diff --git a/src/main/java/de/juplo/thymeleaf/ActiveAttrProcessor.java b/src/main/java/de/juplo/thymeleaf/ActiveAttrProcessor.java new file mode 100644 index 0000000..7dbe0cd --- /dev/null +++ b/src/main/java/de/juplo/thymeleaf/ActiveAttrProcessor.java @@ -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 <strong>, 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; + } +}