| 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.tiny; |
| 9 | |
| 10 | import net.sf.saxon.expr.parser.ExplicitLocation; |
| 11 | import net.sf.saxon.expr.parser.Location; |
| 12 | import net.sf.saxon.om.*; |
| 13 | import net.sf.saxon.type.BuiltInAtomicType; |
| 14 | import net.sf.saxon.type.SimpleType; |
| 15 | |
| 16 | /** |
| 17 | * An implementation of the AttributeCollection interface based directly on the |
| 18 | * TinyTree data structure. |
| 19 | */ |
| 20 | |
| 21 | public class TinyAttributeCollection implements AttributeCollection { |
| 22 | |
| 23 | int element; |
| 24 | TinyTree tree; |
| 25 | int firstAttribute; |
| 26 | |
| 27 | public TinyAttributeCollection(/*@NotNull*/ TinyTree tree, int element) { |
| 28 | this.tree = tree; |
| 29 | this.element = element; |
| 30 | firstAttribute = tree.alpha[element]; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Return the number of attributes in the list. |
| 35 | * |
| 36 | * @return The number of attributes in the list. |
| 37 | */ |
| 38 | |
| 39 | public int getLength() { |
| 40 | int i = firstAttribute; |
| 41 | while (i < tree.numberOfAttributes && tree.attParent[i] == element) { |
| 42 | i++; |
| 43 | } |
| 44 | return i - firstAttribute; |
| 45 | } |
| 46 | |
| 47 | public int getFingerprint(int index) { |
| 48 | int nc = tree.attCode[firstAttribute + index];; |
| 49 | return nc == -1 ? -1 : nc & NamePool.FP_MASK; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Get the node name of an attribute (by position) |
| 54 | * |
| 55 | * @param index The position of the attribute in the list |
| 56 | * @return The node name of the attribute, or null if there is no attribute in that position |
| 57 | */ |
| 58 | /*@NotNull*/ |
| 59 | public NodeName getNodeName(int index) { |
| 60 | return new CodedName(tree.attCode[firstAttribute + index], getPrefix(index), tree.getNamePool()); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Get the type annotation of an attribute (by position). |
| 65 | * |
| 66 | * @param index The position of the attribute in the list. |
| 67 | * @return The type annotation of the attribute |
| 68 | */ |
| 69 | |
| 70 | public SimpleType getTypeAnnotation(int index) { |
| 71 | if (tree.attType == null) { |
| 72 | return BuiltInAtomicType.UNTYPED_ATOMIC; |
| 73 | } |
| 74 | return tree.getAttributeType(firstAttribute + index); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Get the location of an attribute (by position) |
| 79 | * |
| 80 | * @param index The position of the attribute in the list. |
| 81 | * @return The location of the attribute, where available |
| 82 | */ |
| 83 | |
| 84 | public Location getLocation(int index) { |
| 85 | return ExplicitLocation.UNKNOWN_LOCATION; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Get the systemId part of the location of an attribute, at a given index. |
| 90 | * <p>Attribute location information is not available from a SAX parser, so this method |
| 91 | * is not useful for getting the location of an attribute in a source document. However, |
| 92 | * in a Saxon result document, the location information represents the location in the |
| 93 | * stylesheet of the instruction used to generate this attribute, which is useful for |
| 94 | * debugging.</p> |
| 95 | * |
| 96 | * @param index the required attribute |
| 97 | * @return the systemId of the location of the attribute |
| 98 | */ |
| 99 | |
| 100 | public String getSystemId(int index) { |
| 101 | return tree.getSystemId(element); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Get the line number part of the location of an attribute, at a given index. |
| 106 | * <p>Attribute location information is not available from a SAX parser, so this method |
| 107 | * is not useful for getting the location of an attribute in a source document. However, |
| 108 | * in a Saxon result document, the location information represents the location in the |
| 109 | * stylesheet of the instruction used to generate this attribute, which is useful for |
| 110 | * debugging.</p> |
| 111 | * |
| 112 | * @param index the required attribute |
| 113 | * @return the line number of the location of the attribute |
| 114 | */ |
| 115 | |
| 116 | public int getLineNumber(int index) { |
| 117 | return -1; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Get the properties of an attribute (by position) |
| 122 | * |
| 123 | * @param index The position of the attribute in the list. |
| 124 | * @return The properties of the attribute. This is a set |
| 125 | * of bit-settings defined in class {@link net.sf.saxon.event.ReceiverOptions}. The |
| 126 | * most interesting of these is {{@link net.sf.saxon.event.ReceiverOptions#DEFAULTED_ATTRIBUTE}, |
| 127 | * which indicates an attribute that was added to an element as a result of schema validation. |
| 128 | */ |
| 129 | |
| 130 | public int getProperties(int index) { |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Get the prefix of the name of an attribute (by position). |
| 136 | * |
| 137 | * @param index The position of the attribute in the list. |
| 138 | * @return The prefix of the attribute name as a string, or null if there |
| 139 | * is no attribute at that position. Returns "" for an attribute that |
| 140 | * has no prefix. |
| 141 | */ |
| 142 | |
| 143 | public String getPrefix(int index) { |
| 144 | return tree.prefixPool.getPrefix(tree.attCode[firstAttribute + index] >> 20); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Get the lexical QName of an attribute (by position). |
| 149 | * |
| 150 | * @param index The position of the attribute in the list. |
| 151 | * @return The lexical QName of the attribute as a string, or null if there |
| 152 | * is no attribute at that position. |
| 153 | */ |
| 154 | |
| 155 | public String getQName(int index) { |
| 156 | return getNodeName(index).getDisplayName(); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Get the local name of an attribute (by position). |
| 161 | * |
| 162 | * @param index The position of the attribute in the list. |
| 163 | * @return The local name of the attribute as a string, or null if there |
| 164 | * is no attribute at that position. |
| 165 | */ |
| 166 | |
| 167 | public String getLocalName(int index) { |
| 168 | return tree.getNamePool().getLocalName(tree.attCode[firstAttribute + index]); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Get the namespace URI of an attribute (by position). |
| 173 | * |
| 174 | * @param index The position of the attribute in the list. |
| 175 | * @return The local name of the attribute as a string, or null if there |
| 176 | * is no attribute at that position. |
| 177 | */ |
| 178 | |
| 179 | public String getURI(int index) { |
| 180 | return tree.getNamePool().getURI(tree.attCode[firstAttribute + index]); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Get the index of an attribute (by name). |
| 185 | * |
| 186 | * @param uri The namespace uri of the attribute. |
| 187 | * @param localname The local name of the attribute. |
| 188 | * @return The index position of the attribute, or -1 if there is no attribute with this name |
| 189 | */ |
| 190 | |
| 191 | public int getIndex(String uri, String localname) { |
| 192 | int fingerprint = tree.getNamePool().getFingerprint(uri, localname); |
| 193 | return getIndexByFingerprint(fingerprint); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Get the index, given the fingerprint |
| 198 | * |
| 199 | * @param fingerprint the NamePool fingerprint of the required attribute name |
| 200 | * @return The index position of the attribute, or -1 if there is no attribute with this name |
| 201 | */ |
| 202 | |
| 203 | public int getIndexByFingerprint(int fingerprint) { |
| 204 | int i = firstAttribute; |
| 205 | while (tree.attParent[i] == element) { |
| 206 | if ((tree.attCode[i] & NamePool.FP_MASK) == fingerprint) { |
| 207 | return i - firstAttribute; |
| 208 | } |
| 209 | i++; |
| 210 | } |
| 211 | return -1; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Get the attribute value using its fingerprint |
| 216 | */ |
| 217 | |
| 218 | /*@Nullable*/ |
| 219 | public String getValueByFingerprint(int fingerprint) { |
| 220 | return getValue(getIndexByFingerprint(fingerprint)); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Get the value of an attribute (by name). |
| 225 | * |
| 226 | * @param uri The namespace uri of the attribute. |
| 227 | * @param localname The local name of the attribute. |
| 228 | * @return The index position of the attribute |
| 229 | */ |
| 230 | |
| 231 | /*@Nullable*/ |
| 232 | public String getValue(String uri, String localname) { |
| 233 | return getValue(getIndex(uri, localname)); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Get the value of an attribute (by position). |
| 238 | * |
| 239 | * @param index The position of the attribute in the list. |
| 240 | * @return The attribute value as a string, or null if |
| 241 | * there is no attribute at that position. |
| 242 | */ |
| 243 | |
| 244 | /*@Nullable*/ |
| 245 | public String getValue(int index) { |
| 246 | CharSequence cs = tree.attValue[firstAttribute + index]; |
| 247 | return cs == null ? null : cs.toString(); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Determine whether a given attribute has the is-ID property set |
| 252 | */ |
| 253 | |
| 254 | public boolean isId(int index) { |
| 255 | return (getTypeAnnotation(index).getFingerprint() == StandardNames.XS_ID) || |
| 256 | ((tree.attCode[firstAttribute + index] & NamePool.FP_MASK) == StandardNames.XML_ID); |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Determine whether a given attribute has the is-idref property set |
| 261 | */ |
| 262 | |
| 263 | public boolean isIdref(int index) { |
| 264 | return (getTypeAnnotation(index).getFingerprint() == StandardNames.XS_IDREF) || |
| 265 | (getTypeAnnotation(index).getFingerprint() == StandardNames.XS_IDREFS); |
| 266 | } |
| 267 | } |
| 268 |