View Javadoc
1   /*
2    * Copyright 2007 Andrew Fawcett, 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:decimal XML Schema datatype.
22   * 
23   * @author <a href="mailto:andrew DOT fawcett AT coda DOT com">Andrew Fawcett</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 XSDecimal extends AbstractDigitsFacet {
28    // --------------------------------------------------------------------------
29  
30    /** Name of this XSType. */
31    public static final String NAME = "decimal";
32  
33    /** Type number of this XSType. */
34    public static final short TYPE = XSType.DECIMAL_TYPE;
35  
36    /** The JType represented by this XSType. */
37    private static final JType JTYPE = new JClass("java.math.BigDecimal");
38  
39    // --------------------------------------------------------------------------
40  
41    /**
42     * No-arg constructor.
43     */
44    public XSDecimal() {
45      super(false);
46    }
47  
48    // --------------------------------------------------------------------------
49  
50    /**
51     * {@inheritDoc}
52     */
53    public String getName() {
54      return NAME;
55    }
56  
57    /**
58     * {@inheritDoc}
59     */
60    public short getType() {
61      return TYPE;
62    }
63  
64    /**
65     * {@inheritDoc}
66     */
67    public boolean isPrimitive() {
68      return false;
69    }
70  
71    /**
72     * {@inheritDoc}
73     */
74    public boolean isDateTime() {
75      return false;
76    }
77  
78    /**
79     * {@inheritDoc}
80     */
81    public JType getJType() {
82      return JTYPE;
83    }
84  
85    /**
86     * {@inheritDoc}
87     */
88    public String newInstanceCode() {
89      return "new java.math.BigDecimal(0);";
90    }
91  
92    /**
93     * {@inheritDoc}
94     */
95    public String createToJavaObjectCode(final String variableName) {
96      return variableName;
97    }
98  
99    /**
100    * {@inheritDoc}
101    */
102   public String createFromJavaObjectCode(final String variableName) {
103     return "(java.math.BigDecimal) " + variableName;
104   }
105 
106   // --------------------------------------------------------------------------
107 
108   /**
109    * {@inheritDoc}
110    */
111   public void validationCode(final JSourceCode jsc, final String fixedValue,
112       final String validatorInstanceName) {
113     jsc.add("org.exolab.castor.xml.validators.DecimalValidator typeValidator;\n"
114         + "typeValidator = new org.exolab.castor.xml.validators.DecimalValidator();\n"
115         + "{0}.setValidator(typeValidator);", validatorInstanceName);
116 
117     if (fixedValue != null) {
118       jsc.add("typeValidator.setFixed(" + fixedValue + ");");
119     }
120 
121     codePatternFacet(jsc, "typeValidator");
122     codeWhiteSpaceFacet(jsc, "typeValidator");
123 
124     if (getMinExclusive() != null) {
125       jsc.add("java.math.BigDecimal min = new java.math.BigDecimal(\"{0}\");\n"
126           + "typeValidator.setMinExclusive(min);", getMinExclusive());
127     } else if (getMinInclusive() != null) {
128       jsc.add("java.math.BigDecimal min = new java.math.BigDecimal(\"{0}\");\n"
129           + "typeValidator.setMinInclusive(min);", getMinInclusive());
130     }
131 
132     if (getMaxExclusive() != null) {
133       jsc.add("java.math.BigDecimal max = new java.math.BigDecimal(\"{0}\");\n"
134           + "typeValidator.setMaxExclusive(max);", getMaxExclusive());
135     } else if (getMaxInclusive() != null) {
136       jsc.add("java.math.BigDecimal max = new java.math.BigDecimal(\"{0}\");\n"
137           + "typeValidator.setMaxInclusive(max);", getMaxInclusive());
138     }
139 
140     codeDigitsFacet(jsc, "typeValidator");
141   }
142 
143   // --------------------------------------------------------------------------
144 }