1 16 17 package org.apache.axis.encoding.ser; 18 19 import org.apache.axis.Constants; 20 import org.apache.axis.components.logger.LogFactory; 21 import org.apache.axis.encoding.SerializationContext; 22 import org.apache.axis.encoding.Serializer; 23 import org.apache.axis.utils.Messages; 24 import org.apache.axis.wsdl.fromJava.Types; 25 import org.apache.commons.logging.Log; 26 import org.w3c.dom.Element ; 27 import org.xml.sax.Attributes ; 28 import org.xml.sax.helpers.AttributesImpl ; 29 30 import javax.xml.namespace.QName ; 31 import java.io.IOException ; 32 import java.util.Iterator ; 33 import java.util.Map ; 34 35 43 44 public class MapSerializer implements Serializer 45 { 46 protected static Log log = 47 LogFactory.getLog(MapSerializer.class.getName()); 48 49 private static final QName QNAME_KEY = new QName ("","key"); 51 private static final QName QNAME_ITEM = new QName ("", "item"); 52 private static final QName QNAME_VALUE = new QName ("", "value"); 53 private static final QName QNAME_ITEMTYPE = new QName (Constants.NS_URI_XMLSOAP, "item"); 54 55 66 public void serialize(QName name, Attributes attributes, 67 Object value, SerializationContext context) 68 throws IOException 69 { 70 if (!(value instanceof Map )) 71 throw new IOException ( 72 Messages.getMessage("noMap00", "MapSerializer", value.getClass().getName())); 73 74 Map map = (Map )value; 75 76 context.startElement(name, attributes); 77 78 AttributesImpl itemsAttributes = new AttributesImpl (); 79 String encodingURI = context.getMessageContext().getEncodingStyle(); 80 String encodingPrefix = context.getPrefixForURI(encodingURI); 81 String soapPrefix = context.getPrefixForURI(Constants.SOAP_MAP.getNamespaceURI()); 82 itemsAttributes.addAttribute(encodingURI, "type", encodingPrefix + ":type", 83 "CDATA", encodingPrefix + ":Array"); 84 itemsAttributes.addAttribute(encodingURI, "arrayType", encodingPrefix + ":arrayType", 85 "CDATA", soapPrefix + ":item["+map.size()+"]"); 86 87 for (Iterator i = map.entrySet().iterator(); i.hasNext(); ) 88 { 89 Map.Entry entry = (Map.Entry ) i.next(); 90 Object key = entry.getKey(); 91 Object val = entry.getValue(); 92 93 context.startElement(QNAME_ITEM, null); 94 95 context.serialize(QNAME_KEY, null, key, null, null, Boolean.TRUE); 97 context.serialize(QNAME_VALUE, null, val, null, null, Boolean.TRUE); 98 99 context.endElement(); 100 } 101 102 context.endElement(); 103 } 104 105 public String getMechanismType() { return Constants.AXIS_SAX; } 106 107 118 public Element writeSchema(Class javaType, Types types) throws Exception { 119 Element complexType = types.createElement("complexType"); 120 complexType.setAttribute("name", "Map"); 121 Element seq = types.createElement("sequence"); 122 complexType.appendChild(seq); 123 Element element = types.createElement("element"); 124 element.setAttribute("name", "item"); 125 element.setAttribute("minOccurs", "0"); 126 element.setAttribute("maxOccurs", "unbounded"); 127 element.setAttribute("type", types.getQNameString(new QName (Constants.NS_URI_XMLSOAP,"mapItem"))); 128 seq.appendChild(element); 129 130 Element itemType = types.createElement("complexType"); 131 itemType.setAttribute("name", "mapItem"); 132 Element seq2 = types.createElement("sequence"); 133 itemType.appendChild(seq2); 134 Element element2 = types.createElement("element"); 135 element2.setAttribute("name", "key"); 136 element2.setAttribute("nillable", "true"); 137 element2.setAttribute("type", "xsd:anyType"); 138 seq2.appendChild(element2); 139 Element element3 = types.createElement("element"); 140 element3.setAttribute("name", "value"); 141 element3.setAttribute("nillable", "true"); 142 element3.setAttribute("type", "xsd:anyType"); 143 seq2.appendChild(element3); 144 types.writeSchemaTypeDecl(QNAME_ITEMTYPE, itemType); 145 146 return complexType; 147 } 148 } 149 | Popular Tags |