1
2
3
4
5
6
7
8
9
10
11
12
13
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
39
40
41
42
43 public final class BasicParser {
44
45
46 private static final Class CLASS = BasicParser.class;
47
48
49 private SaxDefaultHandler handler;
50
51
52 private XMLReader reader;
53
54
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
70
71
72
73
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
94
95
96
97
98
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
127
128
129
130
131
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
153 throw new RuntimeException(e);
154 }
155 }
156
157
158
159
160
161
162
163
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 }