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