Debian Codegraph @

net/sf/saxon/dom/DocumentBuilderImpl.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.dom;
9
10 import net.sf.saxon.Configuration;
11 import net.sf.saxon.event.Sender;
12 import net.sf.saxon.lib.ParseOptions;
13 import net.sf.saxon.lib.Validation;
14 import net.sf.saxon.trans.XPathException;
15 import net.sf.saxon.tree.tiny.TinyBuilder;
16 import net.sf.saxon.tree.tiny.TinyDocumentImpl;
17 import org.w3c.dom.DOMImplementation;
18 import org.w3c.dom.Document;
19 import org.xml.sax.EntityResolver;
20 import org.xml.sax.ErrorHandler;
21 import org.xml.sax.InputSource;
22 import org.xml.sax.SAXException;
23
24 import javax.xml.parsers.DocumentBuilder;
25 import javax.xml.transform.sax.SAXSource;
26 import java.io.File;
27
28 /**
29 * This class implements the JAXP DocumentBuilder interface, allowing a Saxon TinyTree to be
30 * constructed using standard JAXP parsing interfaces. The returned DOM document node is a wrapper
31 * over the Saxon TinyTree structure. Note that although this wrapper
32 * implements the DOM interfaces, it is read-only, and all attempts to update it will throw
33 * an exception. No schema or DTD validation is carried out on the document.
34 * The DocumentBuilder is always namespace-aware.
35 */
36
37 public class DocumentBuilderImpl extends DocumentBuilder {
38
39 private Configuration config;
40 private ParseOptions parseOptions = new ParseOptions();
41
42 /**
43 * Set the Saxon Configuration to be used by the document builder.
44 * This non-JAXP method must be called if the resulting document is to be used
45 * within a Saxon query or transformation. If no Configuration is supplied,
46 * Saxon creates a Configuration on the first call to the {@link #parse} method,
47 * and subsequent calls reuse the same Configuration.
48 * <p>As an alternative to calling this method, a Configuration can be supplied by calling
49 * <code>setAttribute(FeatureKeys.CONFIGURATION, config)</code> on the <code>DocumentBuilderFactory</code>
50 * object, where <code>config</code> can be obtained by calling
51 * <code>getAttribute(FeatureKeys.CONFIGURATION)</code> on the <code>TransformerFactory</code>.</p>
52 *
53 * @param config the Saxon configuration
54 * @since Saxon 8.8
55 */
56
57 public void setConfiguration(Configuration config) {
58 this.config = config;
59 }
60
61 /**
62 * Get the Saxon Configuration to be used by the document builder. This is
63 * a non-JAXP method.
64 *
65 * @return the Configuration previously supplied to {@link #setConfiguration},
66 * or the Configuration created automatically by Saxon on the first call to the
67 * {@link #parse} method, or a newly constructed Configuration if no Configuration has been supplied and
68 * the {@link #parse} method has not been called.
69 * @since Saxon 8.8
70 */
71
72 public Configuration getConfiguration() {
73 if (config == null) {
74 config = new Configuration();
75 }
76 return config;
77 }
78
79 /**
80 * Indicates whether or not this document builder is configured to
81 * understand namespaces.
82 *
83 * @return true if this document builder is configured to understand
84 * namespaces. This implementation always returns true.
85 */
86
87 public boolean isNamespaceAware() {
88 return true;
89 }
90
91 /**
92 * Determine whether the document builder should perform DTD validation
93 *
94 * @param state set to true to request DTD validation
95 */
96
97 public void setValidating(boolean state) {
98 parseOptions.setDTDValidationMode(state ? Validation.STRICT : Validation.SKIP);
99 }
100
101 /**
102 * Indicates whether or not this document builder is configured to
103 * validate XML documents against a DTD.
104 *
105 * @return true if this parser is configured to validate
106 * XML documents against a DTD; false otherwise.
107 */
108
109 public boolean isValidating() {
110 return parseOptions.getDTDValidationMode() == Validation.STRICT;
111 }
112
113 /**
114 * Create a new Document Node.
115 *
116 * @throws UnsupportedOperationException (always). The only way to build a document using this DocumentBuilder
117 * implementation is by using the parse() method.
118 */
119
120 public Document newDocument() {
121 throw new UnsupportedOperationException("The only way to build a document using this DocumentBuilder is with the parse() method");
122 }
123
124 /**
125 * Parse the content of the given input source as an XML document
126 * and return a new DOM {@link Document} object.
127 * <p>Note: for this document to be usable as part of a Saxon query or transformation,
128 * the document should be built within the {@link Configuration} in which that query
129 * or transformation is running. This can be achieved using the non-JAXP
130 * {@link #setConfiguration} method.</p>
131 *
132 * @param in InputSource containing the content to be parsed. Note that if
133 * an EntityResolver or ErrorHandler has been supplied, then the XMLReader contained
134 * in this InputSource will be modified to register this EntityResolver or ErrorHandler,
135 * replacing any that was previously registered.
136 * @return A new DOM Document object.
137 * @throws SAXException If any parse errors occur.
138 */
139
140 public Document parse(InputSource in) throws SAXException {
141 try {
142 if (config == null) {
143 config = new Configuration();
144 }
145 TinyBuilder builder = new TinyBuilder(config.makePipelineConfiguration());
146 builder.setStatistics(config.getTreeStatistics().SOURCE_DOCUMENT_STATISTICS);
147 SAXSource source = new SAXSource(in);
148 source.setSystemId(in.getSystemId());
149 Sender.send(source, builder, parseOptions);
150 TinyDocumentImpl doc = (TinyDocumentImpl) builder.getCurrentRoot();
151 builder.reset();
152 return (Document) DocumentOverNodeInfo.wrap(doc);
153 } catch (XPathException err) {
154 throw new SAXException(err);
155 }
156 }
157
158 /**
159 * Parse the content of the given file as an XML document
160 * and return a new DOM {@link Document} object.
161 * An <code>IllegalArgumentException</code> is thrown if the
162 * <code>File</code> is <code>null</code> null.
163 * <p><i>This implementation differs from the parent implementation
164 * by using a correct algorithm for filename-to-uri conversion.</i></p>
165 *
166 * @param f The file containing the XML to parse.
167 * @return A new DOM Document object.
168 * @throws SAXException If any parse errors occur.
169 */
170
171 public Document parse(/*@Nullable*/ File f) throws SAXException {
172 if (f == null) {
173 throw new IllegalArgumentException("File cannot be null");
174 }
175
176 String uri = f.toURI().toString();
177 InputSource in = new InputSource(uri);
178 return parse(in);
179 }
180
181
182 /**
183 * Specify the {@link EntityResolver} to be used to resolve
184 * entities present in the XML document to be parsed.
185 *
186 * @param er The <code>EntityResolver</code> to be used to resolve entities
187 * present in the XML document to be parsed.
188 */
189
190 public void setEntityResolver(EntityResolver er) {
191 parseOptions.setEntityResolver(er);
192 }
193
194 /**
195 * Specify the {@link ErrorHandler} to be used by the parser.
196 *
197 * @param eh The <code>ErrorHandler</code> to be used by the parser.
198 */
199
200
201 public void setErrorHandler(ErrorHandler eh) {
202 parseOptions.setErrorHandler(eh);
203 }
204
205 /**
206 * Obtain an instance of a {@link DOMImplementation} object.
207 *
208 * @return A new instance of a <code>DOMImplementation</code>.
209 */
210
211 public DOMImplementation getDOMImplementation() {
212 return newDocument().getImplementation();
213 }
214
215 /**
216 * <p>Set state of XInclude processing.</p>
217 * <p>If XInclude markup is found in the document instance, should it be
218 * processed as specified in <a href="http://www.w3.org/TR/xinclude/">
219 * XML Inclusions (XInclude) Version 1.0</a>.</p>
220 * <p>XInclude processing defaults to <code>false</code>.</p>
221 *
222 * @param state Set XInclude processing to <code>true</code> or
223 * <code>false</code>
224 */
225 public void setXIncludeAware(boolean state) {
226 parseOptions.setXIncludeAware(state);
227 }
228
229
230 /**
231 * <p>Get the XInclude processing mode for this parser.</p>
232 *
233 * @return the return value of
234 * the {@link javax.xml.parsers.DocumentBuilderFactory#isXIncludeAware()}
235 * when this parser was created from factory.
236 * @throws UnsupportedOperationException For backward compatibility, when implementations for
237 * earlier versions of JAXP is used, this exception will be
238 * thrown.
239 * @see javax.xml.parsers.DocumentBuilderFactory#setXIncludeAware(boolean)
240 * @since JAXP 1.5, Saxon 8.9
241 */
242 public boolean isXIncludeAware() {
243 return parseOptions.isXIncludeAware();
244 }
245
246 /**
247 * Set the space-stripping action to be applied to the source document
248 *
249 * @param stripAction one of {@link net.sf.saxon.value.Whitespace#IGNORABLE},
250 * {@link net.sf.saxon.value.Whitespace#ALL}, or {@link net.sf.saxon.value.Whitespace#NONE}
251 * @since 8.9
252 */
253
254 public void setStripSpace(int stripAction) {
255 parseOptions.setStripSpace(stripAction);
256 }
257
258 /**
259 * Get the space-stripping action to be applied to the source document
260 *
261 * @return one of {@link net.sf.saxon.value.Whitespace#IGNORABLE},
262 * {@link net.sf.saxon.value.Whitespace#ALL}, or {@link net.sf.saxon.value.Whitespace#NONE}
263 * @since 8.9
264 */
265
266 public int getStripSpace() {
267 return parseOptions.getStripSpace();
268 }
269
270 /**
271 * Set the XML parsing options to be used
272 *
273 * @param options the XML parsing options. Options set using this method will override any options previously set
274 * using other methods; options subsequently set using other methods will modify the parseOptions
275 * object supplied using this method
276 * @since 9.3
277 */
278
279 public void setParseOptions(ParseOptions options) {
280 this.parseOptions = options;
281 }
282
283 /**
284 * Get the XML parsing options that have been set using setParseOptions and other setter methods
285 *
286 * @return the XML parsing options to be used
287 * @since 9.3
288 */
289
290 public ParseOptions getParseOptions() {
291 return parseOptions;
292 }
293
294 }
295
296