Remove old autovect-branch by moving to "dead" directory.
[official-gcc.git] / old-autovect-branch / libjava / classpath / gnu / CORBA / GIOP / CodeSetServiceContext.java
blobab565db3797ee715a8f9668b28d53035be5b2063
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)
9 any later version.
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
19 02110-1301 USA.
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
24 combination.
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;
43 import gnu.CORBA.IOR;
44 import gnu.CORBA.IOR.CodeSets_profile;
46 import java.io.IOException;
48 /**
49 * The code set service context. This context must be included in all
50 * messages that use wide characters.
52 * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
54 public class CodeSetServiceContext
55 extends ServiceContext
57 /**
58 * The context code sets id.
60 public static final int ID = 1;
62 /**
63 * The standard component to include in the messages.
65 public static final CodeSetServiceContext STANDARD = new CodeSetServiceContext();
67 /**
68 * The encoding, used to transfer the narrow (1 byte) character data.
69 * The default value is taken from {@link CharSets_OSF#NATIVE_CHARACTER}.
71 public int char_data = CharSets_OSF.NATIVE_CHARACTER;
73 /**
74 * The encoding, used to transfer the wide character data.
75 * The default value is taken from
76 * {@link CharSets_OSF#NATIVE_WIDE_CHARACTER}.
78 public int wide_char_data = CharSets_OSF.NATIVE_WIDE_CHARACTER;
80 /**
81 * Find and return the code set service context in the give
82 * contexts array. Returns {@link #STANDARD} if no code set
83 * context is present.
85 * @param contexts the array of contexts, can be null.
87 public static CodeSetServiceContext find(ServiceContext[] contexts)
89 if (contexts != null)
90 for (int i = 0; i < contexts.length; i++)
92 if (contexts [ i ] instanceof CodeSetServiceContext)
93 return (CodeSetServiceContext) contexts [ i ];
95 return STANDARD;
98 /**
99 * Select the suitable encoding that is defined in the provided profile.
101 * TODO character encoding. Now the encoding can be set, but it is ignored.
102 * If you take this task, scan 'TODO character encoding' for
103 * relevant places.
105 public static CodeSetServiceContext negotiate(IOR.CodeSets_profile profile)
107 if (profile.negotiated != null)
108 return profile.negotiated;
110 CodeSetServiceContext use = new CodeSetServiceContext();
112 use.char_data =
113 negotiate(profile.narrow, STANDARD.char_data, CharSets_OSF.ISO8859_1);
115 use.wide_char_data =
116 negotiate(profile.wide, STANDARD.wide_char_data, CharSets_OSF.UTF16);
118 profile.negotiated = use;
120 return use;
124 * Read the context from the given stream. Does not read the
125 * code sets id.
127 public void readContext(AbstractCdrInput input)
129 AbstractCdrInput encap = input.read_encapsulation();
131 char_data = encap.read_ulong();
132 wide_char_data = encap.read_ulong();
136 * Return a string representation.
138 public String toString()
140 return " Encoding: narrow " + name(char_data) + ", wide " +
141 name(wide_char_data) + ". ";
145 * Write the context to the given stream, including the code
146 * sets id.
148 public void write(AbstractCdrOutput output)
150 output.write_ulong(ID);
152 AbstractCdrOutput enout = output.createEncapsulation();
154 enout.write_long(char_data);
155 enout.write_ulong(wide_char_data);
159 enout.close();
161 catch (IOException ex)
163 InternalError t = new InternalError();
164 t.initCause(ex);
165 throw t;
170 * Negotiate about the character encoding. Prefer our native encoding,
171 * if no, prefer IORs native encoding, if no, find any encoding,
172 * supported by both sides, if no, return the specified final decission.
174 * @param profile the component profile in IOR.
175 * @param our_native our native encoding
176 * @param final_decission the encoding that must be returned if no
177 * compromise is found.
179 * @return the resulted encoding.
181 protected static int negotiate(IOR.CodeSets_profile.CodeSet_component profile,
182 int our_native, int final_decission
185 // If our and IORs native sets match, use the native set.
186 if (profile.native_set == our_native)
187 return our_native;
189 // If the native sets do not match, but the IOR says it
190 // supports our native set, use our native set.
191 if (profile.conversion != null)
192 for (int i = 0; i < profile.conversion.length; i++)
194 if (our_native == profile.conversion [ i ])
195 return our_native;
198 // At this point, we suggest to use the IORs native set.
199 int[] allSupported = CharSets_OSF.getSupportedCharSets();
201 for (int s = 0; s < allSupported.length; s++)
202 if (allSupported [ s ] == profile.native_set)
203 return profile.native_set;
205 // Any compromise left?
206 if (profile.conversion != null)
207 for (int s = 0; s < allSupported.length; s++)
208 for (int i = 0; i < profile.conversion.length; i++)
209 if (allSupported [ s ] == profile.conversion [ i ])
210 return allSupported [ s ];
212 // Return the CORBA default char encoding.
213 return final_decission;
217 * Conveniency method, used in toString()
219 private String name(int set)
221 return "0x" + Integer.toHexString(set) + " (" + CharSets_OSF.getName(set) +
222 ") ";