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 import java.math.BigDecimal ; 28 29 38 39 public final class DynFixed 40 extends DynAny 41 implements org.omg.DynamicAny.DynFixed 42 { 43 44 private org.omg.CORBA.Any anyRepresentation = null; 45 46 47 DynFixed( org.omg.DynamicAny.DynAnyFactory dynFactory, 48 org.omg.CORBA.TypeCode tc) 49 throws TypeMismatch 50 { 51 org.omg.CORBA.TypeCode _type = TypeCode.originalType( tc ); 52 53 if( _type.kind().value() != org.omg.CORBA.TCKind._tk_fixed ) 54 throw new TypeMismatch(); 55 56 type = _type; 57 this.orb = org.omg.CORBA.ORB.init(); 58 this.dynFactory = dynFactory; 59 pos = -1; 60 61 anyRepresentation = orb.create_any (); 62 anyRepresentation.insert_fixed (new BigDecimal ("0"), tc); 63 } 64 65 68 69 public void from_any(org.omg.CORBA.Any value) 70 throws InvalidValue, TypeMismatch 71 { 72 checkDestroyed (); 73 if( ! value.type().equivalent( type()) ) 74 throw new TypeMismatch(); 75 76 type = TypeCode.originalType( value.type() ); 77 78 try 79 { 80 anyRepresentation = (org.jacorb.orb.Any)orb.create_any(); 81 anyRepresentation.read_value( value.create_input_stream(), type()); 82 } 83 catch( Exception e) 84 { 85 e.printStackTrace(); 86 throw new InvalidValue(); 87 } 88 } 89 90 public String get_value() 91 { 92 return anyRepresentation.extract_fixed().toString(); 93 } 94 95 public boolean set_value( String val ) 96 throws TypeMismatch, InvalidValue 97 { 98 if ( val == null ) 99 { 100 throw new TypeMismatch(); 101 } 102 103 val = val.trim(); 104 if ( val.endsWith ("D") || val.endsWith ("d") ) 105 { 106 val = val.substring( 0, val.length() - 1 ); 107 } 108 109 BigDecimal fixed_value = null; 110 try 111 { 112 fixed_value = new BigDecimal ( val ); 113 } 114 catch ( NumberFormatException ex ) 115 { 116 throw new TypeMismatch(); 117 } 118 119 boolean truncate = false; 120 try 121 { 122 int extra = fixed_value.scale() - type().fixed_scale(); 123 if ( extra > 0 ) 124 { 125 val = val.substring( 0, val.length() - extra ); 127 truncate = true; 128 } 129 else if ( extra < 0 ) 130 { 131 StringBuffer sb = new StringBuffer (val); 132 133 if ( val.indexOf('.') == -1 ) 135 { 136 sb.append("."); 137 } 138 139 for ( int i = extra; i < 0; i++ ) 141 { 142 sb.append("0"); 143 } 144 val = sb.toString(); 145 } 146 fixed_value = new BigDecimal ( val ); 147 148 org.omg.CORBA.FixedHolder holder = 149 new org.omg.CORBA.FixedHolder ( fixed_value ); 150 org.omg.CORBA.TypeCode tc = holder._type(); 151 152 if ( tc.fixed_digits() > type().fixed_digits() ) 153 { 154 throw new InvalidValue(); 155 } 156 anyRepresentation.insert_fixed( fixed_value, tc ); 157 } 158 catch ( org.omg.CORBA.TypeCodePackage.BadKind bk ) 159 { 160 bk.printStackTrace(); 161 } 163 return( ! truncate ); 164 } 165 166 protected org.omg.CORBA.Any getRepresentation() 167 { 168 return anyRepresentation; 169 } 170 171 } 172 | Popular Tags |