1 package de.juplo.thymeleaf;
4 import org.thymeleaf.Arguments;
5 import org.thymeleaf.Configuration;
6 import org.thymeleaf.dom.Element;
7 import org.thymeleaf.processor.ProcessorResult;
8 import org.thymeleaf.processor.attr.AbstractAttrProcessor;
9 import org.thymeleaf.standard.expression.IStandardExpression;
10 import org.thymeleaf.standard.expression.IStandardExpressionParser;
11 import org.thymeleaf.standard.expression.StandardExpressions;
15 * Subsitutes the element, that is marked with this attribute-processor.
19 public abstract class AbstractSubstituteAttrProcessor extends AbstractAttrProcessor
21 public static final int ATTR_PRECEDENCE = 100;
23 private final String substituteName;
26 public AbstractSubstituteAttrProcessor(String attribute, String substitute)
29 this.substituteName = substitute;
34 protected final ProcessorResult processAttribute(
40 Configuration configuration = arguments.getConfiguration();
41 IStandardExpressionParser parser =
42 StandardExpressions.getExpressionParser(configuration);
43 String value = element.getAttributeValue(name);
44 IStandardExpression expression =
45 parser.parseExpression(configuration, arguments, value);
46 element.removeAttribute(name);
47 if ((Boolean)expression.execute(configuration, arguments))
49 // We must not clone the processors, because we remove attributes
50 Element substituteElement =
51 element.cloneElementNodeWithNewName(element, substituteName, false);
52 substituteElement.removeAttribute("charset");
53 substituteElement.removeAttribute("th:charset");
54 substituteElement.removeAttribute("coords");
55 substituteElement.removeAttribute("href");
56 substituteElement.removeAttribute("th:href");
57 substituteElement.removeAttribute("hreflang");
58 substituteElement.removeAttribute("th:hreflang");
59 substituteElement.removeAttribute("media");
60 substituteElement.removeAttribute("th:media");
61 substituteElement.removeAttribute("name");
62 substituteElement.removeAttribute("th:name");
63 substituteElement.removeAttribute("rel");
64 substituteElement.removeAttribute("th:rel");
65 substituteElement.removeAttribute("ref");
66 substituteElement.removeAttribute("th:ref");
67 substituteElement.removeAttribute("shape");
68 substituteElement.removeAttribute("target");
69 substituteElement.removeAttribute("th:target");
70 substituteElement.removeAttribute("type");
71 substituteElement.removeAttribute("th:type");
72 element.clearChildren();
73 element.addChild(substituteElement);
74 element.getParent().extractChild(element);
76 return ProcessorResult.OK;
80 public final int getPrecedence()
82 return ATTR_PRECEDENCE;