Debian Codegraph @

net/sf/saxon/value/AnyURIValue.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.tree.util.FastStringBuffer;
11 import net.sf.saxon.type.AtomicType;
12 import net.sf.saxon.type.BuiltInAtomicType;
13
14 import java.nio.ByteBuffer;
15 import java.nio.charset.Charset;
16
17
18 /**
19 * An XPath value of type xs:anyURI.
20 * <p>This is implemented as a subtype of StringValue even though xs:anyURI is not a subtype of
21 * xs:string in the XPath type hierarchy. This enables type promotion from URI to String to happen
22 * automatically in most cases where it is appropriate.</p>
23 * <p>This implementation of xs:anyURI allows any string to be contained in the value space. To check that
24 * the URI is valid according to some set of syntax rules, the caller should invoke a {@link net.sf.saxon.lib.StandardURIChecker}
25 * before constructing the AnyURIValue.</p>
26 */
27
28 public final class AnyURIValue extends StringValue {
29
30 /*@NotNull*/ public static final AnyURIValue EMPTY_URI = new AnyURIValue("");
31
32
33 /**
34 * Constructor
35 *
36 * @param value the String value. Null is taken as equivalent to "". This constructor
37 * does not check that the value is a valid anyURI instance. It does however
38 * perform whitespace normalization.
39 */
40
41 public AnyURIValue(/*@Nullable*/ CharSequence value) {
42 this.value = value == null ? "" : Whitespace.collapseWhitespace(value).toString();
43 typeLabel = BuiltInAtomicType.ANY_URI;
44 }
45
46 /**
47 * Constructor for a user-defined subtype of anyURI
48 *
49 * @param value the String value. Null is taken as equivalent to "".
50 * @param type a user-defined subtype of anyURI. It is the caller's responsibility
51 * to ensure that this is actually a subtype of anyURI, and that the value conforms
52 * to the definition of this type.
53 */
54
55 public AnyURIValue(/*@Nullable*/ CharSequence value, AtomicType type) {
56 this.value = value == null ? "" : Whitespace.collapseWhitespace(value).toString();
57 typeLabel = type;
58 }
59
60
61 /**
62 * Create a copy of this atomic value, with a different type label
63 *
64 * @param typeLabel the type label of the new copy. The caller is responsible for checking that
65 * the value actually conforms to this type.
66 */
67
68 /*@NotNull*/
69 public AtomicValue copyAsSubType(AtomicType typeLabel) {
70 AnyURIValue v = new AnyURIValue(value);
71 v.typeLabel = typeLabel;
72 return v;
73 }
74
75 /*@NotNull*/
76 public BuiltInAtomicType getPrimitiveType() {
77 return BuiltInAtomicType.ANY_URI;
78 }
79
80
81 /*@Nullable*/
82 public static String decode(/*@Nullable*/ String s) {
83 // Evaluates all escapes in s, applying UTF-8 decoding if needed. Assumes
84 // that escapes are well-formed syntactically, i.e., of the form %XX. If a
85 // sequence of escaped octets is not valid UTF-8 then the erroneous octets
86 // are replaced with '\uFFFD'.
87 // Exception: any "%" found between "[]" is left alone. It is an IPv6 literal
88 // with a scope_id
89 //
90
91 if (s == null) {
92 return s;
93 }
94 int n = s.length();
95 if (n == 0) {
96 return s;
97 }
98 if (s.indexOf('%') < 0) {
99 return s;
100 }
101
102 FastStringBuffer sb = new FastStringBuffer(n);
103 ByteBuffer bb = ByteBuffer.allocate(n);
104 Charset utf8 = Charset.forName("UTF-8");
105
106 // This is not horribly efficient, but it will do for now
107 char c = s.charAt(0);
108 boolean betweenBrackets = false;
109
110 for (int i = 0; i < n; ) {
111 assert c == s.charAt(i); // Loop invariant
112 if (c == '[') {
113 betweenBrackets = true;
114 } else if (betweenBrackets && c == ']') {
115 betweenBrackets = false;
116 }
117 if (c != '%' || betweenBrackets) {
118 sb.append(c);
119 if (++i >= n) {
120 break;
121 }
122 c = s.charAt(i);
123 continue;
124 }
125 bb.clear();
126 for (; ; ) {
127 assert n - i >= 2;
128 bb.put(hex(s.charAt(++i), s.charAt(++i)));
129 if (++i >= n) {
130 break;
131 }
132 c = s.charAt(i);
133 if (c != '%') {
134 break;
135 }
136 }
137 bb.flip();
138 sb.append(utf8.decode(bb));
139 }
140
141 return sb.toString();
142 }
143
144 private static byte hex(char high, char low) {
145 return (byte) ((hexToDec(high) << 4) | hexToDec(low));
146 }
147
148 private static int hexToDec(char c) {
149 if (c >= '0' && c <= '9') {
150 return c - '0';
151 } else if (c >= 'a' && c <= 'f') {
152 return c - 'a' + 10;
153 } else if (c >= 'A' && c <= 'F') {
154 return c - 'A' + 10;
155 } else {
156 return 0;
157 }
158 }
159
160
161 }
162