1 7 8 package com.sun.corba.se.impl.dynamicany; 9 10 import org.omg.CORBA.Any ; 11 import org.omg.CORBA.TypeCode ; 12 import org.omg.CORBA.TCKind ; 13 import org.omg.CORBA.portable.OutputStream ; 14 import org.omg.CORBA.TypeCodePackage.BadKind ; 16 import org.omg.CORBA.TypeCodePackage.Bounds ; 17 import org.omg.CORBA.portable.InputStream ; 18 import org.omg.DynamicAny.*; 19 import org.omg.DynamicAny.DynAnyPackage.TypeMismatch ; 20 import org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode ; 21 import java.math.BigDecimal ; 22 import com.sun.corba.se.impl.corba.AnyImpl; 23 24 import com.sun.corba.se.spi.orb.ORB ; 25 import com.sun.corba.se.spi.logging.CORBALogDomains ; 26 import com.sun.corba.se.impl.logging.ORBUtilSystemException ; 27 28 public class DynAnyUtil 29 { 30 static boolean isConsistentType(TypeCode typeCode) { 31 int kind = typeCode.kind().value(); 32 return (kind != TCKind._tk_Principal && 33 kind != TCKind._tk_native && 34 kind != TCKind._tk_abstract_interface); 35 } 36 37 static boolean isConstructedDynAny(DynAny dynAny) { 38 int kind = dynAny.type().kind().value(); 41 return (kind == TCKind._tk_sequence || 42 kind == TCKind._tk_struct || 43 kind == TCKind._tk_array || 44 kind == TCKind._tk_union || 45 kind == TCKind._tk_enum || 46 kind == TCKind._tk_fixed || 47 kind == TCKind._tk_value || 48 kind == TCKind._tk_value_box); 49 } 50 51 static DynAny createMostDerivedDynAny(Any any, ORB orb, boolean copyValue) 52 throws org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode 53 { 54 if (any == null || ! DynAnyUtil.isConsistentType(any.type())) 55 throw new org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode (); 56 57 switch (any.type().kind().value()) { 58 case TCKind._tk_sequence: 59 return new DynSequenceImpl(orb, any, copyValue); 60 case TCKind._tk_struct: 61 return new DynStructImpl(orb, any, copyValue); 62 case TCKind._tk_array: 63 return new DynArrayImpl(orb, any, copyValue); 64 case TCKind._tk_union: 65 return new DynUnionImpl(orb, any, copyValue); 66 case TCKind._tk_enum: 67 return new DynEnumImpl(orb, any, copyValue); 68 case TCKind._tk_fixed: 69 return new DynFixedImpl(orb, any, copyValue); 70 case TCKind._tk_value: 71 return new DynValueImpl(orb, any, copyValue); 72 case TCKind._tk_value_box: 73 return new DynValueBoxImpl(orb, any, copyValue); 74 default: 75 return new DynAnyBasicImpl(orb, any, copyValue); 76 } 77 } 78 79 static DynAny createMostDerivedDynAny(TypeCode typeCode, ORB orb) 80 throws org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode 81 { 82 if (typeCode == null || ! DynAnyUtil.isConsistentType(typeCode)) 83 throw new org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode (); 84 85 switch (typeCode.kind().value()) { 86 case TCKind._tk_sequence: 87 return new DynSequenceImpl(orb, typeCode); 88 case TCKind._tk_struct: 89 return new DynStructImpl(orb, typeCode); 90 case TCKind._tk_array: 91 return new DynArrayImpl(orb, typeCode); 92 case TCKind._tk_union: 93 return new DynUnionImpl(orb, typeCode); 94 case TCKind._tk_enum: 95 return new DynEnumImpl(orb, typeCode); 96 case TCKind._tk_fixed: 97 return new DynFixedImpl(orb, typeCode); 98 case TCKind._tk_value: 99 return new DynValueImpl(orb, typeCode); 100 case TCKind._tk_value_box: 101 return new DynValueBoxImpl(orb, typeCode); 102 default: 103 return new DynAnyBasicImpl(orb, typeCode); 104 } 105 } 106 107 116 117 static Any extractAnyFromStream(TypeCode memberType, InputStream input, ORB orb) { 121 return AnyImpl.extractAnyFromStream(memberType, input, orb); 122 } 123 124 static Any createDefaultAnyOfType(TypeCode typeCode, ORB orb) { 126 ORBUtilSystemException wrapper = ORBUtilSystemException.get( orb, 127 CORBALogDomains.RPC_PRESENTATION ) ; 128 129 Any returnValue = orb.create_any(); 130 switch (typeCode.kind().value()) { 140 case TCKind._tk_boolean: 141 returnValue.insert_boolean(false); 143 break; 144 case TCKind._tk_short: 145 returnValue.insert_short((short)0); 147 break; 148 case TCKind._tk_ushort: 149 returnValue.insert_ushort((short)0); 151 break; 152 case TCKind._tk_long: 153 returnValue.insert_long(0); 155 break; 156 case TCKind._tk_ulong: 157 returnValue.insert_ulong(0); 159 break; 160 case TCKind._tk_longlong: 161 returnValue.insert_longlong((long)0); 163 break; 164 case TCKind._tk_ulonglong: 165 returnValue.insert_ulonglong((long)0); 167 break; 168 case TCKind._tk_float: 169 returnValue.insert_float((float)0.0); 171 break; 172 case TCKind._tk_double: 173 returnValue.insert_double((double)0.0); 175 break; 176 case TCKind._tk_octet: 177 returnValue.insert_octet((byte)0); 179 break; 180 case TCKind._tk_char: 181 returnValue.insert_char((char)0); 183 break; 184 case TCKind._tk_wchar: 185 returnValue.insert_wchar((char)0); 187 break; 188 case TCKind._tk_string: 189 returnValue.type(typeCode); 192 returnValue.insert_string(""); 194 break; 195 case TCKind._tk_wstring: 196 returnValue.type(typeCode); 199 returnValue.insert_wstring(""); 201 break; 202 case TCKind._tk_objref: 203 returnValue.insert_Object(null); 205 break; 206 case TCKind._tk_TypeCode: 207 returnValue.insert_TypeCode(returnValue.type()); 210 break; 211 case TCKind._tk_any: 212 returnValue.insert_any(orb.create_any()); 217 break; 218 case TCKind._tk_struct: 219 case TCKind._tk_union: 220 case TCKind._tk_enum: 221 case TCKind._tk_sequence: 222 case TCKind._tk_array: 223 case TCKind._tk_except: 224 case TCKind._tk_value: 225 case TCKind._tk_value_box: 226 returnValue.type(typeCode); 232 break; 233 case TCKind._tk_fixed: 234 returnValue.insert_fixed(new BigDecimal ("0.0"), typeCode); 235 break; 236 case TCKind._tk_native: 237 case TCKind._tk_alias: 238 case TCKind._tk_void: 239 case TCKind._tk_Principal: 240 case TCKind._tk_abstract_interface: 241 returnValue.type(typeCode); 242 break; 243 case TCKind._tk_null: 244 break; 246 case TCKind._tk_longdouble: 247 throw wrapper.tkLongDoubleNotSupported() ; 249 default: 250 throw wrapper.typecodeNotSupported() ; 251 } 252 return returnValue; 253 } 254 262 static Any copy(Any inAny, ORB orb) { 263 return new AnyImpl(orb, inAny); 264 } 265 266 277 278 static DynAny convertToNative(DynAny dynAny, ORB orb) { 279 if (dynAny instanceof DynAnyImpl) { 280 return dynAny; 281 } else { 282 try { 285 return createMostDerivedDynAny(dynAny.to_any(), orb, true); 286 } catch (InconsistentTypeCode ictc) { 287 return null; 288 } 289 } 290 } 291 292 static boolean isInitialized(Any any) { 293 boolean isInitialized = ((AnyImpl)any).isInitialized(); 297 switch (any.type().kind().value()) { 298 case TCKind._tk_string: 299 return (isInitialized && (any.extract_string() != null)); 300 case TCKind._tk_wstring: 301 return (isInitialized && (any.extract_wstring() != null)); 302 } 303 return isInitialized; 304 } 305 306 static boolean set_current_component(DynAny dynAny, DynAny currentComponent) { 309 if (currentComponent != null) { 310 try { 311 dynAny.rewind(); 312 do { 313 if (dynAny.current_component() == currentComponent) 314 return true; 315 } while (dynAny.next()); 316 } catch (TypeMismatch tm) { } 317 } 318 return false; 319 } 320 } 321 | Popular Tags |