Source code: org/apache/axis/encoding/ser/TimeSerializer.java
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 package org.apache.axis.encoding.ser;
17
18 import java.io.IOException;
19 import java.text.SimpleDateFormat;
20 import java.util.Calendar;
21 import java.util.TimeZone;
22
23 import javax.xml.namespace.QName;
24
25 import org.w3c.dom.Element;
26 import org.xml.sax.Attributes;
27
28 import org.apache.axis.Constants;
29 import org.apache.axis.encoding.SerializationContext;
30 import org.apache.axis.encoding.SimpleValueSerializer;
31 import org.apache.axis.wsdl.fromJava.Types;
32
33 /**
34 * Serializer for Time.
35 * @author Florent Benoit
36 */
37 public class TimeSerializer implements SimpleValueSerializer {
38
39 /**
40 * Parser
41 */
42 private static SimpleDateFormat zulu = new SimpleDateFormat("HH:mm:ss.SSS'Z'");
43
44 // We should always format dates in the GMT timezone
45 static {
46 zulu.setTimeZone(TimeZone.getTimeZone("GMT"));
47 }
48
49
50 /**
51 * Serialize a Time.
52 */
53 public void serialize(QName name, Attributes attributes,
54 Object value, SerializationContext context)
55 throws IOException
56 {
57 context.startElement(name, attributes);
58 context.writeString(getValueAsString(value, context));
59 context.endElement();
60 }
61
62 public String getValueAsString(Object value, SerializationContext context) {
63 StringBuffer buf = new StringBuffer();
64 // Reset year, month, day
65 ((Calendar) value).set(0,0,0);
66 buf.append(zulu.format(((Calendar)value).getTime()));
67 return buf.toString();
68 }
69
70 public String getMechanismType() { return Constants.AXIS_SAX; }
71
72 /**
73 * Return XML schema for the specified type, suitable for insertion into
74 * the <types> element of a WSDL document, or underneath an
75 * <element> or <attribute> declaration.
76 *
77 * @param javaType the Java Class we're writing out schema for
78 * @param types the Java2WSDL Types object which holds the context
79 * for the WSDL being generated.
80 * @return a type element containing a schema simpleType/complexType
81 * @see org.apache.axis.wsdl.fromJava.Types
82 */
83 public Element writeSchema(Class javaType, Types types) throws Exception {
84 return null;
85 }
86 }