1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.module.xml.config; |
8 | |
|
9 | |
import org.mule.config.spring.parsers.assembly.BeanAssembler; |
10 | |
import org.mule.config.spring.parsers.generic.ChildDefinitionParser; |
11 | |
import org.mule.module.xml.util.XMLUtils; |
12 | |
|
13 | |
import javax.xml.transform.Result; |
14 | |
import javax.xml.transform.Source; |
15 | |
import javax.xml.transform.dom.DOMSource; |
16 | |
import javax.xml.transform.stream.StreamResult; |
17 | |
|
18 | |
import org.apache.commons.io.output.ByteArrayOutputStream; |
19 | |
import org.springframework.beans.factory.xml.ParserContext; |
20 | |
import org.w3c.dom.Element; |
21 | |
import org.w3c.dom.Node; |
22 | |
import org.w3c.dom.NodeList; |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
public class XsltTextDefinitionParser extends ChildDefinitionParser |
28 | |
{ |
29 | |
public static final String STYLESHEET = "stylesheet"; |
30 | |
public static final int UNDEFINED = -1; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
public XsltTextDefinitionParser(String setterMethod) |
38 | |
{ |
39 | 0 | super(setterMethod); |
40 | 0 | } |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
public XsltTextDefinitionParser(String setterMethod, Class clazz) |
47 | |
{ |
48 | 0 | super(setterMethod, clazz); |
49 | 0 | } |
50 | |
|
51 | |
protected void postProcess(ParserContext context, BeanAssembler assembler, Element element) |
52 | |
{ |
53 | 0 | NodeList children = element.getChildNodes(); |
54 | 0 | if (0 != children.getLength()) |
55 | |
{ |
56 | 0 | Element stylesheet = null; |
57 | 0 | for (int i = 0; i < children.getLength(); i++) |
58 | |
{ |
59 | 0 | if (Node.ELEMENT_NODE == children.item(i).getNodeType()) |
60 | |
{ |
61 | 0 | assertArgument(null == stylesheet, "XSLT transformer can have at most one child element"); |
62 | 0 | stylesheet = (Element) children.item(i); |
63 | |
} |
64 | |
} |
65 | 0 | if (null != stylesheet) |
66 | |
{ |
67 | 0 | assertArgument(STYLESHEET.equals(stylesheet.getLocalName()), |
68 | |
"XSLT transformer child element must be named " + STYLESHEET); |
69 | 0 | assembler.extendTarget("xslt", domToString(stylesheet), false); |
70 | |
|
71 | 0 | element.removeChild(stylesheet); |
72 | |
} |
73 | |
} |
74 | 0 | super.postProcess(context, assembler, element); |
75 | 0 | } |
76 | |
|
77 | |
@Override |
78 | |
public String getPropertyName(Element e) |
79 | |
{ |
80 | |
|
81 | 0 | return null; |
82 | |
} |
83 | |
|
84 | |
protected String domToString(Element dom) |
85 | |
{ |
86 | |
try |
87 | |
{ |
88 | |
|
89 | 0 | Source source = new DOMSource(dom); |
90 | 0 | ByteArrayOutputStream output = new ByteArrayOutputStream(); |
91 | 0 | Result result = new StreamResult(output); |
92 | 0 | XMLUtils.getTransformer().transform(source, result); |
93 | 0 | return output.toString(); |
94 | |
} |
95 | 0 | catch (Exception e) |
96 | |
{ |
97 | 0 | throw (IllegalStateException) new IllegalStateException(e.getMessage()).initCause(e); |
98 | |
} |
99 | |
} |
100 | |
|
101 | |
protected void assertArgument(boolean condition, String message) |
102 | |
{ |
103 | 0 | if (!condition) |
104 | |
{ |
105 | 0 | throw new IllegalArgumentException(message); |
106 | |
} |
107 | 0 | } |
108 | |
} |