Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/axis/encoding/ser/DateSerializer.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  
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;
24  import org.xml.sax.Attributes;
25  
26  import javax.xml.namespace.QName;
27  import java.io.IOException;
28  import java.text.SimpleDateFormat;
29  import java.util.Calendar;
30  import java.util.Date;
31  import java.util.GregorianCalendar;
32  
33  /**
34   * Serializer for Dates.
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 DateSerializer implements SimpleValueSerializer {
41  
42      private static SimpleDateFormat zulu =
43         new SimpleDateFormat("yyyy-MM-dd");
44  
45      private static Calendar calendar = Calendar.getInstance();
46  
47      /**
48       * Serialize a Date.
49       */
50      public void serialize(QName name, Attributes attributes,
51                            Object value, SerializationContext context)
52          throws IOException
53      {
54          context.startElement(name, attributes);
55          context.writeString(getValueAsString(value, context));
56          context.endElement();
57      }
58  
59      public String getValueAsString(Object value, SerializationContext context) {
60          StringBuffer buf = new StringBuffer();
61          synchronized (calendar) {
62              if(value instanceof Calendar) {
63                  value = ((Calendar)value).getTime();
64              } 
65              if (calendar.get(Calendar.ERA) == GregorianCalendar.BC) {
66                  buf.append("-");
67                  calendar.setTime((Date)value);
68                  calendar.set(Calendar.ERA, GregorianCalendar.AD);
69                  value = calendar.getTime();
70              }
71              buf.append(zulu.format((Date)value));
72          }
73          return buf.toString();
74      }
75  
76      public String getMechanismType() { return Constants.AXIS_SAX; }
77  
78      /**
79       * Return XML schema for the specified type, suitable for insertion into
80       * the &lt;types&gt; element of a WSDL document, or underneath an
81       * &lt;element&gt; or &lt;attribute&gt; declaration.
82       *
83       * @param javaType the Java Class we're writing out schema for
84       * @param types the Java2WSDL Types object which holds the context
85       *              for the WSDL being generated.
86       * @return a type element containing a schema simpleType/complexType
87       * @see org.apache.axis.wsdl.fromJava.Types
88       */
89      public Element writeSchema(Class javaType, Types types) throws Exception {
90          return null;
91      }
92  }