KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > encoding > ser > castor > CastorEnumTypeSerializer


1 /*
2  * Copyright 2002,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.castor;
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
29 import javax.xml.namespace.QName JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.lang.reflect.Method JavaDoc;
32 import java.util.Enumeration JavaDoc;
33
34 /**
35  * Castor serializer
36  *
37  * @author Ozzie Gurkan
38  * @version 1.0
39  */

40 public class CastorEnumTypeSerializer implements Serializer {
41
42     protected static Log log =
43             LogFactory.getLog(CastorEnumTypeSerializer.class.getName());
44
45     /**
46      * Serialize a Castor Enum Type object.
47      *
48      * @param name
49      * @param attributes
50      * @param value this must be a castor object for marshalling
51      * @param context
52      * @throws IOException for XML schema noncompliance, bad object type, and any IO
53      * trouble.
54      */

55     public void serialize(
56             QName JavaDoc name,
57             Attributes JavaDoc attributes,
58             Object JavaDoc value,
59             SerializationContext context)
60             throws IOException JavaDoc {
61         context.startElement(name, attributes);
62
63         try {
64             //get the value of the object
65
Method JavaDoc method = value.getClass().getMethod("toString", new Class JavaDoc[]{});
66             
67             //call the method to return the string
68
String JavaDoc string = (String JavaDoc) method.invoke(value, new Object JavaDoc[]{});
69
70             //write the string
71
context.writeString(string);
72
73         } catch (Exception JavaDoc me) {
74             log.error(Messages.getMessage("exception00"), me);
75             throw new IOException JavaDoc("Castor object error: " + me.getLocalizedMessage());
76         } finally {
77             context.endElement();
78         }
79     }
80
81     public String JavaDoc getMechanismType() {
82         return Constants.AXIS_SAX;
83     }
84
85     /**
86      * Return XML schema for the specified type, suitable for insertion into
87      * the <types> element of a WSDL document, or underneath an
88      * <element> or <attribute> declaration.
89      *
90      * @param javaType the Java Class we're writing out schema for
91      * @param types the Java2WSDL Types object which holds the context
92      * for the WSDL being generated.
93      * @return a type element containing a schema simpleType/complexType
94      * @see org.apache.axis.wsdl.fromJava.Types
95      */

96     public Element JavaDoc writeSchema(Class JavaDoc javaType, Types types) throws Exception JavaDoc {
97         /*
98         <simpleType>
99             <restriction base="xsd:string">
100                 <enumeration value="OK"/>
101                 <enumeration value="ERROR"/>
102                 <enumeration value="WARNING"/>
103             </restriction>
104         </simpleType>
105         */

106         Element JavaDoc simpleType = types.createElement("simpleType");
107         Element JavaDoc restriction = types.createElement("restriction");
108         simpleType.appendChild(restriction);
109         restriction.setAttribute("base", Constants.NS_PREFIX_SCHEMA_XSD + ":string");
110
111         Method JavaDoc enumerateMethod = javaType.getMethod("enumerate", new Class JavaDoc[0]);
112         Enumeration JavaDoc en = (Enumeration JavaDoc) enumerateMethod.invoke(null, new Object JavaDoc[0]);
113         while (en.hasMoreElements()) {
114             Object JavaDoc obj = (Object JavaDoc) en.nextElement();
115             Method JavaDoc toStringMethod = obj.getClass().getMethod("toString", new Class JavaDoc[0]);
116             String JavaDoc value = (String JavaDoc) toStringMethod.invoke(obj, new Object JavaDoc[0]);
117
118             Element JavaDoc enumeration = types.createElement("enumeration");
119             restriction.appendChild(enumeration);
120             enumeration.setAttribute("value", value);
121         }
122
123         return simpleType;
124     }
125 }
126
Popular Tags