Re: Marshal customized Float object

Stefan Haustein <[email protected]> Thu, 02 Oct 2003 01:46:50 +0200
Newsgroups gmane.comp.java.enhydra.ksoap
Organization University of Dortmund
Message-ID <[email protected]>
Hi Linh,

I think if you do not declare the variables "static" in lines 2,3, and 
4, the problem will go away....

HTH,
Stefan

Hoang Linh wrote:
> Hi kSOAP users and developers,
> 
> I'd like to have a question about the problem described below.
> 
> A Float object was created to be marshalled with the class
> MarshalFloatDouble below. The readInstance method of the class
> MarshalFloatDouble uses the
> method CnFloat.stringToFloat() from the class CnFloat ( line 29 ). 
> 
> The main code for parsing received Soap message and extracting  data was
> also described below ( line 39). The tested Soap messages for Float type and
> Date type are at lines 62 and 70
> respectively. 
> 
> The problem ( from line 52) is that the 2 extracted objects of type Float
> (number1 and number2) are identical to the last object. Similar procedures that
> was done with kSOAP MarshalDate and IsoDate classes  gave a correct result (
> from line 57).
> 
> After some debugs, I found that, with the received Float type SOAP message,
> the returned object "result"  at line 20 is correct each time the
> readInstance method was called. But  the method recvEnvelope.parse (xp)  at line 49
> returns a Soap Envelope object containing a body with 2 identical properties.
> Some problems with ClassMap ?  
>   
> Any idea of this problem and how to solve it?
> 
> Thank you very much.
> Brgds/Linh.
> 
> 
> 
> 1.    public class Float extends Object {
> 
> 2.    private static String Value = null;
> 3.    private static int IntegerPart;
> 4.    private static int FractionPart;
> 
> 5.    public Float (){
> 6.    }
> 
> 7.    public Float (String value){
> 8.    this.Value=value;
> 9.    }
> 
> 10.    // some methods ...
> 11.    }
> 
> 
> 12.    public class MarshalFloatDouble implements Marshal {
> 
> 13.    public  static Class FLOAT_CLASS = new Float ().getClass ();
> 
> 14.    public Object readInstance (SoapParser parser, String namespace,
> String
> name,     ElementType expected) throws IOException {
> 
> 15.    parser.parser.read (); // start tag
> 16.    Object result;
> 17.    if (name.equals ("float"))
> a.    result = CnFloat.stringToFloat(parser.parser.readText());  
> 18.    else
> a.    throw new RuntimeException ("float  expected");
> 19.    parser.parser.read (); // end tag
> 
> 20.    return result;
> 21.    }
> 
> 
> 22.    public void writeInstance (SoapWriter writer,
> 1.    Object instance) throws IOException {
> 
> 23.    writer.writer.write(CnFloat.floatToString((Float)instance));   
> 24.    }
> 
> 
> 25.    public void register (ClassMap cm) {
> 26.    cm.addMapping (cm.xsd, "float", MarshalFloatDouble.FLOAT_CLASS,
> this);
> 27.    } 
> 
> 28.    }
> 
> 29.    public class CnFloat {
> 
> 30.    public static String floatToString(Float number) {
> 31.    return number.getValue();
> 32.    }
> 
> 33.    public static Float stringToFloat(String value) {
> 
> 34.    Float floatNum= new Float(value);
> 
> 35.    //  method for parsing a string and map it to a Float object.
> 
> 36.    return floatNum;
> 37.    }
> 38.    }
> 
> 
> 39.    /* The main code for parsing soap message */
> 
> 40.    ByteArrayInputStream bis = new ByteArrayInputStream
> (SoapMesg.getBytes());
> 41.    InputStreamReader reader = new InputStreamReader (bis);
> 42.    XmlParser xp = new XmlParser(reader);
> 43.    ClassMap cm =new ClassMap();
> 
> 44.    Marshal md= new MarshalDate();  // register for Date Type
> 45.    md.register(cm);
> 
> 46.    Marshal mf= new MarshalFloatDouble(); // register for Float type
> 47.    mf.register(cm);    
> 
> 48.    SoapEnvelope recvEnvelope = new SoapEnvelope (cm);
> 49.    recvEnvelope.parse (xp);  
> 50.    SoapObject BodyElement = (SoapObject) recvEnvelope.getBody();
> 
> 51.    // 2 objects : number1 and number2 are identical to number2 ???????
> 52.    Float number1=(Float)BodyElement.getProperty(0);
> 53.    System.out.println("FloatNumber1=" +number1.getValue());
> 54.    Float number2=(Float)BodyElement.getProperty(1);
> 55.    System.out.println("FloatNumber2=" + number2.getValue());
> 
> 56.    // Simimlar procedure :  MarshalDate downloaded from kSoap gives 2
> correct objects  date1, date2.
> 57.    Date date1= (Date)BodyElement.getProperty(0);
> 58.    System.out.println("date1=" +date1.toString());
> 59.    Date date2= (Date)BodyElement.getProperty(1);
> 60.    System.out.println("date2=" +date2.toString());
> 
> 
> 61.    /*  Here are the received Soap messages for Date type and Float
> type*/
> 
> 62.    <SOAP-ENV:Envelope
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
> SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
> xmlns:xsd="http://www.w3.org/2001/XMLSchema">
> 63.    <SOAP-ENV:Body>
> 64.    <echoFloat>
> 65.    <Num1 xsi:type="xsd:float">54.45</Num1>
> 66.    <Num2 xsi:type="xsd:float">65.56</Num2>
> 67.    </echoFloat>
> 68.    </SOAP-ENV:Body>
> 69.    </SOAP-ENV:Envelope>
> 
> 
> 70.    <SOAP-ENV:Envelope
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
> SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
> xmlns:xsd="http://www.w3.org/2001/XMLSchema">
> 71.    <SOAP-ENV:Body>
> 72.    <echoDate>
> 73.    <ExecTime1
> xsi:type="xsd:dateTime">2003-08-27T20:14:54.45Z</ExecTime1>
> 74.    <ExecTime2
> xsi:type="xsd:dateTime">2003-09-28T21:15:55.46Z</ExecTime2>
> 75.    </echoDate>
> 76.    </SOAP-ENV:Body>
> 77.    </SOAP-ENV:Envelope>
>