KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.axis.types ;
17
18 import org.apache.axis.utils.Messages;
19
20 import java.io.ByteArrayOutputStream JavaDoc;
21
22 /**
23  * Custom class for supporting primitive XSD data type hexBinary.
24  *
25  * @author Davanum Srinivas <dims@yahoo.com>
26  */

27 public class HexBinary extends Object JavaDoc implements java.io.Serializable JavaDoc{
28
29     byte[] m_value = null;
30
31     public HexBinary() {
32     }
33
34     public HexBinary(String JavaDoc string){
35         m_value = decode(string);
36     }
37
38     public HexBinary(byte[] bytes){
39         m_value = bytes;
40     }
41
42     public byte[] getBytes(){
43         return m_value;
44     }
45
46     public String JavaDoc toString(){
47         return encode(m_value);
48     }
49
50     public int hashCode(){
51         //TODO: How do we hash this?
52
return super.hashCode();
53     }
54
55     public boolean equals(Object JavaDoc object){
56         //TODO: Is this good enough?
57
String JavaDoc s1 = object.toString();
58         String JavaDoc s2 = this.toString();
59         return s1.equals(s2);
60     }
61
62     public static final String JavaDoc ERROR_ODD_NUMBER_OF_DIGITS =
63             Messages.getMessage("oddDigits00");
64     public static final String JavaDoc ERROR_BAD_CHARACTER_IN_HEX_STRING =
65             Messages.getMessage("badChars01");
66
67     // Code from Ajp11, from Apache's JServ
68

69     // Table for HEX to DEC byte translation
70
public static final int[] DEC = {
71         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
72         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
73         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
74         00, 01, 02, 03, 04, 05, 06, 07, 8, 9, -1, -1, -1, -1, -1, -1,
75         -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
76         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
77         -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
78         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
79         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
80         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
81         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
82         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
83         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
84         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
85         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
86         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
87     };
88
89     /**
90      * Convert a String of hexadecimal digits into the corresponding
91      * byte array by encoding each two hexadecimal digits as a byte.
92      *
93      * @param digits Hexadecimal digits representation
94      *
95      * @exception IllegalArgumentException if an invalid hexadecimal digit
96      * is found, or the input string contains an odd number of hexadecimal
97      * digits
98      */

99     public static byte[] decode(String JavaDoc digits) {
100
101         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
102         for (int i = 0; i < digits.length(); i += 2) {
103             char c1 = digits.charAt(i);
104             if ((i+1) >= digits.length())
105                 throw new IllegalArgumentException JavaDoc
106                     (ERROR_ODD_NUMBER_OF_DIGITS);
107             char c2 = digits.charAt(i + 1);
108             byte b = 0;
109             if ((c1 >= '0') && (c1 <= '9'))
110                 b += ((c1 - '0') * 16);
111             else if ((c1 >= 'a') && (c1 <= 'f'))
112                 b += ((c1 - 'a' + 10) * 16);
113             else if ((c1 >= 'A') && (c1 <= 'F'))
114                 b += ((c1 - 'A' + 10) * 16);
115             else
116                 throw new IllegalArgumentException JavaDoc
117                     (ERROR_BAD_CHARACTER_IN_HEX_STRING);
118             if ((c2 >= '0') && (c2 <= '9'))
119                 b += (c2 - '0');
120             else if ((c2 >= 'a') && (c2 <= 'f'))
121                 b += (c2 - 'a' + 10);
122             else if ((c2 >= 'A') && (c2 <= 'F'))
123                 b += (c2 - 'A' + 10);
124             else
125                 throw new IllegalArgumentException JavaDoc
126                     (ERROR_BAD_CHARACTER_IN_HEX_STRING);
127             baos.write(b);
128         }
129         return (baos.toByteArray());
130
131     }
132
133
134     /**
135      * Convert a byte array into a printable format containing a
136      * String of hexadecimal digit characters (two per byte).
137      *
138      * @param bytes Byte array representation
139      */

140     public static String JavaDoc encode(byte bytes[]) {
141
142         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(bytes.length * 2);
143         for (int i = 0; i < bytes.length; i++) {
144             sb.append(convertDigit((int) (bytes[i] >> 4)));
145             sb.append(convertDigit((int) (bytes[i] & 0x0f)));
146         }
147         return (sb.toString());
148
149     }
150
151     /**
152      * Convert 4 hex digits to an int, and return the number of converted
153      * bytes.
154      *
155      * @param hex Byte array containing exactly four hexadecimal digits
156      *
157      * @exception IllegalArgumentException if an invalid hexadecimal digit
158      * is included
159      */

160     public static int convert2Int( byte[] hex ) {
161         // Code from Ajp11, from Apache's JServ
162

163         // assert b.length==4
164
// assert valid data
165
int len;
166         if(hex.length < 4 ) return 0;
167         if( DEC[hex[0]]<0 )
168             throw new IllegalArgumentException JavaDoc(ERROR_BAD_CHARACTER_IN_HEX_STRING);
169         len = DEC[hex[0]];
170         len = len << 4;
171         if( DEC[hex[1]]<0 )
172             throw new IllegalArgumentException JavaDoc(ERROR_BAD_CHARACTER_IN_HEX_STRING);
173         len += DEC[hex[1]];
174         len = len << 4;
175         if( DEC[hex[2]]<0 )
176             throw new IllegalArgumentException JavaDoc(ERROR_BAD_CHARACTER_IN_HEX_STRING);
177         len += DEC[hex[2]];
178         len = len << 4;
179         if( DEC[hex[3]]<0 )
180             throw new IllegalArgumentException JavaDoc(ERROR_BAD_CHARACTER_IN_HEX_STRING);
181         len += DEC[hex[3]];
182         return len;
183     }
184
185     /**
186      * [Private] Convert the specified value (0 .. 15) to the corresponding
187      * hexadecimal digit.
188      *
189      * @param value Value to be converted
190      */

191     private static char convertDigit(int value) {
192
193         value &= 0x0f;
194         if (value >= 10)
195             return ((char) (value - 10 + 'a'));
196         else
197             return ((char) (value + '0'));
198
199     }
200 }
201
Popular Tags