Clover Coverage Report
Coverage timestamp: Sa Aug 2 2008 13:56:27 CEST
../../../../../img/srcFileCovDistChart8.png 47% of files have more coverage
65   242   20   10,83
14   135   0,31   6
6     3,33  
1    
 
  SaxParser       Line # 45 65 20 75,3% 0.7529412
 
  (134)
 
1    /* $Id: SaxParser.java,v 1.1 2008/07/26 08:00:50 m31 Exp $
2    *
3    * This file is part of the project "Hilbert II" - http://www.qedeq.org
4    *
5    * Copyright 2000-2008, Michael Meyling <mime@qedeq.org>.
6    *
7    * "Hilbert II" is free software; you can redistribute
8    * it and/or modify it under the terms of the GNU General Public
9    * License as published by the Free Software Foundation; either
10    * version 2 of the License, or (at your option) any later version.
11    *
12    * This program is distributed in the hope that it will be useful,
13    * but WITHOUT ANY WARRANTY; without even the implied warranty of
14    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15    * GNU General Public License for more details.
16    */
17    package org.qedeq.kernel.xml.parser;
18   
19    import java.io.File;
20    import java.io.FileInputStream;
21    import java.io.IOException;
22    import java.io.InputStream;
23    import java.net.URL;
24   
25    import javax.xml.parsers.ParserConfigurationException;
26    import javax.xml.parsers.SAXParser;
27    import javax.xml.parsers.SAXParserFactory;
28   
29    import org.qedeq.base.trace.Trace;
30    import org.qedeq.kernel.common.DefaultSourceFileExceptionList;
31    import org.qedeq.kernel.common.SourceFileException;
32    import org.qedeq.kernel.common.SourceFileExceptionList;
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   
38   
39    /**
40    * Parser for XML files. This class uses features specific for Xerces.
41    *
42    * @version $Revision: 1.1 $
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 DefaultSourceFileExceptionList exceptionList;
76   
77    /**
78    * Constructor.
79    *
80    * @param handler Default handler for this application.
81    * @throws ParserConfigurationException Severe parser configuration problem.
82    * @throws SAXException
83    */
 
84  234 toggle public SaxParser(final SaxDefaultHandler handler) throws ParserConfigurationException,
85    SAXException {
86  234 super();
87   
88  234 this.handler = handler;
89  234 this.deflt = new SimpleHandler();
90   
91  234 final String factoryImpl = System.getProperty("javax.xml.parsers.SAXParserFactory");
92  234 if (factoryImpl == null) {
93  12 System.setProperty("javax.xml.parsers.SAXParserFactory",
94    "org.apache.xerces.jaxp.SAXParserFactoryImpl");
95    }
96  234 SAXParserFactory factory = SAXParserFactory.newInstance();
97  234 factory.setNamespaceAware(true);
98  234 factory.setValidating(true);
99   
100  234 factory.setFeature(NAMESPACES_FEATURE_ID, true);
101  234 factory.setFeature(VALIDATION_FEATURE_ID, true);
102   
103  234 try {
104  234 factory.setFeature(SCHEMA_VALIDATION_FEATURE_ID, true);
105    } catch (SAXNotRecognizedException e) {
106  0 Trace.trace(CLASS, this, "constructor", e);
107    // ignore
108    }
109  234 try {
110  234 factory.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, true);
111    } catch (SAXNotRecognizedException e) {
112  0 Trace.trace(CLASS, this, "constructor", e);
113    // ignore
114    }
115   
116  234 final SAXParser parser = factory.newSAXParser();
117  234 if (!parser.isNamespaceAware()) {
118  0 throw new ParserConfigurationException(
119    "Current XML parser doesn't support namespaces.");
120    }
121  234 if (!parser.isValidating()) {
122  0 throw new ParserConfigurationException(
123    "Current XML parser doesn't support schema validation.");
124    }
125   
126  234 reader = parser.getXMLReader();
127  234 reader.setEntityResolver(new SaxEntityResolver(handler));
128   
129    // set parser features
130  234 reader.setFeature(NAMESPACES_FEATURE_ID, true);
131  234 reader.setFeature(VALIDATION_FEATURE_ID, true);
132  234 try {
133  234 reader.setFeature(SCHEMA_VALIDATION_FEATURE_ID, true);
134    } catch (SAXNotRecognizedException e) {
135  0 Trace.trace(CLASS, this, "constructor", e);
136    // ignore
137    }
138  234 try {
139  234 reader.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, true);
140    } catch (SAXNotRecognizedException e) {
141  0 Trace.trace(CLASS, this, "constructor", e);
142    // ignore
143    }
144   
145    }
146   
147    /**
148    * Parse input source.
149    * @param in Parse data from this file source.
150    * @param validateOnly validate with {@link #deflt} or parse with {@link #handler}.
151    * @param original Original URL for the file. If this is <code>null</code> same as
152    * file name.
153    *
154    * @throws SourceFileExceptionList Loading failed.
155    */
 
156  458 toggle private void parse(final File in, final boolean validateOnly, final URL original)
157    throws SourceFileExceptionList {
158  458 final String method = "parse(URL, boolean, InputStream)";
159  458 InputStream stream = null;
160  458 exceptionList = new DefaultSourceFileExceptionList();
161  458 try {
162  458 stream = new FileInputStream(in);
163  458 final InputSource input = new InputSource(stream);
164  458 reader.setErrorHandler(new SaxErrorHandler(original, exceptionList));
165  458 handler.setUrl(original);
166  458 deflt.setUrl(original);
167  458 if (validateOnly) {
168  232 reader.setContentHandler(deflt);
169  232 reader.parse(input);
170    } else {
171  226 handler.setExceptionList(exceptionList);
172  226 reader.setContentHandler(handler);
173  226 reader.parse(input);
174    }
175    } catch (SAXException e) {
176  2 final SourceFileException ex = new SourceFileException(e);
177  2 if (exceptionList.size() <= 0) {
178  0 exceptionList.add(ex);
179    }
180  2 throw exceptionList;
181    } catch (IOException e) {
182  0 exceptionList.add(e);
183  0 throw exceptionList;
184    } finally {
185  458 if (stream != null) {
186  458 try {
187  458 stream.close();
188    } catch (Exception e) {
189  0 Trace.trace(CLASS, this, method, e);
190    }
191    }
192    }
193  456 if (exceptionList.size() > 0) {
194  4 throw exceptionList;
195    }
196    }
197   
198    /**
199    * Parses XML file.
200    *
201    * @param fileName File name.
202    * @param original Original URL for the file. If this is <code>null</code> same as
203    * file name.
204    * @throws SourceFileExceptionList Loading failed.
205    */
 
206  0 toggle public final void parse(final String fileName, final URL original)
207    throws SourceFileExceptionList {
208  0 final File file = new File(fileName);
209  0 parse(file.getAbsoluteFile(), original);
210    }
211   
212    /**
213    * Parses the XML file.
214    *
215    * @param file File to parse.
216    * @param original Original URL for the file. If this is <code>null</code> same as
217    * file.
218    * @throws SourceFileExceptionList Loading failed.
219    */
 
220  232 toggle public final void parse(final File file, final URL original) throws SourceFileExceptionList {
221  232 parse(file, true, original);
222  226 parse(file, false, original);
223    }
224   
225    /**
226    * Get errors that occurred during last parsing.
227    *
228    * @return List with collected Exceptions.
229    */
 
230  0 toggle public DefaultSourceFileExceptionList getExceptionList() {
231  0 return exceptionList;
232    }
233   
234    /**
235    * Get encoding of XML document. This value is set during parsing the document.
236    *
237    * @return Encoding. Maybe <code>null</code>.
238    */
 
239  0 toggle public String getEncoding() {
240  0 return deflt.getEncoding();
241    }
242    }