Debian Codegraph @

net/sf/saxon/event/IgnorableWhitespaceStripper.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.om.NodeName;
12 import net.sf.saxon.trans.XPathException;
13 import net.sf.saxon.type.ComplexType;
14 import net.sf.saxon.type.SchemaType;
15 import net.sf.saxon.type.Untyped;
16 import net.sf.saxon.value.Whitespace;
17
18 import java.util.Arrays;
19
20 /**
21 * The IgnorableWhitespaceStripper removes whitespace text nodes belonging to elements
22 * whose schema-defined type defines element-only content
23 */
24
25
26 public class IgnorableWhitespaceStripper extends ProxyReceiver {
27
28 // We implement our own stack to avoid the overhead of allocating objects.
29
30 private boolean[] stripStack = new boolean[100];
31 private int top = 0;
32
33 public IgnorableWhitespaceStripper(Receiver next) {
34 super(next);
35 }
36
37 public void startElement(NodeName nameCode, SchemaType type, Location location, int properties) throws XPathException {
38 //System.err.println("startElement " + getNamePool().getDisplayName(nameCode));
39 nextReceiver.startElement(nameCode, type, location, properties);
40
41 boolean strip = false;
42 if (type != Untyped.getInstance()) {
43 // if the element has element-only content, whitespace stripping is enabled
44 if (type.isComplexType() &&
45 !((ComplexType) type).isSimpleContent() &&
46 !((ComplexType) type).isMixedContent()) {
47 strip = true;
48 }
49 }
50 //System.err.println("strip = " + strip);
51
52 // put "strip" value on top of stack
53
54 top++;
55 if (top >= stripStack.length) {
56 stripStack = Arrays.copyOf(stripStack, top*2);
57 }
58 stripStack[top] = strip;
59 }
60
61 /**
62 * Handle an end-of-element event
63 */
64
65 public void endElement() throws XPathException {
66 nextReceiver.endElement();
67 top--;
68 }
69
70 /**
71 * Handle a text node
72 */
73
74 public void characters(CharSequence chars, Location locationId, int properties) throws XPathException {
75 if (chars.length() > 0 && (!stripStack[top] || !Whitespace.isWhite(chars))) {
76 nextReceiver.characters(chars, locationId, properties);
77 }
78 }
79
80 /**
81 * Ask whether this Receiver (or the downstream pipeline) makes any use of the type annotations
82 * supplied on element and attribute events
83 *
84 * @return true if the Receiver makes any use of this information. If false, the caller
85 * may supply untyped nodes instead of supplying the type annotation
86 */
87
88 public boolean usesTypeAnnotations() {
89 return true;
90 }
91 }
92
93 // Copyright (c) Saxonica 2005 - 2008.