Clover Coverage Report
Coverage timestamp: Fri May 24 2013 13:47:27 UTC
../../../../../img/srcFileCovDistChart8.png 62% of files have more coverage
72   259   22   12
16   150   0.31   6
6     3.67  
1    
 
  SaxParser       Line # 45 72 22 74.5% 0.7446808
 
No Tests
 
1    /* This file is part of the project "Hilbert II" - http://www.qedeq.org
2    *
3    * Copyright 2000-2013, Michael Meyling <mime@qedeq.org>.
4    *
5    * "Hilbert II" is free software; you can redistribute
6    * it and/or modify it under the terms of the GNU General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10    * This program is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13    * GNU General Public License for more details.
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    * Parser for XML files. This class uses features specific for Xerces.
42    *
43    * @author Michael Meyling
44    */
 
45    public final class SaxParser {
46   
47    /** This class. */
48    private static final Class CLASS = SaxParser.class;
49   
50    /** Namespaces feature id (http://xml.org/sax/features/namespaces). */
51    private static final String NAMESPACES_FEATURE_ID = "http://xml.org/sax/features/namespaces";
52   
53    /** Validation feature id (http://xml.org/sax/features/validation). */
54    private static final String VALIDATION_FEATURE_ID = "http://xml.org/sax/features/validation";
55   
56    /** Schema validation feature id (http://apache.org/xml/features/validation/schema). */
57    private static final String SCHEMA_VALIDATION_FEATURE_ID
58    = "http://apache.org/xml/features/validation/schema";
59   
60    /** Schema full checking feature id
61    * (http://apache.org/xml/features/validation/schema-full-checking). */
62    protected static final String SCHEMA_FULL_CHECKING_FEATURE_ID
63    = "http://apache.org/xml/features/validation/schema-full-checking";
64   
65    /** Handler which deals with the XML contents. */
66    private SaxDefaultHandler handler;
67   
68    /** SAX parser. */
69    private XMLReader reader;
70   
71    /** Simple handler for validation purpose only. */
72    private final SimpleHandler deflt;
73   
74    /** Saved errors of parsing. */
75    private SourceFileExceptionList exceptionList;
76   
77    /** Plugin we work for. */
78    private Plugin plugin;
79   
80    /**
81    * Constructor.
82    *
83    * @param plugin We work for this plugin.
84    * @param handler Default handler for this application.
85    * @throws ParserConfigurationException Severe parser configuration problem.
86    * @throws SAXException Option not recognized or supported.
87    */
 
88  715 toggle 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    // ignore
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    // ignore
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    // set parser features
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    // ignore
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    // ignore
148    }
149   
150    }
151   
152    /**
153    * Parse input source.
154    * @param in Parse data from this file source.
155    * @param validateOnly validate with {@link #deflt} or parse with {@link #handler}.
156    * @param original Original URL for the file. If this is <code>null</code> same as
157    * file name.
158    *
159    * @throws SourceFileExceptionList Loading failed.
160    */
 
161  1411 toggle 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) { // do we have already exceptions?
187    // no, we must add this one
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    * Parses XML file.
212    *
213    * @param fileName File name.
214    * @param original Original URL for the file. If this is <code>null</code> same as
215    * file name.
216    * @throws SourceFileExceptionList Loading failed.
217    */
 
218  0 toggle 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    * Parses the XML file.
226    *
227    * @param file File to parse.
228    * @param original Original URL for the file. If this is <code>null</code> same as
229    * file.
230    * @throws SourceFileExceptionList Loading failed.
231    */
 
232  713 toggle 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    * Get errors that occurred during last parsing.
243    *
244    * @return List with collected Exceptions.
245    */
 
246  0 toggle public SourceFileExceptionList getExceptionList() {
247  0 return exceptionList;
248    }
249   
250    /**
251    * Get encoding of XML document. This value is set during parsing the document.
252    *
253    * @return Encoding. Maybe <code>null</code>.
254    */
 
255  0 toggle public String getEncoding() {
256  0 return deflt.getEncoding();
257    }
258   
259    }