KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

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 JavaDoc;
65 import org.xml.sax.Attributes JavaDoc;
66 import org.xml.sax.helpers.AttributesImpl JavaDoc;
67
68 import javax.xml.namespace.QName JavaDoc;
69 import java.io.IOException JavaDoc;
70 import java.util.Iterator JavaDoc;
71 import java.util.Map JavaDoc;
72
73 /**
74  * A <code>MapSerializer</code> is be used to serialize and
75  * deserialize Maps using the <code>SOAP-ENC</code>
76  * encoding style.<p>
77  *
78  * @author Glen Daniels (gdaniels@macromedia.com)
79  * Modified by @author Rich Scheuerle (scheu@us.ibm.com)
80  */

81
82 public class MapSerializer implements Serializer
83 {
84    private static Logger log = Logger.getLogger(MapSerializer.class.getName());
85
86    // QNames we deal with
87
private static final QName JavaDoc QNAME_KEY = new QName JavaDoc("", "key");
88    private static final QName JavaDoc QNAME_ITEM = new QName JavaDoc("", "item");
89    private static final QName JavaDoc QNAME_VALUE = new QName JavaDoc("", "value");
90
91    /**
92     * Serialize a Map
93     * <p/>
94     * Walk the collection of keys, serializing each key/value pair
95     * inside an <item> element.
96     *
97     * @param name the desired QName for the element
98     * @param attributes the desired attributes for the element
99     * @param value the Object to serialize
100     * @param context the SerializationContext in which to do all this
101     * @throws IOException
102     */

103    public void serialize(QName JavaDoc name, Attributes JavaDoc attributes,
104                          Object JavaDoc value, SerializationContext context)
105            throws IOException JavaDoc
106    {
107       if (!(value instanceof Map JavaDoc))
108          throw new IOException JavaDoc(Messages.getMessage("noMap00", "MapSerializer", value.getClass().getName()));
109
110       Map JavaDoc map = (Map JavaDoc)value;
111
112       context.startElement(name, attributes);
113
114       AttributesImpl JavaDoc itemsAttributes = new AttributesImpl JavaDoc();
115       String JavaDoc encodingURI = context.getMessageContext().getSOAPConstants().getEncodingURI();
116       String JavaDoc encodingPrefix = context.getPrefixForURI(encodingURI);
117       String JavaDoc 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 JavaDoc i = map.entrySet().iterator(); i.hasNext();)
124       {
125          Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
126          Object JavaDoc key = entry.getKey();
127          Object JavaDoc 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 JavaDoc getMechanismType()
141    {
142       return Constants.AXIS_SAX;
143    }
144
145    /**
146     * Return XML schema for the specified type, suitable for insertion into
147     * the &lt;types&gt; element of a WSDL document, or underneath an
148     * &lt;element&gt; or &lt;attribute&gt; declaration.
149     *
150     * @param javaType the Java Class we're writing out schema for
151     * @param types the Java2WSDL Types object which holds the context
152     * for the WSDL being generated.
153     * @return a type element containing a schema simpleType/complexType
154     * @see org.jboss.axis.wsdl.fromJava.Types
155     */

156    public Element JavaDoc writeSchema(Class JavaDoc javaType, Types types) throws Exception JavaDoc
157    {
158       Element JavaDoc complexType = types.createElement("complexType");
159       complexType.setAttribute("name", "Map");
160       Element JavaDoc seq = types.createElement("sequence");
161       complexType.appendChild(seq);
162       Element JavaDoc 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 JavaDoc(Constants.NS_URI_XMLSOAP, "mapItem")));
167       seq.appendChild(element);
168
169       Element JavaDoc itemType = types.createElement("complexType");
170       itemType.setAttribute("name", "mapItem");
171       Element JavaDoc seq2 = types.createElement("sequence");
172       itemType.appendChild(seq2);
173       Element JavaDoc 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 JavaDoc 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