Debian Codegraph @

net/sf/saxon/s9api/WhitespaceStrippingPolicy.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.s9api;
9
10 import net.sf.saxon.event.FilterFactory;
11 import net.sf.saxon.event.ProxyReceiver;
12 import net.sf.saxon.event.Receiver;
13 import net.sf.saxon.event.Stripper;
14 import net.sf.saxon.om.*;
15 import net.sf.saxon.style.StylesheetPackage;
16 import net.sf.saxon.trace.ExpressionPresenter;
17 import net.sf.saxon.trans.XPathException;
18 import net.sf.saxon.type.SchemaType;
19 import net.sf.saxon.value.Whitespace;
20
21 import java.util.function.Predicate;
22
23 /**
24 * WhitespaceStrippingPolicy is class defining the possible policies for handling
25 * whitespace text nodes in a source document.
26 */
27
28 public class WhitespaceStrippingPolicy {
29
30 private int policy;
31 private SpaceStrippingRule stripperRules;
32
33 /**
34 * The value NONE indicates that all whitespace text nodes are retained. This is the default
35 * when documents are processed without validation.
36 */
37 public static final WhitespaceStrippingPolicy NONE = new WhitespaceStrippingPolicy(Whitespace.NONE);
38
39 /**
40 * The value IGNORABLE indicates that whitespace text nodes in element-only content are
41 * discarded. Content is element-only if it is defined by a schema or DTD definition that
42 * does not allow mixed or PCDATA content. This option is in effect documentary only, since
43 * this policy is always used when DTD or schema validation is selected for a {@link DocumentBuilder},
44 * and is equivalent to {@link #NONE} otherwise.
45 */
46 public static final WhitespaceStrippingPolicy IGNORABLE = new WhitespaceStrippingPolicy(Whitespace.IGNORABLE);
47
48 /**
49 * The value ALL indicates that all whitespace-only text nodes are discarded.
50 */
51 public static final WhitespaceStrippingPolicy ALL = new WhitespaceStrippingPolicy(Whitespace.ALL);
52
53 /**
54 * UNSPECIFIED means that no other value has been specifically requested.
55 */
56 public static final WhitespaceStrippingPolicy UNSPECIFIED = new WhitespaceStrippingPolicy(Whitespace.UNSPECIFIED);
57
58 /**
59 * Create a custom whitespace stripping policy.
60 * <p>Changed in 9.9 to use the standard Java 8 Predicate class in place of Saxon's version.</p>
61 * @param elementTest a predicate applied to element names, which should return true if whitespace-only
62 * text node children of the element are to be stripped, false if they are to be retained.
63 */
64
65 public static WhitespaceStrippingPolicy makeCustomPolicy(final Predicate<QName> elementTest) {
66 SpaceStrippingRule rule = new SpaceStrippingRule() {
67 @Override
68 public int isSpacePreserving(NodeName nodeName, SchemaType schemaType) {
69 return elementTest.test(new QName(nodeName.getStructuredQName()))
70 ? Stripper.ALWAYS_STRIP
71 : Stripper.ALWAYS_PRESERVE;
72 }
73
74 @Override
75 public ProxyReceiver makeStripper(Receiver next) {
76 return new Stripper(this, next);
77 }
78
79
80 @Override
81 public void export(ExpressionPresenter presenter) throws XPathException {
82 throw new UnsupportedOperationException();
83 }
84 };
85
86 WhitespaceStrippingPolicy wsp = new WhitespaceStrippingPolicy(Whitespace.XSLT);
87 wsp.stripperRules = rule;
88 return wsp;
89 }
90
91 private WhitespaceStrippingPolicy(int policy) {
92 this.policy = policy;
93 switch (policy) {
94 case Whitespace.ALL:
95 stripperRules = AllElementsSpaceStrippingRule.getInstance();
96 break;
97 case Whitespace.NONE:
98 stripperRules = NoElementsSpaceStrippingRule.getInstance();
99 break;
100 case Whitespace.IGNORABLE:
101 stripperRules = IgnorableSpaceStrippingRule.getInstance();
102 break;
103 default:
104 break;
105 }
106 }
107
108 /**
109 * Create a WhitespaceStrippingPolicy based on the xsl:strip-space and xsl:preserve-space declarations
110 * in a given XSLT stylesheet package
111 *
112 * @param pack the stylesheet package containing the xsl:strip-space and xsl:preserve-space declarations
113 */
114
115 protected WhitespaceStrippingPolicy(StylesheetPackage pack) {
116 policy = Whitespace.XSLT;
117 stripperRules = pack.getStripperRules();
118 }
119
120 protected int ordinal() {
121 return policy;
122 }
123
124 protected SpaceStrippingRule getSpaceStrippingRule() {
125 return stripperRules;
126 }
127
128 /*@NotNull*/
129 protected FilterFactory makeStripper() {
130 return new FilterFactory() {
131 public ProxyReceiver makeFilter(Receiver next) {
132 return new Stripper(stripperRules, next);
133 }
134 };
135 }
136 }
137