1 26 27 package org.objectweb.openccm.ast.lib; 28 29 30 import org.objectweb.openccm.ast.api.ASTError; 31 32 33 import org.objectweb.openccm.corba.TheORB; 34 35 36 import org.objectweb.openccm.corba.TheDynamicAnyFactory; 37 38 39 import org.objectweb.openccm.corba.TypeCodeUtils; 40 41 42 import org.objectweb.openccm.corba.UserExceptionWrapper; 43 44 45 import org.omg.CORBA.TypeCode ; 46 47 48 import org.omg.CORBA.TCKind ; 49 50 51 import org.omg.CORBA.Any ; 52 53 54 import org.omg.DynamicAny.DynAny ; 55 56 57 import org.omg.DynamicAny.DynFixed ; 58 import org.omg.DynamicAny.DynFixedHelper ; 59 60 61 import org.omg.DynamicAny.DynEnum ; 62 import org.omg.DynamicAny.DynEnumHelper ; 63 64 76 77 public class AnyValueImpl 78 extends ObjectBase 79 implements org.objectweb.openccm.ast.api.AnyValue 80 { 81 87 88 final private int NONE = 0; 89 90 91 final private int INTEGER = 1; 92 93 94 final private int STRING = 2; 95 96 97 final private int WSTRING = 3; 98 99 100 final private int CHAR = 4; 101 102 103 final private int WCHAR = 5; 104 105 106 final private int FIXED = 6; 107 108 109 final private int FLOATING = 7; 110 111 112 final private int BOOLEAN = 8; 113 114 115 final private int ENUM = 9; 116 117 123 124 private int type_; 125 126 127 private long integer_; 128 129 130 private String string_; 131 132 133 private char char_; 134 135 136 private String fixed_; 137 138 139 private double floating_; 140 141 142 private boolean boolean_; 143 144 145 private TypeCode tc_; 146 147 148 private int enumerator_; 149 150 156 157 public 158 AnyValueImpl() 159 { 160 type_ = NONE; 162 } 163 164 170 181 protected Any 182 computeAny(TypeCode tc) 183 { 184 if(type_ == NONE) { 185 Any any = TheORB.create_any(); 186 any.insert_octet((byte)0); 187 return any; 188 } 189 190 TypeCode origTC = TypeCodeUtils.getOrigType(tc); 191 192 DynAny dynAny = TheDynamicAnyFactory. 195 create_dyn_any_from_type_code(origTC); 196 197 try { 198 switch(origTC.kind().value()) { 199 case TCKind._tk_short: 200 if(type_ != INTEGER) 201 throw new ASTError("Invalid constant value!"); 202 dynAny.insert_short((short)integer_); 203 break; 204 205 case TCKind._tk_long: 206 if(type_ != INTEGER) 207 throw new ASTError("Invalid constant value!"); 208 dynAny.insert_long((int)integer_); 209 break; 210 211 case TCKind._tk_ushort: 212 if(type_ != INTEGER) 213 throw new ASTError("Invalid constant value!"); 214 dynAny.insert_ushort((short)integer_); 215 break; 216 217 case TCKind._tk_ulong: 218 if(type_ != INTEGER) 219 throw new ASTError("Invalid constant value!"); 220 dynAny.insert_ulong((int)integer_); 221 break; 222 223 case TCKind._tk_float: 224 if(type_ != FLOATING) 225 throw new ASTError("Invalid constant value!"); 226 dynAny.insert_float((float)floating_); 227 break; 228 229 case TCKind._tk_double: 230 if(type_ != FLOATING) 231 throw new ASTError("Invalid constant value!"); 232 dynAny.insert_double(floating_); 233 break; 234 235 case TCKind._tk_boolean: 236 if(type_ != BOOLEAN) 237 throw new ASTError("Invalid constant value!"); 238 dynAny.insert_boolean(boolean_); 239 break; 240 241 case TCKind._tk_char: 242 if(type_ != CHAR) 243 throw new ASTError("Invalid constant value!"); 244 dynAny.insert_char(char_); 245 break; 246 247 case TCKind._tk_string: 248 if(type_ != STRING) 249 throw new ASTError("Invalid constant value!"); 250 dynAny.insert_string(string_); 251 break; 252 253 case TCKind._tk_longlong: 254 if(type_ != INTEGER) 255 throw new ASTError("Invalid constant value!"); 256 dynAny.insert_longlong(integer_); 257 break; 258 259 case TCKind._tk_ulonglong: 260 if(type_ != INTEGER) 261 throw new ASTError("Invalid constant value!"); 262 dynAny.insert_ulonglong(integer_); 263 break; 264 265 case TCKind._tk_wchar: 266 if(type_ != WCHAR) 267 throw new ASTError("Invalid constant value!"); 268 dynAny.insert_wchar(char_); 269 break; 270 271 case TCKind._tk_wstring: 272 if(type_ != WSTRING) 273 throw new ASTError("Invalid constant value!"); 274 dynAny.insert_wstring(string_); 275 break; 276 277 case TCKind._tk_fixed: 278 if(type_ != FIXED) 279 throw new ASTError("Invalid constant value!"); 280 DynFixedHelper.narrow(dynAny).set_value(fixed_); 281 break; 282 283 case TCKind._tk_enum: 284 if(type_ != ENUM) 285 throw new ASTError("Invalid constant value!"); 286 if(!tc_.equal(origTC)) 287 throw new ASTError("Invalid constant type!"); 288 DynEnumHelper.narrow(dynAny).set_as_ulong(enumerator_); 289 break; 290 291 default: 292 throw new ASTError("Invalid constant type!"); 293 } 294 return dynAny.to_any(); 295 } 296 catch(org.omg.DynamicAny.DynAnyPackage.InvalidValue exc) 297 { 298 throw new UserExceptionWrapper(exc); 300 } 301 catch(org.omg.DynamicAny.DynAnyPackage.TypeMismatch exc) 302 { 303 throw new UserExceptionWrapper(exc); 305 } 306 } 307 308 313 protected void 314 loadAny(Any any) 315 { 316 TypeCode tc = any.type(); 317 try 318 { 319 switch(tc.kind().value()) 320 { 321 case TCKind._tk_short: 322 setAsInteger(any.extract_short()); 323 break; 324 case TCKind._tk_long: 325 setAsInteger(any.extract_long()); 326 break; 327 case TCKind._tk_ushort: 328 setAsInteger(any.extract_ushort()); 329 break; 330 case TCKind._tk_ulong: 331 setAsInteger(any.extract_ulong()); 332 break; 333 case TCKind._tk_float: 334 setAsFloating(any.extract_float()); 335 break; 336 case TCKind._tk_double: 337 setAsFloating(any.extract_double()); 338 break; 339 case TCKind._tk_boolean: 340 setAsBoolean(any.extract_boolean()); 341 break; 342 case TCKind._tk_char: 343 setAsChar(any.extract_char()); 344 break; 345 case TCKind._tk_wchar: 346 setAsWChar(any.extract_wchar()); 347 break; 348 case TCKind._tk_string: 349 setAsString(any.extract_string()); 350 break; 351 case TCKind._tk_wstring: 352 setAsWString(any.extract_wstring()); 353 break; 354 case TCKind._tk_longlong: 355 setAsInteger(any.extract_longlong()); 356 break; 357 case TCKind._tk_ulonglong: 358 setAsInteger(any.extract_ulonglong()); 359 break; 360 case TCKind._tk_fixed: 361 DynFixed fixed = TheDynamicAnyFactory.create_dyn_fixed(any); 362 setAsFixed(fixed.get_value()); 363 break; 364 case TCKind._tk_enum: 365 DynEnum dynEnum = TheDynamicAnyFactory.create_dyn_enum(any); 366 setAsEnum(tc, dynEnum.get_as_ulong(), dynEnum.get_as_string()); 367 break; 368 default: 369 break; 370 } 371 372 } catch(org.omg.CORBA.BAD_OPERATION ex) { 373 throw new ASTError(ex.getMessage()); 375 } 376 } 377 378 384 390 395 public boolean 396 isInteger() 397 { 398 return type_ == INTEGER; 399 } 400 401 406 public void 407 setAsInteger(long value) 408 { 409 type_ = INTEGER; 410 integer_ = value; 411 } 412 413 418 public long 419 getAsInteger() 420 { 421 return integer_; 422 } 423 424 429 public boolean 430 isString() 431 { 432 return type_ == STRING; 433 } 434 435 440 public void 441 setAsString(String value) 442 { 443 type_ = STRING; 444 string_ = value; 445 } 446 447 452 public String 453 getAsString() 454 { 455 return string_; 456 } 457 458 463 public boolean 464 isWString() 465 { 466 return type_ == WSTRING; 467 } 468 469 474 public void 475 setAsWString(String value) 476 { 477 type_ = WSTRING; 478 string_ = value; 479 } 480 481 486 public String 487 getAsWString() 488 { 489 return string_; 490 } 491 492 497 public boolean 498 isChar() 499 { 500 return type_ == CHAR; 501 } 502 503 508 public void 509 setAsChar(char value) 510 { 511 type_ = CHAR; 512 char_ = value; 513 } 514 515 520 public char 521 getAsChar() 522 { 523 return char_; 524 } 525 526 531 public boolean 532 isWChar() 533 { 534 return type_ == WCHAR; 535 } 536 537 542 public void 543 setAsWChar(char value) 544 { 545 type_ = WCHAR; 546 char_ = value; 547 } 548 549 554 public char 555 getAsWChar() 556 { 557 return char_; 558 } 559 560 565 public boolean 566 isFixed() 567 { 568 return type_ == FIXED; 569 } 570 571 576 public void 577 setAsFixed(String value) 578 { 579 type_ = FIXED; 580 fixed_ = value; 581 } 582 583 588 public String 589 getAsFixed() 590 { 591 return fixed_; 592 } 593 594 599 public boolean 600 isFloating() 601 { 602 return type_ == FLOATING; 603 } 604 605 610 public void 611 setAsFloating(double value) 612 { 613 type_ = FLOATING; 614 floating_ = value; 615 } 616 617 622 public double 623 getAsFloating() 624 { 625 return floating_; 626 } 627 628 633 public boolean 634 isBoolean() 635 { 636 return type_ == BOOLEAN; 637 } 638 639 644 public void 645 setAsBoolean(boolean value) 646 { 647 type_ = BOOLEAN; 648 boolean_ = value; 649 } 650 651 656 public boolean 657 getAsBoolean() 658 { 659 return boolean_; 660 } 661 662 667 public boolean 668 isEnum() 669 { 670 return type_ == ENUM; 671 } 672 673 680 public void 681 setAsEnum(TypeCode tc, 682 int value, 683 String name) 684 { 685 type_ = ENUM; 686 tc_ = tc; 687 enumerator_ = value; 688 string_ = name; 689 } 690 691 696 public String 697 getAsEnum() 698 { 699 return string_; 700 } 701 } 702 | Popular Tags |