1
2
3
4
5
6
7
8
9
10 package org.dom4j;
11
12 import java.util.Enumeration;
13 import java.util.HashMap;
14 import java.util.Hashtable;
15 import java.util.Iterator;
16 import java.util.Map;
17 import javax.xml.parsers.ParserConfigurationException;
18 import javax.xml.parsers.SAXParser;
19 import javax.xml.parsers.SAXParserFactory;
20 import org.dom4j.io.aelfred2.SAXDriver;
21 import org.xml.sax.Parser;
22 import org.xml.sax.SAXException;
23 import org.xml.sax.SAXNotRecognizedException;
24 import org.xml.sax.SAXNotSupportedException;
25 import org.xml.sax.XMLReader;
26 import org.xml.sax.helpers.XMLReaderAdapter;
27
28
29
30
31
32
33
34 /***
35 *
36 * @author Maarten Coene
37 */
38 public class JAXPFactory extends SAXParserFactory {
39
40 private Map features = new HashMap();
41
42 /***
43 * Constructs a factory which normally returns a non-validating
44 * parser.
45 */
46 public JAXPFactory () {
47 }
48
49 public SAXParser newSAXParser () throws ParserConfigurationException, SAXException {
50 SAXParser result = new InternalParser();
51 XMLReader parser = result.getXMLReader ();
52
53 parser.setFeature(
54 "http://xml.org/sax/features/namespaces",
55 isNamespaceAware());
56 parser.setFeature(
57 "http://xml.org/sax/features/validation",
58 isValidating());
59
60 Iterator it = features.entrySet().iterator();
61 while (it.hasNext()) {
62 Map.Entry entry = (Map.Entry) it.next();
63 String name = (String) entry.getKey();
64 Boolean value = (Boolean) entry.getValue();
65 parser.setFeature (name, value.booleanValue ());
66 }
67
68 return result;
69 }
70
71 public void setFeature (String name, boolean value) {
72 features.put(name, new Boolean(value));
73 }
74
75 public boolean getFeature(String name) {
76 Boolean value = (Boolean) features.get (name);
77
78 if (value != null)
79 return value.booleanValue ();
80 else {
81 return false;
82 }
83 }
84
85 private static class InternalParser extends SAXParser {
86
87 private SAXDriver aelfred;
88 private XMLReaderAdapter parser;
89
90 InternalParser() {
91 super();
92
93 aelfred = new SAXDriver();
94 parser = new XMLReaderAdapter(aelfred);
95 }
96
97 public void setProperty(String id, Object value) throws SAXNotRecognizedException,
98 SAXNotSupportedException {
99 aelfred.setProperty(id, value);
100 }
101
102 public Object getProperty(String id) throws SAXNotRecognizedException,
103 SAXNotSupportedException {
104 return aelfred.getProperty(id);
105 }
106
107 public Parser getParser() {
108 return parser;
109 }
110
111 public XMLReader getXMLReader() {
112 return aelfred;
113 }
114
115 public boolean isNamespaceAware() {
116 try {
117 return aelfred.getFeature("http://xml.org/sax/features/namespaces");
118 } catch (Exception e) {
119 return false;
120 }
121 }
122
123 public boolean isValidating() {
124 try {
125 return aelfred.getFeature("http://xml.org/sax/features/validation");
126 } catch (Exception e) {
127 return false;
128 }
129 }
130 }
131 }
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179