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