Debian Codegraph @

net/sf/saxon/tree/util/AttributeCollectionImpl.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.util;
9
10 import net.sf.saxon.Configuration;
11 import net.sf.saxon.event.ReceiverOptions;
12 import net.sf.saxon.expr.parser.ExplicitLocation;
13 import net.sf.saxon.expr.parser.Location;
14 import net.sf.saxon.om.*;
15 import net.sf.saxon.type.BuiltInAtomicType;
16 import net.sf.saxon.type.MissingComponentException;
17 import net.sf.saxon.type.SimpleType;
18 import org.xml.sax.Attributes;
19
20 import java.util.Arrays;
21
22
23 /**
24 * AttributeCollectionImpl is an implementation of both the SAX2 interface Attributes
25 * and the Saxon equivalent AttributeCollection.
26 * <p>As well as providing the information required by the SAX2 interface, an
27 * AttributeCollection can hold type information (as needed to support the JAXP 1.3
28 * {@link javax.xml.validation.ValidatorHandler} interface), and location information
29 * for debugging. The location information is used in the case of attributes on a result
30 * tree to identify the location in the query or stylesheet from which they were
31 * generated.</p>
32 */
33
34 public class AttributeCollectionImpl implements Attributes, AttributeCollection {
35
36 private Configuration config;
37 // Following fields can be null ONLY if used==0. We avoid allocating the arrays for the common
38 // case of an empty attribute collection.
39 private NodeName[] names = null;
40 private String[] values = null;
41 private Location[] locations = null;
42 private int[] props = null;
43 private int used = 0;
44 // the types array can be null even if used>0; this indicates that all attributes are untyped
45 private SimpleType[] types = null;
46
47 // Empty attribute collection. The caller is trusted not to try and modify it.
48
49 /*@NotNull*/ public static AttributeCollectionImpl EMPTY_ATTRIBUTE_COLLECTION =
50 new AttributeCollectionImpl((Configuration) null);
51
52 /**
53 * Create an empty attribute list.
54 *
55 * @param config the Saxon Configuration
56 */
57
58 public AttributeCollectionImpl(Configuration config) {
59 this.config = config;
60 used = 0;
61 }
62
63 /**
64 * Create an attribute list as a copy of an existing attribute list
65 *
66 * @param atts the existing attribute list to be copied
67 * @return the copied attribute list. Note that if the original attribute list
68 * is empty, the method returns the singleton object {@link #EMPTY_ATTRIBUTE_COLLECTION};
69 * this case must therefore be handled specially if the returned attribute list is to
70 * be modified.
71 */
72
73 /*@Nullable*/
74 public static AttributeCollectionImpl copy(/*@NotNull*/ AttributeCollectionImpl atts) {
75 if (atts.getLength() == 0) {
76 return EMPTY_ATTRIBUTE_COLLECTION;
77 }
78 AttributeCollectionImpl t = new AttributeCollectionImpl(atts.config);
79 t.used = atts.used;
80 t.names = new NodeName[atts.used];
81 t.values = new String[atts.used];
82 t.props = new int[atts.used];
83 t.locations = new Location[atts.used];
84 System.arraycopy(atts.names, 0, t.names, 0, atts.used);
85 System.arraycopy(atts.values, 0, t.values, 0, atts.used);
86 System.arraycopy(atts.props, 0, t.props, 0, atts.used);
87 System.arraycopy(atts.locations, 0, t.locations, 0, atts.used);
88 if (atts.types != null) {
89 t.types = new SimpleType[atts.used];
90 System.arraycopy(atts.types, 0, t.types, 0, atts.used);
91 }
92 return t;
93 }
94
95 /**
96 * Add an attribute to an attribute list. The parameters correspond
97 * to the parameters of the {@link net.sf.saxon.event.Receiver#attribute(NodeName, SimpleType, CharSequence, net.sf.saxon.expr.parser.Location, int)}
98 * method. There is no check that the name of the attribute is distinct from other attributes
99 * already in the collection: this check must be made by the caller.
100 *
101 * @param nodeName Object representing the attribute name.
102 * @param type The attribute type
103 * @param value The attribute value (must not be null)
104 * @param locationId Identifies the attribute location.
105 * @param properties Attribute properties
106 */
107
108 public void addAttribute(NodeName nodeName, SimpleType type, String value, Location locationId, int properties) {
109 if (values == null) {
110 names = new NodeName[5];
111 values = new String[5];
112 props = new int[5];
113 locations = new Location[5];
114 if (!type.equals(BuiltInAtomicType.UNTYPED_ATOMIC)) {
115 types = new SimpleType[5];
116 }
117 used = 0;
118 }
119 if (values.length == used) {
120 int newsize = used == 0 ? 5 : used * 2;
121 names = Arrays.copyOf(names, newsize);
122 values = Arrays.copyOf(values, newsize);
123 props = Arrays.copyOf(props, newsize);
124 locations = Arrays.copyOf(locations, newsize);
125 if (types != null) {
126 types = Arrays.copyOf(types, newsize);
127 }
128 }
129 int n = used;
130 names[n] = nodeName;
131 props[n] = properties;
132 locations[n] = locationId.saveLocation();
133 setTypeAnnotation(n, type);
134 values[used++] = value;
135 }
136
137 /**
138 * Set (overwrite) an attribute in the attribute list. The parameters correspond
139 * to the parameters of the {@link net.sf.saxon.event.Receiver#attribute(NodeName, SimpleType, CharSequence, net.sf.saxon.expr.parser.Location, int)}
140 * method.
141 *
142 * @param index Identifies the entry to be replaced. Must be in range (nasty things happen if not)
143 * @param nodeName representing the attribute name.
144 * @param type The attribute type code
145 * @param value The attribute value (must not be null)
146 * @param locationId Identifies the attribtue location.
147 * @param properties Attribute properties
148 */
149
150 public void setAttribute(int index, NodeName nodeName, SimpleType type, String value, Location locationId, int properties) {
151 names[index] = nodeName;
152 props[index] = properties;
153 locations[index] = locationId;
154 setTypeAnnotation(index, type);
155 values[index] = value;
156 }
157
158
159 /**
160 * Clear the attribute list. This removes the values but doesn't free the memory used.
161 * free the memory, use clear() then compact().
162 */
163
164 public void clear() {
165 used = 0;
166 }
167
168 /**
169 * Compact the attribute list to avoid wasting memory
170 */
171
172 public void compact() {
173 if (used == 0) {
174 props = null;
175 values = null;
176 } else if (values != null && values.length > used) {
177 values = Arrays.copyOf(values, used);
178 props = Arrays.copyOf(props, used);
179 names = Arrays.copyOf(names, used);
180 locations = Arrays.copyOf(locations, used);
181 if (types != null) {
182 types = Arrays.copyOf(types, used);
183 }
184 }
185 }
186
187 /**
188 * Return the number of attributes in the list.
189 *
190 * @return The number of attributes that have been created in this attribute collection. This is the number
191 * of slots used in the list, including any slots allocated to attributes that have since been deleted.
192 * Such slots are not reused, to preserve attribute identity.
193 */
194
195 public int getLength() {
196 return values == null ? 0 : used;
197 }
198
199 /**
200 * Get the fingerprint of an attribute (by position).
201 *
202 * @param index The position of the attribute in the list.
203 * @return The display name of the attribute as a string, or -1 if there
204 * is no attribute at that position.
205 */
206
207 public int getFingerprint(int index) {
208 if (names == null) {
209 return -1;
210 }
211 if (index < 0 || index >= used || names[index] == null) {
212 return -1;
213 }
214
215 return names[index].obtainFingerprint(config.getNamePool());
216 }
217
218 /**
219 * Get the node name of an attribute (by position).
220 *
221 * @param index The position of the attribute in the list.
222 * @return The node name, as a NodeName object, or null if there is no name at this index
223 */
224
225 /*@Nullable*/
226 public NodeName getNodeName(int index) {
227 if (names == null) {
228 return null;
229 }
230 if (index < 0 || index >= used) {
231 return null;
232 }
233
234 return names[index];
235 }
236
237
238 /**
239 * Get the type of an attribute (by position).
240 *
241 * @param index The position of the attribute in the list.
242 * @return The type annotation
243 */
244
245 public SimpleType getTypeAnnotation(int index) {
246 if (types == null) {
247 return BuiltInAtomicType.UNTYPED_ATOMIC;
248 }
249 if (index < 0 || index >= used) {
250 return BuiltInAtomicType.UNTYPED_ATOMIC;
251 }
252
253 return types[index];
254 }
255
256 /**
257 * Get the location of an attribute (by position)
258 *
259 * @param index The position of the attribute in the list.
260 * @return The location of the attribute. This can be used to obtain the
261 * actual system identifier and line number of the relevant location
262 */
263
264 public Location getLocation(int index) {
265 if (locations == null) {
266 return ExplicitLocation.UNKNOWN_LOCATION;
267 }
268 if (index < 0 || index >= used) {
269 return ExplicitLocation.UNKNOWN_LOCATION;
270 }
271
272 return locations[index];
273 }
274
275 /**
276 * Get the systemId part of the location of an attribute, at a given index.
277 * <p>Attribute location information is not available from a SAX parser, so this method
278 * is not useful for getting the location of an attribute in a source document. However,
279 * in a Saxon result document, the location information represents the location in the
280 * stylesheet of the instruction used to generate this attribute, which is useful for
281 * debugging.</p>
282 *
283 * @param index the required attribute
284 * @return the systemId of the location of the attribute
285 */
286
287 public String getSystemId(int index) {
288 return getLocation(index).getSystemId();
289 }
290
291 /**
292 * Get the line number part of the location of an attribute, at a given index.
293 * <p>Attribute location information is not available from a SAX parser, so this method
294 * is not useful for getting the location of an attribute in a source document. However,
295 * in a Saxon result document, the location information represents the location in the
296 * stylesheet of the instruction used to generate this attribute, which is useful for
297 * debugging.</p>
298 *
299 * @param index the required attribute
300 * @return the line number of the location of the attribute
301 */
302
303 public int getLineNumber(int index) {
304 return getLocation(index).getLineNumber();
305 }
306
307 /**
308 * Get the properties of an attribute (by position)
309 *
310 * @param index The position of the attribute in the list.
311 * @return The properties of the attribute. This is a set
312 * of bit-settings defined in class {@link net.sf.saxon.event.ReceiverOptions}. The
313 * most interesting of these is {{@link net.sf.saxon.event.ReceiverOptions#DEFAULTED_ATTRIBUTE},
314 * which indicates an attribute that was added to an element as a result of schema validation.
315 */
316
317 public int getProperties(int index) {
318 if (props == null) {
319 return -1;
320 }
321 if (index < 0 || index >= used) {
322 return -1;
323 }
324
325 return props[index];
326 }
327
328 /**
329 * Get the prefix of the name of an attribute (by position).
330 *
331 * @param index The position of the attribute in the list.
332 * @return The prefix of the attribute name as a string, or null if there
333 * is no attribute at that position. Returns "" for an attribute that
334 * has no prefix.
335 */
336
337 /*@Nullable*/
338 public String getPrefix(int index) {
339 if (names == null) {
340 return null;
341 }
342 if (index < 0 || index >= used) {
343 return null;
344 }
345 return names[index].getPrefix();
346 }
347
348 /**
349 * Get the lexical QName of an attribute (by position).
350 *
351 * @param index The position of the attribute in the list.
352 * @return The lexical QName of the attribute as a string, or null if there
353 * is no attribute at that position.
354 */
355
356 /*@Nullable*/
357 public String getQName(int index) {
358 if (names == null) {
359 return null;
360 }
361 if (index < 0 || index >= used) {
362 return null;
363 }
364 return names[index].getDisplayName();
365 }
366
367 /**
368 * Get the local name of an attribute (by position).
369 *
370 * @param index The position of the attribute in the list.
371 * @return The local name of the attribute as a string, or null if there
372 * is no attribute at that position.
373 */
374
375 /*@Nullable*/
376 public String getLocalName(int index) {
377 if (names == null) {
378 return null;
379 }
380 if (index < 0 || index >= used) {
381 return null;
382 }
383 return names[index].getLocalPart();
384 }
385
386 /**
387 * Get the namespace URI of an attribute (by position).
388 *
389 * @param index The position of the attribute in the list.
390 * @return The local name of the attribute as a string, or null if there
391 * is no attribute at that position.
392 */
393
394 /*@Nullable*/
395 public String getURI(int index) {
396 if (names == null) {
397 return null;
398 }
399 if (index < 0 || index >= used) {
400 return null;
401 }
402 return names[index].getURI();
403 }
404
405
406 /**
407 * Get the type of an attribute (by position). This is a SAX2 method,
408 * so it gets the type name as a DTD attribute type, mapped from the
409 * schema type code.
410 *
411 * @param index The position of the attribute in the list.
412 * @return The attribute type as a string ("NMTOKEN" for an
413 * enumeration, and "CDATA" if no declaration was
414 * read), or null if there is no attribute at
415 * that position.
416 */
417
418 /*@NotNull*/
419 public String getType(int index) {
420 int typeCode = getTypeAnnotation(index).getFingerprint();
421 switch (typeCode) {
422 case StandardNames.XS_ID:
423 return "ID";
424 case StandardNames.XS_IDREF:
425 return "IDREF";
426 case StandardNames.XS_NMTOKEN:
427 return "NMTOKEN";
428 case StandardNames.XS_ENTITY:
429 return "ENTITY";
430 case StandardNames.XS_IDREFS:
431 return "IDREFS";
432 case StandardNames.XS_NMTOKENS:
433 return "NMTOKENS";
434 case StandardNames.XS_ENTITIES:
435 return "ENTITIES";
436 default:
437 return "CDATA";
438 }
439 }
440
441 /**
442 * Get the type of an attribute (by name).
443 *
444 * @param uri The namespace uri of the attribute.
445 * @param localname The local name of the attribute.
446 * @return The index position of the attribute
447 */
448
449 /*@Nullable*/
450 public String getType(String uri, String localname) {
451 int index = findByName(uri, localname);
452 return (index < 0 ? null : getType(index));
453 }
454
455 /**
456 * Get the value of an attribute (by position).
457 *
458 * @param index The position of the attribute in the list.
459 * @return The attribute value as a string, or null if
460 * there is no attribute at that position.
461 */
462
463 /*@Nullable*/
464 public String getValue(int index) {
465 if (values == null) {
466 return null;
467 }
468 if (index < 0 || index >= used) {
469 return null;
470 }
471 return values[index];
472 }
473
474 /**
475 * Get the value of an attribute (by name).
476 *
477 * @param uri The namespace uri of the attribute.
478 * @param localname The local name of the attribute.
479 * @return The index position of the attribute
480 */
481
482 /*@Nullable*/
483 public String getValue(String uri, String localname) {
484 int index = findByName(uri, localname);
485 return (index < 0 ? null : getValue(index));
486 }
487
488 /**
489 * Get the attribute value using its fingerprint
490 */
491
492 /*@Nullable*/
493 public String getValueByFingerprint(int fingerprint) {
494 int index = findByFingerprint(fingerprint);
495 return (index < 0 ? null : getValue(index));
496 }
497
498 /**
499 * Get the index of an attribute, from its lexical QName
500 *
501 * @param qname The lexical QName of the attribute. The prefix must match.
502 * @return The index position of the attribute
503 */
504
505 public int getIndex(/*@NotNull*/ String qname) {
506 if (names == null) {
507 return -1;
508 }
509 if (qname.indexOf(':') < 0) {
510 return findByName("", qname);
511 }
512 // Searching using prefix+localname is not recommended, but SAX allows it...
513 String[] parts;
514 try {
515 parts = NameChecker.getQNameParts(qname);
516 } catch (QNameException err) {
517 return -1;
518 }
519 String prefix = parts[0];
520 if (prefix.isEmpty()) {
521 return findByName("", qname);
522 } else {
523 String localName = parts[1];
524 for (int i = 0; i < used; i++) {
525 if (names[i] != null) {
526 String lname = names[i].getLocalPart();
527 String ppref = names[i].getPrefix();
528 if (localName.equals(lname) && prefix.equals(ppref)) {
529 return i;
530 }
531 }
532 }
533 return -1;
534 }
535 }
536
537 /**
538 * Get the index of an attribute (by name).
539 *
540 * @param uri The namespace uri of the attribute.
541 * @param localname The local name of the attribute.
542 * @return The index position of the attribute, or -1 if absent
543 */
544
545 public int getIndex(String uri, String localname) {
546 return findByName(uri, localname);
547 }
548
549 /**
550 * Get the index, given the fingerprint.
551 * Return -1 if not found.
552 */
553
554 public int getIndexByFingerprint(int fingerprint) {
555 return findByFingerprint(fingerprint);
556 }
557
558 /**
559 * Get the type of an attribute (by lexical QName).
560 *
561 * @param name The lexical QName of the attribute.
562 * @return The attribute type as a string (e.g. "NMTOKEN", or
563 * "CDATA" if no declaration was read).
564 */
565
566 /*@NotNull*/
567 public String getType(/*@NotNull*/ String name) {
568 int index = getIndex(name);
569 return getType(index);
570 }
571
572
573 /**
574 * Get the value of an attribute (by lexnical QName).
575 *
576 * @param name The attribute name (a lexical QName).
577 * The prefix must match the prefix originally used. This method is defined in SAX, but is
578 * not recommended except where the prefix is null.
579 */
580
581 /*@Nullable*/
582 public String getValue(/*@NotNull*/ String name) {
583 int index = getIndex(name);
584 return getValue(index);
585 }
586
587 /**
588 * Ask whether the attribute collection contains any attributes
589 * in a specified namespace
590 *
591 * @param uri the specified namespace
592 * @return true if there are one or more attributes in this namespace
593 */
594
595 public boolean hasAttributeInNamespace(String uri) {
596 if (names == null || config == null) {
597 return false; // indicates an empty attribute set
598 }
599 for (int i = 0; i < used; i++) {
600 if (names[i] != null && names[i].hasURI(uri)) {
601 return true;
602 }
603 }
604 return false;
605 }
606
607 /**
608 * Find an attribute by expanded name
609 *
610 * @param uri the namespace uri
611 * @param localName the local name
612 * @return the index of the attribute, or -1 if absent
613 */
614
615 private int findByName(String uri, String localName) {
616 if (names == null || config == null) {
617 return -1; // indicates an empty attribute set
618 }
619 for (int i = 0; i < used; i++) {
620 if (names[i] != null && names[i].hasURI(uri) && localName.equals(names[i].getLocalPart())) {
621 return i;
622 }
623 }
624 return -1;
625 }
626
627 /**
628 * Find an attribute by fingerprint
629 *
630 * @param fingerprint the fingerprint representing the name of the required attribute
631 * @return the index of the attribute, or -1 if absent
632 */
633
634 private int findByFingerprint(int fingerprint) {
635 if (names == null || config == null) {
636 return -1;
637 }
638 NamePool pool = config.getNamePool();
639 for (int i = 0; i < used; i++) {
640 NodeName nn = names[i];
641 if (nn != null) {
642 if (nn.hasFingerprint()) {
643 if (nn.getFingerprint() == fingerprint) {
644 return i;
645 }
646 } else {
647 if (nn.hasURI(pool.getURI(fingerprint)) && nn.getLocalPart().equals(pool.getLocalName(fingerprint))) {
648 return i;
649 }
650 }
651 }
652 }
653 return -1;
654 }
655
656 /**
657 * Find an attribute by node name
658 *
659 * @param nodeName the name of the required attribute
660 * @return the index of the attribute, or -1 if absent
661 */
662
663 public int findByNodeName(/*@NotNull*/ NodeName nodeName) {
664 if (props == null || config == null) {
665 return -1;
666 }
667 if (nodeName.hasFingerprint()) {
668 return findByFingerprint(nodeName.getFingerprint());
669 } else {
670 return findByName(nodeName.getURI(), nodeName.getLocalPart());
671 }
672 }
673
674
675 /**
676 * Determine whether a given attribute has the is-ID property set
677 */
678
679 public boolean isId(int index) {
680 try {
681 return !isDeleted(index) &&
682 (StandardNames.XML_ID_NAME.equals(names[index]) ||
683 (getProperties(index) & ReceiverOptions.IS_ID) != 0 ||
684 getTypeAnnotation(index).isIdType());
685 } catch (MissingComponentException e) {
686 return false;
687 }
688 }
689
690 /**
691 * Determine whether a given attribute has the is-idref property set
692 */
693
694 public boolean isIdref(int index) {
695 try {
696 return getTypeAnnotation(index).isIdRefType();
697 } catch (MissingComponentException e) {
698 return false;
699 }
700 }
701
702 /**
703 * Delete the attribute at a given index position. Note that the index position will not be reused, to ensure
704 * that any new attributes added to the element have a distinct identity. Instead, the slot occupied
705 * by the attribute is nilled out.
706 *
707 * @param index The index position of the attribute to be removed
708 */
709
710 public void removeAttribute(int index) {
711 names[index] = null;
712 props[index] = -1;
713 values[index] = null;
714 locations[index] = null;
715 if (types != null) {
716 types[index] = null;
717 }
718 }
719
720 /**
721 * Test whether the attribute at a given index has been deleted
722 *
723 * @param index the index position of the (ex-) attribute
724 * @return true if the attribute has been deleted
725 */
726
727 public boolean isDeleted(int index) {
728 return names[index] == null;
729 }
730
731 /**
732 * Rename an attribute
733 *
734 * @param index the index position of the attribute
735 * @param newName the namecode of the new name
736 */
737
738 public void renameAttribute(int index, NodeName newName) {
739 names[index] = newName;
740 if (types != null) {
741 types[index] = BuiltInAtomicType.UNTYPED_ATOMIC;
742 }
743 }
744
745 /**
746 * Replace the value of an attribute
747 *
748 * @param index position of the attribute
749 * @param newValue the new string value of the attribute
750 */
751
752 public void replaceAttribute(int index, /*@NotNull*/ CharSequence newValue) {
753 values[index] = newValue.toString();
754 }
755
756 /**
757 * Set the type annotation of an attribute
758 *
759 * @param index the index position of the attribute node
760 * @param type the new type for the attribute
761 */
762
763 public void setTypeAnnotation(int index, SimpleType type) {
764 if (type.equals(BuiltInAtomicType.UNTYPED_ATOMIC)) {
765 if (types != null) {
766 types[index] = type;
767 }
768 } else {
769 if (types == null) {
770 types = new SimpleType[names.length];
771 Arrays.fill(types, BuiltInAtomicType.UNTYPED_ATOMIC);
772 types[index] = type;
773 } else {
774 types[index] = type;
775 }
776 }
777 }
778
779 /**
780 * Swap two attributes (used for sorting)
781 *
782 * @param i the position of one value
783 * @param j the position of the other value
784 */
785
786 public void swap(int i, int j) {
787 NodeName n = names[i];
788 names[i] = names[j];
789 names[j] = n;
790 if (types != null) {
791 SimpleType s = types[i];
792 types[i] = types[j];
793 types[j] = s;
794 }
795 String c = values[i];
796 values[i] = values[j];
797 values[j] = c;
798 int p = props[i];
799 props[i] = props[j];
800 props[j] = p;
801 Location l = locations[i];
802 locations[i] = locations[j];
803 locations[j] = l;
804 }
805
806
807 }
808