| 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.event.Receiver; |
| 11 | import net.sf.saxon.expr.parser.Location; |
| 12 | import net.sf.saxon.lib.FeatureKeys; |
| 13 | import net.sf.saxon.om.*; |
| 14 | import net.sf.saxon.trans.XPathException; |
| 15 | import net.sf.saxon.tree.util.FastStringBuffer; |
| 16 | import net.sf.saxon.type.BuiltInAtomicType; |
| 17 | import net.sf.saxon.type.SchemaType; |
| 18 | import net.sf.saxon.type.SimpleType; |
| 19 | import net.sf.saxon.type.Type; |
| 20 | |
| 21 | |
| 22 | /** |
| 23 | * A node in the XML parse tree representing an attribute. Note that this is |
| 24 | * generated only "on demand", when the attribute is selected by a select pattern. |
| 25 | * |
| 26 | * @author Michael H. Kay |
| 27 | */ |
| 28 | |
| 29 | final public class TinyAttributeImpl extends TinyNodeImpl { |
| 30 | |
| 31 | public TinyAttributeImpl(TinyTree tree, int nodeNr) { |
| 32 | this.tree = tree; |
| 33 | this.nodeNr = nodeNr; |
| 34 | } |
| 35 | |
| 36 | |
| 37 | public void setSystemId(String uri) { |
| 38 | // no action: an attribute has the same base URI as its parent |
| 39 | } |
| 40 | |
| 41 | @Override |
| 42 | public String getSystemId() { |
| 43 | NodeInfo parent = getParent(); |
| 44 | return parent == null ? null : getParent().getSystemId(); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Get the parent node |
| 49 | */ |
| 50 | |
| 51 | public TinyNodeImpl getParent() { |
| 52 | return tree.getNode(tree.attParent[nodeNr]); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Get the root node of the tree (not necessarily a document node) |
| 57 | * |
| 58 | * @return the NodeInfo representing the root of this tree |
| 59 | */ |
| 60 | |
| 61 | public NodeInfo getRoot() { |
| 62 | NodeInfo parent = getParent(); |
| 63 | if (parent == null) { |
| 64 | return this; // doesn't happen - parentless attributes are represented by the Orphan class |
| 65 | } else { |
| 66 | return parent.getRoot(); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Get the node sequence number (in document order). Sequence numbers are monotonic but not |
| 72 | * consecutive. In this implementation, elements have a zero |
| 73 | * least-significant word, while attributes and namespaces use the same value in the top word as |
| 74 | * the containing element, and use the bottom word to hold |
| 75 | * a sequence number, which numbers namespaces first and then attributes. |
| 76 | */ |
| 77 | |
| 78 | protected long getSequenceNumber() { |
| 79 | //noinspection ConstantConditions |
| 80 | return |
| 81 | ((TinyNodeImpl) getParent()).getSequenceNumber() |
| 82 | + 0x8000 + |
| 83 | (nodeNr - tree.alpha[tree.attParent[nodeNr]]); |
| 84 | // note the 0x8000 is to leave room for namespace nodes |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Return the type of node. |
| 89 | * |
| 90 | * @return Node.ATTRIBUTE |
| 91 | */ |
| 92 | |
| 93 | public final int getNodeKind() { |
| 94 | return Type.ATTRIBUTE; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Return the string value of the node. |
| 99 | * |
| 100 | * @return the attribute value |
| 101 | */ |
| 102 | |
| 103 | public CharSequence getStringValueCS() { |
| 104 | return tree.attValue[nodeNr]; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Return the string value of the node. |
| 109 | * |
| 110 | * @return the attribute value |
| 111 | */ |
| 112 | |
| 113 | public String getStringValue() { |
| 114 | return tree.attValue[nodeNr].toString(); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Get the fingerprint of the node, used for matching names |
| 119 | */ |
| 120 | |
| 121 | public int getFingerprint() { |
| 122 | return tree.attCode[nodeNr] & 0xfffff; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Get the name code of the node, used for finding names in the name pool |
| 127 | */ |
| 128 | |
| 129 | public int getNameCode() { |
| 130 | return tree.attCode[nodeNr]; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Get the prefix part of the name of this node. This is the name before the ":" if any. |
| 135 | * |
| 136 | * @return the prefix part of the name. For an unnamed node, return null. |
| 137 | */ |
| 138 | |
| 139 | public String getPrefix() { |
| 140 | int code = tree.attCode[nodeNr]; |
| 141 | if (!NamePool.isPrefixed(code)) { |
| 142 | return ""; |
| 143 | } |
| 144 | return tree.prefixPool.getPrefix(code >> 20); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Get the display name of this node. For elements and attributes this is [prefix:]localname. |
| 149 | * For unnamed nodes, it is an empty string. |
| 150 | * |
| 151 | * @return The display name of this node. |
| 152 | * For a node with no name, return an empty string. |
| 153 | */ |
| 154 | |
| 155 | public String getDisplayName() { |
| 156 | int code = tree.attCode[nodeNr]; |
| 157 | if (code < 0) { |
| 158 | return ""; |
| 159 | } |
| 160 | if (NamePool.isPrefixed(code)) { |
| 161 | return getPrefix() + ":" + getLocalPart(); |
| 162 | } else { |
| 163 | return getLocalPart(); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | |
| 168 | /** |
| 169 | * Get the local name of this node. |
| 170 | * |
| 171 | * @return The local name of this node. |
| 172 | * For a node with no name, return an empty string. |
| 173 | */ |
| 174 | |
| 175 | public String getLocalPart() { |
| 176 | return tree.getNamePool().getLocalName(tree.attCode[nodeNr]); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Get the URI part of the name of this node. |
| 181 | * |
| 182 | * @return The URI of the namespace of this node. For the default namespace, return an |
| 183 | * empty string |
| 184 | */ |
| 185 | |
| 186 | public final String getURI() { |
| 187 | int code = tree.attCode[nodeNr]; |
| 188 | if (!NamePool.isPrefixed(code)) { |
| 189 | return ""; |
| 190 | } |
| 191 | return tree.getNamePool().getURI(code); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Get the type annotation of this node, if any. The type annotation is represented as |
| 196 | * SchemaType object. |
| 197 | * <p>Types derived from a DTD are not reflected in the result of this method.</p> |
| 198 | * |
| 199 | * @return For element and attribute nodes: the type annotation derived from schema |
| 200 | * validation (defaulting to xs:untyped and xs:untypedAtomic in the absence of schema |
| 201 | * validation). For comments, text nodes, processing instructions, and namespaces: null. |
| 202 | * For document nodes, either xs:untyped if the document has not been validated, or |
| 203 | * xs:anyType if it has. |
| 204 | * @since 9.4 |
| 205 | */ |
| 206 | public SchemaType getSchemaType() { |
| 207 | if (tree.attType == null) { |
| 208 | return BuiltInAtomicType.UNTYPED_ATOMIC; |
| 209 | } |
| 210 | return tree.getAttributeType(nodeNr); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Get the typed value. |
| 215 | * |
| 216 | * @return the typed value. This will either be a single AtomicValue or a Value whose items are |
| 217 | * atomic values. |
| 218 | * @since 8.5 |
| 219 | */ |
| 220 | |
| 221 | public AtomicSequence atomize() throws XPathException { |
| 222 | return tree.getTypedValueOfAttribute(this, nodeNr); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Generate id. Returns key of owning element with the attribute namecode as a suffix |
| 227 | * |
| 228 | * @param buffer Buffer to contain the generated ID value |
| 229 | */ |
| 230 | |
| 231 | public void generateId(FastStringBuffer buffer) { |
| 232 | getParent().generateId(buffer); |
| 233 | buffer.append("a"); |
| 234 | buffer.append(Integer.toString(tree.attCode[nodeNr])); |
| 235 | // we previously used the attribute name. But this breaks the requirement |
| 236 | // that the result of generate-id consists entirely of alphanumeric ASCII |
| 237 | // characters |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Copy this node to a given outputter |
| 242 | */ |
| 243 | |
| 244 | public void copy(/*@NotNull*/ Receiver out, int copyOptions, Location locationId) throws XPathException { |
| 245 | SimpleType typeCode = CopyOptions.includes(copyOptions, CopyOptions.TYPE_ANNOTATIONS) ? |
| 246 | (SimpleType) getSchemaType() : BuiltInAtomicType.UNTYPED_ATOMIC; |
| 247 | out.attribute(NameOfNode.makeName(this), typeCode, getStringValue(), locationId, 0); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Get the line number of the node within its source document entity |
| 252 | */ |
| 253 | |
| 254 | public int getLineNumber() { |
| 255 | return getParent().getLineNumber(); |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Get the column number of the node within its source document entity |
| 260 | */ |
| 261 | |
| 262 | public int getColumnNumber() { |
| 263 | return getParent().getColumnNumber(); |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Determine whether this node has the is-id property |
| 268 | * |
| 269 | * @return true if the node is an ID |
| 270 | */ |
| 271 | |
| 272 | public boolean isId() { |
| 273 | return tree.isIdAttribute(nodeNr); |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Determine whether this node has the is-idref property |
| 278 | * |
| 279 | * @return true if the node is an IDREF or IDREFS element or attribute |
| 280 | */ |
| 281 | |
| 282 | public boolean isIdref() { |
| 283 | return tree.isIdrefAttribute(nodeNr); |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Ask whether the attribute results from expansion of attribute defaults |
| 288 | * |
| 289 | * @return true if this attribute resulted from expansion of default or fixed values defined |
| 290 | * in a schema. Note that this property will only be set if both the configuration properties |
| 291 | * {@link FeatureKeys#EXPAND_ATTRIBUTE_DEFAULTS} and {@link FeatureKeys#MARK_DEFAULTED_ATTRIBUTES} |
| 292 | * are set. |
| 293 | */ |
| 294 | |
| 295 | public boolean isDefaultedAttribute() { |
| 296 | return tree.isDefaultedAttribute(nodeNr); |
| 297 | } |
| 298 | |
| 299 | |
| 300 | /** |
| 301 | * The hashCode() method obeys the contract for hashCode(): that is, if two objects are equal |
| 302 | * (represent the same node) then they must have the same hashCode() |
| 303 | * |
| 304 | * @since 8.7 Previously, the effect of the equals() and hashCode() methods was not defined. Callers |
| 305 | * should therefore be aware that third party implementations of the NodeInfo interface may |
| 306 | * not implement the correct semantics. |
| 307 | */ |
| 308 | |
| 309 | public int hashCode() { |
| 310 | return ((int) (tree.getDocumentNumber() & 0x3ff) << 20) ^ nodeNr ^ 7 << 17; |
| 311 | } |
| 312 | |
| 313 | } |
| 314 |