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