axis-java-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gdani...@apache.org
Subject cvs commit: xml-axis/java/samples/encoding Data.java DataSer.java TestSer.java
Date Thu, 03 May 2001 14:34:18 GMT
gdaniels    01/05/03 07:34:18

  Added:       java/samples/encoding Data.java DataSer.java TestSer.java
  Log:
  A little sample illustrating a way to integrate a custom data type (a struct)
  with the encoding system.
  
  Revision  Changes    Path
  1.1                  xml-axis/java/samples/encoding/Data.java
  
  Index: Data.java
  ===================================================================
  package samples.encoding;
  
  public class Data
  {
      public String stringMember;
      public Float floatMember;
  }
  
  
  
  1.1                  xml-axis/java/samples/encoding/DataSer.java
  
  Index: DataSer.java
  ===================================================================
  package samples.encoding;
  
  import org.apache.axis.encoding.*;
  
  import java.util.*;
  import java.io.*;
  import org.xml.sax.*;
  import org.apache.axis.utils.QName;
  
  public class DataSer extends DeserializerBase implements Serializer
  {
      public static final String STRINGMEMBER = "stringMember";
      public static final String FLOATMEMBER = "floatMember";
      
      public static class DataSerFactory implements DeserializerFactory {
          public DeserializerBase getDeserializer() {
              return new DataSer();
          }
      }
      public static DeserializerFactory getFactory()
      {
          return new DataSerFactory();
      }
      
      private Hashtable typesByMemberName = new Hashtable();  
      
      /** Have I received the startelement event for the top-level element?
       */
      private boolean gotMyStart = false;
      
      public DataSer()
      {
          typesByMemberName.put(STRINGMEMBER, SOAPTypeMappingRegistry.XSD_STRING);
          typesByMemberName.put(FLOATMEMBER, SOAPTypeMappingRegistry.XSD_FLOAT);
          value = new Data();
      }
      
      /** DESERIALIZER STUFF - event handlers
       */
      
      public void startElement(String namespace, String localName,
                               String qName, Attributes attributes)
          throws SAXException
      {
          if (!gotMyStart) {
              gotMyStart = true;
              return;
          }
          
          QName typeQName = (QName)typesByMemberName.get(localName);
          if (typeQName == null)
              throw new SAXException("Invalid element in Data struct - " + localName);
          
          // These can come in either order.
          DeserializerBase dSer = context.getDeserializer(typeQName);
          
          if (dSer == null)
              throw new SAXException("No deserializer for a " + typeQName + "???");
  
          dSer.setParent(this);
          
          context.pushElementHandler(dSer);
      }
      
      public void onEndChild(String localName, DeserializerBase deserializer)
          throws SAXException
      {
          if (STRINGMEMBER.equals(localName)) {
              ((Data)value).stringMember = (String)deserializer.getValue();
          } else if (FLOATMEMBER.equals(localName)) {
              ((Data)value).floatMember = (Float)deserializer.getValue();
          } else {
              throw new SAXException("No such child - " + localName);
          }
      }
          
      /** SERIALIZER STUFF
       */
      
      public void serialize(QName name, Attributes attributes,
                            Object value, SerializationContext context)
          throws IOException
      {
          if (!(value instanceof Data))
              throw new IOException("Can't serialize a " + value.getClass().getName() + "
with a DataSerializer.");
          Data data = (Data)value;
          
          context.startElement(name, attributes);
          context.serialize(new QName("", STRINGMEMBER), null, data.stringMember);
          context.serialize(new QName("", FLOATMEMBER), null, data.floatMember);
          context.endElement();
      }
  }
  
  
  
  1.1                  xml-axis/java/samples/encoding/TestSer.java
  
  Index: TestSer.java
  ===================================================================
  package samples.encoding;
  
  import org.apache.axis.message.*;
  import org.apache.axis.encoding.*;
  import org.apache.axis.utils.QName;
  import org.xml.sax.InputSource;
  import org.apache.xerces.parsers.SAXParser;
  import java.io.*;
  import java.util.*;
  
  /** Little serialization test with a struct.
   */
  public class TestSer
  {
      public static final String myNS = "urn:myNS";
      
      public static void main(String args[]) {
          SOAPEnvelope msg = new SOAPEnvelope();
          RPCParam arg1 = new RPCParam("urn:myNamespace", "testParam", "this is a string");
          
          Data data = new Data();
          data.stringMember = "String member";
          data.floatMember = new Float("4.54");
          
          RPCParam arg2 = new RPCParam("", "struct", data);
          RPCElement body = new RPCElement("urn:myNamespace", "method1", new Object[]{ arg1,
arg2 });
          msg.addBodyElement(body);
          
          try {
              Writer stringWriter = new StringWriter();
              SerializationContext context = new SerializationContext(stringWriter);
              
              TypeMappingRegistry reg = context.getTypeMappingRegistry();
              QName dataQName = new QName("typeNS", "Data");
              
              reg.addSerializer(Data.class, dataQName, new DataSer());
  
              msg.output(context);
              
              String msgString = stringWriter.toString();
              System.out.println("Serialized msg:");
              System.out.println(msgString);
              
              System.out.println("-------");
              System.out.println("Testing deserialization...");
              
              StringReader reader = new StringReader(msgString);
              
              SAXAdapter adapter = new SAXAdapter(new SAXParser(), new InputSource(reader));
              reg = adapter.getContext().getTypeMappingRegistry();
              reg.addDeserializerFactory(dataQName, DataSer.getFactory());
              
              SOAPEnvelope env = adapter.getEnvelope();
              RPCElement rpcElem = (RPCElement)env.getFirstBody();
              RPCParam struct = rpcElem.getParam("struct");
              if (struct == null)
                  throw new Exception("No <struct> param");
              
              Data val = (Data)struct.getValue();
              if (val == null)
                  throw new Exception("No value for struct param");
              
              System.out.println("String member is '" + val.stringMember +"'");
              System.out.println("Float member is " + val.floatMember);
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  }
  
  
  

Mime
View raw message