Debian Codegraph @

net/sf/saxon/value/HexBinaryValue.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.expr.sort.AtomicMatchKey;
11 import net.sf.saxon.lib.StringCollator;
12 import net.sf.saxon.om.SequenceTool;
13 import net.sf.saxon.trans.XPathException;
14 import net.sf.saxon.tree.util.FastStringBuffer;
15 import net.sf.saxon.type.AtomicType;
16 import net.sf.saxon.type.BuiltInAtomicType;
17
18 import java.util.Arrays;
19
20 /**
21 * A value of type xs:hexBinary
22 */
23
24 public class HexBinaryValue extends AtomicValue implements AtomicMatchKey, Comparable {
25
26 private byte[] binaryValue;
27
28
29 /**
30 * Constructor: create a hexBinary value from a supplied string, in which
31 * each octet is represented by a pair of values from 0-9, a-f, A-F
32 *
33 * @param in character representation of the hexBinary value
34 * @throws XPathException if the input is invalid
35 */
36
37 public HexBinaryValue(CharSequence in) throws XPathException {
38 CharSequence s = Whitespace.trimWhitespace(in);
39 if ((s.length() & 1) != 0) {
40 XPathException err = new XPathException("A hexBinary value must contain an even number of characters");
41 err.setErrorCode("FORG0001");
42 throw err;
43 }
44 binaryValue = new byte[s.length() / 2];
45 for (int i = 0; i < binaryValue.length; i++) {
46 binaryValue[i] = (byte) ((fromHex(s.charAt(2 * i)) << 4) +
47 fromHex(s.charAt(2 * i + 1)));
48 }
49 typeLabel = BuiltInAtomicType.HEX_BINARY;
50 }
51
52 /**
53 * Constructor: create a HexBinary value from a supplied string in hexBinary encoding,
54 * with a specified type. This method throws no checked exceptions; the caller is expected
55 * to ensure that the string is a valid Base64 lexical representation, that it conforms
56 * to the specified type, and that the type is indeed a subtype of xs:base64Binary.
57 * An unchecked exception such as an IllegalArgumentException may be thrown if these
58 * conditions are not satisfied, but this is not guaranteed.
59 *
60 * @param s the value in hexBinary encoding, with no leading or trailing whitespace
61 * @param type the atomic type. This must be xs:base64binary or a subtype.
62 */
63
64 public HexBinaryValue(/*@NotNull*/ CharSequence s, AtomicType type) {
65 if ((s.length() & 1) != 0) {
66 throw new IllegalArgumentException(
67 "A hexBinary value must contain an even number of characters");
68 }
69 binaryValue = new byte[s.length() / 2];
70 try {
71 for (int i = 0; i < binaryValue.length; i++) {
72 binaryValue[i] = (byte) ((fromHex(s.charAt(2 * i)) << 4) +
73 fromHex(s.charAt(2 * i + 1)));
74 }
75 } catch (XPathException e) {
76 throw new IllegalArgumentException(e.getMessage());
77 }
78 typeLabel = type;
79 }
80
81 /**
82 * Constructor: create a hexBinary value from a given array of bytes
83 *
84 * @param value the value as an array of bytes
85 */
86
87 public HexBinaryValue(byte[] value) {
88 binaryValue = value;
89 typeLabel = BuiltInAtomicType.HEX_BINARY;
90 }
91
92 /**
93 * Create a primitive copy of this atomic value (usually so that the type label can be changed).
94 *
95 * @param typeLabel the target type (a derived type from hexBinary)
96 */
97
98 /*@NotNull*/
99 public AtomicValue copyAsSubType(AtomicType typeLabel) {
100 HexBinaryValue v = new HexBinaryValue(binaryValue);
101 v.typeLabel = typeLabel;
102 return v;
103 }
104
105 /**
106 * Determine the primitive type of the value. This delivers the same answer as
107 * getItemType().getPrimitiveItemType(). The primitive types are
108 * the 19 primitive types of XML Schema, plus xs:integer, xs:dayTimeDuration and xs:yearMonthDuration,
109 * and xs:untypedAtomic. For external objects, the result is AnyAtomicType.
110 */
111
112 /*@NotNull*/
113 public BuiltInAtomicType getPrimitiveType() {
114 return BuiltInAtomicType.HEX_BINARY;
115 }
116
117 /**
118 * Get the binary value
119 *
120 * @return the binary value, as a byte array
121 */
122
123 public byte[] getBinaryValue() {
124 return binaryValue;
125 }
126
127 /**
128 * Decode a single hex digit
129 *
130 * @param c the hex digit
131 * @return the numeric value of the hex digit
132 * @throws XPathException if it isn't a hex digit
133 */
134
135 private int fromHex(char c) throws XPathException {
136 int d = "0123456789ABCDEFabcdef".indexOf(c);
137 if (d > 15) {
138 d = d - 6;
139 }
140 if (d < 0) {
141 XPathException err = new XPathException("Invalid hexadecimal digit '" + c + "'");
142 err.setErrorCode("FORG0001");
143 throw err;
144 }
145 return d;
146 }
147
148 /**
149 * Convert to string
150 *
151 * @return the canonical representation.
152 */
153
154 /*@NotNull*/
155 public CharSequence getPrimitiveStringValue() {
156 String digits = "0123456789ABCDEF";
157 FastStringBuffer sb = new FastStringBuffer(binaryValue.length * 2);
158 for (byte aBinaryValue : binaryValue) {
159 sb.append(digits.charAt((aBinaryValue >> 4) & 0xf));
160 sb.append(digits.charAt(aBinaryValue & 0xf));
161 }
162 return sb;
163 }
164
165
166 /**
167 * Get the number of octets in the value
168 *
169 * @return the number of octets (bytes) in the value
170 */
171
172 public int getLengthInOctets() {
173 return binaryValue.length;
174 }
175
176 /**
177 * Support XML Schema comparison semantics
178 */
179
180 /*@NotNull*/
181 public Comparable<HexBinaryComparable> getSchemaComparable() {
182 return new HexBinaryComparable();
183 }
184
185 private class HexBinaryComparable implements Comparable<HexBinaryComparable> {
186
187 /*@NotNull*/
188 public HexBinaryValue getHexBinaryValue() {
189 return HexBinaryValue.this;
190 }
191
192 public int compareTo(/*@NotNull*/ HexBinaryComparable o) {
193 if (Arrays.equals(getHexBinaryValue().binaryValue,
194 o.getHexBinaryValue().binaryValue)) {
195 return 0;
196 } else {
197 return SequenceTool.INDETERMINATE_ORDERING;
198 }
199 }
200
201 public boolean equals(/*@NotNull*/ Object o) {
202 return o instanceof HexBinaryComparable && compareTo((HexBinaryComparable)o) == 0;
203 }
204
205 public int hashCode() {
206 return HexBinaryValue.this.hashCode();
207 }
208 }
209
210
211 /**
212 * Get an object value that implements the XPath equality and ordering comparison semantics for this value.
213 * If the ordered parameter is set to true, the result will be a Comparable and will support a compareTo()
214 * method with the semantics of the XPath lt/gt operator, provided that the other operand is also obtained
215 * using the getXPathComparable() method. In all cases the result will support equals() and hashCode() methods
216 * that support the semantics of the XPath eq operator, again provided that the other operand is also obtained
217 * using the getXPathComparable() method. A context argument is supplied for use in cases where the comparison
218 * semantics are context-sensitive, for example where they depend on the implicit timezone or the default
219 * collation.
220 *
221 * @param ordered true if an ordered comparison is required. In this case the result is null if the
222 * type is unordered; in other cases the returned value will be a Comparable.
223 * @param collator collation to be used for comparing strings
224 * @param implicitTimezone to be used for comparing dates/times with no timezone
225 * @return a key used for performing the comparison
226 */
227
228 /*@Nullable*/
229 public AtomicMatchKey getXPathComparable(boolean ordered, StringCollator collator, int implicitTimezone) {
230 return this;
231 }
232
233 /**
234 * Test if the two hexBinary or Base64Binaryvalues are equal.
235 */
236
237 public boolean equals(/*@NotNull*/ Object other) {
238 return other instanceof HexBinaryValue && Arrays.equals(binaryValue, ((HexBinaryValue) other).binaryValue);
239 }
240
241 public int hashCode() {
242 return Base64BinaryValue.byteArrayHashCode(binaryValue);
243 }
244
245 public int compareTo(Object o) {
246 byte[] other = ((HexBinaryValue)o).binaryValue;
247 int len0 = binaryValue.length;
248 int len1 = other.length;
249 int shorter = java.lang.Math.min(len0, len1);
250 for (int i=0; i<shorter; i++) {
251 int a = (int)binaryValue[i] & 0xff;
252 int b = (int)other[i] & 0xff;
253 if (a != b) {
254 return a < b ? -1 : +1;
255 }
256 }
257 return Integer.signum(len0 - len1);
258 }
259 }
260