Clover coverage report - QedeqKernelSe Coverage Report
Coverage timestamp: Do Mrz 27 2008 21:46:26 CET
file stats: LOC: 241   Methods: 6
NCLOC: 134   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
SaxParser.java 78,6% 84,4% 83,3% 83,3%
coverage coverage
 1    /* $Id: SaxParser.java,v 1.24 2008/03/27 05:16:29 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.kernel.common.DefaultSourceFileExceptionList;
 30    import org.qedeq.kernel.common.SourceFileException;
 31    import org.qedeq.kernel.common.SourceFileExceptionList;
 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   
 38   
 39    /**
 40    * Parser for XML files. This class uses features specific for Xerces.
 41    *
 42    * @version $Revision: 1.24 $
 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  280 public SaxParser(final SaxDefaultHandler handler) throws ParserConfigurationException,
 85    SAXException {
 86  280 super();
 87   
 88  280 this.handler = handler;
 89  280 this.deflt = new SimpleHandler();
 90   
 91  280 final String factoryImpl = System.getProperty("javax.xml.parsers.SAXParserFactory");
 92  280 if (factoryImpl == null) {
 93  3 System.setProperty("javax.xml.parsers.SAXParserFactory",
 94    "org.apache.xerces.jaxp.SAXParserFactoryImpl");
 95    }
 96  280 SAXParserFactory factory = SAXParserFactory.newInstance();
 97  280 factory.setNamespaceAware(true);
 98  280 factory.setValidating(true);
 99   
 100  280 factory.setFeature(NAMESPACES_FEATURE_ID, true);
 101  280 factory.setFeature(VALIDATION_FEATURE_ID, true);
 102   
 103  280 try {
 104  280 factory.setFeature(SCHEMA_VALIDATION_FEATURE_ID, true);
 105    } catch (SAXNotRecognizedException e) {
 106  0 Trace.trace(CLASS, this, "constructor", e);
 107    // ignore
 108    }
 109  280 try {
 110  280 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  280 final SAXParser parser = factory.newSAXParser();
 117  280 if (!parser.isNamespaceAware()) {
 118  0 throw new ParserConfigurationException(
 119    "Current XML parser doesn't support namespaces.");
 120    }
 121  280 if (!parser.isValidating()) {
 122  0 throw new ParserConfigurationException(
 123    "Current XML parser doesn't support schema validation.");
 124    }
 125   
 126  280 reader = parser.getXMLReader();
 127  280 reader.setEntityResolver(new SaxEntityResolver(handler));
 128   
 129    // set parser features
 130  280 reader.setFeature(NAMESPACES_FEATURE_ID, true);
 131  280 reader.setFeature(VALIDATION_FEATURE_ID, true);
 132  280 try {
 133  280 reader.setFeature(SCHEMA_VALIDATION_FEATURE_ID, true);
 134    } catch (SAXNotRecognizedException e) {
 135  0 Trace.trace(CLASS, this, "constructor", e);
 136    // ignore
 137    }
 138  280 try {
 139  280 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  534 private void parse(final File in, final boolean validateOnly, final URL original)
 157    throws SourceFileExceptionList {
 158  534 final String method = "parse(URL, boolean, InputStream)";
 159  534 InputStream stream = null;
 160  534 try {
 161  534 stream = new FileInputStream(in);
 162  534 final InputSource input = new InputSource(stream);
 163  534 exceptionList = new DefaultSourceFileExceptionList();
 164  534 reader.setErrorHandler(new SaxErrorHandler(original, exceptionList));
 165  534 if (validateOnly) {
 166  278 reader.setContentHandler(deflt);
 167  278 reader.parse(input);
 168    } else {
 169  256 handler.setExceptionList(exceptionList);
 170  256 reader.setContentHandler(handler);
 171  256 handler.setUrl(original);
 172  256 reader.parse(input);
 173    }
 174    } catch (SAXException e) {
 175  7 final SourceFileException ex = new SourceFileException(e);
 176  7 if (exceptionList.size() <= 0) {
 177  2 exceptionList.add(ex);
 178    }
 179  7 throw exceptionList;
 180    } catch (IOException e) {
 181  0 exceptionList.add(e);
 182  0 throw exceptionList;
 183    } finally {
 184  534 if (stream != null) {
 185  534 try {
 186  534 stream.close();
 187    } catch (Exception e) {
 188  0 Trace.trace(CLASS, this, method, e);
 189    }
 190    }
 191    }
 192  527 if (exceptionList.size() > 0) {
 193  15 throw exceptionList;
 194    }
 195    }
 196   
 197    /**
 198    * Parses XML file.
 199    *
 200    * @param fileName File name.
 201    * @param original Original URL for the file. If this is <code>null</code> same as
 202    * file name.
 203    * @throws SourceFileExceptionList Loading failed.
 204    */
 205  2 public final void parse(final String fileName, final URL original)
 206    throws SourceFileExceptionList {
 207  2 final File file = new File(fileName);
 208  2 parse(file.getAbsoluteFile(), original);
 209    }
 210   
 211    /**
 212    * Parses the XML file.
 213    *
 214    * @param file File to parse.
 215    * @param original Original URL for the file. If this is <code>null</code> same as
 216    * file.
 217    * @throws SourceFileExceptionList Loading failed.
 218    */
 219  278 public final void parse(final File file, final URL original) throws SourceFileExceptionList {
 220  278 parse(file, true, original);
 221  256 parse(file, false, original);
 222    }
 223   
 224    /**
 225    * Get errors that occurred during last parsing.
 226    *
 227    * @return List with collected Exceptions.
 228    */
 229  0 public DefaultSourceFileExceptionList getExceptionList() {
 230  0 return exceptionList;
 231    }
 232   
 233    /**
 234    * Get encoding of XML document. This value is set during parsing the document.
 235    *
 236    * @return Encoding. Maybe <code>null</code>.
 237    */
 238  155 public String getEncoding() {
 239  155 return deflt.getEncoding();
 240    }
 241    }