KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > samples > encoding > TestSer


1 package samples.encoding;
2
3 import org.apache.axis.Constants;
4 import org.apache.axis.MessageContext;
5 import org.apache.axis.encoding.DeserializationContext;
6 import org.apache.axis.encoding.SerializationContext;
7 import org.apache.axis.encoding.SerializationContext;
8 import org.apache.axis.encoding.TypeMapping;
9 import org.apache.axis.encoding.TypeMappingRegistry;
10 import org.apache.axis.message.RPCElement;
11 import org.apache.axis.message.RPCParam;
12 import org.apache.axis.message.SOAPEnvelope;
13 import org.apache.axis.server.AxisServer;
14 import org.xml.sax.InputSource JavaDoc;
15
16 import javax.xml.namespace.QName JavaDoc;
17 import java.io.FileReader JavaDoc;
18 import java.io.Reader JavaDoc;
19 import java.io.StringReader JavaDoc;
20 import java.io.StringWriter JavaDoc;
21 import java.io.Writer JavaDoc;
22
23 /** Little serialization test with a struct.
24  */

25 public class TestSer
26 {
27     public static final String JavaDoc myNS = "urn:myNS";
28     
29     public static void main(String JavaDoc args[]) {
30         MessageContext msgContext = new MessageContext(new AxisServer());
31         SOAPEnvelope msg = new SOAPEnvelope();
32         RPCParam arg1 = new RPCParam("urn:myNamespace", "testParam", "this is a string");
33         QName JavaDoc dataQName = new QName JavaDoc("typeNS", "Data");
34
35         Data data = new Data();
36         Data data2 = new Data();
37         data.stringMember = "String member";
38         data.floatMember = new Float JavaDoc("1.23");
39         data.dataMember = data2;
40         
41         data2.stringMember = "another str member";
42         data2.floatMember = new Float JavaDoc("4.56");
43         data2.dataMember = null; // "data;" for loop-test of multi-refs
44

45         RPCParam arg2 = new RPCParam("", "struct", data);
46         RPCElement body = new RPCElement("urn:myNamespace", "method1", new Object JavaDoc[]{ arg1, arg2 });
47         msg.addBodyElement(body);
48         
49         try {
50             Reader JavaDoc reader = null;
51             
52             if (args.length == 0) {
53                 Writer JavaDoc stringWriter = new StringWriter JavaDoc();
54                 SerializationContext context = new SerializationContext(stringWriter, msgContext);
55                 
56                 TypeMappingRegistry reg = context.getTypeMappingRegistry();
57                 TypeMapping tm = (TypeMapping) reg.getOrMakeTypeMapping(Constants.URI_SOAP11_ENC);
58                 tm.register(Data.class, dataQName, new DataSerFactory(), new DataDeserFactory());
59
60                 msg.output(context);
61                 
62                 String JavaDoc msgString = stringWriter.toString();
63                 System.out.println("Serialized msg:");
64                 System.out.println(msgString);
65                 
66                 System.out.println("-------");
67                 System.out.println("Testing deserialization...");
68                 
69                 reader = new StringReader JavaDoc(msgString);
70             } else {
71                 reader = new FileReader JavaDoc(args[0]);
72             }
73             
74             DeserializationContext dser = new DeserializationContext(
75                 new InputSource JavaDoc(reader), msgContext, org.apache.axis.Message.REQUEST);
76             dser.parse();
77             SOAPEnvelope env = dser.getEnvelope();
78             
79             RPCElement rpcElem = (RPCElement)env.getFirstBody();
80             RPCParam struct = rpcElem.getParam("struct");
81             if (struct == null)
82                 throw new Exception JavaDoc("No <struct> param");
83             
84             if (!(struct.getObjectValue() instanceof Data)) {
85                 System.out.println("Not a Data object! ");
86                 System.out.println(struct.getObjectValue());
87                 System.exit(1);
88             }
89             
90             Data val = (Data)struct.getObjectValue();
91             if (val == null)
92                 throw new Exception JavaDoc("No value for struct param");
93             
94             System.out.println(val.toString());
95         } catch (Exception JavaDoc e) {
96             e.printStackTrace();
97         }
98     }
99 }
100
Popular Tags