Clover Coverage Report
Coverage timestamp: Fri May 24 2013 13:47:27 UTC
../../../../../img/srcFileCovDistChart8.png 62% of files have more coverage
46   174   14   6.57
6   102   0.3   7
7     2  
1    
 
  BasicParser       Line # 43 46 14 74.6% 0.7457627
 
  (288)
 
1    /* This file is part of the project "Hilbert II" - http://www.qedeq.org
2    *
3    * Copyright 2000-2013, Michael Meyling <mime@qedeq.org>.
4    *
5    * "Hilbert II" is free software; you can redistribute
6    * it and/or modify it under the terms of the GNU General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10    * This program is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13    * GNU General Public License for more details.
14    */
15   
16    package org.qedeq.kernel.xml.parser;
17   
18    import java.io.BufferedReader;
19    import java.io.IOException;
20    import java.io.Reader;
21   
22    import javax.xml.parsers.ParserConfigurationException;
23    import javax.xml.parsers.SAXParser;
24    import javax.xml.parsers.SAXParserFactory;
25   
26    import org.qedeq.base.io.IoUtility;
27    import org.qedeq.base.trace.Trace;
28    import org.qedeq.kernel.se.base.list.Element;
29    import org.qedeq.kernel.se.common.SourceFileExceptionList;
30    import org.qedeq.kernel.se.common.Plugin;
31    import org.qedeq.kernel.xml.handler.common.SaxDefaultHandler;
32    import org.qedeq.kernel.xml.handler.list.BasicHandler;
33    import org.xml.sax.InputSource;
34    import org.xml.sax.SAXException;
35    import org.xml.sax.XMLReader;
36   
37    /**
38    * Parse {@link Element}s.
39    *
40    * @author Michael Meyling
41    *
42    */
 
43    public final class BasicParser {
44   
45    /** This class. */
46    private static final Class CLASS = BasicParser.class;
47   
48    /** Here we handle the events. */
49    private SaxDefaultHandler handler;
50   
51    /** XML reader. */
52    private XMLReader reader;
53   
54    /** Here we describe this "plugin". */
55    private static Plugin plugin = new Plugin() {
 
56  0 toggle public String getServiceDescription() {
57  0 return "parses element lists and atoms";
58    }
 
59  0 toggle public String getServiceId() {
60  0 return BasicParser.class.getName();
61    }
 
62  0 toggle public String getServiceAction() {
63  0 return "element parser";
64    }
65    };
66   
67   
68    /**
69    * Constructor.
70    *
71    * @param handler Default handler for this application.
72    * @throws ParserConfigurationException Severe parser configuration problem.
73    * @throws SAXException Parse problems.
74    */
 
75  447 toggle private BasicParser(final SaxDefaultHandler handler)
76    throws ParserConfigurationException, SAXException {
77  447 super();
78  447 this.handler = handler;
79  447 final String factoryImpl = System.getProperty("javax.xml.parsers.SAXParserFactory");
80  447 if (factoryImpl == null) {
81  15 System.setProperty("javax.xml.parsers.SAXParserFactory",
82    "org.apache.xerces.jaxp.SAXParserFactoryImpl");
83    }
84  447 SAXParserFactory factory = SAXParserFactory.newInstance();
85  447 factory.setNamespaceAware(false);
86  447 factory.setValidating(false);
87   
88  447 final SAXParser parser = factory.newSAXParser();
89  447 reader = parser.getXMLReader();
90    }
91   
92    /**
93    * Parse input source.
94    *
95    * @param url Source URL. Only for information.
96    * @param in Parse data from this source.
97    * @throws SAXException Syntactical or semantical problem occurred.
98    * @throws IOException Technical problem occurred.
99    */
 
100  447 toggle private void parse(final String url, final Reader in)
101    throws IOException, SAXException {
102  447 final String method = "parse(URL, boolean, InputStream)";
103  447 BufferedReader dis = null;
104  447 SourceFileExceptionList exceptionList = new SourceFileExceptionList();;
105  447 try {
106  447 dis = new BufferedReader(in);
107  447 final InputSource input = new InputSource(dis);
108  447 reader.setErrorHandler(new SaxErrorHandler(plugin,
109    url, exceptionList));
110  447 handler.setExceptionList(exceptionList);
111  447 reader.setContentHandler(handler);
112  447 handler.setUrl(url);
113  447 reader.parse(input);
114    } finally {
115  447 if (dis != null) {
116  447 try {
117  447 dis.close();
118    } catch (Exception e) {
119  0 Trace.trace(CLASS, this, method, e);
120    }
121    }
122    }
123    }
124   
125    /**
126    * Create elements out of XML string.
127    *
128    * @param xml XML document part. XML header not necessary.
129    * @return Created elements.
130    * @throws ParserConfigurationException Problem configuring parser.
131    * @throws SAXException Parsing problem.
132    */
 
133  447 toggle public static final Element[] createElements(final String xml)
134    throws ParserConfigurationException, SAXException {
135  447 try {
136  447 String data = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
137    + "<basic>\n"
138    + xml + "\n"
139    + "</basic>\n";
140  447 SaxDefaultHandler handler = new SaxDefaultHandler(plugin);
141  447 BasicHandler simple = new BasicHandler(handler);
142  447 handler.setBasisDocumentHandler(simple);
143  447 BasicParser parser = new BasicParser(handler);
144  447 parser.parse("memory", IoUtility.stringToReader(data));
145  447 return (Element[]) simple.getElements().toArray(new Element[]{});
146    } catch (SAXException e) {
147  0 Trace.trace(BasicParser.class, "createElement", e);
148  0 Trace.trace(BasicParser.class, "createElement", e.getCause());
149  0 throw e;
150    } catch (IOException e) {
151  0 Trace.trace(BasicParser.class, "createElement", e);
152    // should not happen, hej we are parsing a String!
153  0 throw new RuntimeException(e);
154    }
155    }
156   
157    /**
158    * Create element out of XML string.
159    *
160    * @param xml XML document part. XML header not necessary.
161    * @return Created element.
162    * @throws ParserConfigurationException Problem configuring parser.
163    * @throws SAXException Parsing problem.
164    */
 
165  447 toggle public static final Element createElement(final String xml)
166    throws ParserConfigurationException, SAXException {
167  447 final Element[] elements = createElements(xml);
168  447 if (elements == null || elements.length == 0) {
169  0 return null;
170    }
171  447 return elements[0];
172    }
173   
174    }