KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > encoding > CodeSetComponentInfo


1 /*
2  * @(#)CodeSetComponentInfo.java 1.31 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 /*
8  * Licensed Materials - Property of IBM
9  * RMI-IIOP v1.0
10  * Copyright IBM Corp. 1998 1999 All Rights Reserved
11  *
12  * US Government Users Restricted Rights - Use, duplication or
13  * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
14  */

15
16 package com.sun.corba.se.impl.encoding;
17
18 import java.util.StringTokenizer JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Arrays JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.NoSuchElementException JavaDoc;
23 import org.omg.CORBA.INITIALIZE JavaDoc;
24 import org.omg.CORBA.CompletionStatus JavaDoc;
25
26 import com.sun.corba.se.spi.logging.CORBALogDomains;
27 import com.sun.corba.se.impl.logging.ORBUtilSystemException;
28
29 public final class CodeSetComponentInfo {
30
31     /**
32      * CodeSetComponent is part of an IOR multi-component profile. Two
33      * instances constitute a CodeSetComponentInfo (one for char and one
34      * for wchar data)
35      */

36     public static final class CodeSetComponent {
37         int nativeCodeSet;
38     int[] conversionCodeSets;
39
40     public boolean equals( Object JavaDoc obj )
41     {
42         if (this == obj)
43         return true ;
44
45         if (!(obj instanceof CodeSetComponent))
46         return false ;
47
48         CodeSetComponent other = (CodeSetComponent)obj ;
49
50         return (nativeCodeSet == other.nativeCodeSet) &&
51         Arrays.equals( conversionCodeSets, other.conversionCodeSets ) ;
52     }
53
54     public int hashCode()
55     {
56         int result = nativeCodeSet ;
57         for (int ctr=0; ctr<conversionCodeSets.length; ctr++)
58         result = 37*result + conversionCodeSets[ctr] ;
59         return result ;
60     }
61     
62         public CodeSetComponent() {}
63     
64     public CodeSetComponent(int nativeCodeSet, int[] conversionCodeSets) {
65         this.nativeCodeSet = nativeCodeSet;
66         if (conversionCodeSets == null)
67         this.conversionCodeSets = new int[0];
68         else
69         this.conversionCodeSets = conversionCodeSets;
70     }
71
72     public void read(MarshalInputStream in) {
73         nativeCodeSet = in.read_ulong();
74         int len = in.read_long();
75         conversionCodeSets = new int[len];
76         in.read_ulong_array(conversionCodeSets, 0, len);
77
78     }
79     
80     public void write(MarshalOutputStream out) {
81         out.write_ulong(nativeCodeSet);
82         out.write_long(conversionCodeSets.length);
83         out.write_ulong_array(conversionCodeSets, 0, conversionCodeSets.length);
84     }
85
86         public String JavaDoc toString() {
87             StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc("CodeSetComponent(");
88
89             sbuf.append("native:");
90             sbuf.append(Integer.toHexString(nativeCodeSet));
91             sbuf.append(" conversion:");
92             if (conversionCodeSets == null)
93                 sbuf.append("null");
94             else {
95                 for (int i = 0; i < conversionCodeSets.length; i++) {
96                     sbuf.append(Integer.toHexString(conversionCodeSets[i]));
97                     sbuf.append(' ');
98                 }
99             }
100         sbuf.append( ")" ) ;
101
102             return sbuf.toString();
103         }
104     }
105
106     private CodeSetComponent forCharData;
107     private CodeSetComponent forWCharData;
108
109     public boolean equals( Object JavaDoc obj )
110     {
111     if (this == obj)
112         return true ;
113
114     if (!(obj instanceof CodeSetComponentInfo))
115         return false ;
116
117     CodeSetComponentInfo other = (CodeSetComponentInfo)obj ;
118     return forCharData.equals( other.forCharData ) &&
119         forWCharData.equals( other.forWCharData ) ;
120     }
121
122     public int hashCode()
123     {
124     return forCharData.hashCode() ^ forWCharData.hashCode() ;
125     }
126
127     public String JavaDoc toString() {
128         StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc("CodeSetComponentInfo(");
129
130         sbuf.append("char_data:");
131         sbuf.append(forCharData.toString());
132         sbuf.append(" wchar_data:");
133         sbuf.append(forWCharData.toString());
134     sbuf.append(")");
135
136         return sbuf.toString();
137     }
138
139     public CodeSetComponentInfo() {
140         forCharData = CodeSetComponentInfo.JAVASOFT_DEFAULT_CODESETS.forCharData;
141         forWCharData = CodeSetComponentInfo.JAVASOFT_DEFAULT_CODESETS.forWCharData;
142     }
143
144     public CodeSetComponentInfo(CodeSetComponent charData,
145                                 CodeSetComponent wcharData) {
146         forCharData = charData;
147         forWCharData = wcharData;
148     }
149
150     public void read(MarshalInputStream in) {
151         forCharData = new CodeSetComponent();
152     forCharData.read(in);
153         forWCharData = new CodeSetComponent();
154     forWCharData.read(in);
155     }
156
157     public void write(MarshalOutputStream out) {
158     forCharData.write(out);
159     forWCharData.write(out);
160     }
161
162     public CodeSetComponent getCharComponent() {
163         return forCharData;
164     }
165
166     public CodeSetComponent getWCharComponent() {
167         return forWCharData;
168     }
169
170     /**
171      * CodeSetContext goes in a GIOP service context
172      */

173     public static final class CodeSetContext {
174     private int char_data;
175     private int wchar_data;
176     
177         public CodeSetContext() {}
178
179     public CodeSetContext(int charEncoding, int wcharEncoding) {
180         char_data = charEncoding;
181         wchar_data = wcharEncoding;
182     }
183     
184     public void read(MarshalInputStream in) {
185         char_data = in.read_ulong();
186         wchar_data = in.read_ulong();
187     }
188     
189     public void write(MarshalOutputStream out) {
190         out.write_ulong(char_data);
191         out.write_ulong(wchar_data);
192     }
193
194         public int getCharCodeSet() {
195             return char_data;
196         }
197         
198         public int getWCharCodeSet() {
199             return wchar_data;
200         }
201
202         public String JavaDoc toString() {
203             StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
204             sbuf.append("CodeSetContext char set: ");
205             sbuf.append(Integer.toHexString(char_data));
206             sbuf.append(" wchar set: ");
207             sbuf.append(Integer.toHexString(wchar_data));
208             return sbuf.toString();
209         }
210     }
211
212     /**
213      * Our default code set scheme is as follows:
214      *
215      * char data:
216      *
217      * Native code set: ISO 8859-1 (8-bit)
218      * Conversion sets: UTF-8, ISO 646 (7-bit)
219      *
220      * wchar data:
221      *
222      * Native code set: UTF-16
223      * Conversion sets: UCS-2
224      *
225      * Pre-Merlin/J2EE 1.3 JavaSoft ORBs listed ISO646 for char and
226      * UCS-2 for wchar, and provided no conversion sets. They also
227      * didn't do correct negotiation or provide the fallback sets.
228      * UCS-2 is still in the conversion list for backwards compatibility.
229      *
230      * The fallbacks are UTF-8 for char and UTF-16 for wchar.
231      *
232      * In GIOP 1.1, interoperability with wchar is limited to 2 byte fixed
233      * width encodings since its wchars aren't preceded by a length.
234      * Thus, I've chosen not to include UTF-8 in the conversion set
235      * for wchar data.
236      *
237      */

238     public static final CodeSetComponentInfo JAVASOFT_DEFAULT_CODESETS;
239     static {
240         CodeSetComponent charData
241             = new CodeSetComponent(OSFCodeSetRegistry.ISO_8859_1.getNumber(),
242                                    new int[] {
243                                        OSFCodeSetRegistry.UTF_8.getNumber(),
244                                        OSFCodeSetRegistry.ISO_646.getNumber()
245                                    });
246
247         CodeSetComponent wcharData
248             = new CodeSetComponent(OSFCodeSetRegistry.UTF_16.getNumber(),
249                                    new int[]
250                                    {
251                                        OSFCodeSetRegistry.UCS_2.getNumber()
252                                    });
253
254         JAVASOFT_DEFAULT_CODESETS = new CodeSetComponentInfo(charData, wcharData);
255     }
256
257     /**
258      * Creates a CodeSetComponent from a String which contains a comma
259      * delimited list of OSF Code Set Registry numbers. An INITIALIZE
260      * exception is thrown if any of the numbers are not known by our
261      * registry. Used by corba.ORB init.
262      *
263      * The first number in the list is taken as the native code set,
264      * and the rest is the conversion code set list.
265      *
266      * The numbers can either be decimal or hex.
267      */

268     public static CodeSetComponent createFromString(String JavaDoc str) {
269     ORBUtilSystemException wrapper = ORBUtilSystemException.get(
270         CORBALogDomains.RPC_ENCODING ) ;
271
272         if (str == null || str.length() == 0)
273         throw wrapper.badCodeSetString() ;
274
275         StringTokenizer JavaDoc stok = new StringTokenizer JavaDoc(str, ", ", false);
276         int nativeSet = 0;
277         int conversionInts[] = null;
278
279         try {
280
281             // The first value is the native code set
282
nativeSet = Integer.decode(stok.nextToken()).intValue();
283
284             if (OSFCodeSetRegistry.lookupEntry(nativeSet) == null)
285         throw wrapper.unknownNativeCodeset( new Integer JavaDoc(nativeSet) ) ;
286
287             List JavaDoc conversionList = new ArrayList JavaDoc(10);
288
289             // Now process the other values as part of the
290
// conversion code set list.
291
while (stok.hasMoreTokens()) {
292                 
293                 // decode allows us to specify hex, decimal, etc
294
Integer JavaDoc value = Integer.decode(stok.nextToken());
295
296                 if (OSFCodeSetRegistry.lookupEntry(value.intValue()) == null)
297             throw wrapper.unknownConversionCodeSet( value ) ;
298
299                 conversionList.add(value);
300             }
301
302             conversionInts = new int[conversionList.size()];
303
304             for (int i = 0; i < conversionInts.length; i++)
305                 conversionInts[i] = ((Integer JavaDoc)conversionList.get(i)).intValue();
306
307         } catch (NumberFormatException JavaDoc nfe) {
308         throw wrapper.invalidCodeSetNumber( nfe ) ;
309         } catch (NoSuchElementException JavaDoc nsee) {
310         throw wrapper.invalidCodeSetString( nsee, str ) ;
311         }
312
313         // Otherwise return the CodeSetComponent representing
314
// the given values
315
return new CodeSetComponent(nativeSet, conversionInts);
316     }
317
318     /**
319      * Code sets for local cases without a connection.
320      */

321     public static final CodeSetContext LOCAL_CODE_SETS
322         = new CodeSetContext(OSFCodeSetRegistry.ISO_8859_1.getNumber(),
323                              OSFCodeSetRegistry.UTF_16.getNumber());
324 }
325
326
Popular Tags