1 | |
package org.apache.maven.archiva.xml; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.commons.io.IOUtils; |
23 | |
import org.apache.commons.lang.StringUtils; |
24 | |
import org.dom4j.Attribute; |
25 | |
import org.dom4j.Document; |
26 | |
import org.dom4j.DocumentException; |
27 | |
import org.dom4j.Element; |
28 | |
import org.dom4j.Namespace; |
29 | |
import org.dom4j.Node; |
30 | |
import org.dom4j.QName; |
31 | |
import org.dom4j.XPath; |
32 | |
import org.dom4j.io.SAXReader; |
33 | |
|
34 | |
import java.io.File; |
35 | |
import java.io.IOException; |
36 | |
import java.io.InputStream; |
37 | |
import java.io.InputStreamReader; |
38 | |
import java.net.MalformedURLException; |
39 | |
import java.net.URL; |
40 | |
import java.util.ArrayList; |
41 | |
import java.util.HashMap; |
42 | |
import java.util.Iterator; |
43 | |
import java.util.List; |
44 | |
import java.util.Map; |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
public class XMLReader |
52 | |
{ |
53 | |
private URL xmlUrl; |
54 | |
|
55 | |
private String documentType; |
56 | |
|
57 | |
private Document document; |
58 | |
|
59 | 0 | private Map<String, String> namespaceMap = new HashMap<String, String>(); |
60 | |
|
61 | |
public XMLReader( String type, File file ) |
62 | |
throws XMLException |
63 | 0 | { |
64 | 0 | if ( !file.exists() ) |
65 | |
{ |
66 | 0 | throw new XMLException( "file does not exist: " + file.getAbsolutePath() ); |
67 | |
} |
68 | |
|
69 | 0 | if ( !file.isFile() ) |
70 | |
{ |
71 | 0 | throw new XMLException( "path is not a file: " + file.getAbsolutePath() ); |
72 | |
} |
73 | |
|
74 | 0 | if ( !file.canRead() ) |
75 | |
{ |
76 | 0 | throw new XMLException( "Cannot read xml file due to permissions: " + file.getAbsolutePath() ); |
77 | |
} |
78 | |
|
79 | |
try |
80 | |
{ |
81 | 0 | init( type, file.toURL() ); |
82 | |
} |
83 | 0 | catch ( MalformedURLException e ) |
84 | |
{ |
85 | 0 | throw new XMLException( "Unable to translate file " + file + " to URL: " + e.getMessage(), e ); |
86 | 0 | } |
87 | 0 | } |
88 | |
|
89 | |
public XMLReader( String type, URL url ) |
90 | |
throws XMLException |
91 | 0 | { |
92 | 0 | init( type, url ); |
93 | 0 | } |
94 | |
|
95 | |
private void init( String type, URL url ) |
96 | |
throws XMLException |
97 | |
{ |
98 | 0 | this.documentType = type; |
99 | 0 | this.xmlUrl = url; |
100 | |
|
101 | 0 | InputStream in = null; |
102 | 0 | SAXReader reader = new SAXReader(); |
103 | |
|
104 | |
try |
105 | |
{ |
106 | 0 | in = url.openStream(); |
107 | 0 | InputStreamReader inReader = new InputStreamReader( in, "UTF-8" ); |
108 | 0 | LatinEntityResolutionReader latinReader = new LatinEntityResolutionReader( inReader ); |
109 | 0 | this.document = reader.read( latinReader ); |
110 | |
} |
111 | 0 | catch ( DocumentException e ) |
112 | |
{ |
113 | 0 | throw new XMLException( "Unable to parse " + documentType + " xml " + xmlUrl + ": " + e.getMessage(), e ); |
114 | |
} |
115 | 0 | catch ( IOException e ) |
116 | |
{ |
117 | 0 | throw new XMLException( "Unable to open stream to " + url + ": " + e.getMessage(), e ); |
118 | |
} |
119 | |
finally |
120 | |
{ |
121 | 0 | IOUtils.closeQuietly( in ); |
122 | 0 | } |
123 | |
|
124 | 0 | Element root = this.document.getRootElement(); |
125 | 0 | if ( root == null ) |
126 | |
{ |
127 | 0 | throw new XMLException( "Invalid " + documentType + " xml: root element is null." ); |
128 | |
} |
129 | |
|
130 | 0 | if ( !StringUtils.equals( root.getName(), documentType ) ) |
131 | |
{ |
132 | 0 | throw new XMLException( "Invalid " + documentType + " xml: Unexpected root element <" + root.getName() |
133 | |
+ ">, expected <" + documentType + ">" ); |
134 | |
} |
135 | 0 | } |
136 | |
|
137 | |
public String getDefaultNamespaceURI() |
138 | |
{ |
139 | 0 | Namespace namespace = this.document.getRootElement().getNamespace(); |
140 | 0 | return namespace.getURI(); |
141 | |
} |
142 | |
|
143 | |
public void addNamespaceMapping( String elementName, String uri ) |
144 | |
{ |
145 | 0 | this.namespaceMap.put( elementName, uri ); |
146 | 0 | } |
147 | |
|
148 | |
public Element getElement( String xpathExpr ) |
149 | |
throws XMLException |
150 | |
{ |
151 | 0 | XPath xpath = createXPath( xpathExpr ); |
152 | 0 | Object evaluated = xpath.selectSingleNode( document ); |
153 | |
|
154 | 0 | if ( evaluated == null ) |
155 | |
{ |
156 | 0 | return null; |
157 | |
} |
158 | |
|
159 | 0 | if ( evaluated instanceof Element ) |
160 | |
{ |
161 | 0 | Element evalElem = (Element) evaluated; |
162 | 0 | return evalElem; |
163 | |
} |
164 | |
else |
165 | |
{ |
166 | |
|
167 | 0 | throw new XMLException( ".getElement( Expr: " + xpathExpr + " ) resulted in non-Element type -> (" |
168 | |
+ evaluated.getClass().getName() + ") " + evaluated ); |
169 | |
} |
170 | |
} |
171 | |
|
172 | |
private XPath createXPath( String xpathExpr ) |
173 | |
{ |
174 | 0 | XPath xpath = document.createXPath( xpathExpr ); |
175 | 0 | if ( !this.namespaceMap.isEmpty() ) |
176 | |
{ |
177 | 0 | xpath.setNamespaceURIs( this.namespaceMap ); |
178 | |
} |
179 | 0 | return xpath; |
180 | |
} |
181 | |
|
182 | |
public boolean hasElement( String xpathExpr ) |
183 | |
throws XMLException |
184 | |
{ |
185 | 0 | XPath xpath = createXPath( xpathExpr ); |
186 | 0 | Object evaluated = xpath.selectSingleNode( document ); |
187 | |
|
188 | 0 | if ( evaluated == null ) |
189 | |
{ |
190 | 0 | return false; |
191 | |
} |
192 | |
|
193 | 0 | return true; |
194 | |
} |
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
public void removeNamespaces() |
200 | |
{ |
201 | 0 | removeNamespaces( this.document.getRootElement() ); |
202 | 0 | } |
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
@SuppressWarnings("unchecked") |
208 | |
public void removeNamespaces( Element elem ) |
209 | |
{ |
210 | 0 | elem.setQName( QName.get( elem.getName(), Namespace.NO_NAMESPACE, elem.getQualifiedName() ) ); |
211 | |
|
212 | |
Node n; |
213 | |
|
214 | 0 | Iterator<Node> it = elem.elementIterator(); |
215 | 0 | while ( it.hasNext() ) |
216 | |
{ |
217 | 0 | n = it.next(); |
218 | |
|
219 | 0 | switch ( n.getNodeType() ) |
220 | |
{ |
221 | |
case Node.ATTRIBUTE_NODE: |
222 | 0 | ( (Attribute) n ).setNamespace( Namespace.NO_NAMESPACE ); |
223 | 0 | break; |
224 | |
case Node.ELEMENT_NODE: |
225 | 0 | removeNamespaces( (Element) n ); |
226 | 0 | break; |
227 | |
} |
228 | |
} |
229 | 0 | } |
230 | |
|
231 | |
public String getElementText( Node context, String xpathExpr ) |
232 | |
throws XMLException |
233 | |
{ |
234 | 0 | XPath xpath = createXPath( xpathExpr ); |
235 | 0 | Object evaluated = xpath.selectSingleNode( context ); |
236 | |
|
237 | 0 | if ( evaluated == null ) |
238 | |
{ |
239 | 0 | return null; |
240 | |
} |
241 | |
|
242 | 0 | if ( evaluated instanceof Element ) |
243 | |
{ |
244 | 0 | Element evalElem = (Element) evaluated; |
245 | 0 | return evalElem.getTextTrim(); |
246 | |
} |
247 | |
else |
248 | |
{ |
249 | |
|
250 | 0 | throw new XMLException( ".getElementText( Node, Expr: " + xpathExpr |
251 | |
+ " ) resulted in non-Element type -> (" + evaluated.getClass().getName() + ") " + evaluated ); |
252 | |
} |
253 | |
} |
254 | |
|
255 | |
public String getElementText( String xpathExpr ) |
256 | |
throws XMLException |
257 | |
{ |
258 | 0 | XPath xpath = createXPath( xpathExpr ); |
259 | 0 | Object evaluated = xpath.selectSingleNode( document ); |
260 | |
|
261 | 0 | if ( evaluated == null ) |
262 | |
{ |
263 | 0 | return null; |
264 | |
} |
265 | |
|
266 | 0 | if ( evaluated instanceof Element ) |
267 | |
{ |
268 | 0 | Element evalElem = (Element) evaluated; |
269 | 0 | return evalElem.getTextTrim(); |
270 | |
} |
271 | |
else |
272 | |
{ |
273 | |
|
274 | 0 | throw new XMLException( ".getElementText( Expr: " + xpathExpr + " ) resulted in non-Element type -> (" |
275 | |
+ evaluated.getClass().getName() + ") " + evaluated ); |
276 | |
} |
277 | |
} |
278 | |
|
279 | |
@SuppressWarnings("unchecked") |
280 | |
public List<Element> getElementList( String xpathExpr ) |
281 | |
throws XMLException |
282 | |
{ |
283 | 0 | XPath xpath = createXPath( xpathExpr ); |
284 | 0 | Object evaluated = xpath.evaluate( document ); |
285 | |
|
286 | 0 | if ( evaluated == null ) |
287 | |
{ |
288 | 0 | return null; |
289 | |
} |
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | 0 | if ( evaluated instanceof List ) |
297 | |
{ |
298 | 0 | return (List<Element>) evaluated; |
299 | |
} |
300 | 0 | else if ( evaluated instanceof Node ) |
301 | |
{ |
302 | 0 | List<Element> ret = new ArrayList<Element>(); |
303 | 0 | ret.add( (Element) evaluated ); |
304 | 0 | return ret; |
305 | |
} |
306 | |
else |
307 | |
{ |
308 | |
|
309 | 0 | throw new XMLException( ".getElementList( Expr: " + xpathExpr + " ) resulted in non-List type -> (" |
310 | |
+ evaluated.getClass().getName() + ") " + evaluated ); |
311 | |
} |
312 | |
} |
313 | |
|
314 | |
public List<String> getElementListText( String xpathExpr ) |
315 | |
throws XMLException |
316 | |
{ |
317 | 0 | List<Element> elemList = getElementList( xpathExpr ); |
318 | 0 | if ( elemList == null ) |
319 | |
{ |
320 | 0 | return null; |
321 | |
} |
322 | |
|
323 | 0 | List<String> ret = new ArrayList<String>(); |
324 | 0 | for ( Iterator<Element> iter = elemList.iterator(); iter.hasNext(); ) |
325 | |
{ |
326 | 0 | Element listelem = iter.next(); |
327 | 0 | ret.add( listelem.getTextTrim() ); |
328 | 0 | } |
329 | 0 | return ret; |
330 | |
} |
331 | |
|
332 | |
} |