View Javadoc

1   /* This file is part of the project "Hilbert II" - http://www.qedeq.org" target="alexandria_uri">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  
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.ModuleService;
30  import org.qedeq.kernel.se.common.SourceFileExceptionList;
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 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 }