Clover coverage report - QedeqKernelSe Coverage Report
Coverage timestamp: Sa Dez 22 2007 01:35:21 CET
file stats: LOC: 291   Methods: 8
NCLOC: 163   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
SaxParser.java 77,8% 83,5% 75% 81,9%
coverage coverage
 1    /* $Id: SaxParser.java,v 1.22 2007/12/21 23:33:46 m31 Exp $
 2    *
 3    * This file is part of the project "Hilbert II" - http://www.qedeq.org
 4    *
 5    * Copyright 2000-2007, 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.common.SourceFileException;
 31    import org.qedeq.kernel.common.SourceFileExceptionList;
 32    import org.qedeq.kernel.trace.Trace;
 33    import org.qedeq.kernel.utility.IoUtility;
 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    import org.xml.sax.helpers.DefaultHandler;
 39   
 40   
 41    /**
 42    * Parser for XML files. This class uses features specific for Xerces.
 43    *
 44    * @version $Revision: 1.22 $
 45    * @author Michael Meyling
 46    */
 47    public final class SaxParser {
 48   
 49    /** Namespaces feature id (http://xml.org/sax/features/namespaces). */
 50    private static final String NAMESPACES_FEATURE_ID = "http://xml.org/sax/features/namespaces";
 51   
 52    /** Validation feature id (http://xml.org/sax/features/validation). */
 53    private static final String VALIDATION_FEATURE_ID = "http://xml.org/sax/features/validation";
 54   
 55    /** Schema validation feature id (http://apache.org/xml/features/validation/schema). */
 56    private static final String SCHEMA_VALIDATION_FEATURE_ID
 57    = "http://apache.org/xml/features/validation/schema";
 58   
 59    /** Schema full checking feature id
 60    * (http://apache.org/xml/features/validation/schema-full-checking). */
 61    protected static final String SCHEMA_FULL_CHECKING_FEATURE_ID
 62    = "http://apache.org/xml/features/validation/schema-full-checking";
 63   
 64    /** Handler which deals with the XML contents. */
 65    private SaxDefaultHandler handler;
 66   
 67    /** SAX parser. */
 68    private XMLReader reader;
 69   
 70    /** Default handler for validation purpose only. */
 71    private final DefaultHandler deflt;
 72   
 73    /** Saved errors of parsing. */
 74    private DefaultSourceFileExceptionList exceptionList;
 75   
 76    /**
 77    * Constructor.
 78    *
 79    * @param handler Default handler for this application.
 80    * @throws ParserConfigurationException Severe parser configuration problem.
 81    * @throws SAXException
 82    */
 83  587 public SaxParser(final SaxDefaultHandler handler) throws ParserConfigurationException,
 84    SAXException {
 85  587 super();
 86   
 87  587 this.handler = handler;
 88  587 this.deflt = new DefaultHandler();
 89   
 90  587 final String factoryImpl = System.getProperty("javax.xml.parsers.SAXParserFactory");
 91  587 if (factoryImpl == null) {
 92  8 System.setProperty("javax.xml.parsers.SAXParserFactory",
 93    "org.apache.xerces.jaxp.SAXParserFactoryImpl");
 94    }
 95  587 SAXParserFactory factory = SAXParserFactory.newInstance();
 96  587 factory.setNamespaceAware(true);
 97  587 factory.setValidating(true);
 98   
 99  587 factory.setFeature(NAMESPACES_FEATURE_ID, true);
 100  587 factory.setFeature(VALIDATION_FEATURE_ID, true);
 101  587 try {
 102  587 factory.setFeature(SCHEMA_VALIDATION_FEATURE_ID, true);
 103    } catch (SAXNotRecognizedException e) {
 104  0 Trace.trace(this, "constructor", e);
 105    // ignore
 106    }
 107  587 try {
 108  587 factory.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, true);
 109    } catch (SAXNotRecognizedException e) {
 110  0 Trace.trace(this, "constructor", e);
 111    // ignore
 112    }
 113   
 114  587 final SAXParser parser = factory.newSAXParser();
 115  587 if (!parser.isNamespaceAware()) {
 116  0 throw new ParserConfigurationException(
 117    "Current XML parser doesn't support namespaces.");
 118    }
 119  587 if (!parser.isValidating()) {
 120  0 throw new ParserConfigurationException(
 121    "Current XML parser doesn't support schema validation.");
 122    }
 123   
 124  587 reader = parser.getXMLReader();
 125  587 reader.setEntityResolver(new SaxEntityResolver(handler));
 126   
 127    // set parser features
 128  587 reader.setFeature(NAMESPACES_FEATURE_ID, true);
 129  587 reader.setFeature(VALIDATION_FEATURE_ID, true);
 130  587 try {
 131  587 reader.setFeature(SCHEMA_VALIDATION_FEATURE_ID, true);
 132    } catch (SAXNotRecognizedException e) {
 133  0 Trace.trace(this, "constructor", e);
 134    // ignore
 135    }
 136  587 try {
 137  587 reader.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, true);
 138    } catch (SAXNotRecognizedException e) {
 139  0 Trace.trace(this, "constructor", e);
 140    // ignore
 141    }
 142    }
 143   
 144    /**
 145    * Parse input source.
 146    *
 147    * @param url Parse data from this source.
 148    * @param original Original URL for the file. If this is <code>null</code> same as
 149    * file name.
 150    * @param validateOnly validate with {@link #deflt} or parse with {@link #handler}.
 151    * @throws SourceFileExceptionList Loading failed.
 152    */
 153  1150 private void parse(final URL url, final URL original, final boolean validateOnly)
 154    throws SourceFileExceptionList {
 155  1150 final String method = "parse(URL, boolean)";
 156  1150 Trace.param(this, method, "url", url);
 157   
 158  1150 InputStream in = null;
 159  1150 try {
 160  1150 in = url.openStream();
 161    } catch (IOException e) {
 162  0 throw new DefaultSourceFileExceptionList(e);
 163    }
 164  1150 try {
 165  1150 parse(original, validateOnly, in);
 166    } finally {
 167  1150 if (in != null) {
 168  1150 try {
 169  1150 in.close();
 170    } catch (Exception e) {
 171  0 Trace.trace(this, method, e);
 172    }
 173    }
 174    }
 175    }
 176   
 177    /**
 178    * Parse input source.
 179    *
 180    * @param original Original URL for the file. If this is <code>null</code> same as
 181    * file name.
 182    * @param validateOnly validate with {@link #deflt} or parse with {@link #handler}.
 183    * @param in Parse data from this source.
 184    * @throws SourceFileExceptionList Loading failed.
 185    */
 186  1150 private void parse(final URL original, final boolean validateOnly, final InputStream in)
 187    throws SourceFileExceptionList {
 188  1150 final String method = "parse(URL, boolean, InputStream)";
 189  1150 BufferedReader dis = null;
 190  1150 try {
 191  1150 dis = new BufferedReader(new InputStreamReader(in));
 192  1150 final InputSource input = new InputSource(dis);
 193  1150 exceptionList = new DefaultSourceFileExceptionList();
 194  1150 reader.setErrorHandler(new SaxErrorHandler(original, exceptionList));
 195  1150 if (validateOnly) {
 196  585 reader.setContentHandler(deflt);
 197  585 reader.parse(input);
 198    } else {
 199  565 handler.setExceptionList(exceptionList);
 200  565 reader.setContentHandler(handler);
 201  565 handler.setUrl(original);
 202  565 reader.parse(input);
 203    }
 204    } catch (SAXException e) {
 205  6 final SourceFileException ex = new SourceFileException(e);
 206  6 if (exceptionList.size() <= 0) {
 207  1 exceptionList.add(ex);
 208    }
 209  6 throw exceptionList;
 210    } catch (IOException e) {
 211  0 exceptionList.add(e);
 212  0 throw exceptionList;
 213    } finally {
 214  1150 if (dis != null) {
 215  1150 try {
 216  1150 dis.close();
 217    } catch (Exception e) {
 218  0 Trace.trace(this, method, e);
 219    }
 220    }
 221    }
 222  1144 if (exceptionList.size() > 0) {
 223  14 throw exceptionList;
 224    }
 225    }
 226   
 227    /**
 228    * Parses XML file.
 229    *
 230    * @param fileName File name.
 231    * @param original Original URL for the file. If this is <code>null</code> same as
 232    * file name.
 233    * @throws SourceFileExceptionList Loading failed.
 234    */
 235  2 public final void parse(final String fileName, final URL original)
 236    throws SourceFileExceptionList {
 237  2 final File file = new File(fileName);
 238  2 parse(file.getAbsoluteFile(), original);
 239    }
 240   
 241    /**
 242    * Parses the XML file.
 243    *
 244    * @param file File to parse.
 245    * @param original Original URL for the file. If this is <code>null</code> same as
 246    * file.
 247    * @throws SourceFileExceptionList Loading failed.
 248    */
 249  490 public final void parse(final File file, final URL original) throws SourceFileExceptionList {
 250  490 final URL url = IoUtility.toUrl(file.getAbsoluteFile());
 251  490 parse(url, original);
 252    }
 253   
 254    /**
 255    * Parses the XML file.
 256    *
 257    * @param url URL with File to parse.
 258    * @param original Original URL for the file. If this is <code>null</code> same as
 259    * file.
 260    * @throws SourceFileExceptionList Loading failed.
 261    */
 262  585 public final void parse(final URL url, final URL original) throws SourceFileExceptionList {
 263  585 final URL org = (original != null ? original : url);
 264  585 parse(url, org, true);
 265  565 parse(url, org, false);
 266    }
 267   
 268    /**
 269    * Parse input source.
 270    *
 271    * @param in Parse data from this source.
 272    * @param validateOnly Validate or parse with handler.
 273    * @throws SourceFileExceptionList Loading failed.
 274    */
 275  0 public void parse(final InputStream in, final boolean validateOnly)
 276    throws SourceFileExceptionList {
 277   
 278    // validateOnly validate with {@link #deflt} or parse with {@link #handler}.
 279  0 parse(null, validateOnly, in);
 280    }
 281   
 282    /**
 283    * Get errors that occurred during last parsing.
 284    *
 285    * @return List with collected Exceptions.
 286    */
 287  0 public DefaultSourceFileExceptionList getExceptionList() {
 288  0 return exceptionList;
 289    }
 290   
 291    }