KickJava   Java API By Example, From Geeks To Geeks.

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


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.IdentityHashMap;
24 import org.apache.axis.utils.Messages;
25 import org.apache.axis.wsdl.fromJava.Types;
26 import org.apache.commons.logging.Log;
27 import org.w3c.dom.Element JavaDoc;
28 import org.xml.sax.Attributes 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.Vector JavaDoc;
34
35 /**
36  * A <code>VectorSerializer</code> is be used to serialize and
37  * deserialize Vectors using the <code>SOAP-ENC</code>
38  * encoding style.<p>
39  *
40  * @author Rich Scheuerle (scheu@us.ibm.com)
41  */

42
43 public class VectorSerializer implements Serializer
44 {
45     protected static Log log =
46         LogFactory.getLog(VectorSerializer.class.getName());
47
48     /** Serialize a Vector
49      *
50      * Walk the collection of keys, serializing each key/value pair
51      * inside an <item> element.
52      *
53      * @param name the desired QName for the element
54      * @param attributes the desired attributes for the element
55      * @param value the Object to serialize
56      * @param context the SerializationContext in which to do all this
57      * @exception IOException
58      */

59     public void serialize(QName JavaDoc name, Attributes JavaDoc attributes,
60                           Object JavaDoc value, SerializationContext context)
61         throws IOException JavaDoc
62     {
63         if (!(value instanceof Vector JavaDoc))
64             throw new IOException JavaDoc(
65                 Messages.getMessage("noVector00", "VectorSerializer",
66                                      value.getClass().getName()));
67
68         Vector JavaDoc vector = (Vector JavaDoc)value;
69         
70         // Check for circular references.
71
if(isRecursive(new IdentityHashMap(), vector)){
72             throw new IOException JavaDoc(Messages.getMessage("badVector00"));
73         }
74         
75         context.startElement(name, attributes);
76         for (Iterator JavaDoc i = vector.iterator(); i.hasNext(); )
77         {
78             Object JavaDoc item = i.next();
79             context.serialize(Constants.QNAME_LITERAL_ITEM, null, item);
80         }
81         context.endElement();
82     }
83
84     public boolean isRecursive(IdentityHashMap map, Vector JavaDoc vector)
85     {
86         map.add(vector);
87         boolean recursive = false;
88         for(int i=0;i<vector.size() && !recursive;i++)
89         {
90             Object JavaDoc o = vector.get(i);
91             if(o instanceof Vector JavaDoc) {
92                 if(map.containsKey(o)) {
93                     return true;
94                 } else {
95                     recursive = isRecursive(map, (Vector JavaDoc)o);
96                 }
97             }
98         }
99         return recursive;
100     }
101
102     public String JavaDoc getMechanismType() { return Constants.AXIS_SAX; }
103
104     /**
105      * Return XML schema for the specified type, suitable for insertion into
106      * the &lt;types&gt; element of a WSDL document, or underneath an
107      * &lt;element&gt; or &lt;attribute&gt; declaration.
108      *
109      * @param javaType the Java Class we're writing out schema for
110      * @param types the Java2WSDL Types object which holds the context
111      * for the WSDL being generated.
112      * @return a type element containing a schema simpleType/complexType
113      * @see org.apache.axis.wsdl.fromJava.Types
114      */

115     public Element JavaDoc writeSchema(Class JavaDoc javaType, Types types) throws Exception JavaDoc {
116         Element JavaDoc complexType = types.createElement("complexType");
117         complexType.setAttribute("name", "Vector");
118         types.writeSchemaTypeDecl(Constants.SOAP_VECTOR, complexType);
119         Element JavaDoc seq = types.createElement("sequence");
120         complexType.appendChild(seq);
121
122         Element JavaDoc element = types.createElement("element");
123         element.setAttribute("name", "item");
124         element.setAttribute("minOccurs", "0");
125         element.setAttribute("maxOccurs", "unbounded");
126         element.setAttribute("type", "xsd:anyType");
127         seq.appendChild(element);
128
129         return complexType;
130     }
131 }
132
Popular Tags