1 /* CodeSet_sctx.java --
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.CORBA.GIOP;
41 import gnu.CORBA.CDR.AbstractCdrInput;
42 import gnu.CORBA.CDR.AbstractCdrOutput;
44 import java.io.IOException;
47 * The code set service context. This context must be included in all
48 * messages that use wide characters.
50 * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
52 public class CodeSetServiceContext
53 extends ServiceContext
56 * The context code sets id.
58 public static final int ID = 1;
61 * The standard component to include in the messages.
63 public static final CodeSetServiceContext STANDARD = new CodeSetServiceContext();
66 * The encoding, used to transfer the narrow (1 byte) character data.
67 * The default value is taken from {@link CharSets_OSF#NATIVE_CHARACTER}.
69 public int char_data = CharSets_OSF.NATIVE_CHARACTER;
72 * The encoding, used to transfer the wide character data.
73 * The default value is taken from
74 * {@link CharSets_OSF#NATIVE_WIDE_CHARACTER}.
76 public int wide_char_data = CharSets_OSF.NATIVE_WIDE_CHARACTER;
79 * Find and return the code set service context in the give
80 * contexts array. Returns {@link #STANDARD} if no code set
83 * @param contexts the array of contexts, can be null.
85 public static CodeSetServiceContext find(ServiceContext[] contexts)
88 for (int i = 0; i < contexts.length; i++)
90 if (contexts [ i ] instanceof CodeSetServiceContext)
91 return (CodeSetServiceContext) contexts [ i ];
97 * Select the suitable encoding that is defined in the provided profile.
99 * TODO character encoding. Now the encoding can be set, but it is ignored.
100 * If you take this task, scan 'TODO character encoding' for
103 public static CodeSetServiceContext negotiate(IOR.CodeSets_profile profile)
105 if (profile.negotiated != null)
106 return profile.negotiated;
108 CodeSetServiceContext use = new CodeSetServiceContext();
111 negotiate(profile.narrow, STANDARD.char_data, CharSets_OSF.ISO8859_1);
114 negotiate(profile.wide, STANDARD.wide_char_data, CharSets_OSF.UTF16);
116 profile.negotiated = use;
122 * Read the context from the given stream. Does not read the
125 public void readContext(AbstractCdrInput input)
127 AbstractCdrInput encap = input.read_encapsulation();
129 char_data = encap.read_ulong();
130 wide_char_data = encap.read_ulong();
134 * Return a string representation.
136 public String toString()
138 return " Encoding: narrow " + name(char_data) + ", wide " +
139 name(wide_char_data) + ". ";
143 * Write the context to the given stream, including the code
146 public void write(AbstractCdrOutput output)
148 output.write_ulong(ID);
150 AbstractCdrOutput enout = output.createEncapsulation();
152 enout.write_long(char_data);
153 enout.write_ulong(wide_char_data);
159 catch (IOException ex)
161 InternalError t = new InternalError();
168 * Negotiate about the character encoding. Prefer our native encoding,
169 * if no, prefer IORs native encoding, if no, find any encoding,
170 * supported by both sides, if no, return the specified final decission.
172 * @param profile the component profile in IOR.
173 * @param our_native our native encoding
174 * @param final_decission the encoding that must be returned if no
175 * compromise is found.
177 * @return the resulted encoding.
179 protected static int negotiate(IOR.CodeSets_profile.CodeSet_component profile,
180 int our_native, int final_decission
183 // If our and IORs native sets match, use the native set.
184 if (profile.native_set == our_native)
187 // If the native sets do not match, but the IOR says it
188 // supports our native set, use our native set.
189 if (profile.conversion != null)
190 for (int i = 0; i < profile.conversion.length; i++)
192 if (our_native == profile.conversion [ i ])
196 // At this point, we suggest to use the IORs native set.
197 int[] allSupported = CharSets_OSF.getSupportedCharSets();
199 for (int s = 0; s < allSupported.length; s++)
200 if (allSupported [ s ] == profile.native_set)
201 return profile.native_set;
203 // Any compromise left?
204 if (profile.conversion != null)
205 for (int s = 0; s < allSupported.length; s++)
206 for (int i = 0; i < profile.conversion.length; i++)
207 if (allSupported [ s ] == profile.conversion [ i ])
208 return allSupported [ s ];
210 // Return the CORBA default char encoding.
211 return final_decission;
215 * Conveniency method, used in toString()
217 private String name(int set)
219 return "0x" + Integer.toHexString(set) + " (" + CharSets_OSF.getName(set) +