1 package org.jacorb.orb.dynany; 2 3 22 23 import org.omg.DynamicAny.DynAnyPackage.*; 24 import org.omg.DynamicAny.*; 25 import org.jacorb.orb.*; 26 27 33 34 public final class DynStruct 35 extends DynAny 36 implements org.omg.DynamicAny.DynStruct 37 { 38 private org.omg.DynamicAny.NameValuePair [] members; 39 40 41 private String exception_msg; 42 43 DynStruct( org.omg.DynamicAny.DynAnyFactory dynFactory, 44 org.omg.CORBA.TypeCode tc) 45 throws InvalidValue, TypeMismatch 46 { 47 org.omg.CORBA.TypeCode _type = TypeCode.originalType( tc ); 48 49 if( _type.kind().value() != org.omg.CORBA.TCKind._tk_except && 50 _type.kind().value() != org.omg.CORBA.TCKind._tk_struct ) 51 throw new TypeMismatch(); 52 53 this.orb = org.omg.CORBA.ORB.init(); 54 this.dynFactory = dynFactory; 55 56 type = _type; 57 58 try 59 { 60 61 if( !isEmptyEx () ) 62 { 63 pos = 0; 64 } 65 if( _type.kind().value() == org.omg.CORBA.TCKind._tk_except ) 66 exception_msg = type.id(); 67 68 limit = type.member_count(); 69 members = new NameValuePair[limit]; 70 for( int i = 0 ; i < limit; i++ ) 71 { 72 org.omg.CORBA.TypeCode _tc = 73 TypeCode.originalType( type.member_type(i) ); 74 members[i] = 75 new NameValuePair( 76 type.member_name(i), 77 dynFactory.create_dyn_any_from_type_code( _tc ).to_any()); 78 79 } 80 } 81 catch( org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode itc ) 82 { 83 itc.printStackTrace(); 84 } 85 catch( org.omg.CORBA.TypeCodePackage.BadKind bk ) 86 { 87 bk.printStackTrace(); 88 } 89 catch( org.omg.CORBA.TypeCodePackage.Bounds b ) 90 { 91 b.printStackTrace(); 92 } 93 } 94 DynStruct( org.omg.DynamicAny.DynAnyFactory dynFactory, 95 org.omg.CORBA.TypeCode tc,org.omg.CORBA.ORB orb) 96 throws InvalidValue, TypeMismatch 97 { 98 org.omg.CORBA.TypeCode _type = TypeCode.originalType( tc ); 99 100 if( _type.kind().value() != org.omg.CORBA.TCKind._tk_except && 101 _type.kind().value() != org.omg.CORBA.TCKind._tk_struct ) 102 throw new TypeMismatch(); 103 104 this.orb = orb; 105 this.dynFactory = dynFactory; 106 107 type = _type; 108 109 try 110 { 111 112 if( !isEmptyEx () ) 113 { 114 pos = 0; 115 } 116 if( _type.kind().value() == org.omg.CORBA.TCKind._tk_except ) 117 exception_msg = type.id(); 118 119 limit = type.member_count(); 120 members = new NameValuePair[limit]; 121 for( int i = 0 ; i < limit; i++ ) 122 { 123 org.omg.CORBA.TypeCode _tc = 124 TypeCode.originalType( type.member_type(i) ); 125 members[i] = 126 new NameValuePair( 127 type.member_name(i), 128 dynFactory.create_dyn_any_from_type_code( _tc ).to_any()); 129 130 } 131 } 132 catch( org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode itc ) 133 { 134 itc.printStackTrace(); 135 } 136 catch( org.omg.CORBA.TypeCodePackage.BadKind bk ) 137 { 138 bk.printStackTrace(); 139 } 140 catch( org.omg.CORBA.TypeCodePackage.Bounds b ) 141 { 142 b.printStackTrace(); 143 } 144 } 145 146 public void from_any(org.omg.CORBA.Any value) 147 throws InvalidValue, TypeMismatch 148 { 149 checkDestroyed (); 150 151 if( !value.type().equivalent( type() )) 152 throw new org.omg.DynamicAny.DynAnyPackage.TypeMismatch (); 153 154 type = TypeCode.originalType( value.type() ); 155 156 try 157 { 158 limit = type().member_count(); 159 members = new NameValuePair[limit]; 160 org.omg.CORBA.portable.InputStream is = 161 value.create_input_stream(); 162 163 if( type.kind().value() == org.omg.CORBA.TCKind._tk_except ) 164 { 165 exception_msg = is.read_string(); 166 } 167 168 for( int i = 0 ; i < limit; i++ ) 169 { 170 try 171 { 172 Any a = (org.jacorb.orb.Any)orb.create_any(); 173 a.read_value(is, 174 TypeCode.originalType(type.member_type(i))); 175 176 members[i] = new NameValuePair( type().member_name(i), a); 177 } 178 catch( org.omg.CORBA.TypeCodePackage.Bounds b ) 179 { 180 b.printStackTrace(); 181 } 182 } 183 } 184 catch( org.omg.CORBA.TypeCodePackage.BadKind bk ) 185 { 186 bk.printStackTrace(); 188 } 189 190 super.from_any( value ); 191 } 192 193 194 public org.omg.CORBA.Any to_any() 195 { 196 checkDestroyed (); 197 org.jacorb.orb.Any out_any = 198 (org.jacorb.orb.Any)orb.create_any(); 199 out_any.type( type()); 200 201 CDROutputStream os = new CDROutputStream(); 202 203 if( type().kind().value() == org.omg.CORBA.TCKind._tk_except ) 204 { 205 os.write_string( exception_msg ); 206 } 207 208 for( int i = 0; i < members.length; i++) 209 { 210 os.write_value( members[i].value.type(), 211 (CDRInputStream)members[i].value.create_input_stream()); 212 } 213 214 CDRInputStream is = new CDRInputStream(orb, os.getBufferCopy()); 215 out_any.read_value( is, type()); 216 return out_any; 217 } 218 219 222 223 public boolean equal( org.omg.DynamicAny.DynAny dyn_any ) 224 { 225 checkDestroyed (); 226 if( !type().equal( dyn_any.type()) ) 227 return false; 228 229 org.omg.DynamicAny.DynStruct other = DynStructHelper.narrow( dyn_any ); 230 231 NameValuePair[] elements = get_members(); 232 NameValuePair[] other_elements = other.get_members(); 233 234 for( int i = 0; i < elements.length; i++ ) 235 { 236 if( !(elements[i].value.equal( other_elements[i].value ))) 237 return false; 238 } 239 240 return true; 241 } 242 243 244 245 246 public java.lang.String current_member_name() 247 throws TypeMismatch, InvalidValue 248 { 249 checkDestroyed (); 250 251 if (isEmptyEx ()) 252 { 253 throw new TypeMismatch (); 254 } 255 if (pos == -1) 256 { 257 throw new InvalidValue (); 258 } 259 return members[pos].id; 260 } 261 262 263 public org.omg.CORBA.TCKind current_member_kind() 264 throws TypeMismatch, InvalidValue 265 { 266 checkDestroyed (); 267 268 if (isEmptyEx ()) 269 { 270 throw new TypeMismatch (); 271 } 272 if (pos == -1) 273 { 274 throw new InvalidValue (); 275 } 276 return members[pos].value.type().kind(); 277 } 278 279 280 public NameValuePair[] get_members() 281 { 282 checkDestroyed (); 283 return members; 284 } 285 286 287 public void set_members( NameValuePair[] nvp ) 288 throws InvalidValue, TypeMismatch 289 { 290 checkDestroyed (); 291 if( nvp.length != limit ) 292 throw new org.omg.DynamicAny.DynAnyPackage.InvalidValue (); 293 294 for( int i = 0; i < limit; i++ ) 295 { 296 if( ! nvp[i].value.type().equivalent( members[i].value.type() )) 297 { 298 throw new org.omg.DynamicAny.DynAnyPackage.TypeMismatch (); 299 } 300 301 if(! (nvp[i].id.equals("") || nvp[i].id.equals( members[i].id ))) 302 throw new org.omg.DynamicAny.DynAnyPackage.TypeMismatch (); 303 } 304 members = nvp; 305 } 306 307 308 public org.omg.DynamicAny.NameDynAnyPair [] get_members_as_dyn_any() 309 { 310 checkDestroyed (); 311 NameDynAnyPair[] result = new NameDynAnyPair[limit]; 312 try 313 { 314 for( int i = 0; i < limit; i++ ) 315 { 316 result[i] = new NameDynAnyPair( members[i].id, 317 dynFactory.create_dyn_any( members[i].value )); 318 } 319 } 320 catch( org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode itc ) 321 { 322 itc.printStackTrace(); 323 } 324 return result ; 325 } 326 327 328 public void set_members_as_dyn_any(org.omg.DynamicAny.NameDynAnyPair [] nvp) 329 throws TypeMismatch, InvalidValue 330 { 331 checkDestroyed (); 332 if( nvp.length != limit ) 333 throw new org.omg.DynamicAny.DynAnyPackage.InvalidValue (); 334 335 for( int i = 0; i < limit; i++ ) 336 { 337 if(! nvp[i].value.type().equivalent( members[i].value.type() )) 338 throw new org.omg.DynamicAny.DynAnyPackage.TypeMismatch (); 339 340 if(! (nvp[i].id.equals("") || nvp[i].id.equals( members[i].id ))) 341 throw new org.omg.DynamicAny.DynAnyPackage.TypeMismatch (); 342 343 } 344 members = new NameValuePair[nvp.length]; 345 for( int i = 0; i < limit; i++ ) 346 { 347 members[i] = new NameValuePair( nvp[i].id, nvp[i].value.to_any() ); 348 } 349 } 350 351 public void destroy() 352 { 353 super.destroy(); 354 members = null; 355 } 356 357 362 363 protected org.omg.CORBA.Any getRepresentation() 364 { 365 return members[pos].value; 366 } 367 368 369 370 public org.omg.DynamicAny.DynAny current_component() 371 throws TypeMismatch 372 { 373 checkDestroyed (); 374 try 375 { 376 377 if( isEmptyEx () ) 378 { 379 throw new TypeMismatch (); 380 } 381 382 if( pos == -1 ) 383 { 384 return null; 385 } 386 return dynFactory.create_dyn_any( members[pos].value ); 387 } 388 catch( org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode itc ) 389 { 390 itc.printStackTrace(); 391 } 392 return null; 393 } 394 395 private boolean isEmptyEx () 396 { 397 try 398 { 399 return (type.kind().value() == org.omg.CORBA.TCKind._tk_except && 400 type.member_count() == 0); 401 } 402 catch( org.omg.CORBA.TypeCodePackage.BadKind bk ) 403 { 404 bk.printStackTrace(); 405 } 406 return false; 407 } 408 409 } 410 | Popular Tags |