View Javadoc
1   /*
2    * Copyright 2007 Keith Visco, Ralf Joachim
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    * 
7    * http://www.apache.org/licenses/LICENSE-2.0
8    * 
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  package org.exolab.castor.builder.types;
15  
16  import org.exolab.javasource.JClass;
17  import org.exolab.javasource.JSourceCode;
18  import org.exolab.javasource.JType;
19  
20  /**
21   * The xsd:dateTime XML Schema type.
22   * 
23   * @author <a href="mailto:keith AT kvisco DOT com">Keith Visco</a>
24   * @author <a href="mailto:ralf DOT joachim AT syscon DOT eu">Ralf Joachim</a>
25   * @version $Revision$ $Date: 2005-12-13 14:58:48 -0700 (Tue, 13 Dec 2005) $
26   */
27  public final class XSDateTime extends AbstractRangeFacet {
28    // --------------------------------------------------------------------------
29  
30    /** Name of this XSType. */
31    public static final String NAME = "dateTime";
32  
33    /** Type number of this XSType. */
34    public static final short TYPE = XSType.DATETIME_TYPE;
35  
36    /** The JType represented by this XSType. */
37    private static final JType JTYPE = new JClass("java.util.Date");
38  
39    // --------------------------------------------------------------------------
40  
41    /**
42     * {@inheritDoc}
43     */
44    public String getName() {
45      return NAME;
46    }
47  
48    /**
49     * {@inheritDoc}
50     */
51    public short getType() {
52      return TYPE;
53    }
54  
55    /**
56     * {@inheritDoc}
57     */
58    public boolean isPrimitive() {
59      return false;
60    }
61  
62    /**
63     * {@inheritDoc}
64     */
65    public boolean isDateTime() {
66      return true;
67    }
68  
69    /**
70     * {@inheritDoc}
71     */
72    public JType getJType() {
73      return JTYPE;
74    }
75  
76    /**
77     * {@inheritDoc}
78     */
79    public String newInstanceCode() {
80      return "new java.util.Date();";
81    }
82  
83    /**
84     * {@inheritDoc}
85     */
86    public String createToJavaObjectCode(final String variableName) {
87      return variableName;
88    }
89  
90    /**
91     * {@inheritDoc}
92     */
93    public String createFromJavaObjectCode(final String variableName) {
94      return "(java.util.Date) " + variableName;
95    }
96  
97    // --------------------------------------------------------------------------
98  
99    /**
100    * {@inheritDoc}
101    */
102   public void validationCode(final JSourceCode jsc, final String fixedValue,
103       final String validatorInstanceName) {
104     jsc.add("org.exolab.castor.xml.validators.DateTimeValidator typeValidator;\n"
105         + "typeValidator = new org.exolab.castor.xml.validators.DateTimeValidator();\n"
106         + "{0}.setValidator(typeValidator);", validatorInstanceName);
107 
108     // // TODO We can't validate on the fixed value as long as Castor treats DateTime as
109     // // java.util.Date because in the process any time zone information is discarded
110     // // and comparisons will fail.
111     // if (fixedValue != null) {
112     // jsc.add("try {\n"
113     // + " typeValidator.setFixed({0});\n"
114     // + "} catch (java.text.ParseException pe) {\n"
115     // + " System.out.println(\"ParseException\" + pe);\n"
116     // + "}", fixedValue.replaceFirst(".toDate\\(\\)", ""));
117     // }
118 
119     codePatternFacet(jsc, "typeValidator");
120     codeWhiteSpaceFacet(jsc, "typeValidator");
121 
122     if (hasMinimum() || hasMaximum()) {
123       jsc.add("try {");
124 
125       // minInclusive / minExclusive facets (only one or the other, never both)
126       if (getMinInclusive() != null) {
127         jsc.add(" org.exolab.castor.types.DateTime min;\n"
128             + " min = new org.exolab.castor.types.DateTime(\"{0}\");\n"
129             + " typeValidator.setMinInclusive(min);", getMinInclusive());
130       } else if (getMinExclusive() != null) {
131         jsc.add(" org.exolab.castor.types.DateTime min;\n"
132             + " min = new org.exolab.castor.types.DateTime(\"{0}\");\n"
133             + " typeValidator.setMinExclusive(min);", getMinExclusive());
134       }
135 
136       // maxInclusive / maxExclusive facets (only one or the other, never both)
137       if (getMaxInclusive() != null) {
138         jsc.add(" org.exolab.castor.types.DateTime max;\n"
139             + " max = new org.exolab.castor.types.DateTime(\"{0}\");\n"
140             + " typeValidator.setMaxInclusive(max);", getMaxInclusive());
141       } else if (getMaxExclusive() != null) {
142         jsc.add(" org.exolab.castor.types.DateTime max;\n"
143             + " max = new org.exolab.castor.types.DateTime(\"{0}\");\n"
144             + " typeValidator.setMaxExclusive(max);", getMaxExclusive());
145       }
146 
147       jsc.add("} catch (java.text.ParseException pe) {\n"
148           + " System.out.println(\"ParseException\" + pe);\n" + "}", "");
149     }
150   }
151 
152   // --------------------------------------------------------------------------
153 }