1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
package net.sf.practicalxml; |
16 | |
|
17 | |
import java.io.BufferedInputStream; |
18 | |
import java.io.File; |
19 | |
import java.io.FileInputStream; |
20 | |
import java.io.FileNotFoundException; |
21 | |
import java.io.IOException; |
22 | |
import java.io.InputStream; |
23 | |
import java.io.StringReader; |
24 | |
|
25 | |
import javax.xml.parsers.DocumentBuilder; |
26 | |
import javax.xml.parsers.DocumentBuilderFactory; |
27 | |
import javax.xml.parsers.ParserConfigurationException; |
28 | |
import javax.xml.validation.Schema; |
29 | |
|
30 | |
import org.w3c.dom.Document; |
31 | |
import org.xml.sax.EntityResolver; |
32 | |
import org.xml.sax.ErrorHandler; |
33 | |
import org.xml.sax.InputSource; |
34 | |
import org.xml.sax.SAXException; |
35 | |
|
36 | |
import net.sf.kdgcommons.io.IOUtil; |
37 | |
import net.sf.practicalxml.util.ExceptionErrorHandler; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | 0 | public class ParseUtil |
45 | |
{ |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
public static Document parse(InputSource source, ErrorHandler errHandler) |
53 | |
{ |
54 | 32 | DocumentBuilder db = newNVDocumentBuilder(); |
55 | 32 | if (errHandler != null) |
56 | 32 | db.setErrorHandler(errHandler); |
57 | |
|
58 | |
try |
59 | |
{ |
60 | 32 | return db.parse(source); |
61 | |
} |
62 | 0 | catch (IOException e) |
63 | |
{ |
64 | 0 | throw new XmlException("unable to parse", e); |
65 | |
} |
66 | 0 | catch (SAXException e) |
67 | |
{ |
68 | 0 | throw new XmlException("unable to parse", e); |
69 | |
} |
70 | |
} |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
public static Document parse(InputSource source) |
81 | |
{ |
82 | 30 | return parse(source, new ExceptionErrorHandler()); |
83 | |
} |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
public static Document parse(InputStream in) |
97 | |
{ |
98 | |
try |
99 | |
{ |
100 | 1 | return parse(new InputSource(new BufferedInputStream(in))); |
101 | |
} |
102 | |
finally |
103 | |
{ |
104 | 1 | IOUtil.closeQuietly(in); |
105 | |
} |
106 | |
} |
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
public static Document parse(String xml) |
118 | |
{ |
119 | 14 | return parse(new InputSource(new StringReader(xml))); |
120 | |
} |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
public static Document parse(File file) |
133 | |
{ |
134 | |
try |
135 | |
{ |
136 | 2 | return parse(new FileInputStream(file)); |
137 | |
} |
138 | 1 | catch (FileNotFoundException ex) |
139 | |
{ |
140 | 1 | throw new XmlException("file not found: " + file, ex); |
141 | |
} |
142 | |
} |
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
public static Document validatingParse( |
154 | |
InputSource source, EntityResolver resolver, |
155 | |
ErrorHandler errHandler) |
156 | |
{ |
157 | 4 | DocumentBuilder db = newDTDDocumentBuilder(); |
158 | 4 | if (resolver != null) |
159 | 1 | db.setEntityResolver(resolver); |
160 | 4 | if (errHandler != null) |
161 | 4 | db.setErrorHandler(errHandler); |
162 | |
|
163 | |
try |
164 | |
{ |
165 | 4 | return db.parse(source); |
166 | |
} |
167 | 0 | catch (IOException e) |
168 | |
{ |
169 | 0 | throw new XmlException("unable to parse", e); |
170 | |
} |
171 | 0 | catch (SAXException e) |
172 | |
{ |
173 | 0 | throw new XmlException("unable to parse", e); |
174 | |
} |
175 | |
} |
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
public static Document validatingParse( |
186 | |
InputSource source, ErrorHandler errHandler) |
187 | |
{ |
188 | 3 | return validatingParse(source, (EntityResolver)null, errHandler); |
189 | |
} |
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
public static Document validatingParse( |
201 | |
InputSource source, Schema schema, ErrorHandler errHandler) |
202 | |
{ |
203 | 7 | DocumentBuilder db = newXSDDocumentBuilder(schema); |
204 | 7 | if (errHandler != null) |
205 | 7 | db.setErrorHandler(errHandler); |
206 | |
|
207 | |
try |
208 | |
{ |
209 | 7 | return db.parse(source); |
210 | |
} |
211 | 0 | catch (IOException e) |
212 | |
{ |
213 | 0 | throw new XmlException("unable to parse", e); |
214 | |
} |
215 | 0 | catch (SAXException e) |
216 | |
{ |
217 | 0 | throw new XmlException("unable to parse", e); |
218 | |
} |
219 | |
} |
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
public static Document parseFromClasspath(String resourcePath) |
227 | |
{ |
228 | 1 | return parseFromClasspath(resourcePath, new ExceptionErrorHandler()); |
229 | |
} |
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
public static Document parseFromClasspath(String resourcePath, ErrorHandler errorHandler) |
237 | |
{ |
238 | 2 | return parseFromClasspath(Thread.currentThread().getContextClassLoader(), resourcePath, errorHandler); |
239 | |
} |
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
public static Document parseFromClasspath(String resourcePath, Class<?> klass) |
247 | |
{ |
248 | 1 | return parseFromClasspath(resourcePath, new ExceptionErrorHandler()); |
249 | |
} |
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
public static Document parseFromClasspath(String resourcePath, Class<?> klass, ErrorHandler errorHandler) |
257 | |
{ |
258 | 0 | return parseFromClasspath(klass.getClassLoader(), resourcePath, errorHandler); |
259 | |
} |
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
private static synchronized DocumentBuilder newNVDocumentBuilder() |
270 | |
{ |
271 | 32 | DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
272 | 32 | dbf.setNamespaceAware(true); |
273 | 32 | dbf.setCoalescing(true); |
274 | 32 | dbf.setValidating(false); |
275 | |
|
276 | |
try |
277 | |
{ |
278 | 32 | return dbf.newDocumentBuilder(); |
279 | |
} |
280 | 0 | catch (ParserConfigurationException e) |
281 | |
{ |
282 | 0 | throw new XmlException("unable to confiure parser", e); |
283 | |
} |
284 | |
} |
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
private static synchronized DocumentBuilder newDTDDocumentBuilder() |
291 | |
{ |
292 | 4 | DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
293 | 4 | dbf.setNamespaceAware(true); |
294 | 4 | dbf.setValidating(true); |
295 | 4 | dbf.setCoalescing(true); |
296 | |
|
297 | |
try |
298 | |
{ |
299 | 4 | return dbf.newDocumentBuilder(); |
300 | |
} |
301 | 0 | catch (ParserConfigurationException e) |
302 | |
{ |
303 | 0 | throw new XmlException("unable to confiure parser", e); |
304 | |
} |
305 | |
} |
306 | |
|
307 | |
|
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | |
|
313 | |
private static synchronized DocumentBuilder newXSDDocumentBuilder(Schema schema) |
314 | |
{ |
315 | 7 | DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
316 | 7 | dbf.setNamespaceAware(true); |
317 | 7 | dbf.setCoalescing(true); |
318 | 7 | dbf.setValidating(false); |
319 | 7 | dbf.setSchema(schema); |
320 | |
|
321 | |
try |
322 | |
{ |
323 | 7 | return dbf.newDocumentBuilder(); |
324 | |
} |
325 | 0 | catch (ParserConfigurationException e) |
326 | |
{ |
327 | 0 | throw new XmlException("unable to configure parser", e); |
328 | |
} |
329 | |
} |
330 | |
|
331 | |
|
332 | |
|
333 | |
|
334 | |
|
335 | |
private static Document parseFromClasspath(ClassLoader classloader, String resourcePath, ErrorHandler errorHandler) |
336 | |
{ |
337 | 2 | InputStream in = null; |
338 | |
try |
339 | |
{ |
340 | 2 | in = classloader.getResourceAsStream(resourcePath); |
341 | 2 | return parse(new InputSource(in), errorHandler); |
342 | |
} |
343 | |
finally |
344 | |
{ |
345 | 2 | IOUtil.closeQuietly(in); |
346 | |
} |
347 | |
} |
348 | |
} |