Debian Codegraph @

net/sf/saxon/value/QNameValue.java

raw symbols SCIP
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.functions.AccessorFn;
11 import net.sf.saxon.om.NameChecker;
12 import net.sf.saxon.om.SequenceTool;
13 import net.sf.saxon.om.StructuredQName;
14 import net.sf.saxon.trans.XPathException;
15 import net.sf.saxon.type.AtomicType;
16 import net.sf.saxon.type.BuiltInAtomicType;
17
18 /**
19 * A QName value. This implements the so-called "triples proposal", in which the prefix is retained as
20 * part of the value. The prefix is not used in any operation on a QName other than conversion of the
21 * QName to a string.
22 */
23
24 public class QNameValue extends QualifiedNameValue {
25
26
27 /**
28 * Constructor for a QName that is known to be valid. No validation takes place.
29 *
30 * @param prefix The prefix part of the QName (not used in comparisons). Use "" to represent the
31 * default prefix.
32 * @param uri The namespace part of the QName. Use "" to represent the non-namespace.
33 * @param localName The local part of the QName
34 */
35
36 public QNameValue(String prefix, String uri, String localName) {
37 this(prefix, uri, localName, BuiltInAtomicType.QNAME);
38 }
39
40 /**
41 * Constructor for a QName that is known to be valid, allowing a user-defined subtype of QName
42 * to be specified. No validation takes place.
43 *
44 * @param prefix The prefix part of the QName (not used in comparisons). Use "" to represent the
45 * default prefix (but null is also accepted)
46 * @param uri The namespace part of the QName. Use null to represent the non-namespace (but "" is also
47 * accepted).
48 * @param localName The local part of the QName
49 * @param type The type label, xs:QName or a subtype of xs:QName
50 */
51
52 public QNameValue(String prefix, String uri, String localName, /*@Nullable*/ AtomicType type) {
53 qName = new StructuredQName(prefix, uri, localName);
54 if (type == null) {
55 type = BuiltInAtomicType.QNAME;
56 }
57 typeLabel = type;
58 }
59
60 /**
61 * Constructor. This constructor validates that the local part is a valid NCName.
62 *
63 * @param prefix The prefix part of the QName (not used in comparisons). Use "" to represent the
64 * default prefix (but null is also accepted).
65 * Note that the prefix is not checked for lexical correctness, because in most cases
66 * it will already have been matched against in-scope namespaces. Where necessary the caller must
67 * check the prefix.
68 * @param uri The namespace part of the QName. Use null to represent the non-namespace (but "" is also
69 * accepted).
70 * @param localName The local part of the QName
71 * @param type The atomic type, which must be either xs:QName, or a
72 * user-defined type derived from xs:QName by restriction
73 * @param check Supply false if the name does not need to be checked (the caller asserts that it is known to be valid)
74 * @throws XPathException if the local part of the name is malformed or if the name has a null
75 * namespace with a non-empty prefix
76 */
77
78 public QNameValue(String prefix, String uri, String localName, AtomicType type, boolean check) throws XPathException {
79 if (!NameChecker.isValidNCName(localName)) {
80 XPathException err = new XPathException("Malformed local name in QName: '" + localName + '\'');
81 err.setErrorCode("FORG0001");
82 throw err;
83 }
84 prefix = prefix == null ? "" : prefix;
85 uri = "".equals(uri) ? null : uri;
86 if (check && uri == null && prefix.length() != 0) {
87 XPathException err = new XPathException("QName has null namespace but non-empty prefix");
88 err.setErrorCode("FOCA0002");
89 throw err;
90 }
91 qName = new StructuredQName(prefix, uri, localName);
92 typeLabel = type;
93 }
94
95 /**
96 * Constructor
97 *
98 * @param qName the name as a StructuredQName
99 * @param typeLabel idenfies a subtype of xs:QName
100 */
101
102 public QNameValue(/*@Nullable*/ StructuredQName qName, /*@Nullable*/ AtomicType typeLabel) {
103 if (qName == null) {
104 throw new NullPointerException("qName");
105 }
106 if (typeLabel == null) {
107 throw new NullPointerException("typeLabel");
108 }
109 this.qName = qName;
110 this.typeLabel = typeLabel;
111 }
112
113 /**
114 * Create a copy of this atomic value, with a different type label
115 *
116 * @param typeLabel the type label of the new copy. The caller is responsible for checking that
117 * the value actually conforms to this type.
118 */
119
120 /*@NotNull*/
121 public AtomicValue copyAsSubType(AtomicType typeLabel) {
122 return new QNameValue(qName, typeLabel);
123 }
124
125 /**
126 * Determine the primitive type of the value. This delivers the same answer as
127 * getItemType().getPrimitiveItemType(). The primitive types are
128 * the 19 primitive types of XML Schema, plus xs:integer, xs:dayTimeDuration and xs:yearMonthDuration,
129 * and xs:untypedAtomic. For external objects, the result is AnyAtomicType.
130 */
131
132 /*@NotNull*/
133 public BuiltInAtomicType getPrimitiveType() {
134 return BuiltInAtomicType.QNAME;
135 }
136
137 /**
138 * Get a component. Returns a zero-length string if the namespace-uri component is
139 * requested and is not present.
140 *
141 * @param part either Component.LOCALNAME or Component.NAMESPACE indicating which
142 * component of the value is required
143 * @return either the local name or the namespace URI, in each case as a StringValue
144 */
145
146 /*@Nullable*/
147 public AtomicValue getComponent(AccessorFn.Component part) {
148 switch (part) {
149 case LOCALNAME:
150 return new StringValue(getLocalName(), BuiltInAtomicType.NCNAME);
151 case NAMESPACE:
152 return new AnyURIValue(getNamespaceURI());
153 case PREFIX:
154 String prefix = getPrefix();
155 if (prefix.isEmpty()) {
156 return null;
157 } else {
158 return new StringValue(prefix, BuiltInAtomicType.NCNAME);
159 }
160 default:
161 throw new UnsupportedOperationException("Component of QName must be URI, Local Name, or Prefix");
162 }
163 }
164
165 /**
166 * Determine if two QName values are equal. This comparison ignores the prefix part
167 * of the value.
168 *
169 * @throws ClassCastException if they are not comparable
170 */
171
172 public boolean equals(/*@NotNull*/ Object other) {
173 return other instanceof QNameValue && qName.equals(((QNameValue) other).qName);
174 }
175
176 /*@NotNull*/
177 public Comparable getSchemaComparable() {
178 return new QNameComparable();
179 }
180
181 private class QNameComparable implements Comparable {
182
183 /*@NotNull*/
184 public QNameValue getQNameValue() {
185 return QNameValue.this;
186 }
187
188 public int compareTo(/*@NotNull*/ Object o) {
189 return equals(o) ? 0 : SequenceTool.INDETERMINATE_ORDERING;
190 }
191
192 public boolean equals(/*@NotNull*/ Object o) {
193 return (o instanceof QNameComparable && qName.equals(((QNameComparable) o).getQNameValue().qName));
194 }
195
196 public int hashCode() {
197 return qName.hashCode();
198 }
199 }
200
201 }
202