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