Debian Codegraph @

net/sf/saxon/tree/linked/AttributeImpl.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.tree.linked;
9
10 import net.sf.saxon.event.Receiver;
11 import net.sf.saxon.event.ReceiverOptions;
12 import net.sf.saxon.expr.parser.Location;
13 import net.sf.saxon.om.*;
14 import net.sf.saxon.trans.XPathException;
15 import net.sf.saxon.tree.util.AttributeCollectionImpl;
16 import net.sf.saxon.tree.util.FastStringBuffer;
17 import net.sf.saxon.type.*;
18
19 /**
20 * A node in the "linked" tree representing an attribute. Note that this is
21 * generated only "on demand", when the attribute is selected by a path expression.
22 * <p>It is possible for multiple AttributeImpl objects to represent the same attribute node.
23 * The identity of an attribute node is determined by the identity of the element, and the index
24 * position of the attribute within the element. Index positions are not reused when an attribute
25 * is deleted, and are retained when an attribute is renamed.</p>
26 * <p>This object no longer caches information such as the name code and string value, because
27 * these would become invalid when the element node is modified.</p>
28 *
29 * @author Michael H. Kay
30 */
31
32 public class AttributeImpl extends NodeImpl {
33
34 /**
35 * Construct an Attribute node for the n'th attribute of a given element
36 *
37 * @param element The element containing the relevant attribute
38 * @param index The index position of the attribute starting at zero
39 */
40
41 public AttributeImpl(ElementImpl element, int index) {
42 setRawParent(element);
43 setSiblingPosition(index);
44 }
45
46 /**
47 * Get the name of the node. Returns null for an unnamed node
48 *
49 * @return the name of the node
50 */
51 @Override
52 public NodeName getNodeName() {
53 if (getRawParent() == null || getSiblingPosition() == -1) {
54 // implies this node is deleted
55 return null;
56 }
57 return ((ElementImpl) getRawParent()).getAttributeList().getNodeName(getSiblingPosition());
58 }
59
60 /**
61 * Get the fingerprint of the node. This is used to compare whether two nodes
62 * have equivalent names. Return -1 for a node with no name.
63 */
64 @Override
65 public int getFingerprint() {
66 if (getRawParent() == null || getSiblingPosition() == -1) {
67 // implies this node is deleted
68 return -1;
69 }
70 return ((ElementImpl) getRawParent()).getAttributeList().getFingerprint(getSiblingPosition());
71 }
72
73 /**
74 * Get the type annotation
75 *
76 * @return the type annotation of the node, if any
77 */
78
79 public SchemaType getSchemaType() {
80 return ((ElementImpl) getRawParent()).getAttributeList().getTypeAnnotation(getSiblingPosition());
81 }
82
83
84 /**
85 * Determine whether this node has the is-id property
86 *
87 * @return true if the node is an ID
88 */
89
90 public boolean isId() {
91 try {
92 return getFingerprint() == StandardNames.XML_ID || getSchemaType().isIdType();
93 } catch (MissingComponentException e) {
94 return false;
95 }
96 }
97
98 /**
99 * Determine whether this node has the is-idref property
100 *
101 * @return true if the node is an IDREF or IDREFS element or attribute
102 */
103
104 public boolean isIdref() {
105 AttributeCollection alist = ((ElementImpl) getRawParent()).getAttributeList();
106 if ((alist.getProperties(getSiblingPosition()) & ReceiverOptions.IS_IDREF) != 0) {
107 return true;
108 }
109 return ElementImpl.isIdRefNode(this);
110 }
111
112 /**
113 * Determine whether this is the same node as another node
114 *
115 * @return true if this Node object and the supplied Node object represent the
116 * same node in the tree.
117 */
118
119 public boolean equals(Object other) {
120 if (!(other instanceof AttributeImpl)) {
121 return false;
122 }
123 if (this == other) {
124 return true;
125 }
126 AttributeImpl otherAtt = (AttributeImpl) other;
127 return getRawParent().equals(otherAtt.getRawParent()) && getSiblingPosition() == otherAtt.getSiblingPosition();
128 }
129
130 /**
131 * The hashCode() method obeys the contract for hashCode(): that is, if two objects are equal
132 * (represent the same node) then they must have the same hashCode()
133 */
134
135 public int hashCode() {
136 return getRawParent().hashCode() ^ (getSiblingPosition() << 16);
137 }
138
139 /**
140 * Get the node sequence number (in document order). Sequence numbers are monotonic but not
141 * consecutive. In the current implementation, parent nodes (elements and roots) have a zero
142 * least-significant word, while namespaces, attributes, text nodes, comments, and PIs have
143 * the top word the same as their owner and the bottom half reflecting their relative position.
144 */
145
146 protected long getSequenceNumber() {
147 long parseq = getRawParent().getSequenceNumber();
148 return (parseq == -1L ? parseq : parseq + 0x8000 + getSiblingPosition());
149 // note the 0x8000 is to leave room for namespace nodes
150 }
151
152 /**
153 * Return the type of node.
154 *
155 * @return Node.ATTRIBUTE
156 */
157
158 public final int getNodeKind() {
159 return Type.ATTRIBUTE;
160 }
161
162 /**
163 * Return the character value of the node.
164 *
165 * @return the attribute value
166 */
167
168 public String getStringValue() {
169 return ((ElementImpl) getRawParent()).getAttributeList().getValue(getSiblingPosition());
170 }
171
172 /**
173 * Get next sibling - not defined for attributes
174 */
175
176 /*@Nullable*/
177 public NodeImpl getNextSibling() {
178 return null;
179 }
180
181 /**
182 * Get previous sibling - not defined for attributes
183 */
184
185 /*@Nullable*/
186 public NodeImpl getPreviousSibling() {
187 return null;
188 }
189
190 /**
191 * Get the previous node in document order (skipping attributes)
192 */
193
194 /*@NotNull*/
195 public NodeImpl getPreviousInDocument() {
196 return (NodeImpl) getParent();
197 }
198
199 /**
200 * Get the next node in document order (skipping attributes)
201 */
202
203 /*@Nullable*/
204 public NodeImpl getNextInDocument(NodeImpl anchor) {
205 if (anchor == this) return null;
206 return ((NodeImpl) getParent()).getNextInDocument(anchor);
207 }
208
209 /**
210 * Get sequential key. Returns key of owning element with the attribute index as a suffix
211 *
212 * @param buffer a buffer to which the generated ID will be written
213 */
214
215 public void generateId(/*@NotNull*/ FastStringBuffer buffer) {
216 getParent().generateId(buffer);
217 buffer.append('a');
218 buffer.append(Integer.toString(getSiblingPosition()));
219 }
220
221 /**
222 * Copy this node to a given outputter
223 */
224
225 public void copy(/*@NotNull*/ Receiver out, int copyOptions, Location locationId) throws XPathException {
226 SimpleType typeCode = (CopyOptions.includes(copyOptions, CopyOptions.TYPE_ANNOTATIONS) ?
227 ((SimpleType) getSchemaType()) : BuiltInAtomicType.UNTYPED_ATOMIC);
228 out.attribute(NameOfNode.makeName(this), typeCode, getStringValue(), locationId, 0);
229 }
230
231 /**
232 * Delete this node (that is, detach it from its parent)
233 */
234
235 public void delete() {
236 if (getRawParent() != null) {
237 getRawParent().removeAttribute(this);
238 }
239 setRawParent(null);
240 setSiblingPosition(-1);
241 }
242
243 /**
244 * Test whether this MutableNodeInfo object represents a node that has been deleted.
245 * Generally, such a node is unusable, and any attempt to use it will result in an exception
246 * being thrown
247 *
248 * @return true if this node has been deleted
249 */
250
251 public boolean isDeleted() {
252 // Note that the attribute node may be represented by more than one object
253 return getSiblingPosition() == -1 || getFingerprint() == -1 || (getRawParent() != null && getRawParent().isDeleted());
254 }
255
256 /**
257 * Replace this node with a given sequence of nodes
258 *
259 * @param replacement the replacement nodes (which for this version of the method must be attribute
260 * nodes - they may use any implementation of the NodeInfo interface).
261 * The target attribute node is deleted, and the replacement nodes are added to the
262 * parent element; if they have the same names as existing nodes, then the existing nodes will be
263 * overwritten.
264 * @param inherit set to true if new child elements are to inherit the in-scope namespaces
265 * of their new parent. Not used when replacing attribute nodes.
266 * @throws IllegalArgumentException if any of the replacement nodes is not an attribute
267 * @throws IllegalStateException if this node has been deleted or has no parent node
268 * or if two of the replacement nodes have the same name
269 */
270
271 public void replace(/*@NotNull*/ NodeInfo[] replacement, boolean inherit) {
272 if (isDeleted()) {
273 throw new IllegalStateException("Cannot replace a deleted node");
274 }
275 if (getParent() == null) {
276 throw new IllegalStateException("Cannot replace a parentless node");
277 }
278 ParentNodeImpl element = getRawParent();
279 delete();
280 for (NodeInfo n : replacement) {
281 if (n.getNodeKind() != Type.ATTRIBUTE) {
282 throw new IllegalArgumentException("Replacement nodes must be attributes");
283 }
284 element.addAttribute(NameOfNode.makeName(n), BuiltInAtomicType.UNTYPED_ATOMIC, n.getStringValue(), 0);
285 }
286 }
287
288 /**
289 * Rename this node
290 *
291 * @param newNameCode the NamePool code of the new name
292 */
293
294 public void rename(NodeName newNameCode) {
295 // The attribute node itself is transient; we need to update the attribute collection held in the parent
296 if (getRawParent() != null) {
297 ((AttributeCollectionImpl) ((ElementImpl) getRawParent()).getAttributeList()).renameAttribute(getSiblingPosition(), newNameCode);
298 String newURI = newNameCode.getURI();
299 if (!newURI.isEmpty()) {
300 // new attribute name is in a namespace
301 String newPrefix = newNameCode.getPrefix();
302 NamespaceBinding newBinding = new NamespaceBinding(newPrefix, newURI);
303 String oldURI = ((ElementImpl) getRawParent()).getURIForPrefix(newPrefix, false);
304 if (oldURI == null) {
305 getRawParent().addNamespace(newBinding, false);
306 } else if (!oldURI.equals(newURI)) {
307 throw new IllegalArgumentException(
308 "Namespace binding of new name conflicts with existing namespace binding");
309 }
310 }
311 }
312 }
313
314 public void replaceStringValue(CharSequence stringValue) {
315 if (getRawParent() != null) {
316 AttributeCollectionImpl atts = (AttributeCollectionImpl) ((ElementImpl) getRawParent()).getAttributeList();
317 atts.replaceAttribute(getSiblingPosition(), stringValue);
318 }
319 }
320
321
322 /**
323 * Remove type information from this node (and its ancestors, recursively).
324 * This method implements the upd:removeType() primitive defined in the XQuery Update specification
325 */
326
327 public void removeTypeAnnotation() {
328 if (getRawParent() != null) {
329 AttributeCollectionImpl atts = (AttributeCollectionImpl) ((ElementImpl) getRawParent()).getAttributeList();
330 atts.setTypeAnnotation(getSiblingPosition(), BuiltInAtomicType.UNTYPED_ATOMIC);
331 getRawParent().removeTypeAnnotation();
332 }
333 }
334
335
336 /**
337 * Set the type annotation on a node. This must only be called when the caller has verified (by validation)
338 * that the node is a valid instance of the specified type. The call is ignored if the node is not an element
339 * or attribute node.
340 *
341 * @param type the type annotation (possibly including high bits set to indicate the isID, isIDREF, and
342 * isNilled properties)
343 */
344
345 public void setTypeAnnotation(SchemaType type) {
346 if (getRawParent() != null) {
347 AttributeCollectionImpl atts = (AttributeCollectionImpl) ((ElementImpl) getRawParent()).getAttributeList();
348 atts.setTypeAnnotation(getSiblingPosition(), (SimpleType)type);
349 }
350 }
351 }
352