Clover coverage report - QedeqKernelSe Coverage Report
Coverage timestamp: Sa Okt 21 2006 08:24:31 CEST
file stats: LOC: 246   Methods: 6
NCLOC: 142   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
SaxParser.java 56,2% 81,4% 83,3% 77,2%
coverage coverage
 1    /* $Id: SaxParser.java,v 1.13 2006/10/20 20:23:04 m31 Exp $
 2    *
 3    * This file is part of the project "Hilbert II" - http://www.qedeq.org
 4    *
 5    * Copyright 2000-2006, 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.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.log.Trace;
 31    import org.xml.sax.InputSource;
 32    import org.xml.sax.SAXException;
 33    import org.xml.sax.SAXNotRecognizedException;
 34    import org.xml.sax.XMLReader;
 35    import org.xml.sax.helpers.DefaultHandler;
 36   
 37   
 38    /**
 39    * Parser for XML files. This class uses features specific for Xerces.
 40    *
 41    * @version $Revision: 1.13 $
 42    * @author Michael Meyling
 43    */
 44    public final class SaxParser {
 45   
 46    /** Namespaces feature id (http://xml.org/sax/features/namespaces). */
 47    private static final String NAMESPACES_FEATURE_ID = "http://xml.org/sax/features/namespaces";
 48   
 49    /** Validation feature id (http://xml.org/sax/features/validation). */
 50    private static final String VALIDATION_FEATURE_ID = "http://xml.org/sax/features/validation";
 51   
 52    /** Schema validation feature id (http://apache.org/xml/features/validation/schema). */
 53    private static final String SCHEMA_VALIDATION_FEATURE_ID
 54    = "http://apache.org/xml/features/validation/schema";
 55   
 56    /** Schema full checking feature id
 57    * (http://apache.org/xml/features/validation/schema-full-checking). */
 58    protected static final String SCHEMA_FULL_CHECKING_FEATURE_ID
 59    = "http://apache.org/xml/features/validation/schema-full-checking";
 60   
 61    /** Handler which deals with the XML contents. */
 62    private SaxDefaultHandler handler;
 63   
 64    /** SAX parser. */
 65    private XMLReader reader;
 66   
 67    /** Default handler for validation purpose only. */
 68    private final DefaultHandler deflt;
 69   
 70    /** Saved errors of parsing. */
 71    private ExceptionList exceptionList;
 72   
 73    /**
 74    * Constructor.
 75    *
 76    * @param handler Default handler for this application.
 77    * @throws ParserConfigurationException Severe parser configuration problem.
 78    * @throws SAXException
 79    */
 80  108 public SaxParser(final SaxDefaultHandler handler) throws ParserConfigurationException,
 81    SAXException {
 82  108 super();
 83   
 84  108 this.handler = handler;
 85  108 this.deflt = new DefaultHandler();
 86   
 87  108 final String factoryImpl = System.getProperty("javax.xml.parsers.SAXParserFactory");
 88  108 if (factoryImpl == null) {
 89  1 System.setProperty("javax.xml.parsers.SAXParserFactory",
 90    "org.apache.xerces.jaxp.SAXParserFactoryImpl");
 91    }
 92  108 SAXParserFactory factory = SAXParserFactory.newInstance();
 93  108 factory.setNamespaceAware(true);
 94  108 factory.setValidating(true);
 95   
 96  108 factory.setFeature(NAMESPACES_FEATURE_ID, true);
 97  108 factory.setFeature(VALIDATION_FEATURE_ID, true);
 98  108 try {
 99  108 factory.setFeature(SCHEMA_VALIDATION_FEATURE_ID, true);
 100    } catch (SAXNotRecognizedException e) {
 101  0 Trace.trace(this, "constructor", e);
 102    // ignore
 103    }
 104  108 try {
 105  108 factory.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, true);
 106    } catch (SAXNotRecognizedException e) {
 107  0 Trace.trace(this, "constructor", e);
 108    // ignore
 109    }
 110   
 111  108 final SAXParser parser = factory.newSAXParser();
 112  108 if (!parser.isNamespaceAware()) {
 113  0 throw new ParserConfigurationException(
 114    "Current XML parser doesn't support namespaces.");
 115    }
 116  108 if (!parser.isValidating()) {
 117  0 throw new ParserConfigurationException(
 118    "Current XML parser doesn't support schema validation.");
 119    }
 120   
 121  108 reader = parser.getXMLReader();
 122  108 reader.setEntityResolver(new SaxEntityResolver());
 123   
 124    // set parser features
 125  108 reader.setFeature(NAMESPACES_FEATURE_ID, true);
 126  108 reader.setFeature(VALIDATION_FEATURE_ID, true);
 127  108 try {
 128  108 reader.setFeature(SCHEMA_VALIDATION_FEATURE_ID, true);
 129    } catch (SAXNotRecognizedException e) {
 130  0 Trace.trace(this, "constructor", e);
 131    // ignore
 132    }
 133  108 try {
 134  108 reader.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, true);
 135    } catch (SAXNotRecognizedException e) {
 136  0 Trace.trace(this, "constructor", e);
 137    // ignore
 138    }
 139    }
 140   
 141    /**
 142    * Parse input source.
 143    *
 144    * @param url Parse data from this source.
 145    * @param validateOnly validate with {@link #deflt} or parse with {@link #handler}.
 146    * @throws SAXException Syntactical or semantical problem occurred.
 147    * @throws IOException Technical problem occurred.
 148    */
 149  212 private void parse(final URL url, final boolean validateOnly)
 150    throws SAXException, IOException {
 151  212 final String method = "parse(URL, boolean)";
 152  212 Trace.traceParam(this, method, "url", url);
 153   
 154  212 InputStream in = null;
 155  212 BufferedReader dis = null;
 156  212 try {
 157  212 in = url.openStream();
 158  212 dis = new BufferedReader(new InputStreamReader(in));
 159  212 final InputSource input = new InputSource(dis);
 160  212 exceptionList = new ExceptionList();
 161  212 reader.setErrorHandler(new SaxErrorHandler(url, exceptionList));
 162  212 if (validateOnly) {
 163  106 reader.setContentHandler(deflt);
 164  106 reader.parse(input);
 165    } else {
 166  106 handler.setExceptionList(exceptionList);
 167  106 reader.setContentHandler(handler);
 168  106 handler.setUrl(url);
 169  106 reader.parse(input);
 170    }
 171    } catch (SAXException e) {
 172  0 Trace.trace(this, "parse", e);
 173  0 if (exceptionList.size() == 0) { // should not occur, exception must be already here
 174  0 exceptionList.add(e);
 175    }
 176    } finally {
 177  212 if (in != null) {
 178  212 try {
 179  212 in.close();
 180    } catch (Exception e) {
 181  0 Trace.trace(this, method, e);
 182    }
 183    }
 184  212 if (dis != null) {
 185  212 try {
 186  212 dis.close();
 187    } catch (Exception e) {
 188  0 Trace.trace(this, method, e);
 189    }
 190    }
 191    }
 192  212 if (exceptionList.size() > 0) {
 193  0 throw new SAXException(exceptionList.toString(), (Exception) exceptionList.get(0));
 194    }
 195    }
 196   
 197    /**
 198    * Parses XML file.
 199    *
 200    * @param fileName File name.
 201    * @throws SAXException Syntactical or semantical problem occurred.
 202    * @throws IOException Technical problem occurred.
 203    */
 204  2 public final void parse(final String fileName) throws SAXException,
 205    IOException {
 206  2 final File file = new File(fileName);
 207  2 parse(file.getAbsoluteFile());
 208    }
 209   
 210    /**
 211    * Parses the XML file.
 212    *
 213    * @param file File to parse.
 214    * @throws SAXException Syntactical or semantical problem occurred.
 215    * @throws IOException Technical problem occurred.
 216    */
 217  25 public final void parse(final File file) throws SAXException,
 218    IOException {
 219  25 final URL url = file.getAbsoluteFile().toURI().toURL();
 220  25 parse(url, true);
 221  25 parse(url, false);
 222    }
 223   
 224    /**
 225    * Parses the XML file.
 226    *
 227    * @param url URL with File to parse.
 228    * @throws SAXException Syntactical or semantical problem occurred.
 229    * @throws IOException Technical problem occurred.
 230    */
 231  81 public final void parse(final URL url) throws SAXException,
 232    IOException {
 233  81 parse(url, true);
 234  81 parse(url, false);
 235    }
 236   
 237    /**
 238    * Get errors that occurred during last parsing.
 239    *
 240    * @return List with collected Exceptions.
 241    */
 242  0 public ExceptionList getExceptionList() {
 243  0 return exceptionList;
 244    }
 245   
 246    }