Debian Codegraph @

net/sf/saxon/event/CommentStripper.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.ExplicitLocation;
11 import net.sf.saxon.expr.parser.Location;
12 import net.sf.saxon.om.NodeName;
13 import net.sf.saxon.trans.XPathException;
14 import net.sf.saxon.tree.tiny.CompressedWhitespace;
15 import net.sf.saxon.tree.util.FastStringBuffer;
16 import net.sf.saxon.type.SchemaType;
17
18 /**
19 * The CommentStripper class is a filter that removes all comments and processing instructions.
20 * It also concatenates text nodes that are split by comments and PIs. This follows the rules for
21 * processing stylesheets; it is also used for removing comments and PIs from the tree seen
22 * by XPath expressions used to process XSD 1.1 assertions
23 *
24 * @author Michael H. Kay
25 */
26
27
28 public class CommentStripper extends ProxyReceiver {
29
30 /*@Nullable*/ private CompressedWhitespace savedWhitespace = null;
31 private FastStringBuffer buffer = new FastStringBuffer(FastStringBuffer.C256);
32
33 /**
34 * Default constructor for use in subclasses
35 *
36 * @param next the next receiver in the pipeline
37 */
38
39 public CommentStripper(Receiver next) {
40 super(next);
41 }
42
43 public void startElement(NodeName nameCode, SchemaType typeCode, Location location, int properties)
44 throws XPathException {
45 flush();
46 nextReceiver.startElement(nameCode, typeCode, location, properties);
47 }
48
49 /**
50 * Callback interface for SAX: not for application use
51 */
52
53 public void endElement() throws XPathException {
54 flush();
55 nextReceiver.endElement();
56 }
57
58 /**
59 * Handle a text node. Because we're often handling stylesheets on this path, whitespace text
60 * nodes will often be stripped but we can't strip them immediately because of the case
61 * [element] [!-- comment --]text[/element], where the space before the comment is considered
62 * significant. But it's worth going to some effort to avoid uncompressing the whitespace in the
63 * more common case, so that it can easily be detected and stripped downstream.
64 */
65
66 public void characters(CharSequence chars, Location locationId, int properties) throws XPathException {
67 if (chars instanceof CompressedWhitespace) {
68 if (buffer.isEmpty() && savedWhitespace == null) {
69 savedWhitespace = (CompressedWhitespace) chars;
70 } else {
71 ((CompressedWhitespace) chars).uncompress(buffer);
72 }
73 } else {
74 if (savedWhitespace != null) {
75 savedWhitespace.uncompress(buffer);
76 savedWhitespace = null;
77 }
78 buffer.append(chars);
79 }
80
81 }
82
83 /**
84 * Remove comments
85 */
86
87 public void comment(CharSequence chars, Location locationId, int properties) {
88 }
89
90 /**
91 * Remove processing instructions
92 */
93
94 public void processingInstruction(String name, CharSequence data, Location locationId, int properties) {
95 }
96
97 /**
98 * Flush the character buffer
99 *
100 * @throws net.sf.saxon.trans.XPathException
101 * if a failure occurs writing the output
102 */
103
104 private void flush() throws XPathException {
105 if (!buffer.isEmpty()) {
106 nextReceiver.characters(buffer, ExplicitLocation.UNKNOWN_LOCATION, 0);
107 } else if (savedWhitespace != null) {
108 nextReceiver.characters(savedWhitespace, ExplicitLocation.UNKNOWN_LOCATION, 0);
109 }
110 savedWhitespace = null;
111 buffer.setLength(0);
112 }
113
114 }
115