| 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.value; |
| 9 | |
| 10 | import net.sf.saxon.expr.sort.AtomicMatchKey; |
| 11 | import net.sf.saxon.lib.ConversionRules; |
| 12 | import net.sf.saxon.lib.StringCollator; |
| 13 | import net.sf.saxon.om.StandardNames; |
| 14 | import net.sf.saxon.om.StructuredQName; |
| 15 | import net.sf.saxon.trans.XPathException; |
| 16 | import net.sf.saxon.type.AtomicType; |
| 17 | import net.sf.saxon.type.BuiltInAtomicType; |
| 18 | import net.sf.saxon.type.ValidationFailure; |
| 19 | |
| 20 | import javax.xml.namespace.QName; |
| 21 | |
| 22 | |
| 23 | /** |
| 24 | * A qualified name: this is an abstract superclass for QNameValue and NotationValue, representing the |
| 25 | * XPath primitive types xs:QName and xs:NOTATION respectively |
| 26 | */ |
| 27 | |
| 28 | public abstract class QualifiedNameValue extends AtomicValue implements AtomicMatchKey { |
| 29 | |
| 30 | /*@NotNull*/ protected StructuredQName qName; |
| 31 | |
| 32 | /** |
| 33 | * Factory method to construct either a QName or a NOTATION value, or a subtype of either of these. |
| 34 | * Note that it is the caller's responsibility to resolve the QName prefix into a URI |
| 35 | * |
| 36 | * @param prefix the prefix part of the value. Use "" or null for the empty prefix. |
| 37 | * @param uri the namespace URI part of the value. Use "" or null for the non-namespace |
| 38 | * @param local the local part of the value |
| 39 | * @param targetType the target type, which must be xs:QName or a subtype of xs:NOTATION or xs:QName |
| 40 | * @param lexicalForm the original lexical form of the value. This is needed in case there are facets |
| 41 | * such as pattern that check the lexical form |
| 42 | * @param rules the conversion rules to be applied |
| 43 | * @return the converted value |
| 44 | * @throws XPathException if the value cannot be converted. |
| 45 | */ |
| 46 | |
| 47 | /*@Nullable*/ |
| 48 | public static AtomicValue makeQName(String prefix, String uri, String local, |
| 49 | /*@NotNull*/ AtomicType targetType, CharSequence lexicalForm, ConversionRules rules) |
| 50 | throws XPathException { |
| 51 | |
| 52 | if (targetType.getFingerprint() == StandardNames.XS_QNAME) { |
| 53 | return new QNameValue(prefix, uri, local, BuiltInAtomicType.QNAME, true); |
| 54 | } else { |
| 55 | QualifiedNameValue qnv; |
| 56 | |
| 57 | if (targetType.getPrimitiveType() == StandardNames.XS_QNAME) { |
| 58 | qnv = new QNameValue(prefix, uri, local, targetType, true); |
| 59 | } else { |
| 60 | qnv = new NotationValue(prefix, uri, local, null); |
| 61 | } |
| 62 | ValidationFailure vf = targetType.validate(qnv, lexicalForm, rules); |
| 63 | if (vf != null) { |
| 64 | throw vf.makeException(); |
| 65 | } |
| 66 | qnv.setTypeLabel(targetType); |
| 67 | return qnv; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | |
| 72 | /** |
| 73 | * Get the string value as a String. Returns the QName as a lexical QName, retaining the original |
| 74 | * prefix if available. |
| 75 | */ |
| 76 | |
| 77 | public final String getPrimitiveStringValue() { |
| 78 | return qName.getDisplayName(); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Get the QName in Clark notation, that is "{uri}local" if in a namespace, or "local" otherwise |
| 83 | * |
| 84 | * @return the name in Clark notation |
| 85 | */ |
| 86 | |
| 87 | public final String getClarkName() { |
| 88 | return qName.getClarkName(); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Get the local part |
| 93 | * |
| 94 | * @return the local part of the name (the part after the colon) |
| 95 | */ |
| 96 | |
| 97 | /*@NotNull*/ |
| 98 | public final String getLocalName() { |
| 99 | return qName.getLocalPart(); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Get the namespace part. Returns the empty string for a name in no namespace. |
| 104 | * |
| 105 | * @return the namespace URI component of the name, or "" for a no-namespace name |
| 106 | */ |
| 107 | |
| 108 | /*@NotNull*/ |
| 109 | public final String getNamespaceURI() { |
| 110 | return qName.getURI(); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Get the prefix. Returns the empty string if the name is unprefixed. |
| 115 | * |
| 116 | * @return the prefix, or "" to indicate no prefix |
| 117 | */ |
| 118 | |
| 119 | /*@NotNull*/ |
| 120 | public final String getPrefix() { |
| 121 | return qName.getPrefix(); |
| 122 | } |
| 123 | |
| 124 | |
| 125 | /** |
| 126 | * Get an object value that implements the XPath equality and ordering comparison semantics for this value. |
| 127 | * If the ordered parameter is set to true, the result will be a Comparable and will support a compareTo() |
| 128 | * method with the semantics of the XPath lt/gt operator, provided that the other operand is also obtained |
| 129 | * using the getXPathComparable() method. In all cases the result will support equals() and hashCode() methods |
| 130 | * that support the semantics of the XPath eq operator, again provided that the other operand is also obtained |
| 131 | * using the getXPathComparable() method. A context argument is supplied for use in cases where the comparison |
| 132 | * semantics are context-sensitive, for example where they depend on the implicit timezone or the default |
| 133 | * collation. |
| 134 | * |
| 135 | * @param ordered true if an ordered comparison is required. In this case the result is null if the |
| 136 | * type is unordered; in other cases the returned value will be a Comparable. |
| 137 | * @param collator the collation to be used for the comparison |
| 138 | * @param implicitTimezone the XPath dynamic evaluation context, used in cases where the comparison is context |
| 139 | */ |
| 140 | |
| 141 | /*@Nullable*/ |
| 142 | public AtomicMatchKey getXPathComparable(boolean ordered, StringCollator collator, int implicitTimezone) { |
| 143 | return ordered ? null : this; |
| 144 | } |
| 145 | |
| 146 | public int hashCode() { |
| 147 | return qName.hashCode(); |
| 148 | } |
| 149 | |
| 150 | public boolean isIdentical(/*@NotNull*/ AtomicValue v) { |
| 151 | return super.isIdentical(v) && qName.getPrefix().equals(((QualifiedNameValue) v).getPrefix()); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Get a hashCode that offers the guarantee that if A.isIdentical(B), then A.identityHashCode() == B.identityHashCode() |
| 156 | * |
| 157 | * @return a hashCode suitable for use when testing for identity. |
| 158 | */ |
| 159 | @Override |
| 160 | public int identityHashCode() { |
| 161 | return qName.identityHashCode(); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * The toString() method returns the name in the form QName("uri", "local") |
| 166 | * |
| 167 | * @return the name in in the form QName("uri", "local") |
| 168 | */ |
| 169 | |
| 170 | /*@NotNull*/ |
| 171 | public String toString() { |
| 172 | return "QName(\"" + getNamespaceURI() + "\", \"" + getLocalName() + "\")"; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Construct a javax.xml.namespace.QName from this QualifiedNameValue |
| 177 | * |
| 178 | * @return an equivalent instance of the JAXP QName class |
| 179 | */ |
| 180 | |
| 181 | public QName toJaxpQName() { |
| 182 | return qName.toJaxpQName(); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Get the equivalent StructuredQName |
| 187 | * |
| 188 | * @return the equivalent StructuredQName |
| 189 | */ |
| 190 | |
| 191 | /*@NotNull*/ |
| 192 | public StructuredQName getStructuredQName() { |
| 193 | return qName; |
| 194 | } |
| 195 | } |