Debian Codegraph @

net/sf/saxon/event/Stripper.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.event;
9
10 import net.sf.saxon.expr.parser.Location;
11 import net.sf.saxon.lib.NamespaceConstant;
12 import net.sf.saxon.om.FingerprintedQName;
13 import net.sf.saxon.om.NodeName;
14 import net.sf.saxon.om.SpaceStrippingRule;
15 import net.sf.saxon.om.StandardNames;
16 import net.sf.saxon.trace.ExpressionPresenter;
17 import net.sf.saxon.trans.rules.Rule;
18 import net.sf.saxon.trans.rules.RuleTarget;
19 import net.sf.saxon.trans.XPathException;
20 import net.sf.saxon.type.ComplexType;
21 import net.sf.saxon.type.SchemaType;
22 import net.sf.saxon.type.SimpleType;
23 import net.sf.saxon.type.Untyped;
24 import net.sf.saxon.value.Whitespace;
25
26 import java.util.Arrays;
27
28
29 /**
30 * The RuleBasedStripper class performs whitespace stripping according to the rules of
31 * the xsl:strip-space and xsl:preserve-space instructions.
32 * It maintains details of which elements need to be stripped.
33 * The code is written to act as a SAX-like filter to do the stripping.
34 *
35 * @author Michael H. Kay
36 */
37
38
39 public class Stripper extends ProxyReceiver {
40
41 public final static StripRuleTarget STRIP = new StripRuleTarget() {
42 };
43 public final static StripRuleTarget PRESERVE = new StripRuleTarget() {
44 };
45 protected SpaceStrippingRule rule;
46
47 public Stripper(/*@NotNull*/ SpaceStrippingRule rule, /*@NotNull*/ Receiver next) {
48 super(next);
49 assert rule != null;
50 this.rule = rule;
51 }
52
53 // stripStack is used to hold information used while stripping nodes. We avoid allocating
54 // space on the tree itself to keep the size of nodes down. Each entry on the stack is two
55 // booleans, one indicates the current value of xml-space is "preserve", the other indicates
56 // that we are in a space-preserving element.
57
58 // We implement our own stack to avoid the overhead of allocating objects. The two booleans
59 // are held as the leas-significant bits of a byte.
60
61 private byte[] stripStack = new byte[100];
62 private int top = 0;
63
64 /**
65 * Get a clean copy of this stripper. The new copy shares the same PipelineConfiguration
66 * as the original, but the underlying receiver (that is, the destination for post-stripping
67 * events) is changed.
68 *
69 * @param next the next receiver in the pipeline for the new Stripper
70 * @return a dublicate of this Stripper, with the output sent to "next".
71 */
72
73 public Stripper getAnother(Receiver next) {
74 return new Stripper(rule, next);
75 }
76
77 /**
78 * Decide whether an element is in the set of white-space preserving element types
79 *
80 * @param name Identifies the name of the element whose whitespace is (or is not) to
81 * be preserved
82 * @param type The schema type of this element
83 * @return ALWAYS_PRESERVE if the element is in the set of white-space preserving
84 * element types, ALWAYS_STRIP if the element is to be stripped regardless of the
85 * xml:space setting, and STRIP_DEFAULT otherwise
86 * @throws XPathException if the rules are ambiguous and ambiguities are to be
87 * reported as errors
88 */
89
90 private int isSpacePreserving(NodeName name, SchemaType type) throws XPathException {
91 return rule.isSpacePreserving(name, type);
92 }
93
94 public static final byte ALWAYS_PRESERVE = 0x01; // whitespace always preserved (e.g. xsl:text)
95 public static final byte ALWAYS_STRIP = 0x02; // whitespace always stripped (e.g. xsl:choose)
96 public static final byte STRIP_DEFAULT = 0x00; // no special action
97 public static final byte PRESERVE_PARENT = 0x04; // parent element specifies xml:space="preserve"
98 public static final byte SIMPLE_CONTENT = 0x08; // type annotation indicates simple typed content
99 public static final byte ASSERTIONS_EXIST = 0x10; // XSD 1.1 assertions are in scope
100
101
102 /**
103 * Callback interface for SAX: not for application use
104 */
105
106 public void open() throws XPathException {
107 // System.err.println("Stripper#startDocument()");
108 top = 0;
109 stripStack[top] = ALWAYS_PRESERVE; // {xml:preserve = false, preserve this element = true}
110 super.open();
111 }
112
113 public void startElement(NodeName elemName, SchemaType type, Location location, int properties) throws XPathException {
114 // System.err.println("startElement " + nameCode);
115 nextReceiver.startElement(elemName, type, location, properties);
116
117 byte preserveParent = stripStack[top];
118 byte preserve = (byte) (preserveParent & (PRESERVE_PARENT | ASSERTIONS_EXIST));
119
120 int elementStrip = isSpacePreserving(elemName, type);
121 if (elementStrip == ALWAYS_PRESERVE) {
122 preserve |= ALWAYS_PRESERVE;
123 } else if (elementStrip == ALWAYS_STRIP) {
124 preserve |= ALWAYS_STRIP;
125 }
126 if (type != Untyped.getInstance()) {
127 if (preserve == 0) {
128 // if the element has simple content, whitespace stripping is disabled
129 if (type.isSimpleType() || ((ComplexType) type).isSimpleContent()) {
130 preserve |= SIMPLE_CONTENT;
131 }
132 }
133 if (type instanceof ComplexType && ((ComplexType) type).hasAssertions()) {
134 preserve |= ASSERTIONS_EXIST;
135 }
136 }
137
138 // put "preserve" value on top of stack
139
140 top++;
141 if (top >= stripStack.length) {
142 stripStack = Arrays.copyOf(stripStack, top * 2);
143 }
144 stripStack[top] = preserve;
145 }
146
147 public void attribute(NodeName nameCode, SimpleType typeCode, CharSequence value, Location locationId, int properties)
148 throws XPathException {
149
150 // test for xml:space="preserve" | "default"
151
152 if (nameCode.equals(XML_SPACE)) {
153 if (Whitespace.normalizeWhitespace(value).equals("preserve")) {
154 stripStack[top] |= PRESERVE_PARENT;
155 } else {
156 stripStack[top] &= ~PRESERVE_PARENT;
157 }
158 }
159 nextReceiver.attribute(nameCode, typeCode, value, locationId, properties);
160 }
161
162 private static NodeName XML_SPACE = new FingerprintedQName("xml", NamespaceConstant.XML, "space", StandardNames.XML_SPACE);
163
164 /**
165 * Handle an end-of-element event
166 */
167
168 public void endElement() throws XPathException {
169 nextReceiver.endElement();
170 top--;
171 }
172
173 /**
174 * Handle a text node
175 */
176
177 public void characters(CharSequence chars, Location locationId, int properties) throws XPathException {
178 // assume adjacent chunks of text are already concatenated
179
180 if (((((stripStack[top] & (ALWAYS_PRESERVE | PRESERVE_PARENT | SIMPLE_CONTENT | ASSERTIONS_EXIST)) != 0) &&
181 (stripStack[top] & ALWAYS_STRIP) == 0)
182 || !Whitespace.isWhite(chars))
183 && chars.length() > 0) {
184 nextReceiver.characters(chars, locationId, properties);
185 }
186 }
187
188 /**
189 * Ask whether this Receiver (or the downstream pipeline) makes any use of the type annotations
190 * supplied on element and attribute events
191 *
192 * @return true if the Receiver makes any use of this information. If false, the caller
193 * may supply untyped nodes instead of supplying the type annotation
194 */
195
196 public boolean usesTypeAnnotations() {
197 return true;
198 }
199
200 public static class StripRuleTarget implements RuleTarget {
201 public void export(ExpressionPresenter presenter) throws XPathException {
202 // no-op
203 }
204
205 public void registerRule(Rule rule) {
206 // no action
207 }
208 }
209 }
210