EMMA Coverage Report (generated Fri Feb 14 08:28:31 UTC 2014)
[all classes][org.qedeq.kernel.xml.parser]

COVERAGE SUMMARY FOR SOURCE FILE [BasicParser.java]

nameclass, %method, %block, %line, %
BasicParser.java100% (2/2)67%  (6/9)69%  (156/226)74%  (38.3/52)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class BasicParser$1100% (1/1)25%  (1/4)18%  (3/17)25%  (1/4)
getServiceAction (): String 0%   (0/1)0%   (0/2)0%   (0/1)
getServiceDescription (): String 0%   (0/1)0%   (0/2)0%   (0/1)
getServiceId (): String 0%   (0/1)0%   (0/10)0%   (0/1)
BasicParser$1 (): void 100% (1/1)100% (3/3)100% (1/1)
     
class BasicParser100% (1/1)100% (5/5)73%  (153/209)78%  (38.3/49)
createElements (String): Element [] 100% (1/1)51%  (44/87)50%  (7/14)
parse (String, Reader): void 100% (1/1)84%  (54/64)86%  (15.4/18)
createElement (String): Element 100% (1/1)86%  (12/14)75%  (3/4)
<static initializer> 100% (1/1)93%  (13/14)96%  (1.9/2)
BasicParser (SaxDefaultHandler): void 100% (1/1)100% (30/30)100% (11/11)

1/* This file is part of the project "Hilbert II" - http://www.qedeq.org
2 *
3 * Copyright 2000-2014,  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 
16package org.qedeq.kernel.xml.parser;
17 
18import java.io.BufferedReader;
19import java.io.IOException;
20import java.io.Reader;
21 
22import javax.xml.parsers.ParserConfigurationException;
23import javax.xml.parsers.SAXParser;
24import javax.xml.parsers.SAXParserFactory;
25 
26import org.qedeq.base.io.IoUtility;
27import org.qedeq.base.trace.Trace;
28import org.qedeq.kernel.se.base.list.Element;
29import org.qedeq.kernel.se.common.ModuleService;
30import org.qedeq.kernel.se.common.SourceFileExceptionList;
31import org.qedeq.kernel.xml.handler.common.SaxDefaultHandler;
32import org.qedeq.kernel.xml.handler.list.BasicHandler;
33import org.xml.sax.InputSource;
34import org.xml.sax.SAXException;
35import org.xml.sax.XMLReader;
36 
37/**
38 * Parse {@link Element}s.
39 *
40 * @author  Michael Meyling
41 *
42 */
43public 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 ModuleService plugin = new ModuleService() {
56        public String getServiceDescription() {
57            return "parses element lists and atoms";
58        }
59        public String getServiceId() {
60            return BasicParser.class.getName();
61        }
62        public String getServiceAction() {
63            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    private BasicParser(final SaxDefaultHandler handler)
76            throws ParserConfigurationException, SAXException {
77        super();
78        this.handler = handler;
79        final String factoryImpl = System.getProperty("javax.xml.parsers.SAXParserFactory");
80        if (factoryImpl == null) {
81            System.setProperty("javax.xml.parsers.SAXParserFactory",
82                "org.apache.xerces.jaxp.SAXParserFactoryImpl");
83        }
84        SAXParserFactory factory = SAXParserFactory.newInstance();
85        factory.setNamespaceAware(false);
86        factory.setValidating(false);
87 
88        final SAXParser parser = factory.newSAXParser();
89        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    private void parse(final String url, final Reader in)
101            throws IOException, SAXException {
102        final String method = "parse(URL, boolean, InputStream)";
103        BufferedReader dis = null;
104        SourceFileExceptionList exceptionList = new SourceFileExceptionList();;
105        try {
106            dis = new BufferedReader(in);
107            final InputSource input = new InputSource(dis);
108            reader.setErrorHandler(new SaxErrorHandler(plugin,
109                url, exceptionList));
110            handler.setExceptionList(exceptionList);
111            reader.setContentHandler(handler);
112            handler.setUrl(url);
113            reader.parse(input);
114        } finally {
115            if (dis != null) {
116                try {
117                    dis.close();
118                } catch (Exception e) {
119                    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    public static final Element[] createElements(final String xml)
134            throws ParserConfigurationException, SAXException {
135        try {
136            String data = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
137                + "<basic>\n"
138                + xml + "\n"
139                + "</basic>\n";
140            SaxDefaultHandler handler = new SaxDefaultHandler(plugin);
141            BasicHandler simple = new BasicHandler(handler);
142            handler.setBasisDocumentHandler(simple);
143            BasicParser parser = new BasicParser(handler);
144            parser.parse("memory", IoUtility.stringToReader(data));
145            return (Element[]) simple.getElements().toArray(new Element[]{});
146        } catch (SAXException e) {
147            Trace.trace(BasicParser.class, "createElement", e);
148            Trace.trace(BasicParser.class, "createElement", e.getCause());
149            throw e;
150        } catch (IOException e) {
151            Trace.trace(BasicParser.class, "createElement", e);
152            // should not happen, hej we are parsing a String!
153            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    public static final Element createElement(final String xml)
166            throws ParserConfigurationException, SAXException {
167        final Element[] elements = createElements(xml);
168        if (elements == null || elements.length == 0) {
169            return null;
170        }
171        return elements[0];
172    }
173 
174}

[all classes][org.qedeq.kernel.xml.parser]
EMMA 2.1.5320 (stable) (C) Vladimir Roubtsov