KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > generator > types > DateTimeSG


1 /*
2  * Copyright 2003, 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.ws.jaxme.generator.types;
18
19 import java.text.ParsePosition JavaDoc;
20 import java.util.Calendar JavaDoc;
21 import java.util.Date JavaDoc;
22
23 import org.apache.ws.jaxme.generator.sg.SGFactory;
24 import org.apache.ws.jaxme.generator.sg.SGlet;
25 import org.apache.ws.jaxme.generator.sg.SchemaSG;
26 import org.apache.ws.jaxme.generator.sg.SimpleTypeSG;
27 import org.apache.ws.jaxme.impl.DatatypeConverterImpl;
28 import org.apache.ws.jaxme.impl.JMUnmarshallerImpl;
29 import org.apache.ws.jaxme.js.DirectAccessible;
30 import org.apache.ws.jaxme.js.JavaMethod;
31 import org.apache.ws.jaxme.js.JavaQName;
32 import org.apache.ws.jaxme.js.JavaQNameImpl;
33 import org.apache.ws.jaxme.js.JavaSource;
34 import org.apache.ws.jaxme.js.LocalJavaField;
35 import org.apache.ws.jaxme.js.TypedValue;
36 import org.apache.ws.jaxme.js.impl.TypedValueImpl;
37 import org.apache.ws.jaxme.xs.XSType;
38 import org.apache.ws.jaxme.xs.parser.impl.LocSAXException;
39 import org.apache.ws.jaxme.xs.util.XsDateTimeFormat;
40 import org.xml.sax.SAXException JavaDoc;
41
42 /**
43  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
44  */

45 public class DateTimeSG extends AtomicTypeSGImpl {
46     public static final JavaQName CALENDAR_TYPE = JavaQNameImpl.getInstance(Calendar JavaDoc.class);
47     private static final JavaQName OBJECT_TYPE = JavaQNameImpl.getInstance(Object JavaDoc.class);
48     
49     /** <p>Creates a new instance of DurationSG.</p>
50      */

51     public DateTimeSG(SGFactory pFactory, SchemaSG pSchema, XSType pType) throws SAXException {
52         super(pFactory, pSchema, pType);
53     }
54
55     protected String JavaDoc getDatatypeName() { return "DateTime"; }
56     protected JavaQName getDatatypeType() { return CALENDAR_TYPE; }
57     protected Class JavaDoc getFormatClass() { return XsDateTimeFormat.class; }
58     public JavaQName getRuntimeType(SimpleTypeSG pController) { return CALENDAR_TYPE; }
59
60     public TypedValue getCastFromString(SimpleTypeSG pController, String JavaDoc pValue)
61             throws SAXException {
62         try {
63             Calendar JavaDoc calendar = new DatatypeConverterImpl().parseDate(pValue);
64             return new TypedValueImpl(
65                     new Object JavaDoc[] { "new java.util.GregorianCalendar("
66                             + calendar.get(Calendar.YEAR) + ","
67                             + calendar.get(Calendar.MONTH) + ","
68                             + calendar.get(Calendar.DAY_OF_MONTH) + ")" },
69                     getDatatypeType());
70         } catch (RuntimeException JavaDoc e) {
71             try {
72                 throw new LocSAXException("Failed to convert string value to "
73                         + getDatatypeName() + " instance: " + pValue, getLocator());
74             } catch (Exception JavaDoc e1) {
75                 throw new SAXException("Failed to convert string value to "
76                         + getDatatypeName() + " instance: " + pValue);
77             }
78         }
79     }
80
81     public TypedValue getCastFromString(SimpleTypeSG pController, JavaMethod pMethod, Object JavaDoc pValue, Object JavaDoc pData) throws SAXException {
82         boolean mayBeDate;
83         Object JavaDoc format;
84         if (pData == null) {
85             format = new Object JavaDoc[]{"new ", getFormatClass(), "()"};
86             mayBeDate = false;
87         } else {
88             format = new Object JavaDoc[]{"((", JMUnmarshallerImpl.class, ") ", pData,
89                                   ".getJMUnmarshaller()).get" + getDatatypeName(), "Format()"};
90             mayBeDate = true;
91         }
92         if (!(pValue instanceof DirectAccessible)) {
93             LocalJavaField v = pMethod.newJavaField(String JavaDoc.class);
94             v.addLine(pValue);
95             pValue = v;
96         }
97         LocalJavaField pos = pMethod.newJavaField(ParsePosition JavaDoc.class);
98         pos.addLine("new ", ParsePosition JavaDoc.class, "(0)");
99         LocalJavaField cal = pMethod.newJavaField(mayBeDate ? OBJECT_TYPE : pController.getRuntimeType());
100         cal.addLine(format, ".parseObject(", pValue, ", ", pos, ");");
101         pMethod.addIf(cal, " == null");
102         pMethod.addThrowNew(IllegalArgumentException JavaDoc.class,
103                             JavaSource.getQuoted("Failed to parse dateTime "),
104                             " + ", pValue, " + ", JavaSource.getQuoted(" at: "),
105                             " + ", pValue, ".substring(", pos, ".getErrorIndex())");
106         pMethod.addEndIf();
107         if (mayBeDate) {
108             LocalJavaField result = pMethod.newJavaField(pController.getRuntimeType());
109             pMethod.addIf(cal, " instanceof ", Calendar JavaDoc.class);
110             pMethod.addLine(result, " = (", Calendar JavaDoc.class, ") ", cal, ";");
111             pMethod.addElse();
112             pMethod.addLine(result, " = ", Calendar JavaDoc.class, ".getInstance();");
113             pMethod.addLine(result, ".setTime((", Date JavaDoc.class, ") ", cal, ");");
114             pMethod.addEndIf();
115             return result;
116         } else {
117             return cal;
118         }
119     }
120
121   public void forAllNonNullValues(SimpleTypeSG pController, JavaMethod pMethod, Object JavaDoc pValue, SGlet pSGlet) throws SAXException {
122     LocalJavaField f = pMethod.newJavaField(CALENDAR_TYPE);
123     f.addLine(pValue);
124     pMethod.addIf(f, " != null");
125     pSGlet.generate(pMethod, pValue);
126     pMethod.addEndIf();
127   }
128
129   public void forAllValues(SimpleTypeSG pController, JavaMethod pMethod, Object JavaDoc pValue, SGlet pSGlet) throws SAXException {
130     pSGlet.generate(pMethod, pValue);
131   }
132 }
133
Popular Tags