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