1 55 56 package org.jboss.axis.encoding.ser; 57 58 import org.jboss.axis.Constants; 59 import org.jboss.axis.encoding.SerializationContext; 60 import org.jboss.axis.encoding.Serializer; 61 import org.jboss.axis.utils.Messages; 62 import org.jboss.axis.wsdl.fromJava.Types; 63 import org.jboss.logging.Logger; 64 import org.w3c.dom.Element ; 65 import org.xml.sax.Attributes ; 66 import org.xml.sax.helpers.AttributesImpl ; 67 68 import javax.xml.namespace.QName ; 69 import java.io.IOException ; 70 import java.util.Iterator ; 71 import java.util.Map ; 72 73 81 82 public class MapSerializer implements Serializer 83 { 84 private static Logger log = Logger.getLogger(MapSerializer.class.getName()); 85 86 private static final QName QNAME_KEY = new QName ("", "key"); 88 private static final QName QNAME_ITEM = new QName ("", "item"); 89 private static final QName QNAME_VALUE = new QName ("", "value"); 90 91 103 public void serialize(QName name, Attributes attributes, 104 Object value, SerializationContext context) 105 throws IOException 106 { 107 if (!(value instanceof Map )) 108 throw new IOException (Messages.getMessage("noMap00", "MapSerializer", value.getClass().getName())); 109 110 Map map = (Map )value; 111 112 context.startElement(name, attributes); 113 114 AttributesImpl itemsAttributes = new AttributesImpl (); 115 String encodingURI = context.getMessageContext().getSOAPConstants().getEncodingURI(); 116 String encodingPrefix = context.getPrefixForURI(encodingURI); 117 String soapPrefix = context.getPrefixForURI(Constants.SOAP_MAP.getNamespaceURI()); 118 itemsAttributes.addAttribute(encodingURI, "type", encodingPrefix + ":type", 119 "CDATA", encodingPrefix + ":Array"); 120 itemsAttributes.addAttribute(encodingURI, "arrayType", encodingPrefix + ":arrayType", 121 "CDATA", soapPrefix + ":item[" + map.size() + "]"); 122 123 for (Iterator i = map.entrySet().iterator(); i.hasNext();) 124 { 125 Map.Entry entry = (Map.Entry )i.next(); 126 Object key = entry.getKey(); 127 Object val = entry.getValue(); 128 129 context.startElement(QNAME_ITEM, null); 130 131 context.serialize(QNAME_KEY, null, key); 132 context.serialize(QNAME_VALUE, null, val); 133 134 context.endElement(); 135 } 136 137 context.endElement(); 138 } 139 140 public String getMechanismType() 141 { 142 return Constants.AXIS_SAX; 143 } 144 145 156 public Element writeSchema(Class javaType, Types types) throws Exception 157 { 158 Element complexType = types.createElement("complexType"); 159 complexType.setAttribute("name", "Map"); 160 Element seq = types.createElement("sequence"); 161 complexType.appendChild(seq); 162 Element element = types.createElement("element"); 163 element.setAttribute("name", "item"); 164 element.setAttribute("minOccurs", "0"); 165 element.setAttribute("maxOccurs", "unbounded"); 166 element.setAttribute("type", types.getQNameString(new QName (Constants.NS_URI_XMLSOAP, "mapItem"))); 167 seq.appendChild(element); 168 169 Element itemType = types.createElement("complexType"); 170 itemType.setAttribute("name", "mapItem"); 171 Element seq2 = types.createElement("sequence"); 172 itemType.appendChild(seq2); 173 Element element2 = types.createElement("element"); 174 element2.setAttribute("name", "key"); 175 element2.setAttribute("nillable", "true"); 176 element2.setAttribute("type", "xsd:string"); 177 seq2.appendChild(element2); 178 Element element3 = types.createElement("element"); 179 element3.setAttribute("name", "value"); 180 element3.setAttribute("nillable", "true"); 181 element3.setAttribute("type", "xsd:string"); 182 seq2.appendChild(element3); 183 types.writeSchemaElement(Constants.SOAP_MAP, itemType); 184 185 return complexType; 186 } 187 } 188 | Popular Tags |