Debian Codegraph @

net/sf/saxon/lib/AugmentedSource.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.lib;
9
10 import net.sf.saxon.event.FilterFactory;
11 import net.sf.saxon.om.StructuredQName;
12 import net.sf.saxon.om.TreeModel;
13 import net.sf.saxon.type.SchemaType;
14 import org.xml.sax.EntityResolver;
15 import org.xml.sax.XMLReader;
16
17 import javax.xml.transform.ErrorListener;
18 import javax.xml.transform.Source;
19 import javax.xml.transform.sax.SAXSource;
20 import java.util.List;
21
22 /**
23 * This class is an extension of the JAXP Source interface. The class can be used
24 * wherever a JAXP Source object can be used, and it provides additional information
25 * about the way that the Source is to be processed: for example, it indicates
26 * whether or not it should be validated against a schema. Other options that can
27 * be set include the SAX XMLReader to be used, and the choice of whether a source
28 * in the form of an existing tree should be copied or wrapped.
29 * <p>Internally, an AugmentedSource combines an underlying Source object with a
30 * {@link ParseOptions} object holding the selected options. Many Saxon interfaces allow
31 * the ParseOptions to be supplied directly, making this class unnecessary; but it is useful
32 * when passing a Source to a JAXP interface that does not allow further options to be
33 * supplied.</p>
34 * <p>Note that in general a <code>Source</code> object can only be used once; it is
35 * consumed by use. An augmentedSource object is consumed by use if the underlying
36 * source object is consumed by use.</p>
37 *
38 * @since 8.8
39 */
40
41 public class AugmentedSource implements Source {
42
43 private Source source;
44 private ParseOptions options = new ParseOptions();
45 private String systemID;
46
47 /**
48 * Create an AugmentedSource that wraps a given Source object (which must not itself be an
49 * AugmentedSource)
50 *
51 * @param source the Source object to be wrapped. This must be an implementation of Source
52 * that Saxon recognizes, or an implementation for which a {@link net.sf.saxon.lib.SourceResolver} has been
53 * registered with the {@link net.sf.saxon.Configuration}. The source must not itself be an
54 * AugmentedSource.
55 * <p>As an alternative to this constructor, consider using the factory method
56 * {@link #makeAugmentedSource}, which does accept any kind of Source including an
57 * AugmentedSource as input.</p>
58 * @throws IllegalArgumentException if the wrapped source is an AugmentedSource
59 * @since 8.8
60 */
61
62 private AugmentedSource(Source source) {
63 if (source instanceof AugmentedSource) {
64 throw new IllegalArgumentException("Contained source must not be an AugmentedSource");
65 }
66 this.source = source;
67 }
68
69 /**
70 * Create an AugmentedSource that wraps a given Source object (which must not itself be an
71 * AugmentedSource), with supplied ParseOptions
72 *
73 * @param source the Source object to be wrapped. This must be an implementation of Source
74 * that Saxon recognizes, or an implementation for which a {@link net.sf.saxon.lib.SourceResolver} has been
75 * registered with the {@link net.sf.saxon.Configuration}. The source must not itself be an
76 * AugmentedSource.
77 * <p>As an alternative to this constructor, consider using the factory method
78 * {@link #makeAugmentedSource}, which does accept any kind of Source including an
79 * AugmentedSource as input.</p>
80 * @param options the ParseOptions to be used. This object may be subsequently modified and the changes
81 * will be effective; furthermore, methods on the AugumentedSource will modify the
82 * supplied ParseOptions.
83 * @throws IllegalArgumentException if the wrapped source is an AugmentedSource
84 * @since 8.8
85 */
86
87 public AugmentedSource(Source source, ParseOptions options) {
88 if (source instanceof AugmentedSource) {
89 throw new IllegalArgumentException("Contained source must not be an AugmentedSource");
90 }
91 this.source = source;
92 this.options = options;
93 }
94
95 /**
96 * Create an AugmentedSource that wraps a given Source object. If this is already
97 * an AugmentedSource, the original AugmentedSource is returned. Note that this means that
98 * setting any properties on the returned AugmentedSource will also affect the original.
99 *
100 * @param source the Source object to be wrapped
101 * @return an AugmentedSource
102 * @since 8.8
103 */
104
105 public static AugmentedSource makeAugmentedSource(Source source) {
106 if (source instanceof AugmentedSource) {
107 return (AugmentedSource) source;
108 }
109 return new AugmentedSource(source);
110 }
111
112 /**
113 * Add a filter to the list of filters to be applied to the raw input
114 *
115 * @param filter a factory for the filter to be added
116 */
117
118 public void addFilter(FilterFactory filter) {
119 options.addFilter(filter);
120 }
121
122 /**
123 * Get the list of filters to be applied to the input. Returns null if there are no filters.
124 *
125 * @return the list of filters, if there are any
126 */
127
128 public List<FilterFactory> getFilters() {
129 return options.getFilters();
130 }
131
132 /**
133 * Get the Source object wrapped by this AugmentedSource
134 *
135 * @return the contained Source object
136 * @since 8.8
137 */
138
139 public Source getContainedSource() {
140 return source;
141 }
142
143 /**
144 * Get the ParseOptions defined in this AugmentedSource
145 *
146 * @return the ParseOptions, a bundle of options equivalent to obtaining all the
147 * properties individually. Changes to this object will be live, that is, they will
148 * affect the AugmentedSource from which they came.
149 */
150
151 public ParseOptions getParseOptions() {
152 return options;
153 }
154
155 /**
156 * Set the space-stripping action to be applied to the source document
157 *
158 * @param stripAction one of {@link net.sf.saxon.value.Whitespace#IGNORABLE},
159 * {@link net.sf.saxon.value.Whitespace#ALL}, or {@link net.sf.saxon.value.Whitespace#NONE}
160 * @since 8.8
161 * @deprecated since 9.8 - use getParseOptions().setSpaceStrippingRules()
162 */
163
164 public void setStripSpace(int stripAction) {
165 options.setStripSpace(stripAction);
166 }
167
168 /**
169 * Get the space-stripping action to be applied to the source document
170 *
171 * @return one of {@link net.sf.saxon.value.Whitespace#IGNORABLE},
172 * {@link net.sf.saxon.value.Whitespace#ALL}, or {@link net.sf.saxon.value.Whitespace#NONE}
173 * @since 8.8
174 * @deprecated since 9.8 - use getParseOptions().getSpaceStrippingRules()
175 */
176
177 public int getStripSpace() {
178 return options.getStripSpace();
179 }
180
181 /**
182 * Set the tree model to use. Default is the tiny tree
183 *
184 * @param model typically one of the constants {@link net.sf.saxon.om.TreeModel#TINY_TREE},
185 * {@link net.sf.saxon.om.TreeModel#TINY_TREE_CONDENSED}, or {@link net.sf.saxon.om.TreeModel#LINKED_TREE}. However, in principle
186 * a user-defined tree model can be used.
187 * @since 9.2
188 */
189
190 public void setModel(TreeModel model) {
191 options.setModel(model);
192 }
193
194 /**
195 * Get the tree model that will be used.
196 *
197 * @return typically one of the constants {@link net.sf.saxon.om.TreeModel#TINY_TREE},
198 * {@link TreeModel#TINY_TREE_CONDENSED}, or {@link TreeModel#LINKED_TREE}. However, in principle
199 * a user-defined tree model can be used.
200 * @since 9.2
201 */
202
203 public TreeModel getModel() {
204 return options.getModel();
205 }
206
207
208 /**
209 * Set whether or not schema validation of this source is required
210 *
211 * @param option one of {@link Validation#STRICT},
212 * {@link Validation#LAX}, {@link Validation#STRIP},
213 * {@link Validation#PRESERVE}, {@link Validation#DEFAULT}
214 * @since 8.8
215 */
216
217 public void setSchemaValidationMode(int option) {
218 options.setSchemaValidationMode(option);
219 }
220
221 /**
222 * Get whether or not schema validation of this source is required
223 *
224 * @return the validation mode requested, or {@link Validation#DEFAULT}
225 * to use the default validation mode from the Configuration.
226 * @since 8.8
227 */
228
229 public int getSchemaValidation() {
230 return options.getSchemaValidationMode();
231 }
232
233 /**
234 * Set the name of the top-level element for validation.
235 * If a top-level element is set then the document
236 * being validated must have this as its outermost element
237 *
238 * @param elementName the QName of the required top-level element, or null to unset the value
239 * @since 9.0. Type of argument changed in 9.5.
240 */
241
242 public void setTopLevelElement(StructuredQName elementName) {
243 options.setTopLevelElement(elementName);
244 }
245
246 /**
247 * Get the name of the top-level element for validation.
248 * If a top-level element is set then the document
249 * being validated must have this as its outermost element
250 *
251 * @return the QName of the required top-level element, or null if no value is set
252 * @since 9.0. Type of result changed in 9.5.
253 */
254
255 public StructuredQName getTopLevelElement() {
256 return options.getTopLevelElement();
257 }
258
259 /**
260 * Set the type of the top-level element for validation.
261 * If this is set then the document element is validated against this type
262 *
263 * @param type the schema type required for the document element, or null to unset the value
264 * @since 9.0
265 */
266
267 public void setTopLevelType(SchemaType type) {
268 options.setTopLevelType(type);
269 }
270
271 /**
272 * Get the type of the document element for validation.
273 * If this is set then the document element of the document
274 * being validated must have this type
275 *
276 * @return the type of the required top-level element, or null if no value is set
277 * @since 9.0
278 */
279
280 public SchemaType getTopLevelType() {
281 return options.getTopLevelType();
282 }
283
284 /**
285 * Set whether or not DTD validation of this source is required
286 *
287 * @param option one of {@link Validation#STRICT},
288 * {@link Validation#STRIP}, {@link Validation#DEFAULT}
289 * @since 8.8
290 */
291
292 public void setDTDValidationMode(int option) {
293 options.setDTDValidationMode(option);
294 }
295
296 /**
297 * Get whether or not DTD validation of this source is required
298 *
299 * @return the validation mode requested, or {@link Validation#DEFAULT}
300 * to use the default validation mode from the Configuration.
301 * @since 8.8
302 */
303
304 public int getDTDValidation() {
305 return options.getDTDValidationMode();
306 }
307
308
309 /**
310 * Set whether line numbers are to be maintained in the constructed document
311 *
312 * @param lineNumbering true if line numbers are to be maintained
313 * @since 8.8
314 */
315
316 public void setLineNumbering(boolean lineNumbering) {
317 options.setLineNumbering(lineNumbering);
318 }
319
320 /**
321 * Get whether line numbers are to be maintained in the constructed document
322 *
323 * @return true if line numbers are maintained
324 * @since 8.8
325 */
326
327 public boolean isLineNumbering() {
328 return options.isLineNumbering();
329 }
330
331 /**
332 * Determine whether setLineNumbering() has been called
333 *
334 * @return true if setLineNumbering() has been called
335 * @since 8.9
336 */
337
338 public boolean isLineNumberingSet() {
339 return options.isLineNumberingSet();
340 }
341
342 /**
343 * Set the SAX parser (XMLReader) to be used
344 *
345 * @param parser the SAX parser
346 * @since 8.8
347 */
348
349 public void setXMLReader(XMLReader parser) {
350 options.setXMLReader(parser);
351 if (source instanceof SAXSource) {
352 ((SAXSource) source).setXMLReader(parser);
353 }
354 }
355
356 /**
357 * Get the SAX parser (XMLReader) to be used
358 *
359 * @return the parser
360 * @since 8.8
361 */
362
363 /*@Nullable*/
364 public XMLReader getXMLReader() {
365 XMLReader parser = options.getXMLReader();
366 if (parser != null) {
367 return parser;
368 } else if (source instanceof SAXSource) {
369 return ((SAXSource) source).getXMLReader();
370 } else {
371 return null;
372 }
373 }
374
375 /**
376 * Assuming that the contained Source is a node in a tree, indicate whether a tree should be created
377 * as a view of this supplied tree, or as a copy.
378 * <p>This option is used only when the Source is supplied to an interface such as the JAXP
379 * Transformer.transform() method where there is no other way of indicating whether a supplied
380 * external document should be wrapped or copied. It is not used when the Source is supplied to
381 * a Saxon-defined interface.</p>
382 *
383 * @param wrap if true, the node in the supplied Source is wrapped, to create a view. If false, the node
384 * and its contained subtree is copied. If null, the system default is chosen.
385 * @since 8.8
386 */
387
388 public void setWrapDocument(Boolean wrap) {
389 options.setWrapDocument(wrap);
390 }
391
392 /**
393 * Assuming that the contained Source is a node in a tree, determine whether a tree will be created
394 * as a view of this supplied tree, or as a copy.
395 * <p>This option is used only when the Source is supplied to an interface such as the JAXP
396 * Transformer.transform() method where there is no other way of indicating whether a supplied
397 * external document should be wrapped or copied. It is not used when the Source is supplied to
398 * a Saxon-defined interface.</p>
399 *
400 * @return if true, the node in the supplied Source is wrapped, to create a view. If false, the node
401 * and its contained subtree is copied. If null, the system default is chosen.
402 * @since 8.8
403 */
404
405 public Boolean getWrapDocument() {
406 return options.getWrapDocument();
407 }
408
409 /**
410 * Set the System ID. This sets the System Id to be returned by getSystemId.
411 *
412 * @param id the System ID. This provides a base URI for the document, and also the result
413 * of the document-uri() function
414 * @since 8.8, changed in 9.7 it no longer modifies the original source object
415 */
416
417 public void setSystemId(String id) {
418 systemID = id;
419 }
420
421 /**
422 * Get the System ID. Returns the System Id set by setSystemId if available,
423 * or the System Id on the underlying Source object otherwise.
424 *
425 * @return the System ID: effectively the base URI.
426 * @since 8.8
427 */
428
429 public String getSystemId() {
430 return systemID != null ? systemID : source.getSystemId();
431 }
432
433 /**
434 * <p>Set state of XInclude processing.</p>
435 * <p>If XInclude markup is found in the document instance, should it be
436 * processed as specified in <a href="http://www.w3.org/TR/xinclude/">
437 * XML Inclusions (XInclude) Version 1.0</a>.</p>
438 * <p>XInclude processing defaults to <code>false</code>.</p>
439 *
440 * @param state Set XInclude processing to <code>true</code> or
441 * <code>false</code>
442 * @since 8.9
443 */
444 public void setXIncludeAware(boolean state) {
445 options.setXIncludeAware(state);
446 }
447
448 /**
449 * <p>Determine whether setXIncludeAware() has been called.</p>
450 *
451 * @return true if setXIncludeAware() has been called
452 * @since 8.9
453 */
454
455 public boolean isXIncludeAwareSet() {
456 return options.isXIncludeAwareSet();
457 }
458
459 /**
460 * <p>Get state of XInclude processing.</p>
461 *
462 * @return current state of XInclude processing. Default value is false.
463 * @since 8.9
464 */
465
466 public boolean isXIncludeAware() {
467 return options.isXIncludeAware();
468 }
469
470 /**
471 * Set an EntityResolver to be used when parsing. Note this is used only
472 * when a system-allocated parser is used; when a user-supplied parser
473 * is used (for example in a SAXSource), the EntityResolver should be preinitialized.
474 *
475 * @param resolver the EntityResolver to be used
476 * @since 8.9. The method had no useful effect in releases prior to 9.2.
477 */
478
479 public void setEntityResolver(EntityResolver resolver) {
480 options.setEntityResolver(resolver);
481 }
482
483 /**
484 * Get the EntityResolver that will be used when parsing
485 *
486 * @return the EntityResolver, if one has been set using {@link #setEntityResolver},
487 * otherwise null.
488 * @since 8.9 The method had no useful effect in releases prior to 9.2.
489 */
490
491 public EntityResolver getEntityResolver() {
492 return options.getEntityResolver();
493 }
494
495 /**
496 * Set an ErrorListener to be used when parsing
497 *
498 * @param listener the ErrorListener to be used
499 * @since 8.9
500 */
501
502 public void setErrorListener(ErrorListener listener) {
503 options.setErrorListener(listener);
504 }
505
506 /**
507 * Get the ErrorListener that will be used when parsing
508 *
509 * @return the ErrorListener, if one has been set using {@link #setErrorListener},
510 * otherwise null.
511 * @since 8.9
512 */
513
514 public UnfailingErrorListener getErrorListener() {
515 return options.getErrorListener();
516 }
517
518
519 /**
520 * Set whether or not the user of this Source is encouraged to close it as soon as reading is finished.
521 * Normally the expectation is that any Stream in a StreamSource will be closed by the component that
522 * created the Stream. However, in the case of a Source returned by a URIResolver, there is no suitable
523 * interface (the URIResolver has no opportunity to close the stream). Also, in some cases such as reading
524 * of stylesheet modules, it is possible to close the stream long before control is returned to the caller
525 * who supplied it. This tends to make a difference on .NET, where a file often can't be opened if there
526 * is a stream attached to it.
527 *
528 * @param close true if the source should be closed as soon as it has been consumed
529 * @since 8.8
530 */
531
532 public void setPleaseCloseAfterUse(boolean close) {
533 options.setPleaseCloseAfterUse(close);
534 }
535
536 /**
537 * Determine whether or not the user of this Source is encouraged to close it as soon as reading is
538 * finished.
539 *
540 * @return true if the source should be closed as soon as it has been consumed
541 * @since 8.8
542 */
543
544 public boolean isPleaseCloseAfterUse() {
545 return options.isPleaseCloseAfterUse();
546 }
547
548 /**
549 * Close any resources held by this Source. This only works if the underlying Source is one that is
550 * recognized as holding closable resources.
551 *
552 * @since 8.8
553 */
554
555 public void close() {
556 ParseOptions.close(source);
557 }
558
559
560 }
561