KickJava   Java API By Example, From Geeks To Geeks.

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


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.components.logger.LogFactory;
20 import org.apache.axis.encoding.SerializationContext;
21 import org.apache.axis.utils.Messages;
22 import org.apache.axis.wsdl.fromJava.Types;
23 import org.apache.commons.logging.Log;
24 import org.w3c.dom.Element JavaDoc;
25 import org.xml.sax.Attributes JavaDoc;
26
27 import javax.xml.namespace.QName JavaDoc;
28 import java.io.IOException JavaDoc;
29
30 /**
31  * Serializer for a JAX-RPC enum.
32  *
33  * @author Rich Scheuerle <scheu@us.ibm.com>
34  * @author Sam Ruby <rubys@us.ibm.com>
35  */

36 public class EnumSerializer extends SimpleSerializer
37 {
38     protected static Log log =
39         LogFactory.getLog(EnumSerializer.class.getName());
40
41     private java.lang.reflect.Method JavaDoc toStringMethod = null;
42
43     public EnumSerializer(Class JavaDoc javaType, QName JavaDoc xmlType) {
44         super(javaType, xmlType);
45     }
46
47     /**
48      * Serialize an enumeration
49      */

50     public void serialize(QName JavaDoc name, Attributes JavaDoc attributes,
51                           Object JavaDoc value, SerializationContext context)
52         throws IOException JavaDoc
53     {
54         context.startElement(name, attributes);
55         context.writeString(getValueAsString(value, context));
56         context.endElement();
57     }
58
59     public String JavaDoc getValueAsString(Object JavaDoc value, SerializationContext context) {
60         // Invoke the toString method on the enumeration class and
61
// write out the result as a string.
62
try {
63             if (toStringMethod == null) {
64                 toStringMethod = javaType.getMethod("toString", null);
65             }
66             return (String JavaDoc) toStringMethod.invoke(value, null);
67         } catch (Exception JavaDoc e) {
68             log.error(Messages.getMessage("exception00"), e);
69         }
70         return null;
71     }
72
73     /**
74      * Return XML schema for the specified type, suitable for insertion into
75      * the &lt;types&gt; element of a WSDL document, or underneath an
76      * &lt;element&gt; or &lt;attribute&gt; declaration.
77      *
78      * @param javaType the Java Class we're writing out schema for
79      * @param types the Java2WSDL Types object which holds the context
80      * for the WSDL being generated.
81      * @return a type element containing a schema simpleType/complexType
82      * @see org.apache.axis.wsdl.fromJava.Types
83      */

84     public Element JavaDoc writeSchema(Class JavaDoc javaType, Types types) throws Exception JavaDoc {
85         // Use Types helper method.
86
return types.writeEnumType(xmlType, javaType);
87     }
88 }
89
Popular Tags