FileDocCategorySizeDatePackage
MapSerializer.javaAPI DocApache Axis 1.46007Sat Apr 22 18:57:28 BST 2006org.apache.axis.encoding.ser

MapSerializer

public class MapSerializer extends Object implements org.apache.axis.encoding.Serializer
A MapSerializer is be used to serialize and deserialize Maps using the SOAP-ENC encoding style.

author
Glen Daniels (gdaniels@apache.org) Modified by @author Rich Scheuerle (scheu@us.ibm.com)

Fields Summary
protected static Log
log
private static final QName
QNAME_KEY
private static final QName
QNAME_ITEM
private static final QName
QNAME_VALUE
private static final QName
QNAME_ITEMTYPE
Constructors Summary
Methods Summary
public java.lang.StringgetMechanismType()

 return Constants.AXIS_SAX; 
public voidserialize(javax.xml.namespace.QName name, org.xml.sax.Attributes attributes, java.lang.Object value, org.apache.axis.encoding.SerializationContext context)
Serialize a Map Walk the collection of keys, serializing each key/value pair inside an element.

param
name the desired QName for the element
param
attributes the desired attributes for the element
param
value the Object to serialize
param
context the SerializationContext in which to do all this
exception
IOException

    

                                                           
         
                             
         
    
        if (!(value instanceof Map))
            throw new IOException(
                Messages.getMessage("noMap00", "MapSerializer", value.getClass().getName()));

        Map map = (Map)value;

        context.startElement(name, attributes);
        
        AttributesImpl itemsAttributes = new AttributesImpl();
        String encodingURI = context.getMessageContext().getEncodingStyle();
        String encodingPrefix = context.getPrefixForURI(encodingURI);
        String soapPrefix = context.getPrefixForURI(Constants.SOAP_MAP.getNamespaceURI());
        itemsAttributes.addAttribute(encodingURI, "type", encodingPrefix + ":type",
                                   "CDATA", encodingPrefix + ":Array");        
        itemsAttributes.addAttribute(encodingURI, "arrayType", encodingPrefix + ":arrayType",
                                   "CDATA", soapPrefix + ":item["+map.size()+"]");        

        for (Iterator i = map.entrySet().iterator(); i.hasNext(); )
        {
            Map.Entry entry = (Map.Entry) i.next();
            Object key = entry.getKey();
            Object val = entry.getValue();

            context.startElement(QNAME_ITEM, null);

            // Since the Key and Value can be any type, send type info
            context.serialize(QNAME_KEY,   null, key, null, null, Boolean.TRUE);
            context.serialize(QNAME_VALUE, null, val, null, null, Boolean.TRUE);

            context.endElement();
        }

        context.endElement();
    
public org.w3c.dom.ElementwriteSchema(java.lang.Class javaType, org.apache.axis.wsdl.fromJava.Types types)
Return XML schema for the specified type, suitable for insertion into the <types> element of a WSDL document, or underneath an <element> or <attribute> declaration.

param
javaType the Java Class we're writing out schema for
param
types the Java2WSDL Types object which holds the context for the WSDL being generated.
return
a type element containing a schema simpleType/complexType
see
org.apache.axis.wsdl.fromJava.Types

        Element complexType = types.createElement("complexType");
        complexType.setAttribute("name", "Map");
        Element seq = types.createElement("sequence");
        complexType.appendChild(seq);
        Element element = types.createElement("element");
        element.setAttribute("name", "item");
        element.setAttribute("minOccurs", "0");
        element.setAttribute("maxOccurs", "unbounded");
        element.setAttribute("type", types.getQNameString(new QName(Constants.NS_URI_XMLSOAP,"mapItem")));
        seq.appendChild(element);
    
        Element itemType = types.createElement("complexType");
        itemType.setAttribute("name", "mapItem");
        Element seq2 = types.createElement("sequence");
        itemType.appendChild(seq2);
        Element element2 = types.createElement("element");
        element2.setAttribute("name", "key");
        element2.setAttribute("nillable", "true");
        element2.setAttribute("type", "xsd:anyType");
        seq2.appendChild(element2);
        Element element3 = types.createElement("element");
        element3.setAttribute("name", "value");
        element3.setAttribute("nillable", "true");
        element3.setAttribute("type", "xsd:anyType");
        seq2.appendChild(element3);
        types.writeSchemaTypeDecl(QNAME_ITEMTYPE, itemType);

        return complexType;