2 Copyright (C) 2001 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
40 import java
.io
.IOException
;
42 import org
.w3c
.dom
.Document
;
43 import org
.w3c
.dom
.DOMImplementation
;
45 import org
.xml
.sax
.EntityResolver
;
46 import org
.xml
.sax
.ErrorHandler
;
47 import org
.xml
.sax
.InputSource
;
48 import org
.xml
.sax
.XMLReader
;
49 import org
.xml
.sax
.SAXException
;
50 import org
.xml
.sax
.SAXParseException
;
52 import javax
.xml
.XMLConstants
;
53 import javax
.xml
.parsers
.DocumentBuilder
;
54 import javax
.xml
.parsers
.DocumentBuilderFactory
;
55 import javax
.xml
.parsers
.ParserConfigurationException
;
56 import javax
.xml
.parsers
.SAXParserFactory
;
60 * DOM bootstrapping API, for use with JAXP.
64 * @author David Brownell
66 public final class JAXPFactory
67 extends DocumentBuilderFactory
70 private static final String PROPERTY
= "http://xml.org/sax/properties/";
71 private static final String FEATURE
= "http://xml.org/sax/features/";
73 private SAXParserFactory pf
;
74 private boolean secureProcessing
;
77 * Default constructor.
84 * Constructs a JAXP document builder which uses the default
85 * JAXP SAX2 parser and the DOM implementation in this package.
87 public DocumentBuilder
newDocumentBuilder()
88 throws ParserConfigurationException
92 // Force use of AElfred2 since not all JAXP parsers
93 // conform very well to the SAX2 API spec ...
94 pf
= new gnu
.xml
.aelfred2
.JAXPFactory();
95 // pf = SAXParserFactory.newInstance ();
98 // JAXP default: false
99 pf
.setValidating(isValidating());
101 // FIXME: this namespace setup may cause errors in some
102 // conformant SAX2 parsers, which we CAN patch up by
103 // splicing a "NSFilter" stage up front ...
105 // JAXP default: false
106 pf
.setNamespaceAware(isNamespaceAware());
110 // undo rude "namespace-prefixes=false" default
111 pf
.setFeature(FEATURE
+ "namespace-prefixes", true);
113 return new JAXPBuilder(pf
.newSAXParser().getXMLReader(), this);
115 catch (SAXException e
)
117 String msg
= "can't create JAXP DocumentBuilder: " + e
.getMessage();
118 throw new ParserConfigurationException(msg
);
122 /** There seems to be no useful specification for attribute names */
123 public void setAttribute(String name
, Object value
)
124 throws IllegalArgumentException
126 if ("http://java.sun.com/xml/jaxp/properties/schemaLanguage".equals(name
))
132 throw new IllegalArgumentException(name
);
136 /** There seems to be no useful specification for attribute names */
137 public Object
getAttribute(String name
)
138 throws IllegalArgumentException
140 throw new IllegalArgumentException(name
);
143 public void setFeature(String name
, boolean value
)
144 throws ParserConfigurationException
147 throw new NullPointerException();
148 if (XMLConstants
.FEATURE_SECURE_PROCESSING
.equals(name
))
150 secureProcessing
= true;
153 throw new ParserConfigurationException(name
);
156 public boolean getFeature(String name
)
157 throws ParserConfigurationException
159 if (XMLConstants
.FEATURE_SECURE_PROCESSING
.equals(name
))
160 return secureProcessing
;
161 throw new ParserConfigurationException(name
);
164 static final class JAXPBuilder
165 extends DocumentBuilder
166 implements ErrorHandler
169 private Consumer consumer
;
170 private XMLReader producer
;
171 private DomImpl impl
;
173 JAXPBuilder(XMLReader parser
, JAXPFactory factory
)
174 throws ParserConfigurationException
176 impl
= new DomImpl();
178 // set up consumer side
181 consumer
= new Consumer();
183 catch (SAXException e
)
185 throw new ParserConfigurationException(e
.getMessage());
188 // JAXP defaults: true, noise nodes are good (bleech)
189 consumer
.setHidingReferences(factory
.isExpandEntityReferences());
190 consumer
.setHidingComments(factory
.isIgnoringComments());
191 consumer
.setHidingWhitespace(factory
.isIgnoringElementContentWhitespace());
192 consumer
.setHidingCDATA(factory
.isCoalescing());
194 // set up producer side
196 producer
.setContentHandler(consumer
.getContentHandler());
197 producer
.setDTDHandler(consumer
.getDTDHandler());
203 // if validating, report validity errors, and default
204 // to treating them as fatal
205 if (factory
.isValidating ())
207 producer
.setFeature(FEATURE
+ "validation", true);
208 producer
.setErrorHandler(this);
211 // always save prefix info, maybe do namespace processing
212 producer
.setFeature(FEATURE
+ "namespace-prefixes", true);
213 producer
.setFeature(FEATURE
+ "namespaces",
214 factory
.isNamespaceAware());
216 // set important handlers
217 id
= PROPERTY
+ "lexical-handler";
218 producer
.setProperty(id
, consumer
.getProperty(id
));
220 id
= PROPERTY
+ "declaration-handler";
221 producer
.setProperty(id
, consumer
.getProperty(id
));
224 catch (SAXException e
)
226 throw new ParserConfigurationException(e
.getMessage());
230 public Document
parse(InputSource source
)
231 throws SAXException
, IOException
233 producer
.parse(source
);
234 Document doc
= consumer
.getDocument();
235 // TODO inputEncoding
236 doc
.setDocumentURI(source
.getSystemId());
240 public boolean isNamespaceAware()
244 return producer
.getFeature(FEATURE
+ "namespaces");
246 catch (SAXException e
)
249 throw new RuntimeException(e
.getMessage());
253 public boolean isValidating()
257 return producer
.getFeature(FEATURE
+ "validation");
259 catch (SAXException e
)
262 throw new RuntimeException(e
.getMessage());
266 public void setEntityResolver(EntityResolver resolver
)
268 producer
.setEntityResolver(resolver
);
271 public void setErrorHandler(ErrorHandler handler
)
273 producer
.setErrorHandler(handler
);
274 consumer
.setErrorHandler(handler
);
277 public DOMImplementation
getDOMImplementation()
282 public Document
newDocument()
284 return new DomDocument();
287 // implementation of error handler that's used when validating
288 public void fatalError(SAXParseException e
)
294 public void error(SAXParseException e
)
300 public void warning(SAXParseException e
)