Debian Codegraph @

net/sf/saxon/lib/ParseOptions.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.Configuration;
11 import net.sf.saxon.event.Builder;
12 import net.sf.saxon.event.FilterFactory;
13 import net.sf.saxon.expr.accum.Accumulator;
14 import net.sf.saxon.om.*;
15 import net.sf.saxon.trans.Maker;
16 import net.sf.saxon.trans.XPathException;
17 import net.sf.saxon.type.SchemaType;
18 import net.sf.saxon.type.ValidationParams;
19 import net.sf.saxon.value.Whitespace;
20 import org.xml.sax.EntityResolver;
21 import org.xml.sax.ErrorHandler;
22 import org.xml.sax.InputSource;
23 import org.xml.sax.XMLReader;
24
25 import javax.xml.transform.ErrorListener;
26 import javax.xml.transform.Source;
27 import javax.xml.transform.sax.SAXSource;
28 import javax.xml.transform.stream.StreamSource;
29 import java.io.IOException;
30 import java.util.*;
31
32
33 /**
34 * This class defines options for parsing and/or validating a source document. Some of the options
35 * are relevant only when parsing, some only when validating, but they are combined into a single
36 * class because the two operations are often performed together.
37 */
38
39 public class ParseOptions {
40
41 private int schemaValidation = Validation.DEFAULT;
42 private int dtdValidation = Validation.DEFAULT;
43 private StructuredQName topLevelElement;
44 private SchemaType topLevelType;
45 /*@Nullable*/ private transient XMLReader parser = null;
46 private Maker<XMLReader> parserMaker;
47 /*@Nullable*/ private Boolean wrapDocument = null;
48 /*@Nullable*/ private TreeModel treeModel = null;
49 //private int stripSpace = Whitespace.UNSPECIFIED;
50 private SpaceStrippingRule spaceStrippingRule = null;
51 /*@Nullable*/ private Boolean lineNumbering = null;
52 private boolean pleaseClose = false;
53 /*@Nullable*/ private transient UnfailingErrorListener errorListener = null;
54 /*@Nullable*/ private transient EntityResolver entityResolver = null;
55 /*@Nullable*/ private transient ErrorHandler errorHandler = null;
56 /*@Nullable*/ private List<FilterFactory> filters = null;
57 private boolean continueAfterValidationErrors = false;
58 private boolean addCommentsAfterValidationErrors = false;
59 private boolean expandAttributeDefaults = true;
60 private boolean useXsiSchemaLocation = true;
61 private boolean checkEntityReferences = false;
62 private boolean stable = true;
63 private int validationErrorLimit = Integer.MAX_VALUE;
64 /*@Nullable*/ private ValidationParams validationParams = null;
65 /*@Nullable*/ private ValidationStatisticsRecipient validationStatisticsRecipient = null;
66 private Map<String, Boolean> parserFeatures = null;
67 private Map<String, Object> parserProperties = null;
68 private InvalidityHandler invalidityHandler = null;
69 private Set<? extends Accumulator> applicableAccumulators = null; // null means "all"
70
71 /**
72 * Create a ParseOptions object with default options set
73 */
74
75 public ParseOptions() {
76 //entityResolver = new StandardEntityResolver();
77 }
78
79 /**
80 * Create a ParseOptions object as a copy of another ParseOptions
81 *
82 * @param p the ParseOptions to be copied
83 */
84
85 public ParseOptions(/*@NotNull*/ ParseOptions p) {
86 schemaValidation = p.schemaValidation;
87 validationParams = p.validationParams;
88 setDTDValidationMode(p.dtdValidation);
89 topLevelElement = p.topLevelElement;
90 topLevelType = p.topLevelType;
91 parserMaker = p.getXMLReaderMaker();
92 parser = p.parser;
93 wrapDocument = p.wrapDocument;
94 treeModel = p.treeModel;
95 spaceStrippingRule = p.spaceStrippingRule;
96 lineNumbering = p.lineNumbering;
97 pleaseClose = p.pleaseClose;
98 errorHandler = p.errorHandler;
99 errorListener = p.errorListener;
100 entityResolver = p.entityResolver;
101 invalidityHandler = p.invalidityHandler;
102 stable = p.stable;
103 if (p.filters != null) {
104 filters = new ArrayList<>(p.filters);
105 }
106 setExpandAttributeDefaults(p.expandAttributeDefaults);
107 useXsiSchemaLocation = p.useXsiSchemaLocation;
108 validationErrorLimit = p.validationErrorLimit;
109 continueAfterValidationErrors = p.continueAfterValidationErrors;
110 addCommentsAfterValidationErrors = p.addCommentsAfterValidationErrors;
111 if (p.parserFeatures != null) {
112 parserFeatures = new HashMap<>(p.parserFeatures);
113 }
114 if (p.parserProperties != null) {
115 parserProperties = new HashMap<>(p.parserProperties);
116 }
117 applicableAccumulators = p.applicableAccumulators;
118 checkEntityReferences = p.checkEntityReferences;
119 validationStatisticsRecipient = p.validationStatisticsRecipient;
120 }
121
122 /**
123 * Merge another set of {@code ParseOptions} into these {@code ParseOptions}.
124 * Properties in the other {@code ParseOptions} take precedence over properties
125 * in these {@code ParseOptions}. "Taking precedence" here means:
126 *
127 * <ul>
128 * <li>If a non-default value for a property is present in one {@code ParseOptions} and the
129 * default value is present in the other, the non-default value is used.</li>
130 * <li>If both {@code ParseOptions} objects have non-default values for a property, then the
131 * value is taken from the one that "takes precedence".</li>
132 * </ul>
133 *
134 * @param options the set of {@code ParseOptions} properties to be merged in.
135 */
136
137 public void merge(/*@NotNull*/ ParseOptions options) {
138 if (options.dtdValidation != Validation.DEFAULT) {
139 setDTDValidationMode(options.dtdValidation);
140 }
141 if (options.schemaValidation != Validation.DEFAULT) {
142 schemaValidation = options.schemaValidation;
143 }
144 if (options.invalidityHandler != null) {
145 invalidityHandler = options.invalidityHandler;
146 }
147 if (options.topLevelElement != null) {
148 topLevelElement = options.topLevelElement;
149 }
150 if (options.topLevelType != null) {
151 topLevelType = options.topLevelType;
152 }
153 if (options.parser != null) {
154 parser = options.parser;
155 }
156 if (options.wrapDocument != null) {
157 wrapDocument = options.wrapDocument;
158 }
159 if (options.treeModel != null) {
160 treeModel = options.treeModel;
161 }
162 if (options.spaceStrippingRule != null) {
163 spaceStrippingRule = options.spaceStrippingRule;
164 }
165 if (options.lineNumbering != null) {
166 lineNumbering = options.lineNumbering;
167 }
168 if (options.pleaseClose) {
169 pleaseClose = true;
170 }
171 if (options.errorListener != null) {
172 errorListener = options.errorListener;
173 }
174 if (options.entityResolver != null) {
175 entityResolver = options.entityResolver;
176 }
177 if (options.filters != null) {
178 if (filters == null) {
179 filters = new ArrayList<>();
180 }
181 filters.addAll(options.filters);
182 }
183 if (options.parserFeatures != null) {
184 if (parserFeatures == null) {
185 parserFeatures = new HashMap<>();
186 }
187 parserFeatures.putAll(options.parserFeatures);
188 }
189 if (options.parserProperties != null) {
190 if (parserProperties == null) {
191 parserProperties = new HashMap<>();
192 }
193 parserProperties.putAll(options.parserProperties);
194 }
195 if (!options.expandAttributeDefaults) {
196 // expand defaults unless the other options says don't
197 setExpandAttributeDefaults(false);
198 }
199 if (!options.useXsiSchemaLocation) {
200 // expand defaults unless the other options says don't
201 useXsiSchemaLocation = false;
202 }
203 if (options.addCommentsAfterValidationErrors) {
204 // add comments if either set of options requests it
205 addCommentsAfterValidationErrors = true;
206 }
207 validationErrorLimit = java.lang.Math.min(validationErrorLimit, options.validationErrorLimit);
208 }
209
210 /**
211 * Merge settings from the Configuration object into these parseOptions
212 *
213 * @param config the Configuration. Settings from the Configuration are
214 * used only where no setting is present in this ParseOptions object
215 */
216
217 public void applyDefaults(/*@NotNull*/ Configuration config) {
218 if (dtdValidation == Validation.DEFAULT) {
219 setDTDValidationMode(config.isValidation() ? Validation.STRICT : Validation.SKIP);
220 }
221 if (schemaValidation == Validation.DEFAULT) {
222 schemaValidation = config.getSchemaValidationMode();
223 }
224 if (treeModel == null) {
225 treeModel = TreeModel.getTreeModel(config.getTreeModel());
226 }
227 if (spaceStrippingRule == null) {
228 spaceStrippingRule = config.getParseOptions().getSpaceStrippingRule();
229 }
230 if (lineNumbering == null) {
231 lineNumbering = config.isLineNumbering();
232 }
233 if (errorListener == null) {
234 setErrorListener(config.getErrorListener());
235 }
236 // if (invalidityHandler == null) {
237 // invalidityHandler = new InvalidityHandlerWrappingErrorListener(errorListener);
238 // }
239
240 }
241
242 /**
243 * Add a filter to the list of filters to be applied to the raw input
244 *
245 * @param filterFactory the filterFactory to be added
246 */
247
248 public void addFilter(FilterFactory filterFactory) {
249 if (filters == null) {
250 filters = new ArrayList<>(5);
251 }
252 filters.add(filterFactory);
253 }
254
255 /**
256 * Get the list of filters to be applied to the input. Returns null if there are no filters.
257 *
258 * @return the list of filters, if there are any
259 */
260
261 /*@Nullable*/
262 public List<FilterFactory> getFilters() {
263 return filters;
264 }
265
266 /**
267 * Set the space-stripping action to be applied to the source document
268 *
269 * @param stripAction one of {@link net.sf.saxon.value.Whitespace#IGNORABLE},
270 * {@link net.sf.saxon.value.Whitespace#ALL}, or {@link net.sf.saxon.value.Whitespace#NONE}
271 * @deprecated since 9.8. Use {@link #setSpaceStrippingRule}
272 */
273
274 public void setStripSpace(int stripAction) {
275 switch (stripAction) {
276 case Whitespace.ALL:
277 spaceStrippingRule = AllElementsSpaceStrippingRule.getInstance();
278 break;
279 case Whitespace.NONE:
280 spaceStrippingRule = NoElementsSpaceStrippingRule.getInstance();
281 break;
282 case Whitespace.IGNORABLE:
283 spaceStrippingRule = IgnorableSpaceStrippingRule.getInstance();
284 break;
285 case Whitespace.UNSPECIFIED:
286 spaceStrippingRule = null;
287 break;
288 default:
289 throw new IllegalArgumentException();
290 }
291 }
292
293 /**
294 * Get the space-stripping action to be applied to the source document
295 *
296 * @return one of {@link net.sf.saxon.value.Whitespace#IGNORABLE},
297 * {@link net.sf.saxon.value.Whitespace#ALL}, or {@link net.sf.saxon.value.Whitespace#NONE}
298 * @deprecated since 9.8. Use {@link #getSpaceStrippingRule}
299 */
300
301 public int getStripSpace() {
302 if (spaceStrippingRule == AllElementsSpaceStrippingRule.getInstance()) {
303 return Whitespace.ALL;
304 } else if (spaceStrippingRule == NoElementsSpaceStrippingRule.getInstance()) {
305 return Whitespace.NONE;
306 } else if (spaceStrippingRule == IgnorableSpaceStrippingRule.getInstance()) {
307 return Whitespace.IGNORABLE;
308 } else if (spaceStrippingRule == null) {
309 return Whitespace.IGNORABLE;
310 } else {
311 return Whitespace.XSLT;
312 }
313 }
314
315 public SpaceStrippingRule getSpaceStrippingRule() {
316 return spaceStrippingRule;
317 }
318
319 public void setSpaceStrippingRule(SpaceStrippingRule rule) {
320 spaceStrippingRule = rule;
321 }
322
323 /**
324 * Set the tree model to use. Default is the tiny tree
325 *
326 * @param model one of {@link net.sf.saxon.event.Builder#TINY_TREE},
327 * {@link net.sf.saxon.event.Builder#LINKED_TREE} or {@link net.sf.saxon.event.Builder#TINY_TREE_CONDENSED}
328 */
329
330 public void setTreeModel(int model) {
331 treeModel = TreeModel.getTreeModel(model);
332 }
333
334 /**
335 * Add a parser feature to a map, which will be applied to the XML parser later
336 *
337 * @param uri The features as a URIs
338 * @param value The value given to the feature as boolean
339 */
340 public void addParserFeature(String uri, boolean value) {
341 if (parserFeatures == null) {
342 parserFeatures = new HashMap<>();
343 }
344 parserFeatures.put(uri, value);
345 }
346
347 /**
348 * Add a parser property to a map, which is applied to the XML parser later
349 *
350 * @param uri The properties as a URIs
351 * @param value The value given to the properties as a string
352 */
353 public void addParserProperties(String uri, Object value) {
354 if (parserProperties == null) {
355 parserProperties = new HashMap<>();
356 }
357 parserProperties.put(uri, value);
358 }
359
360 /**
361 * Get a particular parser feature added
362 *
363 * @param uri The feature name as a URIs
364 * @return The feature value as boolean
365 */
366 public boolean getParserFeature(String uri) {
367 return parserFeatures.get(uri);
368 }
369
370 /**
371 * Get a particular parser property added
372 *
373 * @param name The properties as a URIs
374 * @return The property value (which may be any object)
375 */
376 public Object getParserProperty(String name) {
377 return parserProperties.get(name);
378 }
379
380 /**
381 * Get all the parser features added
382 *
383 * @return A map of (feature, value) pairs
384 */
385 public Map<String, Boolean> getParserFeatures() {
386 return parserFeatures;
387 }
388
389 /**
390 * Get all the parser properties added
391 *
392 * @return A map of (feature, string) pairs
393 */
394 public Map<String, Object> getParserProperties() {
395 return parserProperties;
396 }
397
398 /**
399 * Get the tree model that will be used.
400 *
401 * @return one of {@link net.sf.saxon.event.Builder#TINY_TREE}, {@link net.sf.saxon.event.Builder#LINKED_TREE},
402 * or {@link net.sf.saxon.event.Builder#TINY_TREE_CONDENSED},
403 * or {@link net.sf.saxon.event.Builder#UNSPECIFIED_TREE_MODEL} if no call on setTreeModel() has been made
404 */
405
406 public int getTreeModel() {
407 if (treeModel == null) {
408 return Builder.UNSPECIFIED_TREE_MODEL;
409 }
410 return treeModel.getSymbolicValue();
411 }
412
413 /**
414 * Set the tree model to use. Default is the tiny tree
415 *
416 * @param model typically one of the constants {@link net.sf.saxon.om.TreeModel#TINY_TREE},
417 * {@link TreeModel#TINY_TREE_CONDENSED}, or {@link TreeModel#LINKED_TREE}. However, in principle
418 * a user-defined tree model can be used.
419 * @since 9.2
420 */
421
422 public void setModel(TreeModel model) {
423 treeModel = model;
424 }
425
426 /**
427 * Get the tree model that will be used.
428 *
429 * @return typically one of the constants {@link net.sf.saxon.om.TreeModel#TINY_TREE},
430 * {@link TreeModel#TINY_TREE_CONDENSED}, or {@link TreeModel#LINKED_TREE}. However, in principle
431 * a user-defined tree model can be used.
432 */
433
434 public TreeModel getModel() {
435 return treeModel == null ? TreeModel.TINY_TREE : treeModel;
436 }
437
438
439 /**
440 * Set whether or not schema validation of this source is required
441 *
442 * @param option one of {@link Validation#STRICT},
443 * {@link Validation#LAX}, {@link Validation#STRIP},
444 * {@link Validation#PRESERVE}, {@link Validation#DEFAULT}
445 */
446
447 public void setSchemaValidationMode(int option) {
448 schemaValidation = option;
449 }
450
451 /**
452 * Get whether or not schema validation of this source is required
453 *
454 * @return the validation mode requested, or {@link Validation#DEFAULT}
455 * to use the default validation mode from the Configuration.
456 */
457
458 public int getSchemaValidationMode() {
459 return schemaValidation;
460 }
461
462 /**
463 * Set whether to expand default attributes defined in a DTD or schema.
464 * By default, default attribute values are expanded
465 *
466 * @param expand true if missing attribute values are to take the default value
467 * supplied in a DTD or schema, false if they are to be left as absent
468 */
469
470 public void setExpandAttributeDefaults(boolean expand) {
471 this.expandAttributeDefaults = expand;
472 }
473
474 /**
475 * Ask whether to expand default attributes defined in a DTD or schema.
476 * By default, default attribute values are expanded
477 *
478 * @return true if missing attribute values are to take the default value
479 * supplied in a DTD or schema, false if they are to be left as absent
480 */
481
482 public boolean isExpandAttributeDefaults() {
483 return expandAttributeDefaults;
484 }
485
486 /**
487 * Set the name of the top-level element for validation.
488 * If a top-level element is set then the document
489 * being validated must have this as its outermost element
490 *
491 * @param elementName the QName of the required top-level element, or null to unset the value
492 */
493
494 public void setTopLevelElement(StructuredQName elementName) {
495 topLevelElement = elementName;
496 }
497
498 /**
499 * Get the name of the top-level element for validation.
500 * If a top-level element is set then the document
501 * being validated must have this as its outermost element
502 *
503 * @return the QName of the required top-level element, or null if no value is set
504 * @since 9.0
505 */
506
507 public StructuredQName getTopLevelElement() {
508 return topLevelElement;
509 }
510
511 /**
512 * Set the type of the top-level element for validation.
513 * If this is set then the document element is validated against this type
514 *
515 * @param type the schema type required for the document element, or null to unset the value
516 */
517
518 public void setTopLevelType(SchemaType type) {
519 topLevelType = type;
520 }
521
522 /**
523 * Get the type of the document element for validation.
524 * If this is set then the document element of the document
525 * being validated must have this type
526 *
527 * @return the type of the required top-level element, or null if no value is set
528 */
529
530 public SchemaType getTopLevelType() {
531 return topLevelType;
532 }
533
534 /**
535 * Set whether or not to use the xsi:schemaLocation and xsi:noNamespaceSchemaLocation attributes
536 * in an instance document to locate a schema for validation. Note, these attribute are only used
537 * if validation is requested.
538 *
539 * @param use true if these attributes are to be used, false if they are to be ignored
540 */
541
542 public void setUseXsiSchemaLocation(boolean use) {
543 useXsiSchemaLocation = use;
544 }
545
546 /**
547 * Ask whether or not to use the xsi:schemaLocation and xsi:noNamespaceSchemaLocation attributes
548 * in an instance document to locate a schema for validation. Note, these attribute are only used
549 * if validation is requested.
550 *
551 * @return true (the default) if these attributes are to be used, false if they are to be ignored
552 */
553
554 public boolean isUseXsiSchemaLocation() {
555 return useXsiSchemaLocation;
556 }
557
558 /**
559 * Get the limit on the number of errors reported before schema validation is abandoned. Default
560 * is unlimited (Integer.MAX_VALUE)
561 *
562 * @return the limit on the number of errors
563 */
564
565 public int getValidationErrorLimit() {
566 return validationErrorLimit;
567 }
568
569 /**
570 * Set a limit on the number of errors reported before schema validation is abandoned. Default
571 * is unlimited (Integer.MAX_VALUE). If set to one, validation is terminated as soon as a single
572 * validation error is detected.
573 *
574 * @param validationErrorLimit the limit on the number of errors
575 */
576
577 public void setValidationErrorLimit(int validationErrorLimit) {
578 this.validationErrorLimit = validationErrorLimit;
579 }
580
581 /**
582 * Set whether or not DTD validation of this source is required
583 *
584 * @param option one of {@link Validation#STRICT}, {@link Validation#LAX},
585 * {@link Validation#STRIP}, {@link Validation#DEFAULT}.
586 * <p>The value {@link Validation#LAX} indicates that DTD validation is
587 * requested, but validation failures are treated as warnings only.</p>
588 */
589
590 public void setDTDValidationMode(int option) {
591 dtdValidation = option;
592 addParserFeature("http://xml.org/sax/features/validation",
593 option == Validation.STRICT || option == Validation.LAX);
594 }
595
596 /**
597 * Get whether or not DTD validation of this source is required
598 *
599 * @return the validation mode requested, or {@link Validation#DEFAULT}
600 * to use the default validation mode from the Configuration.
601 * <p>The value {@link Validation#LAX} indicates that DTD validation is
602 * requested, but validation failures are treated as warnings only.</p>
603 */
604
605 public int getDTDValidationMode() {
606 return dtdValidation;
607 }
608
609 /**
610 * Say that statistics of component usage are maintained during schema validation, and indicate where
611 * they should be sent
612 *
613 * @param recipient the agent to be notified of the validation statistics on completion of the
614 * validation episode, May be set to null if no agent is to be notified.
615 */
616
617 public void setValidationStatisticsRecipient(/*@Nullable*/ ValidationStatisticsRecipient recipient) {
618 validationStatisticsRecipient = recipient;
619 }
620
621 /**
622 * Ask whether statistics of component usage are maintained during schema validation,
623 * and where they will be sent
624 *
625 * @return the agent to be notified of the validation statistics on completion of the
626 * validation episode, or null if none has been nominated
627 */
628
629 /*@Nullable*/
630 public ValidationStatisticsRecipient getValidationStatisticsRecipient() {
631 return validationStatisticsRecipient;
632 }
633
634 /**
635 * Set whether line numbers are to be maintained in the constructed document
636 *
637 * @param lineNumbering true if line numbers are to be maintained
638 */
639
640 public void setLineNumbering(boolean lineNumbering) {
641 this.lineNumbering = lineNumbering;
642 }
643
644 /**
645 * Get whether line numbers are to be maintained in the constructed document
646 *
647 * @return true if line numbers are maintained
648 */
649
650 public boolean isLineNumbering() {
651 return lineNumbering != null && lineNumbering;
652 }
653
654 /**
655 * Determine whether setLineNumbering() has been called
656 *
657 * @return true if setLineNumbering() has been called
658 */
659
660 public boolean isLineNumberingSet() {
661 return lineNumbering != null;
662 }
663
664 /**
665 * Set the SAX parser (XMLReader) to be used. This method must be used with care, because
666 * an XMLReader is not thread-safe. If there is any chance that this ParseOptions object will
667 * be used in multiple threads, then this property should not be set. Instead, set the XMLReaderMaker
668 * property, which allows a new parser to be created each time it is needed.
669 *
670 * @param parser the SAX parser
671 */
672
673 public void setXMLReader(XMLReader parser) {
674 this.parser = parser;
675 }
676
677 /**
678 * Get the SAX parser (XMLReader) to be used.
679 * @return the parser
680 */
681
682 /*@Nullable*/
683 public XMLReader getXMLReader() {
684 return parser;
685 }
686
687 /**
688 * Set the parser factory class to be used.
689 *
690 * @param parserMaker a factory object that delivers an XMLReader on demand
691 */
692
693 public void setXMLReaderMaker(Maker<XMLReader> parserMaker) {
694 this.parserMaker = parserMaker;
695 }
696
697 /**
698 * Get the parser factory class to be used
699 *
700 * @return a factory object that delivers an XMLReader on demand, or null if none has been set
701 */
702
703 /*@Nullable*/
704 public Maker<XMLReader> getXMLReaderMaker() {
705 return parserMaker;
706 }
707
708 /**
709 * Obtain an XMLReader (parser), by making one using the XMLReaderMaker if available, or by
710 * returning the registered XMLReader if available, or failing that, return null
711 */
712
713 public XMLReader obtainXMLReader() throws XPathException {
714 if (parserMaker != null) {
715 return parserMaker.make();
716 } else if (parser != null) {
717 return parser;
718 } else {
719 return null;
720 }
721 }
722
723
724 /**
725 * Set an EntityResolver to be used when parsing. Note that this will not be used if an XMLReader
726 * has been supplied (in that case, the XMLReader should be initialized with the EntityResolver
727 * already set.)
728 *
729 * @param resolver the EntityResolver to be used. May be null, in which case any existing
730 * EntityResolver is removed from the options
731 */
732
733 public void setEntityResolver(/*@Nullable*/ EntityResolver resolver) {
734 entityResolver = resolver;
735 }
736
737 /**
738 * Get the EntityResolver that will be used when parsing
739 *
740 * @return the EntityResolver, if one has been set using {@link #setEntityResolver},
741 * otherwise null.
742 */
743
744 /*@Nullable*/
745 public EntityResolver getEntityResolver() {
746 return entityResolver;
747 }
748
749 /**
750 * Set an ErrorHandler to be used when parsing. Note that this will not be used if an XMLReader
751 * has been supplied (in that case, the XMLReader should be initialized with the ErrorHandler
752 * already set.)
753 *
754 * @param handler the ErrorHandler to be used, or null to indicate that no ErrorHandler is to be used.
755 */
756
757 public void setErrorHandler(/*@Nullable*/ ErrorHandler handler) {
758 errorHandler = handler;
759 }
760
761 /**
762 * Get the ErrorHandler that will be used when parsing
763 *
764 * @return the ErrorHandler, if one has been set using {@link #setErrorHandler},
765 * otherwise null.
766 */
767
768 /*@Nullable*/
769 public ErrorHandler getErrorHandler() {
770 return errorHandler;
771 }
772
773
774 /**
775 * Assuming that the contained Source is a node in a tree, indicate whether a tree should be created
776 * as a view of this supplied tree, or as a copy.
777 *
778 * @param wrap if true, the node in the supplied Source is wrapped, to create a view. If false, the node
779 * and its contained subtree is copied. If null, the system default is chosen.
780 */
781
782 public void setWrapDocument(/*@Nullable*/ Boolean wrap) {
783 wrapDocument = wrap;
784 }
785
786 /**
787 * Assuming that the contained Source is a node in a tree, determine whether a tree will be created
788 * as a view of this supplied tree, or as a copy.
789 *
790 * @return if true, the node in the supplied Source is wrapped, to create a view. If false, the node
791 * and its contained subtree is copied. If null, the system default is chosen.
792 * @since 8.8
793 */
794
795 /*@Nullable*/
796 public Boolean getWrapDocument() {
797 return wrapDocument;
798 }
799
800 /**
801 * <p>Set state of XInclude processing.</p>
802 * <p>If XInclude markup is found in the document instance, should it be
803 * processed as specified in <a href="http://www.w3.org/TR/xinclude/">
804 * XML Inclusions (XInclude) Version 1.0</a>.</p>
805 * <p>XInclude processing defaults to <code>false</code>.</p>
806 *
807 * @param state Set XInclude processing to <code>true</code> or
808 * <code>false</code>
809 * @since 8.9
810 */
811 public void setXIncludeAware(boolean state) {
812 addParserFeature("http://apache.org/xml/features/xinclude", state);
813 }
814
815 /**
816 * <p>Determine whether setXIncludeAware() has been called.</p>
817 *
818 * @return true if setXIncludeAware() has been called
819 */
820
821 public boolean isXIncludeAwareSet() {
822 return parserFeatures != null && parserFeatures.get("http://apache.org/xml/features/xinclude") != null;
823 }
824
825 /**
826 * <p>Get state of XInclude processing.</p>
827 *
828 * @return current state of XInclude processing. Default value is false.
829 */
830
831 public boolean isXIncludeAware() {
832 if (parserFeatures == null) {
833 return false;
834 } else {
835 Boolean b = parserFeatures.get("http://apache.org/xml/features/xinclude");
836 return b != null && b;
837 }
838 }
839
840 /**
841 * Set an ErrorListener to be used when parsing
842 *
843 * @param listener the ErrorListener to be used; or null, to indicate that the standard ErrorListener is to be used.
844 * If necessary the supplied ErrorListener will be wrapped with one that suppresses any checked Exceptions thrown
845 * by its methods.
846 */
847
848 public void setErrorListener(/*@Nullable*/ ErrorListener listener) {
849 if (listener == null) {
850 listener = new StandardErrorListener();
851 }
852 if (listener instanceof UnfailingErrorListener) {
853 errorListener = (UnfailingErrorListener) listener;
854 } else {
855 errorListener = new DelegatingErrorListener(listener);
856 }
857 }
858
859 /**
860 * Get the ErrorListener that will be used when parsing
861 *
862 * @return the ErrorListener, if one has been set using {@link #setErrorListener},
863 * otherwise null.
864 */
865
866 /*@Nullable*/
867 public UnfailingErrorListener getErrorListener() {
868 return errorListener;
869 }
870
871 /**
872 * Say that processing should continue after a validation error. Note that all validation
873 * errors are reported to the error() method of the ErrorListener, and processing always
874 * continues except when this method chooses to throw an exception. At the end of the document,
875 * a fatal error is thrown if (a) there have been any validation errors, and (b) this option
876 * is not set.
877 *
878 * @param keepGoing true if processing should continue
879 */
880
881 public void setContinueAfterValidationErrors(boolean keepGoing) {
882 continueAfterValidationErrors = keepGoing;
883 }
884
885 /**
886 * Ask whether processing should continue after a validation error. Note that all validation
887 * errors are reported to the error() method of the ErrorListener, and processing always
888 * continues except when this method chooses to throw an exception. At the end of the document,
889 * a fatal error is thrown if (a) there have been any validation errors, and (b) this option
890 * is not set.
891 *
892 * @return true if processing should continue
893 */
894
895 public boolean isContinueAfterValidationErrors() {
896 return continueAfterValidationErrors;
897 }
898
899 /**
900 * Say that on validation errors, messages explaining the error should (where possible)
901 * be written as comments in the validated source document. This option is only relevant when
902 * processing continues after a validation error
903 *
904 * @param keepGoing true if comments should be added
905 * @since 9.3. Default is now false; in previous releases this option was always on.
906 */
907
908 public void setAddCommentsAfterValidationErrors(boolean keepGoing) {
909 addCommentsAfterValidationErrors = keepGoing;
910 }
911
912 /**
913 * Ask whether on validation errors, messages explaining the error should (where possible)
914 * be written as comments in the validated source document. This option is only relevant when
915 * processing continues after a validation error
916 *
917 * @return true if comments should be added
918 * @since 9.3
919 */
920
921 public boolean isAddCommentsAfterValidationErrors() {
922 return addCommentsAfterValidationErrors;
923 }
924
925 /**
926 * Set the validation parameters. These are the values of variables declared in the schema
927 * using the saxon:param extension, and referenced in XSD assertions (or CTA expressions)
928 * associated with user-defined types
929 *
930 * @param params the validation parameters
931 */
932
933 public void setValidationParams(ValidationParams params) {
934 validationParams = params;
935 }
936
937 /**
938 * Get the validation parameters. These are the values of variables declared in the schema
939 * using the saxon:param extension, and referenced in XSD assertions (or CTA expressions)
940 * associated with user-defined types
941 *
942 * @return the validation parameters
943 */
944
945 public ValidationParams getValidationParams() {
946 return validationParams;
947 }
948
949
950 /**
951 * Say whether to check elements and attributes of type xs:ENTITY (or xs:ENTITIES)
952 * against the unparsed entities declared in the document's DTD. This is normally
953 * true when performing standalone schema validation, false when invoking validation
954 * from XSLT or XQuery.
955 *
956 * @param check true if entities are to be checked, false otherwise
957 */
958
959 public void setCheckEntityReferences(boolean check) {
960 this.checkEntityReferences = check;
961 }
962
963 /**
964 * Ask whether to check elements and attributes of type xs:ENTITY (or xs:ENTITIES)
965 * against the unparsed entities declared in the document's DTD. This is normally
966 * true when performing standalone schema validation, false when invoking validation
967 * from XSLT or XQuery.
968 *
969 * @return true if entities are to be checked, false otherwise
970 */
971
972 public boolean isCheckEntityReferences() {
973 return this.checkEntityReferences;
974 }
975
976 /**
977 * Ask whether the document (or collection) should be stable, that is, if repeated attempts to dereference
978 * the same URI are guaranteed to return the same result. By default, documents and collections are stable.
979 *
980 * @return true if the document or collection is stable
981 */
982
983 public boolean isStable() {
984 return stable;
985 }
986
987 /**
988 * Say whether the document (or collection) should be stable, that is, if repeated attempts to dereference
989 * the same URI are guaranteed to return the same result. By default, documents and collections are stable.
990 *
991 * @param stable true if the document or collection is stable
992 */
993
994 public void setStable(boolean stable) {
995 this.stable = stable;
996 }
997
998 /**
999 * Get the callback for reporting validation errors
1000 *
1001 * @return the registered InvalidityHandler
1002 */
1003
1004 public InvalidityHandler getInvalidityHandler() {
1005 return invalidityHandler;
1006 }
1007
1008 /**
1009 * Set the callback for reporting validation errors
1010 *
1011 * @param invalidityHandler the InvalidityHandler to be used for reporting validation failures
1012 */
1013
1014 public void setInvalidityHandler(InvalidityHandler invalidityHandler) {
1015 this.invalidityHandler = invalidityHandler;
1016 }
1017
1018 /**
1019 * Set the list of XSLT 3.0 accumulators that apply to this tree.
1020 *
1021 * @param accumulators the accumulators that apply; or null if all accumulators apply.
1022 * (Note, this is not the same as the meaning of #all in the
1023 * use-accumulators attribute, which refers to all accumulators
1024 * declared in a given package).
1025 */
1026
1027 public void setApplicableAccumulators(Set<? extends Accumulator> accumulators) {
1028 this.applicableAccumulators = accumulators;
1029 }
1030
1031 /**
1032 * Set the list of XSLT 3.0 accumulators that apply to this tree.
1033 *
1034 * @return the accumulators that apply; or null if all accumulators apply.
1035 * (Note, this is not the same as the meaning of #all in the
1036 * use-accumulators attribute, which refers to all accumulators
1037 * declared in a given package).
1038 */
1039
1040 public Set<? extends Accumulator> getApplicableAccumulators() {
1041 return applicableAccumulators;
1042 }
1043
1044
1045 /**
1046 * Set whether or not the user of this Source is encouraged to close it as soon as reading is finished.
1047 * Normally the expectation is that any Stream in a StreamSource will be closed by the component that
1048 * created the Stream. However, in the case of a Source returned by a URIResolver, there is no suitable
1049 * interface (the URIResolver has no opportunity to close the stream). Also, in some cases such as reading
1050 * of stylesheet modules, it is possible to close the stream long before control is returned to the caller
1051 * who supplied it. This tends to make a difference on .NET, where a file often can't be opened if there
1052 * is a stream attached to it.
1053 *
1054 * @param close true if the source should be closed as soon as it has been consumed
1055 */
1056
1057 public void setPleaseCloseAfterUse(boolean close) {
1058 pleaseClose = close;
1059 }
1060
1061 /**
1062 * Determine whether or not the user of this Source is encouraged to close it as soon as reading is
1063 * finished.
1064 *
1065 * @return true if the source should be closed as soon as it has been consumed
1066 */
1067
1068 public boolean isPleaseCloseAfterUse() {
1069 return pleaseClose;
1070 }
1071
1072 /**
1073 * Close any resources held by a given Source. This only works if the underlying Source is one that is
1074 * recognized as holding closable resources.
1075 *
1076 * @param source the source to be closed
1077 * @since 9.2
1078 */
1079
1080 public static void close(/*@NotNull*/ Source source) {
1081 try {
1082 if (source instanceof StreamSource) {
1083 StreamSource ss = (StreamSource) source;
1084 if (ss.getInputStream() != null) {
1085 ss.getInputStream().close();
1086 }
1087 if (ss.getReader() != null) {
1088 ss.getReader().close();
1089 }
1090 } else if (source instanceof SAXSource) {
1091 InputSource is = ((SAXSource) source).getInputSource();
1092 if (is != null) {
1093 if (is.getByteStream() != null) {
1094 is.getByteStream().close();
1095 }
1096 if (is.getCharacterStream() != null) {
1097 is.getCharacterStream().close();
1098 }
1099 }
1100 } else if (source instanceof AugmentedSource) {
1101 ((AugmentedSource)source).close();
1102 }
1103 } catch (IOException err) {
1104 // no action
1105 }
1106 }
1107
1108 }
1109