I think BeanSerializer/Deserializer is the better way, too, but if you
want your example to work, here is a way.
R J Scheuerle Jr schrieb:
> 
> Why can't you just extend the existing BeanSerializer/Deserializer?
> 
> Or please explain what you are trying to do which the existing serializers
> can't do.
> 
> Why don't you try using BeanPropertyTarget instead of ValueTarget or
> MethodTarget ?
BeanPropertyTarget is not public.


> Rich Scheuerle
> XML & Web Services Development
> 512-838-5115  (IBM TL 678-5115)
> 
> 
>                       David Turner
>                       <[EMAIL PROTECTED]        To:       [EMAIL PROTECTED]
>                       .mit.edu>                cc:
>                                                Subject:  Deserializer (MethodTarget, 
>FieldTarget)
>                       04/05/2002 08:32
>                       AM
>                       Please respond to
>                       axis-dev
> 
> 
> 
> I've posted a couple of messages already both to the user and dev lists,
> but got no response.
> 
> I've been trying to write my own deserializer for the following simple
> class which is based on your encoding example:
> 
> public class Data
> {
>     private String name;
>     private int age;
> 
>     public Data() {}
> 
>     pubic Data(String name, int age)
>     {
>         this.name = name;
>         this.age = age;
>     }
> 
>     public void setName(String name)
>     {
>         this.name = name;
>     }
> 
>     public String getName() { return this.name; }
> 
>     public void setAge(int age)
>     {
>         this.age = age;
>     }
> 
>     public int getAge() { return this.age; }
> 
> }
> 
> What doesn't work is the parameter to the method registerValueTarget(new
> FieldTarget(value, localname)) or registerValueTarget(new
> MethodTarget(value, methodName)).
> 
> Using FieldTarget requires that the class being deserialized have public
> field members. FieldTarget is using Class.getField() which only works on
> public fields.
> 
> MethodTarget, on the other hand doesn't work either.  I pass in the name
> of the method so it can set the private field, but it can't find it
> because MethodTarget is using Class.getMethod(methodName, Class[] {
> Object.class }).  My setters have a String parameter and the other has a
> int parameter.  MethodTarget can't find my method because it's looking
> for an Object parameter in the method parm list.
> 
> I need to deserialize and serialize some complex objects in my project,
> but this is stopping me.  I need to have something that goes beyond the
> BeanSerializer.
> 
> I could use some help and/or feedback.
> 
> Thanks
> 
> David
package samples.encoding;

import org.apache.axis.MessageContext;
import org.apache.axis.encoding.DeserializationContext;
import org.apache.axis.encoding.DeserializationContextImpl;
import org.apache.axis.encoding.SerializationContext;
import org.apache.axis.encoding.SerializationContextImpl;
import org.apache.axis.encoding.TypeMappingRegistry;
import org.apache.axis.encoding.TypeMapping;
import org.apache.axis.Constants;
import org.apache.axis.message.RPCElement;
import org.apache.axis.message.RPCParam;
import org.apache.axis.message.SOAPEnvelope;
import org.apache.axis.server.AxisServer;
import org.xml.sax.InputSource;

import javax.xml.rpc.namespace.QName;
import java.io.FileReader;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;

/** Little serialization test with a struct.
 */
public class TestSer2
{
    public static final String myNS = "urn:myNS";

    public static void main(String args[]) {
        MessageContext msgContext = new MessageContext(new AxisServer());
        SOAPEnvelope msg = new SOAPEnvelope();
        RPCParam arg1 = new RPCParam("urn:myNamespace", "testParam", "this is a 
string");
        QName data2QName = new QName("typeNS", "Data2");

        Data2 data2 = new Data2();
        data2.setName("Test name");
        data2.setAge(100);


        RPCParam arg2 = new RPCParam("", "struct", data2);
        RPCElement body = new RPCElement("urn:myNamespace", "method1", new Object[]{ 
arg1, arg2 });
        msg.addBodyElement(body);

        try {
            Reader reader = null;

            if (args.length == 0) {
                Writer stringWriter = new StringWriter();
                SerializationContext context = new 
SerializationContextImpl(stringWriter, msgContext);

                TypeMappingRegistry reg = context.getTypeMappingRegistry();
                TypeMapping tm = (TypeMapping) 
reg.getTypeMapping(Constants.URI_SOAP_ENC);
                if (tm == null) {
                    tm = (TypeMapping) reg.createTypeMapping();
                    reg.register(Constants.URI_CURRENT_SOAP_ENC, tm);
                }
                tm.register(Data2.class, data2QName, new Data2SerFactory(), new 
Data2DeserFactory());

                msg.output(context);

                String msgString = stringWriter.toString();
                System.out.println("Serialized msg:");
                System.out.println(msgString);

                System.out.println("-------");
                System.out.println("Testing deserialization...");

                reader = new StringReader(msgString);
            } else {
                reader = new FileReader(args[0]);
            }

            DeserializationContext dser = new DeserializationContextImpl(
                new InputSource(reader), msgContext, org.apache.axis.Message.REQUEST);
            dser.parse();
            SOAPEnvelope env = dser.getEnvelope();

            RPCElement rpcElem = (RPCElement)env.getFirstBody();
            RPCParam struct = rpcElem.getParam("struct");
            if (struct == null)
                throw new Exception("No <struct> param");

            if (!(struct.getValue() instanceof Data2)) {
                System.out.println("Not a Data2 object! ");
                System.out.println(struct.getValue());
                System.exit(1);
            }

            Data2 val = (Data2)struct.getValue();
            if (val == null)
                throw new Exception("No value for struct param");

            System.out.println(val.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
package samples.encoding;

import org.apache.axis.encoding.DeserializationContext;
import org.apache.axis.encoding.Deserializer;
import org.apache.axis.encoding.DeserializerImpl;
import org.apache.axis.encoding.MethodTarget;
import org.apache.axis.Constants;
import org.apache.axis.message.SOAPHandler;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import javax.xml.rpc.namespace.QName;
import java.io.IOException;
import java.util.Hashtable;

public class Data2Deser extends DeserializerImpl
{
    public static final String STRING_MEMBER = "Name";
    public static final String INT_MEMBER = "Age";
    public static final QName myTypeQName = new QName("typeNS", "Data2");

    private Hashtable typesByMemberName = new Hashtable();

    public Data2Deser()
    {
        typesByMemberName.put(STRING_MEMBER, Constants.XSD_STRING);
        typesByMemberName.put(INT_MEMBER, Constants.XSD_INT);
        value = new Data2();
    }

    /** DESERIALIZER STUFF - event handlers
     */

    /**
     * This method is invoked when an element start tag is encountered.
     * @param namespace is the namespace of the element
     * @param localName is the name of the element
     * @param qName is the prefixed qName of the element
     * @param attributes are the attributes on the element...used to get the type
     * @param context is the DeserializationContext
     */
    public SOAPHandler onStartChild(String namespace,
                                    String localName,
                                    String prefix,
                                    Attributes attributes,
                                    DeserializationContext context)
        throws SAXException
    {
        QName typeQName = (QName)typesByMemberName.get(localName);
        if (typeQName == null)
            throw new SAXException("Invalid element in Data2 struct - " + localName);

        // These can come in either order.
        Deserializer dSer = context.getDeserializerForType(typeQName);
        try {
            dSer.registerValueTarget(new MethodTarget(this, "set" + localName));
        } catch (NoSuchMethodException e) {
            throw new SAXException(e);
        }

        if (dSer == null)
            throw new SAXException("No deserializer for a " + typeQName + "???");

        return (SOAPHandler) dSer;
    }

    public void setName(Object name) {
                ((Data2)this.value).setName((String) name);
        }

        public void setAge(Object age) {
                ((Data2)this.value).setAge(((Integer)age).intValue());
        }
}
/*
 * The Apache Software License, Version 1.1
 *
 *
 * Copyright (c) 2001 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Axis" and "Apache Software Foundation" must
 *    not be used to endorse or promote products derived from this
 *    software without prior written permission. For written
 *    permission, please contact [EMAIL PROTECTED]
 *
 * 5. Products derived from this software may not be called "Apache",
 *    nor may "Apache" appear in their name, without prior written
 *    permission of the Apache Software Foundation.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 */

package samples.encoding;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import javax.xml.rpc.namespace.QName;
import java.io.IOException;

import org.apache.axis.encoding.Serializer;
import org.apache.axis.encoding.SerializerFactory;
import org.apache.axis.encoding.SerializationContext;
import org.apache.axis.encoding.Deserializer;
import org.apache.axis.encoding.DeserializerFactory;
import org.apache.axis.encoding.DeserializationContext;
import org.apache.axis.encoding.DeserializerImpl;
import org.apache.axis.Constants;

import java.util.Iterator;
import java.util.Vector;

/**
 * DeserializerFactory for DataDeser
 *
 * @author Rich Scheuerle <[EMAIL PROTECTED]>
 */
public class Data2DeserFactory implements DeserializerFactory {
    private Vector mechanisms;

    public Data2DeserFactory() {
                super();
    }
    public javax.xml.rpc.encoding.Deserializer getDeserializerAs(String mechanismType) 
{
        return new Data2Deser();
    }
    public Iterator getSupportedMechanismTypes() {
        if (mechanisms == null) {
            mechanisms = new Vector();
            mechanisms.add(Constants.AXIS_SAX);
        }
        return mechanisms.iterator();
    }
}
package samples.encoding;

import org.apache.axis.encoding.SerializationContext;
import org.apache.axis.encoding.Serializer;
import org.apache.axis.message.SOAPHandler;
import org.apache.axis.Constants;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.apache.axis.Constants;
import org.apache.axis.wsdl.fromJava.Types;

import javax.xml.rpc.namespace.QName;
import java.io.IOException;
import java.util.Hashtable;

public class Data2Ser implements Serializer
{
    public static final String STRING_MEMBER = "Name";
    public static final String INT_MEMBER = "Age";
    public static final QName myTypeQName = new QName("typeNS", "Data2");

    /** SERIALIZER STUFF
     */
    /**
     * Serialize an element named name, with the indicated attributes
     * and value.
     * @param name is the element name
     * @param attributes are the attributes...serialize is free to add more.
     * @param value is the value
     * @param context is the SerializationContext
     */
    public void serialize(QName name, Attributes attributes,
                          Object value, SerializationContext context)
        throws IOException
    {
        if (!(value instanceof Data2))
            throw new IOException("Can't serialize a " + value.getClass().getName() + 
" with a Data2Serializer.");
        Data2 data2 = (Data2)value;

        context.startElement(name, attributes);
        context.serialize(new QName("", STRING_MEMBER), null, data2.getName(), 
String.class);
        context.serialize(new QName("", INT_MEMBER), null, new 
Integer(data2.getAge()), int.class);
        context.endElement();
    }
    public String getMechanismType() { return Constants.AXIS_SAX; }

    /**
     * Return XML schema for the specified type, suitable for insertion into
     * the <types> element of a WSDL document.
     *
     * @param types the Java2WSDL Types object which holds the context
     *              for the WSDL being generated.
     * @return true if we wrote a schema, false if we didn't.
     * @see org.apache.axis.wsdl.fromJava.Types
     */
    public boolean writeSchema(Types types) throws Exception {
        return false;
    }
}
/*
 * The Apache Software License, Version 1.1
 *
 *
 * Copyright (c) 2001 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Axis" and "Apache Software Foundation" must
 *    not be used to endorse or promote products derived from this
 *    software without prior written permission. For written
 *    permission, please contact [EMAIL PROTECTED]
 *
 * 5. Products derived from this software may not be called "Apache",
 *    nor may "Apache" appear in their name, without prior written
 *    permission of the Apache Software Foundation.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 */

package samples.encoding;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import javax.xml.rpc.namespace.QName;
import java.io.IOException;

import org.apache.axis.encoding.Serializer;
import org.apache.axis.encoding.SerializerFactory;
import org.apache.axis.encoding.SerializationContext;
import org.apache.axis.encoding.Deserializer;
import org.apache.axis.encoding.DeserializerFactory;
import org.apache.axis.encoding.DeserializationContext;
import org.apache.axis.encoding.DeserializerImpl;
import org.apache.axis.Constants;

import java.util.Iterator;
import java.util.Vector;

/**
 * SerializerFactory for Data2Ser
 *
 * @author Rich Scheuerle <[EMAIL PROTECTED]>
 */
public class Data2SerFactory implements SerializerFactory {
    private Vector mechanisms;

    public Data2SerFactory() {
    }
    public javax.xml.rpc.encoding.Serializer getSerializerAs(String mechanismType) {
        return new Data2Ser();
    }
    public Iterator getSupportedMechanismTypes() {
        if (mechanisms == null) {
            mechanisms = new Vector();
            mechanisms.add(Constants.AXIS_SAX);
        }
        return mechanisms.iterator();
    }
}
package samples.encoding;

public class Data2
{
    private String name;
    private int age;

    public Data2() {}

    public Data2(String name, int age)
    {
        this.name = name;
        this.age = age;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getName() { return this.name; }

    public void setAge(int age)
    {
        this.age = age;
    }

    public int getAge() { return this.age; }

}


Reply via email to