| 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.om; |
| 9 | |
| 10 | /** |
| 11 | * An implementation of NodeName that encapsulates an integer fingerprint, a string prefix, and a reference to the NamePool from which |
| 12 | * the fingerprint was allocated. |
| 13 | */ |
| 14 | public class CodedName implements NodeName { |
| 15 | |
| 16 | private int fingerprint; |
| 17 | private String prefix; |
| 18 | private NamePool pool; |
| 19 | |
| 20 | public CodedName(int fingerprint, String prefix, NamePool pool) { |
| 21 | // if (fingerprint >> 20 != 0) { |
| 22 | // throw new IllegalArgumentException(); |
| 23 | // } |
| 24 | this.fingerprint = fingerprint; |
| 25 | this.prefix = prefix; |
| 26 | this.pool = pool; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Get the prefix of the QName. |
| 31 | * |
| 32 | * @return the prefix. Returns the empty string if the name is unprefixed. |
| 33 | */ |
| 34 | public String getPrefix() { |
| 35 | return prefix; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Get the namespace URI of the QName. |
| 40 | * |
| 41 | * @return the URI. Returns the empty string to represent the no-namespace |
| 42 | */ |
| 43 | public String getURI() { |
| 44 | return pool.getURI(fingerprint); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Get the local part of the QName |
| 49 | * |
| 50 | * @return the local part of the QName |
| 51 | */ |
| 52 | public String getLocalPart() { |
| 53 | return pool.getLocalName(fingerprint); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Get the display name, that is the lexical QName in the form [prefix:]local-part |
| 58 | * |
| 59 | * @return the lexical QName |
| 60 | */ |
| 61 | public String getDisplayName() { |
| 62 | return prefix.isEmpty() ? getLocalPart() : prefix + ":" + getLocalPart(); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Get the name in the form of a StructuredQName |
| 67 | * |
| 68 | * @return the name in the form of a StructuredQName |
| 69 | */ |
| 70 | public StructuredQName getStructuredQName() { |
| 71 | StructuredQName qn = pool.getUnprefixedQName(fingerprint); |
| 72 | if (prefix.isEmpty()) { |
| 73 | return qn; |
| 74 | } else { |
| 75 | return new StructuredQName(prefix, qn.getURI(), qn.getLocalPart()); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Test whether this name is in the same namespace as another name |
| 81 | * |
| 82 | * @return true if the two names are in the same namespace |
| 83 | */ |
| 84 | public boolean isInSameNamespace(/*@NotNull*/ NodeName other) { |
| 85 | return getURI().equals(other.getURI()); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Test whether this name is in a given namespace |
| 90 | * |
| 91 | * @param ns the namespace to be tested against |
| 92 | * @return true if the name is in the specified namespace |
| 93 | */ |
| 94 | public boolean hasURI(String ns) { |
| 95 | return getURI().equals(ns); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Get a {@link net.sf.saxon.om.NamespaceBinding} whose (prefix, uri) pair are the prefix and URI of this |
| 100 | * node name |
| 101 | * |
| 102 | * @return the corresponding NamespaceBinding |
| 103 | */ |
| 104 | |
| 105 | public NamespaceBinding getNamespaceBinding() { |
| 106 | return new NamespaceBinding(prefix, pool.getURI(fingerprint)); |
| 107 | } |
| 108 | |
| 109 | |
| 110 | /** |
| 111 | * Ask whether this node name representation has a known fingerprint |
| 112 | * |
| 113 | * @return true if the method getFingerprint() will |
| 114 | * return a result other than -1 |
| 115 | */ |
| 116 | public boolean hasFingerprint() { |
| 117 | return true; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Get the fingerprint of this name if known. This method should not to any work to allocate |
| 122 | * a fingerprint if none is already available |
| 123 | * |
| 124 | * @return the fingerprint if known; otherwise -1 |
| 125 | */ |
| 126 | public int getFingerprint() { |
| 127 | return fingerprint; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Get the nameCode of this name, allocating a new code from the namepool if necessary |
| 132 | * |
| 133 | * @param namePool the NamePool used to allocate the name |
| 134 | * @return a nameCode for this name, newly allocated if necessary |
| 135 | */ |
| 136 | public int obtainFingerprint(NamePool namePool) { |
| 137 | return fingerprint; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Returns a hash code value for the object. |
| 142 | */ |
| 143 | @Override |
| 144 | public int hashCode() { |
| 145 | return StructuredQName.computeHashCode(getURI(), getLocalPart()); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Indicates whether some other object is "equal to" this one. |
| 150 | */ |
| 151 | @Override |
| 152 | public boolean equals(Object obj) { |
| 153 | if (obj instanceof NodeName) { |
| 154 | NodeName n = (NodeName) obj; |
| 155 | if (n.hasFingerprint()) { |
| 156 | return getFingerprint() == n.getFingerprint(); |
| 157 | } else { |
| 158 | return n.getLocalPart().equals(getLocalPart()) && n.hasURI(getURI()); |
| 159 | } |
| 160 | } else { |
| 161 | return false; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | public boolean isIdentical(IdentityComparable other) { |
| 166 | return other instanceof NodeName && |
| 167 | this.equals(other) && this.getPrefix().equals(((NodeName) other).getPrefix()); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Get a hashCode that offers the guarantee that if A.isIdentical(B), then A.identityHashCode() == B.identityHashCode() |
| 172 | * |
| 173 | * @return a hashCode suitable for use when testing for identity. |
| 174 | */ |
| 175 | public int identityHashCode() { |
| 176 | return hashCode() ^ getPrefix().hashCode(); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * The toString() method returns the value of getDisplayName(), that is, the lexical QName |
| 181 | * @return the value as a lexical QName |
| 182 | */ |
| 183 | |
| 184 | @Override |
| 185 | public String toString() { |
| 186 | return getDisplayName(); |
| 187 | } |
| 188 | } |
| 189 |