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:unsignedLong 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: 6317 $ $Date: 2006-04-25 15:08:23 -0600 (Tue, 25 Apr 2006) $
26   */
27  public final class XSUnsignedLong extends AbstractDigitsFacet {
28  
29    /**
30     * Name of the Java class to be generated.
31     */
32    private static final String JAVA_CLASS_NAME = "java.math.BigInteger";
33  
34    /** Name of this XSType. */
35    public static final String NAME = "unsignedLong";
36  
37    /** Type number of this XSType. */
38    public static final short TYPE = XSType.UNSIGNED_LONG_TYPE;
39  
40    /** The JType represented by this XSType. */
41    private static final JType JTYPE = new JClass(JAVA_CLASS_NAME);
42  
43    /** A constant holding the minimum value an xsd:long can have, 0. */
44    public static final String MIN_VALUE = "0";
45  
46    /**
47     * A constant holding the maximum value an xsd:long can have, 2<sup>64</sup>-1.
48     */
49    public static final String MAX_VALUE = "18446744073709551615";
50  
51    /**
52     * No-arg constructor.
53     */
54    public XSUnsignedLong() {
55      super();
56      setMinInclusive(MIN_VALUE);
57      setMaxInclusive(MAX_VALUE);
58    }
59  
60    public String getName() {
61      return NAME;
62    }
63  
64    public short getType() {
65      return TYPE;
66    }
67  
68    public boolean isPrimitive() {
69      return false;
70    }
71  
72    public boolean isDateTime() {
73      return false;
74    }
75  
76    public JType getJType() {
77      return JTYPE;
78    }
79  
80    public String newInstanceCode() {
81      return "new java.math.BigInteger(\"0\");";
82    }
83  
84    public String createToJavaObjectCode(final String variableName) {
85      return variableName;
86    }
87  
88    public String createFromJavaObjectCode(final String variableName) {
89      return "((java.math.BigInteger) " + variableName + ")";
90    }
91  
92    public void validationCode(final JSourceCode jsc, final String fixedValue,
93        final String validatorInstanceName) {
94      jsc.add("org.exolab.castor.xml.validators.BigIntegerValidator typeValidator;\n"
95          + "typeValidator = new org.exolab.castor.xml.validators.BigIntegerValidator();\n"
96          + "{0}.setValidator(typeValidator);", validatorInstanceName);
97  
98      if (fixedValue != null) {
99        jsc.add("typeValidator.setFixed(new BigInteger(\"" + fixedValue + "\");");
100     }
101 
102     codePatternFacet(jsc, "typeValidator");
103     codeWhiteSpaceFacet(jsc, "typeValidator");
104 
105     if (getMinExclusive() != null) {
106       jsc.add("java.math.BigInteger min = new java.math.BigInteger(\"{0}\");\n"
107           + "typeValidator.setMinExclusive(min);", getMinExclusive());
108     } else if (getMinInclusive() != null) {
109       jsc.add("java.math.BigInteger min = new java.math.BigInteger(\"{0}\");\n"
110           + "typeValidator.setMinInclusive(min);", getMinInclusive());
111     }
112 
113     if (getMaxExclusive() != null) {
114       jsc.add("java.math.BigInteger max = new java.math.BigInteger(\"{0}\");\n"
115           + "typeValidator.setMaxExclusive(max);", getMaxExclusive());
116     } else if (getMaxInclusive() != null) {
117       jsc.add("java.math.BigInteger max = new java.math.BigInteger(\"{0}\");\n"
118           + "typeValidator.setMaxInclusive(max);", getMaxInclusive());
119     }
120 
121     codeDigitsFacet(jsc, "typeValidator");
122   }
123 }