WebReference.com - Part 4 of chapter 3 from Professional Java Web Services, Wrox Press Ltd. (4/6)
[previous] [next] |
Professional Java Web Services
The QName is the fully qualified name for the XML element that represents the Java type. In other
words, each user-defined type must have a namespace associated with it. For example, a user-defined
Java type Myint would look this:
<x:Myint xmlns:x="urn:MyNamespace">2</Myint>
The name of the fully qualified (package.classname) Java class that implements
the type should be specified. For the Myint type the class would be specified as
something like this: com.wrox.example.Myint.
In order for a type to be transmitted it must capable of being serialized to/deserialized from
XML. Apache SOAP handles serialization through the use of Java classes that know how to handle the
serialization needs of a type. A class that handles the serialization must implement the
org.apache.soap.util.xml.Serializer. A class that handles deserialization must implement
org.apache.soap.util.xml.Deserializer. A single class may implement both to provide both
serialization and deserialization for the data class. As to whether to provide one or two classes, it
really doesn't matter if one or two classes are used. It only matters that the methods defined by the
interface are implemented. The Serializer interface requires that a marshall()
method be implemented. Likewise the Deserializer interface requires an unmarshall()
method to be implemented. So, a class that handles both serialization and deserialization must define
both the marshall() and unmarshall() methods.
Below is the Apache SOAP deserializer class for the int type:
package org.apache.soap.encoding.soapenc;
import java.io.*;
import org.w3c.dom.*;
import org.apache.soap.util.xml.*;
import org.apache.soap.*;
import org.apache.soap.util.*;
import org.apache.soap.rpc.*;
public class IntDeserializer implements Deserializer {
public Bean unmarshall(String inScopeEncStyle, QName elementType,
Node src, XMLJavaMappingRegistry xjmr,
SOAPContext ctx)
throws IllegalArgumentException {
Element root = (Element)src;
String value = DOMUtils.getChildCharacterData(root);
return new Bean(int.class, new Integer(value));
}
}
The src argument contains a XML element with a type of int that needs to be
unmarshaled back into an int primitive type. The src variable is type cast
upward to an Element object, the DomUtils.getChildCharacterData() method is used
to obtain the value of the XML element and the value is returned via a class called Bean.
The purpose of the Bean class is to simply represent the type and value of an unmarshaled
element.
Default Type Mappings
In efforts to speed up the development of SOAP applications and for convenience purposes, Apache
SOAP predefines and registers type mappings in the org.apache.soap.encoding.SOAPMappingRegistry
class for primitive types (that is, int, float, and so on), wrapper classes (that
is, java.lang.Integer, java.lang.Float), and other types. Below is a list of
the other types:
- Java arrays
java.lang.Stringjava.util.Datejava.util.GregorianCalendarjava.util.Vectorjava.util.Hashtablejava.util.Map(available when using JDK 1.2 and above)java.math.BigDecimaljavax.mail.internet.MimeBodyPartjava.io.InputStreamjavax.activation.DataSourcejavax.activation.DataHandlerorg.apache.soap.util.xml.QNameorg.apache.soap.rpc.Parameter
[previous] [next] |
Created: June 5, 2002
Revised: June 5, 2002
URL: http://webreference.com/programming/java/webservices/chap3/4/4.html

Find a programming school near you