KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > orb > dynany > DynFixed


1 package org.jacorb.orb.dynany;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1997-2004 Gerald Brose.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 import org.omg.DynamicAny.DynAnyPackage.*;
24 import org.omg.DynamicAny.*;
25 import org.jacorb.orb.*;
26
27 import java.math.BigDecimal JavaDoc;
28
29 /**
30  * CORBA DynFixed
31  *
32  * Written by Jason Courage
33  *
34  * @author Jason Courage, PrismTech Ltd, March 2002
35  * $Id: DynFixed.java,v 1.7 2004/05/06 12:40:00 nicolas Exp $
36  *
37  */

38
39 public final class DynFixed
40    extends DynAny
41    implements org.omg.DynamicAny.DynFixed JavaDoc
42 {
43    /* our representation of a fixed type any is the any itself */
44    private org.omg.CORBA.Any JavaDoc anyRepresentation = null;
45
46
47    DynFixed( org.omg.DynamicAny.DynAnyFactory JavaDoc dynFactory,
48              org.omg.CORBA.TypeCode JavaDoc tc)
49       throws TypeMismatch
50    {
51       org.omg.CORBA.TypeCode JavaDoc _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 JavaDoc ("0"), tc);
63    }
64
65    /**
66     * Overrides from_any() in DynAny
67     */

68
69    public void from_any(org.omg.CORBA.Any JavaDoc 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 JavaDoc e)
84       {
85          e.printStackTrace();
86          throw new InvalidValue();
87       }
88    }
89
90    public String JavaDoc get_value()
91    {
92       return anyRepresentation.extract_fixed().toString();
93    }
94
95    public boolean set_value( String JavaDoc 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 JavaDoc fixed_value = null;
110       try
111       {
112          fixed_value = new BigDecimal JavaDoc( val );
113       }
114       catch ( NumberFormatException JavaDoc 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             // truncate the value to fit the scale of the typecode
126
val = val.substring( 0, val.length() - extra );
127             truncate = true;
128          }
129          else if ( extra < 0 )
130          {
131             StringBuffer JavaDoc sb = new StringBuffer JavaDoc (val);
132
133             // add the decimal point if necessary
134
if ( val.indexOf('.') == -1 )
135             {
136                sb.append(".");
137             }
138
139             // pad the value with zeros to fit the scale of the typecode
140
for ( int i = extra; i < 0; i++ )
141             {
142                sb.append("0");
143             }
144             val = sb.toString();
145          }
146          fixed_value = new BigDecimal JavaDoc( val );
147       
148          org.omg.CORBA.FixedHolder JavaDoc holder =
149             new org.omg.CORBA.FixedHolder JavaDoc( fixed_value );
150          org.omg.CORBA.TypeCode JavaDoc 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 JavaDoc bk )
159       {
160           bk.printStackTrace();
161          // should never happen
162
}
163       return( ! truncate );
164    }
165    
166    protected org.omg.CORBA.Any JavaDoc getRepresentation()
167    {
168       return anyRepresentation;
169    }
170
171 }
172
Popular Tags