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