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