Debian Codegraph @

net/sf/saxon/tree/linked/ElementImpl.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.CopyInformee;
11 import net.sf.saxon.event.Receiver;
12 import net.sf.saxon.event.ReceiverOptions;
13 import net.sf.saxon.expr.parser.ExplicitLocation;
14 import net.sf.saxon.expr.parser.Location;
15 import net.sf.saxon.lib.NamespaceConstant;
16 import net.sf.saxon.om.*;
17 import net.sf.saxon.pattern.NodeKindTest;
18 import net.sf.saxon.trans.XPathException;
19 import net.sf.saxon.tree.iter.AxisIterator;
20 import net.sf.saxon.tree.util.AttributeCollectionImpl;
21 import net.sf.saxon.tree.util.FastStringBuffer;
22 import net.sf.saxon.tree.util.NamespaceIterator;
23 import net.sf.saxon.tree.util.Navigator;
24 import net.sf.saxon.type.*;
25 import net.sf.saxon.value.AtomicValue;
26 import net.sf.saxon.value.Whitespace;
27
28 import java.util.Arrays;
29 import java.util.HashSet;
30 import java.util.Iterator;
31 import java.util.Set;
32
33 /**
34 * ElementImpl implements an element node in the Linked tree model. Subclasses of ElementImpl,
35 * for holding particular kinds of element, can be defined by using a {@link NodeFactory}
36 * registered with the {@link LinkedTreeBuilder} used to construct the tree.
37 */
38
39
40 public class ElementImpl extends ParentNodeImpl implements NamespaceResolver {
41
42 private NodeName nodeName;
43 private SchemaType type = Untyped.getInstance();
44 private AttributeCollection attributeList; // this excludes namespace attributes
45 private NamespaceBinding[] namespaceList = null; // list of namespace codes
46
47 /**
48 * Construct an empty ElementImpl
49 */
50
51 public ElementImpl() {
52 }
53
54 /**
55 * Set the attribute list
56 *
57 * @param atts the list of attributes of this element (not including namespace attributes)
58 */
59
60 public void setAttributeList(AttributeCollection atts) {
61 this.attributeList = atts;
62 }
63
64 /**
65 * Set the namespace list
66 *
67 * @param namespaces an integer array of namespace codes
68 */
69
70 public void setNamespaceList(NamespaceBinding[] namespaces) {
71 this.namespaceList = namespaces;
72 }
73
74 /**
75 * Set the node name
76 * @param name the node name
77 */
78
79 public void setNodeName(NodeName name) {
80 this.nodeName = name;
81 }
82
83 /**
84 * Initialise a new ElementImpl with an element name
85 *
86 * @param elemName Integer representing the element name, with namespaces resolved
87 * @param elementType the schema type of the element node
88 * @param atts The attribute list: always null
89 * @param parent The parent node
90 * @param sequenceNumber Integer identifying this element within the document
91 */
92
93 public void initialise(/*@NotNull*/ NodeName elemName, SchemaType elementType, AttributeCollectionImpl atts, /*@NotNull*/ NodeInfo parent,
94 int sequenceNumber) {
95 this.nodeName = elemName;
96 this.type = elementType;
97 setRawParent((ParentNodeImpl) parent);
98 setRawSequenceNumber(sequenceNumber);
99 attributeList = atts;
100 }
101
102 /**
103 * Get the name of the node. Returns null for an unnamed node
104 *
105 * @return the name of the node
106 */
107 @Override
108 public NodeName getNodeName() {
109 return nodeName;
110 }
111
112 /**
113 * Set location information for this node
114 *
115 * @param systemId the base URI
116 * @param line the line number if known
117 * @param column the column number if known
118 */
119
120 public void setLocation(String systemId, int line, int column) {
121 DocumentImpl root = getRawParent().getPhysicalRoot();
122 root.setLineAndColumn(getRawSequenceNumber(), line, column);
123 root.setSystemId(getRawSequenceNumber(), systemId);
124 }
125
126 /**
127 * Set the system ID of this node. This method is provided so that a NodeInfo
128 * implements the javax.xml.transform.Source interface, allowing a node to be
129 * used directly as the Source of a transformation
130 */
131
132 public void setSystemId(String uri) {
133 getPhysicalRoot().setSystemId(getRawSequenceNumber(), uri);
134 }
135
136 /**
137 * Get the root node
138 */
139
140 public NodeInfo getRoot() {
141 ParentNodeImpl up = getRawParent();
142 if (up == null || (up instanceof DocumentImpl && ((DocumentImpl) up).isImaginary())) {
143 return this;
144 } else {
145 return up.getRoot();
146 }
147 }
148
149 /**
150 * Get the system ID of the entity containing this element node.
151 */
152
153 /*@Nullable*/
154 public final String getSystemId() {
155 DocumentImpl root = getPhysicalRoot();
156 return root == null ? null : root.getSystemId(getRawSequenceNumber());
157 }
158
159 /**
160 * Get the base URI of this element node. This will be the same as the System ID unless
161 * xml:base has been used.
162 */
163
164 public String getBaseURI() {
165 return Navigator.getBaseURI(this, n -> getPhysicalRoot().isTopWithinEntity((ElementImpl)n));
166 }
167
168 /**
169 * Get the attribute list. Note that if the attribute list is empty, it should not be modified, as it
170 * will be shared by other elements. Instead, set a new attribute list.
171 *
172 * @return the list of attributes of this element (not including namespace attributes)
173 */
174
175 public AttributeCollection gsetAttributeCollection() {
176 return this.attributeList;
177 }
178
179
180 /**
181 * Determine whether the node has the is-nilled property
182 *
183 * @return true if the node has the is-nilled property
184 */
185
186 public boolean isNilled() {
187 return getPhysicalRoot().isNilledElement(this);
188 }
189
190 /**
191 * Set the type annotation on a node. This must only be called when the caller has verified (by validation)
192 * that the node is a valid instance of the specified type. The call is ignored if the node is not an element
193 * or attribute node.
194 *
195 * @param type the type annotation
196 */
197
198 public void setTypeAnnotation(SchemaType type) {
199 this.type = type;
200 }
201
202 /**
203 * Say that the element has the nilled property
204 */
205
206 public void setNilled() {
207 getPhysicalRoot().addNilledElement(this);
208 }
209
210 /**
211 * Get the type annotation
212 *
213 * @return the type annotation of the node
214 */
215
216 @Override
217 public SchemaType getSchemaType() {
218 return type;
219 }
220
221 /**
222 * Get the line number of the node within its source document entity
223 */
224
225 public int getLineNumber() {
226 DocumentImpl root = getPhysicalRoot();
227 if (root == null) {
228 return -1;
229 } else {
230 return root.getLineNumber(getRawSequenceNumber());
231 }
232 }
233
234 /**
235 * Get the line number of the node within its source document entity
236 */
237
238 public int getColumnNumber() {
239 DocumentImpl root = getPhysicalRoot();
240 if (root == null) {
241 return -1;
242 } else {
243 return root.getColumnNumber(getRawSequenceNumber());
244 }
245 }
246
247 /**
248 * Get a character string that uniquely identifies this node
249 *
250 * @param buffer to contain the generated ID
251 */
252
253 public void generateId(/*@NotNull*/ FastStringBuffer buffer) {
254 int sequence = getRawSequenceNumber();
255 if (sequence >= 0) {
256 getPhysicalRoot().generateId(buffer);
257 buffer.append("e");
258 buffer.append(Integer.toString(sequence));
259 } else {
260 getRawParent().generateId(buffer);
261 buffer.append("f");
262 buffer.append(Integer.toString(getSiblingPosition()));
263 }
264 }
265
266 /**
267 * Return the kind of node.
268 *
269 * @return Type.ELEMENT
270 */
271
272 public final int getNodeKind() {
273 return Type.ELEMENT;
274 }
275
276 /**
277 * Copy this node to a given Receiver.
278 * <p>This method is primarily for internal use. It should not be considered a stable
279 * part of the Saxon API.</p>
280 *
281 * @param out the Receiver to which the node should be copied. It is the caller's
282 * responsibility to ensure that this Receiver is open before the method is called
283 * (or that it is self-opening), and that it is closed after use.
284 * @param copyOptions a selection of the options defined in {@link CopyOptions}
285 * @param location If non-null, identifies the location of the instruction
286 * that requested this copy. If zero, indicates that the location information
287 * is not available
288 * @throws XPathException if any downstream error occurs
289 */
290
291 public void copy(Receiver out, int copyOptions, Location location) throws XPathException {
292
293 SchemaType typeCode = CopyOptions.includes(copyOptions, CopyOptions.TYPE_ANNOTATIONS) ?
294 getSchemaType() : Untyped.getInstance();
295 CopyInformee informee = (CopyInformee) out.getPipelineConfiguration().getComponent(CopyInformee.class.getName());
296 if (informee != null) {
297 Object o = informee.notifyElementNode(this);
298 if (o instanceof Location) {
299 location = (Location)o;
300 }
301 }
302
303 out.startElement(NameOfNode.makeName(this), typeCode, location, 0);
304
305 // output the namespaces
306
307 int childCopyOptions = copyOptions & ~CopyOptions.ALL_NAMESPACES;
308 if ((copyOptions & CopyOptions.LOCAL_NAMESPACES) != 0) {
309 NamespaceBinding[] localNamespaces = getDeclaredNamespaces(null);
310 for (NamespaceBinding ns : localNamespaces) {
311 if (ns == null) {
312 break;
313 }
314 out.namespace(ns, 0);
315 }
316 } else if ((copyOptions & CopyOptions.ALL_NAMESPACES) != 0) {
317 NamespaceIterator.sendNamespaces(this, out);
318 childCopyOptions |= CopyOptions.LOCAL_NAMESPACES;
319 }
320
321 // output the attributes
322
323 if (attributeList != null) {
324 for (int i = 0; i < attributeList.getLength(); i++) {
325 NodeName nc = attributeList.getNodeName(i);
326 if (nc != null) {
327 // if attribute hasn't been deleted
328 out.attribute(nc, BuiltInAtomicType.UNTYPED_ATOMIC, attributeList.getValue(i), ExplicitLocation.UNKNOWN_LOCATION, 0);
329 }
330 }
331 }
332
333 out.startContent();
334
335 // output the children
336
337 NodeImpl next = getFirstChild();
338 while (next != null) {
339 next.copy(out, childCopyOptions, location);
340 next = next.getNextSibling();
341 }
342
343 out.endElement();
344 }
345
346 /**
347 * Delete this node (that is, detach it from its parent)
348 */
349
350 public void delete() {
351 DocumentImpl root = getPhysicalRoot();
352 super.delete();
353 if (root != null) {
354 AxisIterator iter = iterateAxis(AxisInfo.DESCENDANT_OR_SELF, NodeKindTest.ELEMENT);
355 while (true) {
356 ElementImpl n = (ElementImpl) iter.next();
357 int atts = attributeList.getLength();
358 for (int index = 0; index < atts; index++) {
359 if (attributeList.isId(index)) {
360 root.deregisterID(attributeList.getValue(index));
361 }
362 }
363 if (n == null) {
364 break;
365 }
366 root.deIndex(n);
367 }
368 }
369 }
370
371 /**
372 * Rename this node
373 *
374 * @param newName the new name
375 */
376
377 public void rename(NodeName newName) {
378 String prefix = newName.getPrefix();
379 String uri = newName.getURI();
380 NamespaceBinding ns = new NamespaceBinding(prefix, uri);
381 String uc = getURIForPrefix(prefix, true);
382 if (uc == null) {
383 uc = "";
384 }
385 if (!uc.equals(uri)) {
386 if (uc.isEmpty()) {
387 addNamespace(ns, false);
388 } else {
389 throw new IllegalArgumentException(
390 "Namespace binding of new name conflicts with existing namespace binding");
391 }
392 }
393 nodeName = newName;
394 }
395
396 /**
397 * Add a namespace binding (that is, a namespace node) to this element. This call has no effect if applied
398 * to a node other than an element.
399 *
400 * @param nscode The namespace code representing the (prefix, uri) pair of the namespace binding to be
401 * added. If the target element already has a namespace binding with this (prefix, uri) pair, the call has
402 * no effect. If the target element currently has a namespace binding with this prefix and a different URI, an
403 * exception is raised.
404 * @param unused
405 * @throws IllegalArgumentException if the target element already has a namespace binding for this prefix,
406 * or if the namespace code represents a namespace undeclaration
407 */
408
409 public void addNamespace(/*@NotNull*/ NamespaceBinding nscode, boolean unused) {
410 if (nscode.getURI().isEmpty()) {
411 throw new IllegalArgumentException("Cannot add a namespace undeclaration");
412 }
413 addNamespaceInternal(nscode, true);
414 }
415
416 private void addNamespaceInternal(/*@NotNull*/ NamespaceBinding nscode, boolean externalCall) {
417 if (namespaceList == null) {
418 namespaceList = new NamespaceBinding[]{nscode};
419 } else {
420 NamespaceBinding[] nsList = namespaceList;
421 for (int i = 0; i < nsList.length; i++) {
422 if (nsList[i].equals(nscode)) {
423 return;
424 }
425 if (nsList[i].getPrefix().equals(nscode.getPrefix())) {
426 if (nsList[i].getURI().isEmpty()) {
427 // this is an undeclaration; replace it with the new declaration
428 nsList[i] = nscode;
429 return;
430 } else if (externalCall) {
431 throw new IllegalArgumentException("New namespace conflicts with existing namespace binding");
432 } else {
433 return;
434 }
435 }
436 }
437 int len = nsList.length;
438 NamespaceBinding[] ns2 = new NamespaceBinding[len + 1];
439 System.arraycopy(nsList, 0, ns2, 0, len);
440 ns2[len] = nscode;
441 namespaceList = ns2;
442 }
443 }
444
445
446 /**
447 * Replace the string-value of this node
448 *
449 * @param stringValue the new string value
450 */
451
452 public void replaceStringValue(/*@NotNull*/ CharSequence stringValue) {
453 if (stringValue.length() == 0) {
454 setChildren(null);
455 } else {
456 TextImpl text = new TextImpl(stringValue.toString());
457 text.setRawParent(this);
458 setChildren(text);
459 }
460 }
461
462 /**
463 * Add an attribute to this element node.
464 * <p>If this node is not an element, or if the supplied node is not an attribute, the method
465 * takes no action. If the element already has an attribute with this name, the method
466 * throws an exception.</p>
467 * <p>This method does not perform any namespace fixup. It is the caller's responsibility
468 * to ensure that any namespace prefix used in the name of the attribute (or in its value
469 * if it has a namespace-sensitive type) is declared on this element.</p>
470 *
471 * @param nameCode the name of the new attribute
472 * @param attType the type annotation of the new attribute
473 * @param value the string value of the new attribute
474 * @param properties properties including IS_ID and IS_IDREF properties
475 * @throws IllegalStateException if the element already has an attribute with the given name.
476 */
477
478 public void addAttribute(/*@NotNull*/ NodeName nameCode, SimpleType attType, /*@NotNull*/ CharSequence value, int properties) {
479 if (attributeList == null || attributeList.getLength() == 0) {
480 attributeList = new AttributeCollectionImpl(getConfiguration());
481 }
482 AttributeCollectionImpl atts = (AttributeCollectionImpl) attributeList;
483 int index = atts.findByNodeName(nameCode);
484 if (index == -1) {
485 atts.addAttribute(nameCode, attType, value.toString(), ExplicitLocation.UNKNOWN_LOCATION, 0);
486 } else {
487 throw new IllegalStateException(
488 "Cannot add an attribute to an element as it already has an attribute with the specified name");
489 }
490 if (!nameCode.hasURI("")) {
491 // The new attribute name is in a namespace
492 NamespaceBinding binding = nameCode.getNamespaceBinding();
493 String prefix = binding.getPrefix();
494 String uc = getURIForPrefix(prefix, false);
495 if (uc == null) {
496 // The namespace is not already declared on the element
497 addNamespace(binding, false);
498 } else if (!uc.equals(binding.getURI())) {
499 throw new IllegalStateException(
500 "Namespace binding of new name conflicts with existing namespace binding");
501 }
502 }
503 if ((properties & ReceiverOptions.IS_ID) != 0) {
504 DocumentImpl root = getPhysicalRoot();
505 if (root != null) {
506 root.registerID(this, Whitespace.trim(value));
507 }
508 }
509 }
510
511 /**
512 * Remove an attribute from this element node
513 * <p>If this node is not an element, or if the specified node is not an attribute
514 * of this element, this method takes no action.</p>
515 * <p>The attribute object itself becomes unusable; any attempt to use this attribute object,
516 * or any other object representing the same node, is likely to result in an exception.</p>
517 *
518 * @param attribute the attribute node to be removed
519 */
520
521 public void removeAttribute(/*@NotNull*/ NodeInfo attribute) {
522 if (!(attribute instanceof AttributeImpl)) {
523 return; // no action
524 }
525 AttributeCollectionImpl atts = (AttributeCollectionImpl) getAttributeList();
526 int index = ((AttributeImpl) attribute).getSiblingPosition();
527 if (index >= 0 && atts.isId(index)) {
528 DocumentImpl root = getPhysicalRoot();
529 root.deregisterID(atts.getValue(index));
530 }
531 atts.removeAttribute(index);
532 ((AttributeImpl) attribute).setRawParent(null);
533 }
534
535
536 /**
537 * Remove type information from this node (and its ancestors, recursively).
538 * This method implements the upd:removeType() primitive defined in the XQuery Update specification
539 */
540
541 public void removeTypeAnnotation() {
542 if (getSchemaType() != Untyped.getInstance()) {
543 type = AnyType.getInstance();
544 getRawParent().removeTypeAnnotation();
545 }
546 }
547
548 /**
549 * Set the namespace declarations for the element
550 *
551 * @param namespaces the list of namespace codes
552 * @param namespacesUsed the number of entries in the list that are used
553 */
554
555 public void setNamespaceDeclarations(NamespaceBinding[] namespaces, int namespacesUsed) {
556 namespaceList = new NamespaceBinding[namespacesUsed];
557 System.arraycopy(namespaces, 0, namespaceList, 0, namespacesUsed);
558 }
559
560 /**
561 * Get the namespace URI corresponding to a given prefix. Return null
562 * if the prefix is not in scope.
563 *
564 * @param prefix the namespace prefix. May be the zero-length string, indicating
565 * that there is no prefix. This indicates either the default namespace or the
566 * null namespace, depending on the value of useDefault.
567 * @param useDefault true if the default namespace is to be used when the
568 * prefix is "". If false, the method returns "" when the prefix is "".
569 * @return the uri for the namespace, or null if the prefix is not in scope.
570 * The "null namespace" is represented by the pseudo-URI "".
571 */
572
573 /*@Nullable*/
574 public String getURIForPrefix(/*@NotNull*/ String prefix, boolean useDefault) {
575 if (prefix.equals("xml")) {
576 return NamespaceConstant.XML;
577 }
578 if (prefix.isEmpty() && !useDefault) {
579 return NamespaceConstant.NULL;
580 }
581
582 if (namespaceList != null) {
583 for (NamespaceBinding aNamespaceList : namespaceList) {
584 if (aNamespaceList.getPrefix().equals(prefix)) {
585 String uri = aNamespaceList.getURI();
586 return uri.isEmpty() && !prefix.isEmpty() ? null : uri;
587 }
588 }
589 }
590 NodeInfo next = getRawParent();
591 if (next.getNodeKind() == Type.DOCUMENT) {
592 return prefix.isEmpty() ? NamespaceConstant.NULL : null;
593 } else {
594 return ((ElementImpl) next).getURIForPrefix(prefix, useDefault);
595 }
596 }
597
598 /**
599 * Get an iterator over all the prefixes declared in this namespace context. This will include
600 * the default namespace (prefix="") and the XML namespace where appropriate
601 */
602
603 /*@Nullable*/
604 public Iterator<String> iteratePrefixes() {
605 return new Iterator<String>() {
606 /*@Nullable*/ private NamePool pool = null;
607 private Iterator<NamespaceBinding> iter = NamespaceIterator.iterateNamespaces(ElementImpl.this);
608
609 public boolean hasNext() {
610 return pool == null || iter.hasNext();
611 }
612
613 public String next() {
614 if (pool == null) {
615 pool = getNamePool();
616 return "xml";
617 } else {
618 return iter.next().getPrefix();
619 }
620 }
621
622 public void remove() {
623 throw new UnsupportedOperationException("remove");
624 }
625 };
626 }
627
628 /**
629 * Search the in-scope namespaces to see whether a given namespace is in scope.
630 *
631 * @param uri The URI to be matched.
632 * @return true if the namespace is in scope
633 */
634
635 /*@Nullable*/
636 public boolean isInScopeNamespace(/*@NotNull*/ String uri) {
637 if (uri.equals(NamespaceConstant.XML)) {
638 return true;
639 }
640 for (NamespaceBinding b : namespaceList) {
641 if (b.getURI().equals(uri)) {
642 return true;
643 }
644 }
645 return false;
646 }
647
648 /**
649 * Get all namespace undeclarations and undeclarations defined on this element.
650 *
651 * @param buffer If this is non-null, and the result array fits in this buffer, then the result
652 * may overwrite the contents of this array, to avoid the cost of allocating a new array on the heap.
653 * @return An array of NamespaceBinding objects representing the namespace declarations and undeclarations present on
654 * this element. For a node other than an element, return null.
655 * The XML namespace is never included in the list. If the supplied array is larger than required,
656 * then the first unused entry will be set to null.
657 * <p>For a node other than an element, the method returns null.</p>
658 */
659
660 public NamespaceBinding[] getDeclaredNamespaces(NamespaceBinding[] buffer) {
661 return namespaceList == null ? NamespaceBinding.EMPTY_ARRAY : namespaceList;
662 }
663
664 /**
665 * Ensure that a child element being inserted into a tree has the right namespace declarations.
666 * Redundant declarations should be removed. If the child is in the null namespace but the parent has a default
667 * namespace, xmlns="" should be added. If inherit is false, namespace undeclarations should be added for all
668 * namespaces that are declared on the parent but not on the child.
669 *
670 * @param inherit true if the child is to inherit the inscope namespaces of its new parent
671 */
672
673 protected void fixupInsertedNamespaces(boolean inherit) {
674 if (getRawParent().getNodeKind() == Type.DOCUMENT) {
675 return;
676 }
677
678 Set<NamespaceBinding> childNamespaces = new HashSet<>();
679 if (namespaceList != null) {
680 childNamespaces.addAll(Arrays.asList(namespaceList));
681 }
682
683 NamespaceResolver inscope = new InscopeNamespaceResolver(getRawParent());
684
685 // If the child is in the null namespace but the parent has a default namespace, xmlns="" should be added.
686
687 if (getURI().isEmpty() && !inscope.getURIForPrefix("", true).isEmpty()) {
688 childNamespaces.add(NamespaceBinding.DEFAULT_UNDECLARATION);
689 }
690
691 // Namespaces present on the parent but not on the child should be undeclared (if requested)
692
693 if (!inherit) {
694 Iterator it = inscope.iteratePrefixes();
695 while (it.hasNext()) {
696 String prefix = (String) it.next();
697 if (!prefix.equals("xml")) {
698 boolean found = false;
699 if (namespaceList != null) {
700 for (NamespaceBinding aNamespaceList : namespaceList) {
701 if (aNamespaceList.getPrefix().equals(prefix)) {
702 found = true;
703 break;
704 }
705 }
706 }
707 if (!found) {
708 childNamespaces.add(new NamespaceBinding(prefix, ""));
709 }
710 }
711 }
712 }
713
714 // Redundant namespaces should be removed
715
716 if (namespaceList != null) {
717 for (NamespaceBinding nscode : namespaceList) {
718 String prefix = nscode.getPrefix();
719 String uri = nscode.getURI();
720 String parentUri = inscope.getURIForPrefix(prefix, true);
721 if (parentUri != null && parentUri.equals(uri)) {
722 // the namespace declaration is redundant
723 childNamespaces.remove(nscode);
724 }
725 }
726 }
727 NamespaceBinding[] n2 = new NamespaceBinding[childNamespaces.size()];
728 int j = 0;
729 for (NamespaceBinding childNamespace : childNamespaces) {
730 n2[j++] = childNamespace;
731 }
732 namespaceList = n2;
733 }
734
735 /**
736 * Get the attribute list for this element.
737 *
738 * @return The attribute list. This will not include any
739 * namespace attributes. The attribute names will be in expanded form, with prefixes
740 * replaced by URIs
741 */
742
743 public AttributeCollection getAttributeList() {
744 return attributeList == null ? AttributeCollectionImpl.EMPTY_ATTRIBUTE_COLLECTION : attributeList;
745 }
746
747 /**
748 * Get the namespace list for this element.
749 *
750 * @return The raw namespace list, as an array of name codes
751 */
752
753 /*@Nullable*/
754 public NamespaceBinding[] getNamespaceList() {
755 return namespaceList;
756 }
757
758 /**
759 * Get the value of a given attribute of this node
760 *
761 * @param uri the namespace URI of the attribute name, or "" if the attribute is not in a namepsace
762 * @param localName the local part of the attribute name
763 * @return the attribute value if it exists or null if not
764 */
765
766 /*@Nullable*/
767 public String getAttributeValue(/*@NotNull*/ String uri, /*@NotNull*/ String localName) {
768 return attributeList == null ? null : attributeList.getValue(uri, localName);
769 }
770
771 /**
772 * Determine whether this node has the is-id property
773 *
774 * @return true if the node is an ID
775 */
776
777 public boolean isId() {
778 // This is an approximation. For a union type, we check that the actual value is a valid NCName,
779 // but we don't check that it was validated against the member type of the union that is an ID type.
780 try {
781 SchemaType type = getSchemaType();
782 return type.getFingerprint() == StandardNames.XS_ID ||
783 type.isIdType() && NameChecker.isValidNCName(getStringValueCS());
784 } catch (MissingComponentException e) {
785 return false;
786 }
787 }
788
789 public boolean isIdref() {
790 return isIdRefNode(this);
791 }
792
793 static boolean isIdRefNode(NodeImpl node) {
794 SchemaType type = node.getSchemaType();
795 try {
796 if (type.isIdRefType()) {
797 if (type == BuiltInAtomicType.IDREF || type == BuiltInListType.IDREFS) {
798 return true;
799 }
800 try {
801 for (AtomicValue av : node.atomize()) {
802 if (av.getItemType().isIdRefType()) {
803 return true;
804 }
805 }
806 } catch (XPathException err) {
807 // no action
808 }
809 }
810 } catch (MissingComponentException e) {
811 return false;
812 }
813 return false;
814 }
815
816
817 }
818