KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > orb > giop > CodeSet


1 /*
2  * JacORB - a free Java ORB
3  *
4  * Copyright (C) 1997-2004 Gerald Brose.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */

20
21 package org.jacorb.orb.giop;
22
23 import org.omg.CONV_FRAME.*;
24 import org.omg.IOP.*;
25
26 import org.jacorb.orb.CDRInputStream;
27 import org.jacorb.orb.CDROutputStream;
28
29 public class CodeSet
30 {
31     public static final int ISO8859_1=0x00010001; /* standard ASCII */
32     public static final int UTF16= 0x00010109; /* extended UCS2,
33                                                        2 or 4 bytes
34                                                        for every char */

35     public static final int UTF8 = 0x05010001; /* 1-6 bytes for
36                                                        every character */

37
38     public static String JavaDoc csName(int cs)
39     {
40         switch(cs)
41         {
42             case ISO8859_1: return "ISO-8859-1";
43             case UTF16: return "UTF-16";
44             case UTF8: return "UTF-8";
45         }
46         return "Unknown TCS: " + Integer.toHexString(cs);
47     }
48
49     public static int getTCSDefault()
50     {
51         return ISO8859_1;
52     }
53
54     public static int getTCSWDefault()
55     {
56         return UTF16;
57     }
58
59     public static int getConversionDefault()
60     {
61         return UTF8;
62     }
63
64     /**
65      * This method compares the codesets in the component with our
66      * native codeset.
67      */

68     public static int selectTCS( CodeSetComponentInfo cs_info )
69     {
70         int with_native = selectCodeSet( cs_info.ForCharData,
71                                          getTCSDefault() );
72
73         if( with_native == -1 )
74         {
75             //no match with native codeset, so try with conversion
76
//codeset
77

78             return selectCodeSet( cs_info.ForCharData, getConversionDefault() );
79         }
80         else
81         {
82             return with_native;
83         }
84     }
85
86     /**
87      * This method compares the wide codesets in the component with our
88      * native wide codeset.
89      */

90     public static int selectTCSW( CodeSetComponentInfo cs_info )
91     {
92         int with_native = selectCodeSet( cs_info.ForWcharData,
93                                          getTCSWDefault() );
94
95         if( with_native == -1 )
96         {
97             //no match with native codeset, so try with conversion
98
//codeset
99

100             return selectCodeSet( cs_info.ForWcharData,
101                                   getConversionDefault() );
102         }
103         else
104         {
105             return with_native;
106         }
107     }
108
109     private static int selectCodeSet( CodeSetComponent cs_component,
110                                       int native_cs )
111     {
112         // check if we support server's native sets
113
if( cs_component.native_code_set == native_cs )
114         {
115             return native_cs;
116         }
117
118         // is our native CS supported at server ?
119
for( int i = 0; i < cs_component.conversion_code_sets.length; i++ )
120         {
121             if( cs_component.conversion_code_sets[i] == native_cs )
122             {
123                 return native_cs;
124             }
125         }
126
127         // can't find supported set ..
128
return -1;
129     }
130
131     public static ServiceContext createCodesetContext( int tcs, int tcsw )
132     {
133         // encapsulate context
134
CDROutputStream os = new CDROutputStream();
135         os.beginEncapsulatedArray();
136         CodeSetContextHelper.write( os, new CodeSetContext( tcs, tcsw ));
137
138         return new ServiceContext( TAG_CODE_SETS.value,
139                                    os.getBufferCopy() );
140     }
141
142     public static CodeSetContext getCodeSetContext( ServiceContext[] contexts )
143     {
144         for( int i = 0; i < contexts.length; i++ )
145         {
146             if( contexts[i].context_id == TAG_CODE_SETS.value )
147             {
148                 // TAG_CODE_SETS found, demarshall
149
CDRInputStream is =
150                 new CDRInputStream( (org.omg.CORBA.ORB JavaDoc) null,
151                                     contexts[i].context_data );
152                 is.openEncapsulatedArray();
153
154                 return CodeSetContextHelper.read( is );
155             }
156         }
157
158         return null;
159     }
160 }
161
Popular Tags