KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > encoding > ser > MapSerializer


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
27 import org.xml.sax.Attributes JavaDoc;
28 import org.xml.sax.helpers.AttributesImpl JavaDoc;
29
30 import javax.xml.namespace.QName JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.Map JavaDoc;
34
35 /**
36  * A <code>MapSerializer</code> is be used to serialize and
37  * deserialize Maps using the <code>SOAP-ENC</code>
38  * encoding style.<p>
39  *
40  * @author Glen Daniels (gdaniels@apache.org)
41  * Modified by @author Rich Scheuerle (scheu@us.ibm.com)
42  */

43
44 public class MapSerializer implements Serializer
45 {
46     protected static Log log =
47         LogFactory.getLog(MapSerializer.class.getName());
48
49     // QNames we deal with
50
private static final QName JavaDoc QNAME_KEY = new QName JavaDoc("","key");
51     private static final QName JavaDoc QNAME_ITEM = new QName JavaDoc("", "item");
52     private static final QName JavaDoc QNAME_VALUE = new QName JavaDoc("", "value");
53     private static final QName JavaDoc QNAME_ITEMTYPE = new QName JavaDoc(Constants.NS_URI_XMLSOAP, "item");
54
55     /** Serialize a Map
56      *
57      * Walk the collection of keys, serializing each key/value pair
58      * inside an <item> element.
59      *
60      * @param name the desired QName for the element
61      * @param attributes the desired attributes for the element
62      * @param value the Object to serialize
63      * @param context the SerializationContext in which to do all this
64      * @exception IOException
65      */

66     public void serialize(QName JavaDoc name, Attributes JavaDoc attributes,
67                           Object JavaDoc value, SerializationContext context)
68         throws IOException JavaDoc
69     {
70         if (!(value instanceof Map JavaDoc))
71             throw new IOException JavaDoc(
72                 Messages.getMessage("noMap00", "MapSerializer", value.getClass().getName()));
73
74         Map JavaDoc map = (Map JavaDoc)value;
75
76         context.startElement(name, attributes);
77         
78         AttributesImpl JavaDoc itemsAttributes = new AttributesImpl JavaDoc();
79         String JavaDoc encodingURI = context.getMessageContext().getEncodingStyle();
80         String JavaDoc encodingPrefix = context.getPrefixForURI(encodingURI);
81         String JavaDoc 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 JavaDoc i = map.entrySet().iterator(); i.hasNext(); )
88         {
89             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
90             Object JavaDoc key = entry.getKey();
91             Object JavaDoc val = entry.getValue();
92
93             context.startElement(QNAME_ITEM, null);
94
95             // Since the Key and Value can be any type, send type info
96
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 JavaDoc getMechanismType() { return Constants.AXIS_SAX; }
106
107     /**
108      * Return XML schema for the specified type, suitable for insertion into
109      * the &lt;types&gt; element of a WSDL document, or underneath an
110      * &lt;element&gt; or &lt;attribute&gt; declaration.
111      *
112      * @param javaType the Java Class we're writing out schema for
113      * @param types the Java2WSDL Types object which holds the context
114      * for the WSDL being generated.
115      * @return a type element containing a schema simpleType/complexType
116      * @see org.apache.axis.wsdl.fromJava.Types
117      */

118     public Element JavaDoc writeSchema(Class JavaDoc javaType, Types types) throws Exception JavaDoc {
119         Element JavaDoc complexType = types.createElement("complexType");
120         complexType.setAttribute("name", "Map");
121         Element JavaDoc seq = types.createElement("sequence");
122         complexType.appendChild(seq);
123         Element JavaDoc 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 JavaDoc(Constants.NS_URI_XMLSOAP,"mapItem")));
128         seq.appendChild(element);
129     
130         Element JavaDoc itemType = types.createElement("complexType");
131         itemType.setAttribute("name", "mapItem");
132         Element JavaDoc seq2 = types.createElement("sequence");
133         itemType.appendChild(seq2);
134         Element JavaDoc 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 JavaDoc 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