001: package org.jacorb.orb.dynany;
002:
003: /*
004: * JacORB - a free Java ORB
005: *
006: * Copyright (C) 1997-2004 Gerald Brose.
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Library General Public License for more details.
017: *
018: * You should have received a copy of the GNU Library General Public
019: * License along with this library; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: */
022:
023: import org.omg.DynamicAny.DynAnyPackage.*;
024: import org.apache.avalon.framework.logger.Logger;
025: import org.jacorb.orb.*;
026:
027: import java.math.BigDecimal;
028:
029: /**
030: * CORBA DynFixed
031: *
032: * @author Jason Courage
033: * @version $Id: DynFixed.java,v 1.9 2006/05/17 13:15:27 alphonse.bendt Exp $
034: */
035:
036: public final class DynFixed extends DynAny implements
037: org.omg.DynamicAny.DynFixed {
038: /**
039: * our representation of a fixed type any is the any itself
040: */
041: private org.omg.CORBA.Any anyRepresentation;
042:
043: DynFixed(org.omg.DynamicAny.DynAnyFactory dynFactory,
044: org.omg.CORBA.TypeCode type, ORB orb, Logger logger)
045: throws TypeMismatch {
046: super (dynFactory, orb, logger);
047:
048: org.omg.CORBA.TypeCode _type = TypeCode.originalType(type);
049:
050: if (_type.kind().value() != org.omg.CORBA.TCKind._tk_fixed) {
051: throw new TypeMismatch();
052: }
053:
054: typeCode = _type;
055: pos = -1;
056:
057: anyRepresentation = orb.create_any();
058: anyRepresentation.insert_fixed(new BigDecimal("0"), type);
059: }
060:
061: /**
062: * Overrides from_any() in DynAny
063: */
064:
065: public void from_any(org.omg.CORBA.Any value) throws InvalidValue,
066: TypeMismatch {
067: checkDestroyed();
068: if (!value.type().equivalent(type())) {
069: throw new TypeMismatch();
070: }
071:
072: typeCode = TypeCode.originalType(value.type());
073:
074: try {
075: anyRepresentation = (org.jacorb.orb.Any) orb.create_any();
076: anyRepresentation.read_value(value.create_input_stream(),
077: type());
078: } catch (Exception e) {
079: throw new InvalidValue(e.toString());
080: }
081: }
082:
083: public String get_value() {
084: return anyRepresentation.extract_fixed().toString();
085: }
086:
087: public boolean set_value(String value) throws TypeMismatch,
088: InvalidValue {
089: if (value == null) {
090: throw new TypeMismatch();
091: }
092: String val = value.trim();
093: if (val.endsWith("D") || val.endsWith("d")) {
094: val = val.substring(0, val.length() - 1);
095: }
096:
097: BigDecimal fixed_value = null;
098: try {
099: fixed_value = new BigDecimal(val);
100: } catch (NumberFormatException ex) {
101: throw new TypeMismatch();
102: }
103:
104: boolean truncate = false;
105: try {
106: int extra = fixed_value.scale() - type().fixed_scale();
107: if (extra > 0) {
108: // truncate the value to fit the scale of the typecode
109: val = val.substring(0, val.length() - extra);
110: truncate = true;
111: } else if (extra < 0) {
112: StringBuffer buffer = new StringBuffer(val);
113:
114: // add the decimal point if necessary
115: if (val.indexOf('.') == -1) {
116: buffer.append('.');
117: }
118:
119: // pad the value with zeros to fit the scale of the typecode
120: for (int i = extra; i < 0; i++) {
121: buffer.append('0');
122: }
123: val = buffer.toString();
124: }
125: fixed_value = new BigDecimal(val);
126:
127: org.omg.CORBA.FixedHolder holder = new org.omg.CORBA.FixedHolder(
128: fixed_value);
129:
130: org.omg.CORBA.TypeCode type = holder._type();
131:
132: if (type.fixed_digits() > type().fixed_digits()) {
133: throw new InvalidValue();
134: }
135: anyRepresentation.insert_fixed(fixed_value, type);
136: } catch (org.omg.CORBA.TypeCodePackage.BadKind e) {
137: throw unexpectedException(e);
138: }
139:
140: return (!truncate);
141: }
142:
143: protected org.omg.CORBA.Any getRepresentation() {
144: return anyRepresentation;
145: }
146: }
|