001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002 *
003 * Copyright 2000-2011, Michael Meyling <mime@qedeq.org>.
004 *
005 * "Hilbert II" is free software; you can redistribute
006 * it and/or modify it under the terms of the GNU General Public
007 * License as published by the Free Software Foundation; either
008 * version 2 of the License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU General Public License for more details.
014 */
015
016 package org.qedeq.kernel.xml.parser;
017
018 import java.io.BufferedReader;
019 import java.io.IOException;
020 import java.io.Reader;
021
022 import javax.xml.parsers.ParserConfigurationException;
023 import javax.xml.parsers.SAXParser;
024 import javax.xml.parsers.SAXParserFactory;
025
026 import org.qedeq.base.io.IoUtility;
027 import org.qedeq.base.trace.Trace;
028 import org.qedeq.kernel.se.base.list.Element;
029 import org.qedeq.kernel.se.common.DefaultSourceFileExceptionList;
030 import org.qedeq.kernel.se.common.Plugin;
031 import org.qedeq.kernel.xml.handler.common.SaxDefaultHandler;
032 import org.qedeq.kernel.xml.handler.list.BasicHandler;
033 import org.xml.sax.InputSource;
034 import org.xml.sax.SAXException;
035 import org.xml.sax.XMLReader;
036
037 /**
038 * Parse {@link Element}s.
039 *
040 * @author Michael Meyling
041 *
042 */
043 public final class BasicParser {
044
045 /** This class. */
046 private static final Class CLASS = BasicParser.class;
047
048 /** Here we handle the events. */
049 private SaxDefaultHandler handler;
050
051 /** XML reader. */
052 private XMLReader reader;
053
054 /** Here we describe this "plugin". */
055 private static Plugin plugin = new Plugin() {
056 public String getPluginDescription() {
057 return "parses element lists and atoms";
058 }
059 public String getPluginId() {
060 return BasicParser.class.getName();
061 }
062 public String getPluginActionName() {
063 return "element parser";
064 }
065 };
066
067
068 /**
069 * Constructor.
070 *
071 * @param handler Default handler for this application.
072 * @throws ParserConfigurationException Severe parser configuration problem.
073 * @throws SAXException Parse problems.
074 */
075 private BasicParser(final SaxDefaultHandler handler)
076 throws ParserConfigurationException, SAXException {
077 super();
078 this.handler = handler;
079 final String factoryImpl = System.getProperty("javax.xml.parsers.SAXParserFactory");
080 if (factoryImpl == null) {
081 System.setProperty("javax.xml.parsers.SAXParserFactory",
082 "org.apache.xerces.jaxp.SAXParserFactoryImpl");
083 }
084 SAXParserFactory factory = SAXParserFactory.newInstance();
085 factory.setNamespaceAware(false);
086 factory.setValidating(false);
087
088 final SAXParser parser = factory.newSAXParser();
089 reader = parser.getXMLReader();
090 }
091
092 /**
093 * Parse input source.
094 *
095 * @param url Source URL. Only for information.
096 * @param in Parse data from this source.
097 * @throws SAXException Syntactical or semantical problem occurred.
098 * @throws IOException Technical problem occurred.
099 */
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 DefaultSourceFileExceptionList exceptionList = new DefaultSourceFileExceptionList();;
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 }
|