1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| package org.qedeq.kernel.xml.parser; |
18 |
| |
19 |
| import java.io.BufferedReader; |
20 |
| import java.io.File; |
21 |
| import java.io.IOException; |
22 |
| import java.io.InputStream; |
23 |
| import java.io.InputStreamReader; |
24 |
| import java.net.URL; |
25 |
| |
26 |
| import javax.xml.parsers.ParserConfigurationException; |
27 |
| import javax.xml.parsers.SAXParser; |
28 |
| import javax.xml.parsers.SAXParserFactory; |
29 |
| |
30 |
| import org.qedeq.kernel.common.SyntaxExceptionList; |
31 |
| import org.qedeq.kernel.common.XmlFileExceptionList; |
32 |
| import org.qedeq.kernel.trace.Trace; |
33 |
| import org.xml.sax.InputSource; |
34 |
| import org.xml.sax.SAXException; |
35 |
| import org.xml.sax.SAXNotRecognizedException; |
36 |
| import org.xml.sax.XMLReader; |
37 |
| import org.xml.sax.helpers.DefaultHandler; |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| public final class SaxParser { |
47 |
| |
48 |
| |
49 |
| private static final String NAMESPACES_FEATURE_ID = "http://xml.org/sax/features/namespaces"; |
50 |
| |
51 |
| |
52 |
| private static final String VALIDATION_FEATURE_ID = "http://xml.org/sax/features/validation"; |
53 |
| |
54 |
| |
55 |
| private static final String SCHEMA_VALIDATION_FEATURE_ID |
56 |
| = "http://apache.org/xml/features/validation/schema"; |
57 |
| |
58 |
| |
59 |
| |
60 |
| protected static final String SCHEMA_FULL_CHECKING_FEATURE_ID |
61 |
| = "http://apache.org/xml/features/validation/schema-full-checking"; |
62 |
| |
63 |
| |
64 |
| private SaxDefaultHandler handler; |
65 |
| |
66 |
| |
67 |
| private XMLReader reader; |
68 |
| |
69 |
| |
70 |
| private final DefaultHandler deflt; |
71 |
| |
72 |
| |
73 |
| private SyntaxExceptionList exceptionList; |
74 |
| |
75 |
| |
76 |
| |
77 |
| |
78 |
| |
79 |
| |
80 |
| |
81 |
| |
82 |
130
| public SaxParser(final SaxDefaultHandler handler) throws ParserConfigurationException,
|
83 |
| SAXException { |
84 |
130
| super();
|
85 |
| |
86 |
130
| this.handler = handler;
|
87 |
130
| this.deflt = new DefaultHandler();
|
88 |
| |
89 |
130
| final String factoryImpl = System.getProperty("javax.xml.parsers.SAXParserFactory");
|
90 |
130
| if (factoryImpl == null) {
|
91 |
3
| System.setProperty("javax.xml.parsers.SAXParserFactory",
|
92 |
| "org.apache.xerces.jaxp.SAXParserFactoryImpl"); |
93 |
| } |
94 |
130
| SAXParserFactory factory = SAXParserFactory.newInstance();
|
95 |
130
| factory.setNamespaceAware(true);
|
96 |
130
| factory.setValidating(true);
|
97 |
| |
98 |
130
| factory.setFeature(NAMESPACES_FEATURE_ID, true);
|
99 |
130
| factory.setFeature(VALIDATION_FEATURE_ID, true);
|
100 |
130
| try {
|
101 |
130
| factory.setFeature(SCHEMA_VALIDATION_FEATURE_ID, true);
|
102 |
| } catch (SAXNotRecognizedException e) { |
103 |
0
| Trace.trace(this, "constructor", e);
|
104 |
| |
105 |
| } |
106 |
130
| try {
|
107 |
130
| factory.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, true);
|
108 |
| } catch (SAXNotRecognizedException e) { |
109 |
0
| Trace.trace(this, "constructor", e);
|
110 |
| |
111 |
| } |
112 |
| |
113 |
130
| final SAXParser parser = factory.newSAXParser();
|
114 |
130
| if (!parser.isNamespaceAware()) {
|
115 |
0
| throw new ParserConfigurationException(
|
116 |
| "Current XML parser doesn't support namespaces."); |
117 |
| } |
118 |
130
| if (!parser.isValidating()) {
|
119 |
0
| throw new ParserConfigurationException(
|
120 |
| "Current XML parser doesn't support schema validation."); |
121 |
| } |
122 |
| |
123 |
130
| reader = parser.getXMLReader();
|
124 |
130
| reader.setEntityResolver(new SaxEntityResolver());
|
125 |
| |
126 |
| |
127 |
130
| reader.setFeature(NAMESPACES_FEATURE_ID, true);
|
128 |
130
| reader.setFeature(VALIDATION_FEATURE_ID, true);
|
129 |
130
| try {
|
130 |
130
| reader.setFeature(SCHEMA_VALIDATION_FEATURE_ID, true);
|
131 |
| } catch (SAXNotRecognizedException e) { |
132 |
0
| Trace.trace(this, "constructor", e);
|
133 |
| |
134 |
| } |
135 |
130
| try {
|
136 |
130
| reader.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, true);
|
137 |
| } catch (SAXNotRecognizedException e) { |
138 |
0
| Trace.trace(this, "constructor", e);
|
139 |
| |
140 |
| } |
141 |
| } |
142 |
| |
143 |
| |
144 |
| |
145 |
| |
146 |
| |
147 |
| |
148 |
| |
149 |
| |
150 |
252
| private void parse(final URL url, final boolean validateOnly)
|
151 |
| throws XmlFileExceptionList { |
152 |
252
| final String method = "parse(URL, boolean)";
|
153 |
252
| Trace.param(this, method, "url", url);
|
154 |
| |
155 |
252
| InputStream in = null;
|
156 |
252
| try {
|
157 |
252
| in = url.openStream();
|
158 |
| } catch (IOException e) { |
159 |
0
| throw new DefaultXmlFileExceptionList(e);
|
160 |
| } |
161 |
252
| try {
|
162 |
252
| parse(url, validateOnly, in);
|
163 |
| } finally { |
164 |
252
| if (in != null) {
|
165 |
252
| try {
|
166 |
252
| in.close();
|
167 |
| } catch (Exception e) { |
168 |
0
| Trace.trace(this, method, e);
|
169 |
| } |
170 |
| } |
171 |
| } |
172 |
| } |
173 |
| |
174 |
| |
175 |
| |
176 |
| |
177 |
| |
178 |
| |
179 |
| |
180 |
| |
181 |
| |
182 |
252
| private void parse(final URL url, final boolean validateOnly, final InputStream in)
|
183 |
| throws XmlFileExceptionList { |
184 |
252
| final String method = "parse(URL, boolean, InputStream)";
|
185 |
252
| BufferedReader dis = null;
|
186 |
252
| try {
|
187 |
252
| dis = new BufferedReader(new InputStreamReader(in));
|
188 |
252
| final InputSource input = new InputSource(dis);
|
189 |
252
| exceptionList = new SyntaxExceptionList();
|
190 |
252
| reader.setErrorHandler(new SaxErrorHandler(url, exceptionList));
|
191 |
252
| if (validateOnly) {
|
192 |
128
| reader.setContentHandler(deflt);
|
193 |
128
| reader.parse(input);
|
194 |
| } else { |
195 |
124
| handler.setExceptionList(exceptionList);
|
196 |
124
| reader.setContentHandler(handler);
|
197 |
124
| handler.setUrl(url);
|
198 |
124
| reader.parse(input);
|
199 |
| } |
200 |
| } catch (SAXException e) { |
201 |
1
| final DefaultXmlFileExceptionList list
|
202 |
| = new DefaultXmlFileExceptionList(exceptionList); |
203 |
1
| if (exceptionList.size() <= 0) {
|
204 |
0
| list.add(e);
|
205 |
| } |
206 |
1
| throw list;
|
207 |
| } catch (IOException e) { |
208 |
0
| final DefaultXmlFileExceptionList list
|
209 |
| = new DefaultXmlFileExceptionList(exceptionList); |
210 |
0
| list.add(e);
|
211 |
0
| throw list;
|
212 |
| } finally { |
213 |
252
| if (dis != null) {
|
214 |
252
| try {
|
215 |
252
| dis.close();
|
216 |
| } catch (Exception e) { |
217 |
0
| Trace.trace(this, method, e);
|
218 |
| } |
219 |
| } |
220 |
| } |
221 |
251
| if (exceptionList.size() > 0) {
|
222 |
3
| throw new DefaultXmlFileExceptionList(exceptionList);
|
223 |
| } |
224 |
| } |
225 |
| |
226 |
| |
227 |
| |
228 |
| |
229 |
| |
230 |
| |
231 |
| |
232 |
2
| public final void parse(final String fileName) throws XmlFileExceptionList {
|
233 |
2
| final File file = new File(fileName);
|
234 |
2
| parse(file.getAbsoluteFile());
|
235 |
| } |
236 |
| |
237 |
| |
238 |
| |
239 |
| |
240 |
| |
241 |
| |
242 |
| |
243 |
46
| public final void parse(final File file) throws XmlFileExceptionList {
|
244 |
46
| final URL url;
|
245 |
46
| try {
|
246 |
46
| url = file.getAbsoluteFile().toURI().toURL();
|
247 |
| } catch (IOException e) { |
248 |
0
| final DefaultXmlFileExceptionList list
|
249 |
| = new DefaultXmlFileExceptionList(); |
250 |
0
| list.add(e);
|
251 |
0
| throw list;
|
252 |
| } |
253 |
46
| parse(url, true);
|
254 |
42
| parse(url, false);
|
255 |
| } |
256 |
| |
257 |
| |
258 |
| |
259 |
| |
260 |
| |
261 |
| |
262 |
| |
263 |
82
| public final void parse(final URL url) throws XmlFileExceptionList {
|
264 |
82
| parse(url, true);
|
265 |
82
| parse(url, false);
|
266 |
| } |
267 |
| |
268 |
| |
269 |
| |
270 |
| |
271 |
| |
272 |
| |
273 |
| |
274 |
| |
275 |
0
| public void parse(final InputStream in, final boolean validateOnly)
|
276 |
| throws XmlFileExceptionList { |
277 |
| |
278 |
| |
279 |
0
| parse(null, validateOnly, in);
|
280 |
| } |
281 |
| |
282 |
| |
283 |
| |
284 |
| |
285 |
| |
286 |
| |
287 |
0
| public SyntaxExceptionList getExceptionList() {
|
288 |
0
| return exceptionList;
|
289 |
| } |
290 |
| |
291 |
| } |