KickJava   Java API By Example, From Geeks To Geeks.

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


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.encoding.SerializationContext;
21 import org.apache.axis.encoding.SimpleValueSerializer;
22 import org.apache.axis.wsdl.fromJava.Types;
23 import org.w3c.dom.Element JavaDoc;
24 import org.xml.sax.Attributes JavaDoc;
25
26 import javax.xml.namespace.QName JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.text.SimpleDateFormat JavaDoc;
29 import java.util.Calendar JavaDoc;
30 import java.util.Date JavaDoc;
31 import java.util.TimeZone JavaDoc;
32
33 /**
34  * Serializer for dateTime (Calendar).
35  *
36  * @author Sam Ruby <rubys@us.ibm.com>
37  * Modified by @author Rich scheuerle <scheu@us.ibm.com>
38  * @see <a HREF="http://www.w3.org/TR/xmlschema-2/#dateTime">XML Schema 3.2.16</a>
39  */

40 public class CalendarSerializer implements SimpleValueSerializer {
41
42     private static SimpleDateFormat JavaDoc zulu =
43        new SimpleDateFormat JavaDoc("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
44                          // 0123456789 0 123456789
45

46     static {
47         zulu.setTimeZone(TimeZone.getTimeZone("GMT"));
48     }
49
50     /**
51      * Serialize a Date.
52      */

53     public void serialize(QName JavaDoc name, Attributes JavaDoc attributes,
54                           Object JavaDoc value, SerializationContext context)
55         throws IOException JavaDoc
56     {
57         context.startElement(name, attributes);
58         context.writeString(getValueAsString(value, context));
59         context.endElement();
60     }
61
62     public String JavaDoc getValueAsString(Object JavaDoc value, SerializationContext context) {
63         Date JavaDoc date = value instanceof Date JavaDoc ? (Date JavaDoc) value :
64                 ((Calendar JavaDoc) value).getTime();
65
66         // Serialize including convert to GMT
67
synchronized (zulu) {
68             // Sun JDK bug http://developer.java.sun.com/developer/bugParade/bugs/4229798.html
69
return zulu.format(date);
70         }
71     }
72
73     public String JavaDoc getMechanismType() { return Constants.AXIS_SAX; }
74
75     /**
76      * Return XML schema for the specified type, suitable for insertion into
77      * the &lt;types&gt; element of a WSDL document, or underneath an
78      * &lt;element&gt; or &lt;attribute&gt; declaration.
79      *
80      * @param javaType the Java Class we're writing out schema for
81      * @param types the Java2WSDL Types object which holds the context
82      * for the WSDL being generated.
83      * @return a type element containing a schema simpleType/complexType
84      * @see org.apache.axis.wsdl.fromJava.Types
85      */

86     public Element JavaDoc writeSchema(Class JavaDoc javaType, Types types) throws Exception JavaDoc {
87         return null;
88     }
89 }
90
Popular Tags