60 |
* |
* |
61 |
* @author David Brownell |
* @author David Brownell |
62 |
*/ |
*/ |
63 |
public final class JAXPFactory extends SAXParserFactory |
public final class JAXPFactory |
64 |
|
extends SAXParserFactory |
65 |
{ |
{ |
66 |
private Hashtable flags = new Hashtable (); |
|
67 |
|
private Hashtable flags = new Hashtable(); |
68 |
|
|
69 |
/** |
/** |
70 |
* Constructs a factory which normally returns a non-validating |
* Constructs a factory which normally returns a non-validating |
71 |
* parser. |
* parser. |
72 |
*/ |
*/ |
73 |
public JAXPFactory () { } |
public JAXPFactory() |
74 |
|
{ |
75 |
|
} |
76 |
|
|
77 |
public SAXParser newSAXParser () |
public SAXParser newSAXParser() |
78 |
throws ParserConfigurationException, SAXException |
throws ParserConfigurationException, SAXException |
79 |
|
{ |
80 |
|
JaxpParser jaxp = new JaxpParser(); |
81 |
|
Enumeration e = flags.keys(); |
82 |
|
XMLReader parser = jaxp.getXMLReader(); |
83 |
|
|
84 |
|
parser.setFeature(SAXDriver.FEATURE + "namespaces", |
85 |
|
isNamespaceAware()); |
86 |
|
parser.setFeature(SAXDriver.FEATURE + "validation", |
87 |
|
isValidating()); |
88 |
|
// that makes SAX2 feature flags trump JAXP |
89 |
|
|
90 |
|
while (e.hasMoreElements()) |
91 |
|
{ |
92 |
|
String uri = (String) e.nextElement(); |
93 |
|
Boolean value = (Boolean) flags.get(uri); |
94 |
|
parser.setFeature(uri, value.booleanValue()); |
95 |
|
} |
96 |
|
|
97 |
|
return jaxp; |
98 |
|
} |
99 |
|
|
100 |
|
// yes, this "feature transfer" mechanism doesn't play well |
101 |
|
|
102 |
|
public void setFeature(String name, boolean value) |
103 |
|
throws ParserConfigurationException, SAXNotRecognizedException, |
104 |
|
SAXNotSupportedException |
105 |
|
{ |
106 |
|
try |
107 |
|
{ |
108 |
|
// force "early" detection of errors where possible |
109 |
|
// (flags can't necessarily be set before parsing) |
110 |
|
new JaxpParser().getXMLReader().setFeature(name, value); |
111 |
|
|
112 |
|
flags.put(name, new Boolean(value)); |
113 |
|
} |
114 |
|
catch (SAXNotRecognizedException e) |
115 |
|
{ |
116 |
|
throw new SAXNotRecognizedException(name); |
117 |
|
} |
118 |
|
catch (SAXNotSupportedException e) |
119 |
|
{ |
120 |
|
throw new SAXNotSupportedException(name); |
121 |
|
} |
122 |
|
catch (Exception e) |
123 |
|
{ |
124 |
|
throw new ParserConfigurationException(e.getClass().getName() |
125 |
|
+ ": " |
126 |
|
+ e.getMessage()); |
127 |
|
} |
128 |
|
} |
129 |
|
|
130 |
|
public boolean getFeature(String name) |
131 |
|
throws ParserConfigurationException, SAXNotRecognizedException, |
132 |
|
SAXNotSupportedException |
133 |
|
{ |
134 |
|
Boolean value = (Boolean) flags.get(name); |
135 |
|
|
136 |
|
if (value != null) |
137 |
|
{ |
138 |
|
return value.booleanValue(); |
139 |
|
} |
140 |
|
else |
141 |
|
{ |
142 |
|
try |
143 |
|
{ |
144 |
|
return new JaxpParser().getXMLReader().getFeature(name); |
145 |
|
} |
146 |
|
catch (SAXNotRecognizedException e) |
147 |
|
{ |
148 |
|
throw new SAXNotRecognizedException(name); |
149 |
|
} |
150 |
|
catch (SAXNotSupportedException e) |
151 |
|
{ |
152 |
|
throw new SAXNotSupportedException(name); |
153 |
|
} |
154 |
|
catch (SAXException e) |
155 |
|
{ |
156 |
|
throw new ParserConfigurationException(e.getClass().getName() |
157 |
|
+ ": " |
158 |
|
+ e.getMessage()); |
159 |
|
} |
160 |
|
} |
161 |
|
} |
162 |
|
|
163 |
|
private static class JaxpParser |
164 |
|
extends SAXParser |
165 |
|
{ |
166 |
|
|
167 |
|
private XmlReader ae2 = new XmlReader(); |
168 |
|
private XMLReaderAdapter parser = null; |
169 |
|
|
170 |
|
JaxpParser() |
171 |
{ |
{ |
172 |
JaxpParser jaxp = new JaxpParser (); |
} |
|
Enumeration e = flags.keys (); |
|
|
XMLReader parser = jaxp.getXMLReader (); |
|
|
|
|
|
parser.setFeature ( |
|
|
SAXDriver.FEATURE + "namespaces", |
|
|
isNamespaceAware ()); |
|
|
parser.setFeature ( |
|
|
SAXDriver.FEATURE + "validation", |
|
|
isValidating ()); |
|
|
// that makes SAX2 feature flags trump JAXP |
|
|
|
|
|
while (e.hasMoreElements ()) { |
|
|
String uri = (String) e.nextElement (); |
|
|
Boolean value = (Boolean) flags.get (uri); |
|
|
parser.setFeature (uri, value.booleanValue ()); |
|
|
} |
|
|
|
|
|
return jaxp; |
|
|
} |
|
|
|
|
|
// yes, this "feature transfer" mechanism doesn't play well |
|
|
|
|
|
public void setFeature (String name, boolean value) |
|
|
throws |
|
|
ParserConfigurationException, |
|
|
SAXNotRecognizedException, |
|
|
SAXNotSupportedException |
|
|
{ |
|
|
try { |
|
|
// force "early" detection of errors where possible |
|
|
// (flags can't necessarily be set before parsing) |
|
|
new JaxpParser ().getXMLReader ().setFeature (name, value); |
|
|
|
|
|
flags.put (name, new Boolean (value)); |
|
|
} catch (SAXNotRecognizedException e) { |
|
|
throw new SAXNotRecognizedException (name); |
|
|
} catch (SAXNotSupportedException e) { |
|
|
throw new SAXNotSupportedException (name); |
|
|
} catch (Exception e) { |
|
|
throw new ParserConfigurationException ( |
|
|
e.getClass ().getName () |
|
|
+ ": " |
|
|
+ e.getMessage ()); |
|
|
} |
|
|
} |
|
|
|
|
|
public boolean getFeature (String name) |
|
|
throws |
|
|
ParserConfigurationException, |
|
|
SAXNotRecognizedException, |
|
|
SAXNotSupportedException |
|
|
{ |
|
|
Boolean value = (Boolean) flags.get (name); |
|
|
|
|
|
if (value != null) |
|
|
return value.booleanValue (); |
|
|
else |
|
|
try { |
|
|
return new JaxpParser ().getXMLReader ().getFeature (name); |
|
|
} catch (SAXNotRecognizedException e) { |
|
|
throw new SAXNotRecognizedException (name); |
|
|
} catch (SAXNotSupportedException e) { |
|
|
throw new SAXNotSupportedException (name); |
|
|
} catch (SAXException e) { |
|
|
throw new ParserConfigurationException ( |
|
|
e.getClass ().getName () |
|
|
+ ": " |
|
|
+ e.getMessage ()); |
|
|
} |
|
|
} |
|
|
|
|
|
private static class JaxpParser extends SAXParser |
|
|
{ |
|
|
private XmlReader ae2 = new XmlReader (); |
|
|
private XMLReaderAdapter parser = null; |
|
|
|
|
|
JaxpParser () { } |
|
|
|
|
|
public void setProperty (String id, Object value) |
|
|
throws SAXNotRecognizedException, SAXNotSupportedException |
|
|
{ ae2.setProperty (id, value); } |
|
|
|
|
|
public Object getProperty (String id) |
|
|
throws SAXNotRecognizedException, SAXNotSupportedException |
|
|
{ return ae2.getProperty (id); } |
|
|
|
|
|
public Parser getParser () |
|
|
throws SAXException |
|
|
{ |
|
|
if (parser == null) |
|
|
parser = new XMLReaderAdapter (ae2); |
|
|
return parser; |
|
|
} |
|
|
|
|
|
public XMLReader getXMLReader () |
|
|
throws SAXException |
|
|
{ return ae2; } |
|
|
|
|
|
public boolean isNamespaceAware () |
|
|
{ |
|
|
try { |
|
|
return ae2.getFeature (SAXDriver.FEATURE + "namespaces"); |
|
|
} catch (Exception e) { |
|
|
throw new Error (); |
|
|
} |
|
|
} |
|
|
|
|
|
public boolean isValidating () |
|
|
{ |
|
|
try { |
|
|
return ae2.getFeature (SAXDriver.FEATURE + "validation"); |
|
|
} catch (Exception e) { |
|
|
throw new Error (); |
|
|
} |
|
|
} |
|
173 |
|
|
174 |
// TODO isXIncludeAware() |
public void setProperty(String id, Object value) |
175 |
|
throws SAXNotRecognizedException, SAXNotSupportedException |
176 |
|
{ |
177 |
|
ae2.setProperty(id, value); |
178 |
|
} |
179 |
|
|
180 |
|
public Object getProperty(String id) |
181 |
|
throws SAXNotRecognizedException, SAXNotSupportedException |
182 |
|
{ |
183 |
|
return ae2.getProperty(id); |
184 |
|
} |
185 |
|
|
186 |
|
public Parser getParser() |
187 |
|
throws SAXException |
188 |
|
{ |
189 |
|
if (parser == null) |
190 |
|
{ |
191 |
|
parser = new XMLReaderAdapter(ae2); |
192 |
|
} |
193 |
|
return parser; |
194 |
} |
} |
195 |
|
|
196 |
|
public XMLReader getXMLReader () |
197 |
|
throws SAXException |
198 |
|
{ |
199 |
|
return ae2; |
200 |
|
} |
201 |
|
|
202 |
|
public boolean isNamespaceAware() |
203 |
|
{ |
204 |
|
try |
205 |
|
{ |
206 |
|
return ae2.getFeature(SAXDriver.FEATURE + "namespaces"); |
207 |
|
} |
208 |
|
catch (Exception e) |
209 |
|
{ |
210 |
|
throw new Error(); |
211 |
|
} |
212 |
|
} |
213 |
|
|
214 |
|
public boolean isValidating() |
215 |
|
{ |
216 |
|
try |
217 |
|
{ |
218 |
|
return ae2.getFeature(SAXDriver.FEATURE + "validation"); |
219 |
|
} |
220 |
|
catch (Exception e) |
221 |
|
{ |
222 |
|
throw new Error(); |
223 |
|
} |
224 |
|
} |
225 |
|
|
226 |
|
// TODO isXIncludeAware() |
227 |
|
|
228 |
|
} |
229 |
|
|
230 |
} |
} |
231 |
|
|