Refactored ActiveAttrProcessor and InactiveAttrProcessor because of DRY
[juplo-dialect] / src / main / java / de / juplo / thymeleaf / AbstractSubstituteAttrProcessor.java
1 package de.juplo.thymeleaf;
2
3
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;
12
13
14 /**
15  * Subsitutes the element, that is marked with this attribute-processor.
16  *
17  * @author kai
18  */
19 public abstract class AbstractSubstituteAttrProcessor extends AbstractAttrProcessor
20 {
21   public static final int ATTR_PRECEDENCE = 100;
22
23   private final String substituteName;
24
25
26   public AbstractSubstituteAttrProcessor(String attribute, String substitute)
27   {
28     super(attribute);
29     this.substituteName = substitute;
30   }
31
32
33   @Override
34   protected final ProcessorResult processAttribute(
35       Arguments arguments,
36       Element element,
37       String name
38       )
39   {
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))
48     {
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);
75     }
76     return ProcessorResult.OK;
77   }
78
79   @Override
80   public final int getPrecedence()
81   {
82     return ATTR_PRECEDENCE;
83   }
84 }