Source for gnu.CORBA.GIOP.CodeSetServiceContext

   1: /* CodeSet_sctx.java --
   2:    Copyright (C) 2005 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   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.
  10: 
  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.
  15: 
  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.
  20: 
  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.
  25: 
  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. */
  37: 
  38: 
  39: package gnu.CORBA.GIOP;
  40: 
  41: import gnu.CORBA.CDR.AbstractCdrInput;
  42: import gnu.CORBA.CDR.AbstractCdrOutput;
  43: import gnu.CORBA.IOR;
  44: import java.io.IOException;
  45: 
  46: /**
  47:  * The code set service context. This context must be included in all
  48:  * messages that use wide characters.
  49:  *
  50:  * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
  51:  */
  52: public class CodeSetServiceContext
  53:   extends ServiceContext
  54: {
  55:   /**
  56:    * The context code sets id.
  57:    */
  58:   public static final int ID = 1;
  59: 
  60:   /**
  61:    * The standard component to include in the messages.
  62:    */
  63:   public static final CodeSetServiceContext STANDARD = new CodeSetServiceContext();
  64: 
  65:   /**
  66:    * The encoding, used to transfer the narrow (1 byte) character data.
  67:    * The default value is taken from {@link CharSets_OSF#NATIVE_CHARACTER}.
  68:    */
  69:   public int char_data = CharSets_OSF.NATIVE_CHARACTER;
  70: 
  71:   /**
  72:    * The encoding, used to transfer the wide character data.
  73:    * The default value is taken from
  74:    * {@link CharSets_OSF#NATIVE_WIDE_CHARACTER}.
  75:    */
  76:   public int wide_char_data = CharSets_OSF.NATIVE_WIDE_CHARACTER;
  77: 
  78:   /**
  79:    * Find and return the code set service context in the give
  80:    * contexts array. Returns {@link #STANDARD} if no code set
  81:    * context is present.
  82:    *
  83:    * @param contexts the array of contexts, can be null.
  84:    */
  85:   public static CodeSetServiceContext find(ServiceContext[] contexts)
  86:   {
  87:     if (contexts != null)
  88:       for (int i = 0; i < contexts.length; i++)
  89:         {
  90:           if (contexts [ i ] instanceof CodeSetServiceContext)
  91:             return (CodeSetServiceContext) contexts [ i ];
  92:         }
  93:     return STANDARD;
  94:   }
  95: 
  96:   /**
  97:    * Select the suitable encoding that is defined in the provided profile.
  98:    *
  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
 101:    * relevant places.
 102:    */
 103:   public static CodeSetServiceContext negotiate(IOR.CodeSets_profile profile)
 104:   {
 105:     if (profile.negotiated != null)
 106:       return profile.negotiated;
 107: 
 108:     CodeSetServiceContext use = new CodeSetServiceContext();
 109: 
 110:     use.char_data =
 111:       negotiate(profile.narrow, STANDARD.char_data, CharSets_OSF.ISO8859_1);
 112: 
 113:     use.wide_char_data =
 114:       negotiate(profile.wide, STANDARD.wide_char_data, CharSets_OSF.UTF16);
 115: 
 116:     profile.negotiated = use;
 117: 
 118:     return use;
 119:   }
 120: 
 121:   /**
 122:    * Read the context from the given stream. Does not read the
 123:    * code sets id.
 124:    */
 125:   public void readContext(AbstractCdrInput input)
 126:   {
 127:     AbstractCdrInput encap = input.read_encapsulation();
 128: 
 129:     char_data = encap.read_ulong();
 130:     wide_char_data = encap.read_ulong();
 131:   }
 132: 
 133:   /**
 134:    * Return a string representation.
 135:    */
 136:   public String toString()
 137:   {
 138:     return " Encoding: narrow " + name(char_data) + ", wide " +
 139:            name(wide_char_data) + ". ";
 140:   }
 141: 
 142:   /**
 143:    * Write the context to the given stream, including the code
 144:    * sets id.
 145:    */
 146:   public void write(AbstractCdrOutput output)
 147:   {
 148:     output.write_ulong(ID);
 149: 
 150:     AbstractCdrOutput enout = output.createEncapsulation();
 151: 
 152:     enout.write_long(char_data);
 153:     enout.write_ulong(wide_char_data);
 154: 
 155:     try
 156:       {
 157:         enout.close();
 158:       }
 159:     catch (IOException ex)
 160:       {
 161:         InternalError t = new InternalError();
 162:         t.initCause(ex);
 163:         throw t;
 164:       }
 165:   }
 166: 
 167:   /**
 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.
 171:    *
 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.
 176:    *
 177:    * @return the resulted encoding.
 178:    */
 179:   protected static int negotiate(IOR.CodeSets_profile.CodeSet_component profile,
 180:                                  int our_native, int final_decission
 181:                                 )
 182:   {
 183:     // If our and IORs native sets match, use the native set.
 184:     if (profile.native_set == our_native)
 185:       return our_native;
 186: 
 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++)
 191:         {
 192:           if (our_native == profile.conversion [ i ])
 193:             return our_native;
 194:         }
 195: 
 196:     // At this point, we suggest to use the IORs native set.
 197:     int[] allSupported = CharSets_OSF.getSupportedCharSets();
 198: 
 199:     for (int s = 0; s < allSupported.length; s++)
 200:       if (allSupported [ s ] == profile.native_set)
 201:         return profile.native_set;
 202: 
 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 ];
 209: 
 210:     // Return the CORBA default char encoding.
 211:     return final_decission;
 212:   }
 213: 
 214:   /**
 215:    * Conveniency method, used in toString()
 216:    */
 217:   private String name(int set)
 218:   {
 219:     return "0x" + Integer.toHexString(set) + " (" + CharSets_OSF.getName(set) +
 220:            ") ";
 221:   }
 222: }