Keep as close to the implementations in thymeleaf, as possible
[juplo-dialect] / src / main / java / de / juplo / thymeleaf / InactiveAttrProcessor.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  * Replaces the element by the tag <code>&lt;span&gt;</code>, if it is
16  * marked as inactive.
17  * @author Kai Moritz
18  */
19 public class InactiveAttrProcessor extends AbstractAttrProcessor
20 {
21   public static final int ATTR_PRECEDENCE = 100;
22
23
24   public InactiveAttrProcessor()
25   {
26     super("inactive");
27   }
28
29
30   @Override
31   protected ProcessorResult processAttribute(
32       Arguments arguments,
33       Element element,
34       String name
35       )
36   {
37     Configuration configuration = arguments.getConfiguration();
38     IStandardExpressionParser parser =
39         StandardExpressions.getExpressionParser(configuration);
40     String value = element.getAttributeValue(name);
41     IStandardExpression expression =
42         parser.parseExpression(configuration, arguments, value);
43     element.removeAttribute(name);
44     if ((Boolean)expression.execute(configuration, arguments))
45     {
46       // We must not clone the processors, because we remove attributes
47       Element strong =
48           element.cloneElementNodeWithNewName(element, "span", false);
49       strong.removeAttribute("charset");
50       strong.removeAttribute("th:charset");
51       strong.removeAttribute("coords");
52       strong.removeAttribute("href");
53       strong.removeAttribute("th:href");
54       strong.removeAttribute("hreflang");
55       strong.removeAttribute("th:hreflang");
56       strong.removeAttribute("media");
57       strong.removeAttribute("th:media");
58       strong.removeAttribute("name");
59       strong.removeAttribute("th:name");
60       strong.removeAttribute("rel");
61       strong.removeAttribute("th:rel");
62       strong.removeAttribute("ref");
63       strong.removeAttribute("th:ref");
64       strong.removeAttribute("shape");
65       strong.removeAttribute("target");
66       strong.removeAttribute("th:target");
67       strong.removeAttribute("type");
68       strong.removeAttribute("th:type");
69       element.clearChildren();
70       element.addChild(strong);
71       element.getParent().extractChild(element);
72     }
73     return ProcessorResult.OK;
74   }
75
76   @Override
77   public int getPrecedence()
78   {
79     return ATTR_PRECEDENCE;
80   }
81 }