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:integer 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: 2006-04-25 15:08:23 -0600 (Tue, 25 Apr 2006) $
26   */
27  public final class XSInteger extends AbstractDigitsFacet {
28    // --------------------------------------------------------------------------
29  
30    /** Name of this XSType. */
31    public static final String NAME = "integer";
32  
33    /** Type number of this XSType. */
34    public static final short TYPE = XSType.INTEGER_TYPE;
35  
36    // --------------------------------------------------------------------------
37  
38    /** True if this type is implemented using the wrapper class. */
39    private final boolean _asWrapper;
40  
41    /** The JType represented by this XSType. */
42    private final JType _jType;
43  
44    // --------------------------------------------------------------------------
45  
46    /**
47     * No-arg constructor.
48     */
49    public XSInteger() {
50      this(false);
51    }
52  
53    /**
54     * Constructs a new XSInteger.
55     * 
56     * @param asWrapper If true, use the java.lang wrapper class.
57     */
58    public XSInteger(final boolean asWrapper) {
59      super();
60  
61      _asWrapper = asWrapper;
62      if (_asWrapper) {
63        _jType = new JClass("java.lang.Long");
64      } else {
65        _jType = JType.LONG;
66      }
67    }
68  
69    // --------------------------------------------------------------------------
70  
71    /**
72     * {@inheritDoc}
73     */
74    public String getName() {
75      return NAME;
76    }
77  
78    /**
79     * {@inheritDoc}
80     */
81    public short getType() {
82      return TYPE;
83    }
84  
85    /**
86     * {@inheritDoc}
87     */
88    public boolean isPrimitive() {
89      return true;
90    }
91  
92    /**
93     * {@inheritDoc}
94     */
95    public boolean isDateTime() {
96      return false;
97    }
98  
99    /**
100    * {@inheritDoc}
101    */
102   public JType getJType() {
103     return _jType;
104   }
105 
106   /**
107    * {@inheritDoc}
108    */
109   public String newInstanceCode() {
110     return "new java.lang.Long(0);";
111   }
112 
113   /**
114    * {@inheritDoc}
115    */
116   public String createDefaultValueWithString(final String variableName) {
117     if (_asWrapper) {
118       return "new java.lang.Long(" + variableName + ")";
119     }
120     return "new java.lang.Long(" + variableName + ").longValue()";
121   }
122 
123   /**
124    * {@inheritDoc}
125    */
126   public String createToJavaObjectCode(final String variableName) {
127     if (_asWrapper) {
128       return variableName;
129     }
130     return "new java.lang.Long(" + variableName + ")";
131   }
132 
133   /**
134    * {@inheritDoc}
135    */
136   public String createFromJavaObjectCode(final String variableName) {
137     if (_asWrapper) {
138       return "((java.lang.Long) " + variableName + ")";
139     }
140     return "((java.lang.Long) " + variableName + ").longValue()";
141   }
142 
143   // --------------------------------------------------------------------------
144 
145   /**
146    * {@inheritDoc}
147    */
148   public void validationCode(final JSourceCode jsc, final String fixedValue,
149       final String validatorInstanceName) {
150     jsc.add("org.exolab.castor.xml.validators.LongValidator typeValidator;\n"
151         + "typeValidator = new org.exolab.castor.xml.validators.LongValidator();\n"
152         + "{0}.setValidator(typeValidator);", validatorInstanceName);
153 
154     if (fixedValue != null) {
155       jsc.add("typeValidator.setFixed(" + fixedValue + ");");
156     }
157 
158     codePatternFacet(jsc, "typeValidator");
159     codeWhiteSpaceFacet(jsc, "typeValidator");
160 
161     if (getMinExclusive() != null) {
162       jsc.add("typeValidator.setMinExclusive(" + getMinExclusive() + "L);");
163     } else if (getMinInclusive() != null) {
164       jsc.add("typeValidator.setMinInclusive(" + getMinInclusive() + "L);");
165     }
166 
167     if (getMaxExclusive() != null) {
168       jsc.add("typeValidator.setMaxExclusive(" + getMaxExclusive() + "L);");
169     } else if (getMaxInclusive() != null) {
170       jsc.add("typeValidator.setMaxInclusive(" + getMaxInclusive() + "L);");
171     }
172 
173     codeDigitsFacet(jsc, "typeValidator");
174   }
175 
176   // --------------------------------------------------------------------------
177 }