KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > types > HexBinary


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55 package org.jboss.axis.types;
56
57 import org.jboss.axis.utils.JavaUtils;
58 import org.jboss.axis.utils.Messages;
59
60 import java.io.ByteArrayOutputStream JavaDoc;
61
62 /**
63  * Custom class for supporting primitive XSD data type hexBinary.
64  *
65  * @author Davanum Srinivas <dims@yahoo.com>
66  */

67 public class HexBinary
68 {
69    byte[] m_value = null;
70
71    public HexBinary()
72    {
73    }
74
75    public HexBinary(String JavaDoc string)
76    {
77       m_value = decode(string);
78    }
79
80    public HexBinary(byte[] bytes)
81    {
82       m_value = bytes;
83    }
84
85    public HexBinary(Byte JavaDoc[] bytes)
86    {
87       m_value = new byte[bytes.length];
88       for (int i = 0; i < bytes.length; i++)
89          m_value[i] = bytes[i].byteValue();
90    }
91
92    public byte[] getBytes()
93    {
94       return m_value;
95    }
96
97    public String JavaDoc toString()
98    {
99       return encode(m_value);
100    }
101
102    public int hashCode()
103    {
104       //TODO: How do we hash this?
105
return super.hashCode();
106    }
107
108    public boolean equals(Object JavaDoc object)
109    {
110       //TODO: Is this good enough?
111
String JavaDoc s1 = object.toString();
112       String JavaDoc s2 = this.toString();
113       return s1.equals(s2);
114    }
115
116    public static final String JavaDoc ERROR_ODD_NUMBER_OF_DIGITS =
117            Messages.getMessage("oddDigits00");
118    public static final String JavaDoc ERROR_BAD_CHARACTER_IN_HEX_STRING =
119            Messages.getMessage("badChars01");
120
121    // Code from Ajp11, from Apache's JServ
122

123    // Table for HEX to DEC byte translation
124
public static final int[] DEC = {
125       -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
126       -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
127       -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
128       00, 01, 02, 03, 04, 05, 06, 07, 8, 9, -1, -1, -1, -1, -1, -1,
129       -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
130       -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
131       -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
132       -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
133       -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
134       -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
135       -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
136       -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
137       -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
138       -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
139       -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
140       -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
141    };
142
143    /**
144     * Convert a String of hexadecimal digits into the corresponding
145     * byte array by encoding each two hexadecimal digits as a byte.
146     *
147     * @param digits Hexadecimal digits representation
148     * @throws IllegalArgumentException if an invalid hexadecimal digit
149     * is found, or the input string contains an odd number of hexadecimal
150     * digits
151     */

152    public static byte[] decode(String JavaDoc digits)
153    {
154
155       ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
156       for (int i = 0; i < digits.length(); i += 2)
157       {
158          char c1 = digits.charAt(i);
159          if ((i + 1) >= digits.length())
160             throw new IllegalArgumentException JavaDoc
161                     (ERROR_ODD_NUMBER_OF_DIGITS);
162          char c2 = digits.charAt(i + 1);
163          byte b = 0;
164          if ((c1 >= '0') && (c1 <= '9'))
165             b += ((c1 - '0') * 16);
166          else if ((c1 >= 'a') && (c1 <= 'f'))
167             b += ((c1 - 'a' + 10) * 16);
168          else if ((c1 >= 'A') && (c1 <= 'F'))
169             b += ((c1 - 'A' + 10) * 16);
170          else
171             throw new IllegalArgumentException JavaDoc
172                     (ERROR_BAD_CHARACTER_IN_HEX_STRING);
173          if ((c2 >= '0') && (c2 <= '9'))
174             b += (c2 - '0');
175          else if ((c2 >= 'a') && (c2 <= 'f'))
176             b += (c2 - 'a' + 10);
177          else if ((c2 >= 'A') && (c2 <= 'F'))
178             b += (c2 - 'A' + 10);
179          else
180             throw new IllegalArgumentException JavaDoc
181                     (ERROR_BAD_CHARACTER_IN_HEX_STRING);
182          baos.write(b);
183       }
184       return (baos.toByteArray());
185
186    }
187
188
189    /**
190     * Convert a byte array into a printable format containing a
191     * String of hexadecimal digit characters (two per byte).
192     *
193     * @param bytes Byte array representation
194     */

195    public static String JavaDoc encode(byte bytes[])
196    {
197
198       StringBuffer JavaDoc sb = new StringBuffer JavaDoc(bytes.length * 2);
199       for (int i = 0; i < bytes.length; i++)
200       {
201          sb.append(convertDigit((int)(bytes[i] >> 4)));
202          sb.append(convertDigit((int)(bytes[i] & 0x0f)));
203       }
204       return (sb.toString());
205    }
206
207    /**
208     * Convert a Byte array into a printable format containing a
209     * String of hexadecimal digit characters (two per byte).
210     *
211     * @param bytes Byte array representation
212     */

213    public static String JavaDoc encode(Byte JavaDoc bytes[])
214    {
215       return encode((byte[])JavaUtils.convert(bytes, byte[].class));
216    }
217
218    /**
219     * Convert 4 hex digits to an int, and return the number of converted
220     * bytes.
221     *
222     * @param hex Byte array containing exactly four hexadecimal digits
223     * @throws IllegalArgumentException if an invalid hexadecimal digit
224     * is included
225     */

226    public static int convert2Int(byte[] hex)
227    {
228       // Code from Ajp11, from Apache's JServ
229

230       // assert b.length==4
231
// assert valid data
232
int len;
233       if (hex.length < 4) return 0;
234       if (DEC[hex[0]] < 0)
235          throw new IllegalArgumentException JavaDoc(ERROR_BAD_CHARACTER_IN_HEX_STRING);
236       len = DEC[hex[0]];
237       len = len << 4;
238       if (DEC[hex[1]] < 0)
239          throw new IllegalArgumentException JavaDoc(ERROR_BAD_CHARACTER_IN_HEX_STRING);
240       len += DEC[hex[1]];
241       len = len << 4;
242       if (DEC[hex[2]] < 0)
243          throw new IllegalArgumentException JavaDoc(ERROR_BAD_CHARACTER_IN_HEX_STRING);
244       len += DEC[hex[2]];
245       len = len << 4;
246       if (DEC[hex[3]] < 0)
247          throw new IllegalArgumentException JavaDoc(ERROR_BAD_CHARACTER_IN_HEX_STRING);
248       len += DEC[hex[3]];
249       return len;
250    }
251
252    /**
253     * [Private] Convert the specified value (0 .. 15) to the corresponding
254     * hexadecimal digit.
255     *
256     * @param value Value to be converted
257     */

258    private static char convertDigit(int value)
259    {
260
261       value &= 0x0f;
262       if (value >= 10)
263          return ((char)(value - 10 + 'a'));
264       else
265          return ((char)(value + '0'));
266
267    }
268 }
269
Popular Tags