KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > value > HexBinaryValue


1 package net.sf.saxon.value;
2 import net.sf.saxon.expr.XPathContext;
3 import net.sf.saxon.om.FastStringBuffer;
4 import net.sf.saxon.trans.DynamicError;
5 import net.sf.saxon.trans.XPathException;
6 import net.sf.saxon.type.BuiltInAtomicType;
7 import net.sf.saxon.type.ItemType;
8 import net.sf.saxon.type.Type;
9 import net.sf.saxon.type.ValidationException;
10 import net.sf.saxon.ConversionContext;
11
12 /**
13 * A value of type xs:hexBinary
14 */

15
16 public class HexBinaryValue extends AtomicValue {
17
18     private byte[] binaryValue;
19
20
21     /**
22     * Constructor: create a hexBinary value from a supplied string, in which
23      * each octet is represented by a pair of values from 0-9, a-f, A-F
24     */

25
26     public HexBinaryValue(CharSequence JavaDoc s) throws XPathException {
27         if ((s.length() & 1) != 0) {
28             DynamicError err = new DynamicError(
29                     "A hexBinary value must contain an even number of characters");
30             err.setErrorCode("FORG0001");
31             throw err;
32         }
33         binaryValue = new byte[s.length() / 2];
34         for (int i=0; i<binaryValue.length; i++) {
35             binaryValue[i] = (byte)((fromHex(s.charAt(2*i))<<4) +
36                     (fromHex(s.charAt(2*i+1))));
37         }
38     }
39
40     /**
41      * Constructor: create a hexBinary value from a given array of bytes
42      */

43
44     public HexBinaryValue(byte[] value) {
45         this.binaryValue = value;
46     }
47
48    /**
49      * Get the binary value
50      */

51
52     public byte[] getBinaryValue() {
53         return binaryValue;
54     }
55
56     /**
57      * Decode a single hex digit
58      * @param c the hex digit
59      * @return the numeric value of the hex digit
60      * @throws XPathException if it isn't a hex digit
61      */

62
63     private int fromHex(char c) throws XPathException {
64         int d = "0123456789ABCDEFabcdef".indexOf(c);
65         if (d > 15) {
66             d = d - 6;
67         }
68         if (d < 0) {
69             DynamicError err = new DynamicError("Invalid hexadecimal digit");
70             err.setErrorCode("FORG0001");
71             throw err;
72         }
73         return d;
74     }
75
76     /**
77     * Convert to target data type
78     * @param requiredType an integer identifying the required atomic type
79     * @param conversion
80      * @return an AtomicValue, a value of the required type; or an ErrorValue
81     */

82
83     public AtomicValue convertPrimitive(BuiltInAtomicType requiredType, boolean validate, ConversionContext conversion) {
84         switch(requiredType.getPrimitiveType()) {
85         case Type.HEX_BINARY:
86         case Type.ATOMIC:
87         case Type.ITEM:
88             return this;
89         case Type.STRING:
90             return new StringValue(getStringValueCS());
91         case Type.UNTYPED_ATOMIC:
92             return new UntypedAtomicValue(getStringValueCS());
93         case Type.BASE64_BINARY:
94             return new Base64BinaryValue(binaryValue);
95
96         default:
97             ValidationException err = new ValidationException("Cannot convert hexBinarry to " +
98                                      requiredType.getDisplayName());
99             //err.setXPathContext(context);
100
err.setErrorCode("FORG0001");
101             return new ValidationErrorValue(err);
102         }
103     }
104
105     /**
106     * Convert to string
107     * @return the canonical representation.
108     */

109
110     public String JavaDoc getStringValue() {
111         String JavaDoc digits = "0123456789ABCDEF";
112         FastStringBuffer sb = new FastStringBuffer(binaryValue.length * 2);
113         for (int i=0; i<binaryValue.length; i++) {
114             sb.append(digits.charAt((binaryValue[i]>>4)&0xf));
115             sb.append(digits.charAt(binaryValue[i]&0xf));
116         }
117         return sb.toString();
118     }
119
120
121     /**
122     * Determine the data type of the exprssion
123     * @return Type.HEX_BINARY_TYPE
124     */

125
126     public ItemType getItemType() {
127         return Type.HEX_BINARY_TYPE;
128     }
129
130     /**
131      * Get the number of octets in the value
132      */

133
134     public int getLengthInOctets() {
135         return binaryValue.length;
136     }
137
138     /**
139     * Convert to Java object (for passing to external functions)
140     */

141
142     public Object JavaDoc convertToJava(Class JavaDoc target, XPathContext context) throws XPathException {
143
144         if (target.isAssignableFrom(HexBinaryValue.class)) {
145             return this;
146         } else if (target.isAssignableFrom(String JavaDoc.class)) {
147             return getStringValue();
148         } else {
149             Object JavaDoc o = super.convertToJava(target, context);
150             if (o == null) {
151                 throw new DynamicError("Conversion of hexBinary to " + target.getName() +
152                         " is not supported");
153             }
154             return o;
155         }
156     }
157
158
159     /**
160     * Test if the two hexBinary values are equal.
161     */

162
163     public boolean equals(Object JavaDoc other) {
164         HexBinaryValue v2;
165         if (other instanceof HexBinaryValue) {
166             v2 = (HexBinaryValue)other;
167         } else if (other instanceof AtomicValue) {
168             try {
169                 v2 = (HexBinaryValue)((AtomicValue)other).convert(Type.HEX_BINARY, null);
170             } catch (XPathException err) {
171                 return false;
172             }
173         } else {
174             return false;
175         }
176         if (binaryValue.length != v2.binaryValue.length) {
177             return false;
178         };
179         for (int i = 0; i < binaryValue.length; i++) {
180             if (binaryValue[i] != v2.binaryValue[i]) {
181                 return false;
182             };
183         }
184         return true;
185     }
186
187      public int hashCode() {
188         return Base64BinaryValue.byteArrayHashCode(binaryValue);
189     }
190
191 }
192
193 //
194
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
195
// you may not use this file except in compliance with the License. You may obtain a copy of the
196
// License at http://www.mozilla.org/MPL/
197
//
198
// Software distributed under the License is distributed on an "AS IS" basis,
199
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
200
// See the License for the specific language governing rights and limitations under the License.
201
//
202
// The Original Code is: all this file.
203
//
204
// The Initial Developer of the Original Code is Michael H. Kay, Saxonica.
205
//
206
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
207
//
208
// Contributor(s): none.
209
//
210

211
Popular Tags