View Javadoc
1   /*
2    * Copyright 2007 Arnaud Blandin, 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:gDay XML Schema type.
22   * 
23   * @author <a href="mailto:blandin AT intalio DOT com">Arnaud Blandin</a>
24   * @author <a href="mailto:ralf DOT joachim AT syscon DOT eu">Ralf Joachim</a>
25   * @version $Revision $ $Date: 2005-03-05 06:42:06 -0700 (Sat, 05 Mar 2005) $
26   */
27  public final class XSGDay extends AbstractRangeFacet {
28    // --------------------------------------------------------------------------
29  
30    /** Name of this XSType. */
31    public static final String NAME = "gDay";
32  
33    /** Type number of this XSType. */
34    public static final short TYPE = XSType.GDAY_TYPE;
35  
36    /** The JType represented by this XSType. */
37    private static final JType JTYPE = new JClass("org.exolab.castor.types.GDay");
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 " + getJType().getName() + "();";
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 "(" + getJType().getName() + ") " + 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     if (fixedValue != null) {
109       jsc.add("try {\n" + " typeValidator.setFixed({0});\n"
110           + "} catch (java.text.ParseException pe) {\n"
111           + " System.out.println(\"ParseException\" + pe);\n" + "}", fixedValue);
112     }
113 
114     codePatternFacet(jsc, "typeValidator");
115     codeWhiteSpaceFacet(jsc, "typeValidator");
116 
117     if (hasMinimum() || hasMaximum()) {
118       jsc.add("try {");
119 
120       // minInclusive / minExclusive facets (only one or the other, never both)
121       if (getMinInclusive() != null) {
122         jsc.add(" org.exolab.castor.types.GDay min;\n"
123             + " min = org.exolab.castor.types.GDay.parseGDay(\"{0}\");\n"
124             + " typeValidator.setMinInclusive(min);", getMinInclusive());
125       } else if (getMinExclusive() != null) {
126         jsc.add(" org.exolab.castor.types.GDay min;\n"
127             + " min = org.exolab.castor.types.GDay.parseGDay(\"{0}\");\n"
128             + " typeValidator.setMinExclusive(min);", getMinExclusive());
129       }
130 
131       // maxInclusive / maxExclusive facets (only one or the other, never both)
132       if (getMaxInclusive() != null) {
133         jsc.add(" org.exolab.castor.types.GDay max;\n"
134             + " max = org.exolab.castor.types.GDay.parseGDay(\"{0}\");\n"
135             + " typeValidator.setMaxInclusive(max);", getMaxInclusive());
136       } else if (getMaxExclusive() != null) {
137         jsc.add(" org.exolab.castor.types.GDay max;\n"
138             + " max = org.exolab.castor.types.GDay.parseGDay(\"{0}\");\n"
139             + " typeValidator.setMaxExclusive(max);", getMaxExclusive());
140       }
141 
142       jsc.add("} catch (java.text.ParseException pe) {\n"
143           + " System.out.println(\"ParseException\" + pe);\n" + "}", "");
144     }
145   }
146 
147   // --------------------------------------------------------------------------
148 }