Debian Codegraph @

net/sf/saxon/event/IDFilter.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.om.StandardNames;
13 import net.sf.saxon.trans.XPathException;
14 import net.sf.saxon.type.AtomicType;
15 import net.sf.saxon.type.BuiltInAtomicType;
16 import net.sf.saxon.type.SchemaType;
17 import net.sf.saxon.type.SimpleType;
18
19 import java.util.HashSet;
20
21
22 /**
23 * IDFilter is a ProxyReceiver that extracts the subtree of a document rooted at the
24 * element with a given ID value. Namespace declarations outside this subtree are
25 * treated as if they were present on the identified element.
26 */
27
28 public class IDFilter extends StartTagBuffer {
29
30 private String requiredId;
31 private int activeDepth = 0;
32 private boolean matched = false;
33 private HashSet<SimpleType> nonIDs;
34
35 public IDFilter(Receiver next, String id) {
36 // System.err.println("IDFilter, looking for " + id);
37 super(next);
38 this.requiredId = id;
39 }
40
41 /**
42 * startElement
43 */
44
45 public void startElement(NodeName nameCode, SchemaType typeCode, Location location, int properties) throws XPathException {
46 matched = false;
47 if (activeDepth > 0) {
48 activeDepth++;
49 }
50 super.startElement(nameCode, typeCode, location, properties); // this remembers the details
51 }
52
53 /**
54 * Notify an attribute. Attributes are notified after the startElement event, and before any
55 * children. Namespaces and attributes may be intermingled.
56 *
57 * @param attName The name of the attribute
58 * @param typeCode The type of the attribute
59 * @param locationId the location of the node in the source, or of the instruction that created it
60 * @param properties Bit significant value. The following bits are defined, inter alia:
61 * <dl>
62 * <dd>DISABLE_ESCAPING</dd> <dt>Disable escaping for this attribute</dt>
63 * <dd>NO_SPECIAL_CHARACTERS</dd> <dt>Attribute value contains no special characters</dt>
64 * <dd>IS_ID</dd> <dt>Attribute is an ID</dt>
65 * </dl>
66 * @throws IllegalStateException: attempt to output an attribute when there is no open element
67 * start tag
68 */
69
70 public void attribute(NodeName attName, SimpleType typeCode, CharSequence value, Location locationId, int properties) throws XPathException {
71 super.attribute(attName, typeCode, value, locationId, properties);
72 if ((attName.equals(StandardNames.XML_ID_NAME)) || ((properties & ReceiverOptions.IS_ID) != 0)) {
73 if (value.toString().equals(requiredId)) {
74 matched = true;
75 }
76 }
77 }
78
79 /**
80 * startContent: Test if a matching ID attribute was found; if so, start outputting.
81 */
82
83 public void startContent() throws XPathException {
84 if (activeDepth > 0) {
85 super.startContent();
86 } else if (matched) {
87 activeDepth = 1;
88 super.startContent();
89 }
90 }
91
92 protected void declareNamespacesForStartElement() throws XPathException {
93 if (activeDepth == 1) {
94 declareAllNamespaces();
95 } else {
96 super.declareNamespacesForStartElement();
97 }
98 }
99
100 /**
101 * endElement:
102 */
103
104 public void endElement() throws XPathException {
105 if (activeDepth > 0) {
106 nextReceiver.endElement();
107 activeDepth--;
108 } else {
109 undeclareNamespacesForElement();
110 }
111 }
112
113 /**
114 * Character data
115 */
116
117 public void characters(CharSequence chars, Location locationId, int properties) throws XPathException {
118 if (activeDepth > 0) {
119 super.characters(chars, locationId, properties);
120 }
121 }
122
123 /**
124 * Processing Instruction
125 */
126
127 public void processingInstruction(String target, CharSequence data, Location locationId, int properties) throws XPathException {
128 if (activeDepth > 0) {
129 super.processingInstruction(target, data, locationId, properties);
130 }
131 }
132
133 /**
134 * Output a comment
135 */
136
137 public void comment(CharSequence chars, Location locationId, int properties) throws XPathException {
138 if (activeDepth > 0) {
139 super.comment(chars, locationId, properties);
140 }
141 }
142
143 /**
144 * Ask whether this Receiver (or the downstream pipeline) makes any use of the type annotations
145 * supplied on element and attribute events
146 *
147 * @return true if the Receiver makes any use of this information. If false, the caller
148 * may supply untyped nodes instead of supplying the type annotation
149 */
150
151 public boolean usesTypeAnnotations() {
152 return true;
153 }
154
155 /**
156 * Test whether a type annotation code represents the type xs:ID or one of its subtypes
157 *
158 * @param typeCode the fingerprint of the type name
159 * @return true if the type is an ID type; false if it is not (or if the type code does not
160 * resolve to a known type)
161 */
162
163 private boolean isIDCode(SimpleType typeCode) {
164 if (typeCode == BuiltInAtomicType.ID) {
165 return true;
166 }
167 if (typeCode instanceof BuiltInAtomicType) {
168 return false; // No other built-in type is an ID
169 }
170
171 if (nonIDs == null) {
172 nonIDs = new HashSet<SimpleType>(20);
173 }
174 if (nonIDs.contains(typeCode)) {
175 return false;
176 }
177 if (typeCode.isAtomicType()) {
178 if (getConfiguration().getTypeHierarchy().isSubType((AtomicType) typeCode, BuiltInAtomicType.ID)) {
179 return true;
180 } else {
181 nonIDs.add(typeCode);
182 return false;
183 }
184 } else {
185 return false;
186 }
187 }
188
189 }
190