1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.module.xml.transformer; |
12 | |
|
13 | |
import org.mule.api.lifecycle.InitialisationException; |
14 | |
import org.mule.api.transformer.TransformerException; |
15 | |
import org.mule.config.i18n.MessageFactory; |
16 | |
import org.mule.transformer.AbstractTransformer; |
17 | |
import org.mule.transformer.types.DataTypeFactory; |
18 | |
|
19 | |
import java.util.Arrays; |
20 | |
import java.util.Collections; |
21 | |
import java.util.Iterator; |
22 | |
import java.util.Map; |
23 | |
|
24 | |
import javax.xml.namespace.NamespaceContext; |
25 | |
import javax.xml.namespace.QName; |
26 | |
import javax.xml.xpath.XPath; |
27 | |
import javax.xml.xpath.XPathConstants; |
28 | |
import javax.xml.xpath.XPathExpressionException; |
29 | |
import javax.xml.xpath.XPathFactory; |
30 | |
|
31 | |
import org.xml.sax.InputSource; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
public class XPathExtractor extends AbstractTransformer |
40 | |
{ |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | 0 | public enum ResultType |
46 | |
{ |
47 | 0 | NODESET, |
48 | 0 | NODE, |
49 | 0 | STRING, |
50 | 0 | BOOLEAN, |
51 | 0 | NUMBER |
52 | |
} |
53 | |
|
54 | 0 | private volatile XPath xpath = XPathFactory.newInstance().newXPath(); |
55 | 0 | private volatile Map<String, String> prefixToNamespaceMap = null; |
56 | |
private volatile String expression; |
57 | 0 | private volatile ResultType resultType = ResultType.STRING; |
58 | |
|
59 | |
public XPathExtractor() |
60 | 0 | { |
61 | 0 | registerSourceType(DataTypeFactory.create(org.w3c.dom.Node.class)); |
62 | 0 | registerSourceType(DataTypeFactory.create(InputSource.class)); |
63 | 0 | } |
64 | |
|
65 | |
@Override |
66 | |
public void initialise() throws InitialisationException |
67 | |
{ |
68 | 0 | super.initialise(); |
69 | |
|
70 | 0 | if (expression == null) |
71 | |
{ |
72 | 0 | throw new InitialisationException( |
73 | |
MessageFactory.createStaticMessage("An expression must be supplied to the StandardXPathExtractor"), |
74 | |
this); |
75 | |
} |
76 | |
|
77 | 0 | final Map<String, String> prefixToNamespaceMap = this.prefixToNamespaceMap; |
78 | 0 | if (prefixToNamespaceMap != null) |
79 | |
{ |
80 | 0 | getXpath().setNamespaceContext(new NamespaceContext() |
81 | 0 | { |
82 | |
public String getNamespaceURI(String prefix) |
83 | |
{ |
84 | 0 | return prefixToNamespaceMap.get(prefix); |
85 | |
} |
86 | |
|
87 | |
public String getPrefix(String namespaceURI) |
88 | |
{ |
89 | |
|
90 | 0 | for (Map.Entry<String, String> entry : prefixToNamespaceMap.entrySet()) |
91 | |
{ |
92 | 0 | if (namespaceURI.equals(entry.getValue())) |
93 | |
{ |
94 | 0 | return entry.getKey(); |
95 | |
} |
96 | |
} |
97 | |
|
98 | 0 | return null; |
99 | |
} |
100 | |
|
101 | |
public Iterator getPrefixes(String namespaceURI) |
102 | |
{ |
103 | 0 | String prefix = getPrefix(namespaceURI); |
104 | 0 | if (prefix == null) |
105 | |
{ |
106 | 0 | return Collections.emptyList().iterator(); |
107 | |
} |
108 | |
else |
109 | |
{ |
110 | 0 | return Arrays.asList(prefix).iterator(); |
111 | |
} |
112 | |
} |
113 | |
}); |
114 | |
} |
115 | 0 | } |
116 | |
|
117 | |
@Override |
118 | |
public Object doTransform(Object src, String encoding) throws TransformerException |
119 | |
{ |
120 | |
QName resultType; |
121 | 0 | switch (getResultType()) |
122 | |
{ |
123 | |
case BOOLEAN : |
124 | 0 | resultType = XPathConstants.BOOLEAN; |
125 | 0 | break; |
126 | |
case NODE : |
127 | 0 | resultType = XPathConstants.NODE; |
128 | 0 | break; |
129 | |
case NODESET : |
130 | 0 | resultType = XPathConstants.NODESET; |
131 | 0 | break; |
132 | |
case NUMBER : |
133 | 0 | resultType = XPathConstants.NUMBER; |
134 | 0 | break; |
135 | |
default : |
136 | 0 | resultType = XPathConstants.STRING; |
137 | |
break; |
138 | |
} |
139 | |
|
140 | |
try |
141 | |
{ |
142 | 0 | if (src instanceof InputSource) |
143 | |
{ |
144 | 0 | return xpath.evaluate(expression, (InputSource) src, resultType); |
145 | |
} |
146 | |
else |
147 | |
{ |
148 | 0 | return xpath.evaluate(expression, src, resultType); |
149 | |
} |
150 | |
} |
151 | 0 | catch (XPathExpressionException e) |
152 | |
{ |
153 | 0 | throw new TransformerException(this, e); |
154 | |
} |
155 | |
} |
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
public String getExpression() |
161 | |
{ |
162 | 0 | return expression; |
163 | |
} |
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
public void setExpression(String expression) |
169 | |
{ |
170 | 0 | this.expression = expression; |
171 | 0 | } |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
public ResultType getResultType() |
179 | |
{ |
180 | 0 | return resultType; |
181 | |
} |
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
public void setResultType(ResultType resultType) |
189 | |
{ |
190 | 0 | this.resultType = resultType; |
191 | 0 | } |
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
public XPath getXpath() |
199 | |
{ |
200 | 0 | return xpath; |
201 | |
} |
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
public void setXpath(XPath xPath) |
209 | |
{ |
210 | 0 | this.xpath = xPath; |
211 | 0 | } |
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
public Map<String, String> getNamespaces() |
219 | |
{ |
220 | 0 | return prefixToNamespaceMap; |
221 | |
} |
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
public void setNamespaces(Map<String, String> prefixToNamespaceMap) |
229 | |
{ |
230 | 0 | this.prefixToNamespaceMap = prefixToNamespaceMap; |
231 | 0 | } |
232 | |
} |