1:
37:
38: package ;
39:
40: import ;
41:
42: import ;
43: import ;
44:
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51:
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57:
58:
59:
66: public final class JAXPFactory
67: extends DocumentBuilderFactory
68: {
69:
70: private static final String PROPERTY = "http://xml.org/sax/properties/";
71: private static final String FEATURE = "http://xml.org/sax/features/";
72:
73: private SAXParserFactory pf;
74: private boolean secureProcessing;
75:
76:
79: public JAXPFactory()
80: {
81: }
82:
83:
87: public DocumentBuilder newDocumentBuilder()
88: throws ParserConfigurationException
89: {
90: if (pf == null)
91: {
92:
93:
94: pf = new gnu.xml.aelfred2.JAXPFactory();
95:
96: }
97:
98:
99: pf.setValidating(isValidating());
100:
101:
102:
103:
104:
105:
106: pf.setNamespaceAware(isNamespaceAware());
107:
108: try
109: {
110:
111: pf.setFeature(FEATURE + "namespace-prefixes", true);
112:
113: return new JAXPBuilder(pf.newSAXParser().getXMLReader(), this);
114: }
115: catch (SAXException e)
116: {
117: String msg = "can't create JAXP DocumentBuilder: " + e.getMessage();
118: throw new ParserConfigurationException(msg);
119: }
120: }
121:
122:
123: public void setAttribute(String name, Object value)
124: throws IllegalArgumentException
125: {
126: if ("http://java.sun.com/xml/jaxp/properties/schemaLanguage".equals(name))
127: {
128:
129: }
130: else
131: {
132: throw new IllegalArgumentException(name);
133: }
134: }
135:
136:
137: public Object getAttribute(String name)
138: throws IllegalArgumentException
139: {
140: throw new IllegalArgumentException(name);
141: }
142:
143: public void setFeature(String name, boolean value)
144: throws ParserConfigurationException
145: {
146: if (name == null)
147: throw new NullPointerException();
148: if (XMLConstants.FEATURE_SECURE_PROCESSING.equals(name))
149: {
150: secureProcessing = true;
151: return;
152: }
153: throw new ParserConfigurationException(name);
154: }
155:
156: public boolean getFeature(String name)
157: throws ParserConfigurationException
158: {
159: if (XMLConstants.FEATURE_SECURE_PROCESSING.equals(name))
160: return secureProcessing;
161: throw new ParserConfigurationException(name);
162: }
163:
164: static final class JAXPBuilder
165: extends DocumentBuilder
166: implements ErrorHandler
167: {
168:
169: private Consumer consumer;
170: private XMLReader producer;
171: private DomImpl impl;
172:
173: JAXPBuilder(XMLReader parser, JAXPFactory factory)
174: throws ParserConfigurationException
175: {
176: impl = new DomImpl();
177:
178:
179: try
180: {
181: consumer = new Consumer();
182: }
183: catch (SAXException e)
184: {
185: throw new ParserConfigurationException(e.getMessage());
186: }
187:
188:
189: consumer.setHidingReferences(factory.isExpandEntityReferences());
190: consumer.setHidingComments(factory.isIgnoringComments());
191: consumer.setHidingWhitespace(factory.isIgnoringElementContentWhitespace());
192: consumer.setHidingCDATA(factory.isCoalescing());
193:
194:
195: producer = parser;
196: producer.setContentHandler(consumer.getContentHandler());
197: producer.setDTDHandler(consumer.getDTDHandler());
198:
199: try
200: {
201: String id;
202:
203:
204:
205: if (factory.isValidating ())
206: {
207: producer.setFeature(FEATURE + "validation", true);
208: producer.setErrorHandler(this);
209: }
210:
211:
212: producer.setFeature(FEATURE + "namespace-prefixes", true);
213: producer.setFeature(FEATURE + "namespaces",
214: factory.isNamespaceAware());
215:
216:
217: id = PROPERTY + "lexical-handler";
218: producer.setProperty(id, consumer.getProperty(id));
219:
220: id = PROPERTY + "declaration-handler";
221: producer.setProperty(id, consumer.getProperty(id));
222:
223: }
224: catch (SAXException e)
225: {
226: throw new ParserConfigurationException(e.getMessage());
227: }
228: }
229:
230: public Document parse(InputSource source)
231: throws SAXException, IOException
232: {
233: producer.parse(source);
234: Document doc = consumer.getDocument();
235:
236: doc.setDocumentURI(source.getSystemId());
237: return doc;
238: }
239:
240: public boolean isNamespaceAware()
241: {
242: try
243: {
244: return producer.getFeature(FEATURE + "namespaces");
245: }
246: catch (SAXException e)
247: {
248:
249: throw new RuntimeException(e.getMessage());
250: }
251: }
252:
253: public boolean isValidating()
254: {
255: try
256: {
257: return producer.getFeature(FEATURE + "validation");
258: }
259: catch (SAXException e)
260: {
261:
262: throw new RuntimeException(e.getMessage());
263: }
264: }
265:
266: public void setEntityResolver(EntityResolver resolver)
267: {
268: producer.setEntityResolver(resolver);
269: }
270:
271: public void setErrorHandler(ErrorHandler handler)
272: {
273: producer.setErrorHandler(handler);
274: consumer.setErrorHandler(handler);
275: }
276:
277: public DOMImplementation getDOMImplementation()
278: {
279: return impl;
280: }
281:
282: public Document newDocument()
283: {
284: return new DomDocument();
285: }
286:
287:
288: public void fatalError(SAXParseException e)
289: throws SAXException
290: {
291: throw e;
292: }
293:
294: public void error(SAXParseException e)
295: throws SAXException
296: {
297: throw e;
298: }
299:
300: public void warning(SAXParseException e)
301: throws SAXException
302: {
303:
304: }
305:
306: }
307:
308: }