1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sourceforge.jeuclid.biparser;
20
21 import java.io.IOException;
22 import java.io.StringReader;
23
24 import javax.xml.parsers.ParserConfigurationException;
25 import javax.xml.parsers.SAXParser;
26 import javax.xml.parsers.SAXParserFactory;
27
28 import org.xml.sax.InputSource;
29 import org.xml.sax.SAXException;
30 import org.xml.sax.SAXParseException;
31 import org.xml.sax.helpers.DefaultHandler;
32
33
34
35
36
37
38 public final class SAXBiParser {
39
40
41 private SAXBiParser() {
42 }
43
44
45 private static final class SingletonHolder {
46
47
48 private static final SAXBiParser INSTANCE = new SAXBiParser();
49
50
51 private SingletonHolder() {
52 }
53 }
54
55
56
57
58
59
60 public static SAXBiParser getInstance() {
61 return SAXBiParser.SingletonHolder.INSTANCE;
62 }
63
64
65
66
67
68
69
70
71
72
73
74 public BiTree parse(final String text)
75 throws NonIncrementalElementException {
76 BiTree tree;
77 DefaultHandler handler;
78 SAXParserFactory factory;
79 SAXParser saxParser;
80 StringReader inStream;
81 InputSource inSource;
82
83 tree = new BiTree();
84 handler = new JEuclidSAXHandler(text, tree);
85 factory = SAXParserFactory.newInstance();
86 inStream = new StringReader(text);
87 inSource = new InputSource(inStream);
88
89 try {
90 factory.setNamespaceAware(true);
91 factory.setValidating(false);
92
93 saxParser = factory.newSAXParser();
94 saxParser.parse(inSource, handler);
95 } catch (final NonIncrementalElementException e) {
96 throw e;
97 } catch (final SAXParseException e) {
98 tree = null;
99 } catch (final ParserConfigurationException e) {
100 e.printStackTrace();
101 tree = null;
102 } catch (final SAXException e) {
103 e.printStackTrace();
104 tree = null;
105 } catch (final IOException e) {
106 e.printStackTrace();
107 tree = null;
108 }
109 return tree;
110 }
111 }