View Javadoc
1   /*
2    * Copyright 2005-2007 Werner Guttmann, 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:unsignedByte XML Schema type.
22   *
23   * @author <a href="mailto:werner DOT guttmann AT gmx DOT net">Werner Guttmann</a>
24   * @author <a href="mailto:ralf DOT joachim AT syscon DOT eu">Ralf Joachim</a>
25   * @version $Revision: 5951 $ $Date: 2005-12-13 14:58:48 -0700 (Tue, 13 Dec 2005) $
26   */
27  public final class XSUnsignedByte extends AbstractDigitsFacet {
28    // --------------------------------------------------------------------------
29  
30    /** Name of this XSType. */
31    public static final String NAME = "unsignedByte";
32  
33    /** Type number of this XSType. */
34    public static final short TYPE = XSType.UNSIGNED_BYTE_TYPE;
35  
36    /** A constant holding the minimum value an xsd:unsignedByte can have, 0. */
37    public static final String MIN_VALUE = "0";
38  
39    /** A constant holding the maximum value an xsd:unsignedByte can have, 255. */
40    public static final String MAX_VALUE = "255";
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 XSUnsignedByte() {
56      this(false);
57    }
58  
59    /**
60     * Constructs a new XSUnsignedByte.
61     * 
62     * @param asWrapper if true, use the java.lang wrapper class.
63     */
64    public XSUnsignedByte(final boolean asWrapper) {
65      super();
66  
67      _asWrapper = asWrapper;
68      if (asWrapper) {
69        _jType = new JClass("java.lang.Short");
70      } else {
71        _jType = JType.SHORT;
72      }
73  
74      setMinInclusive(MIN_VALUE);
75      setMaxInclusive(MAX_VALUE);
76    }
77  
78    // --------------------------------------------------------------------------
79  
80    /**
81     * {@inheritDoc}
82     */
83    public String getName() {
84      return NAME;
85    }
86  
87    /**
88     * {@inheritDoc}
89     */
90    public short getType() {
91      return TYPE;
92    }
93  
94    /**
95     * {@inheritDoc}
96     */
97    public boolean isPrimitive() {
98      return true;
99    }
100 
101   /**
102    * {@inheritDoc}
103    */
104   public boolean isDateTime() {
105     return false;
106   }
107 
108   /**
109    * {@inheritDoc}
110    */
111   public JType getJType() {
112     return _jType;
113   }
114 
115   /**
116    * {@inheritDoc}
117    */
118   public String newInstanceCode() {
119     return "new java.lang.Short((short) 0);";
120   }
121 
122   /**
123    * {@inheritDoc}
124    */
125   public String createDefaultValueWithString(final String variableName) {
126     if (_asWrapper) {
127       return "new java.lang.Short(" + variableName + ")";
128     }
129     return "new java.lang.Short(" + variableName + ").shortValue()";
130   }
131 
132   /**
133    * {@inheritDoc}
134    */
135   public String createToJavaObjectCode(final String variableName) {
136     if (_asWrapper) {
137       return variableName;
138     }
139     return "new java.lang.Short(" + variableName + ")";
140   }
141 
142   /**
143    * {@inheritDoc}
144    */
145   public String createFromJavaObjectCode(final String variableName) {
146     if (_asWrapper) {
147       return "((java.lang.Short) " + variableName + ")";
148     }
149     return "((java.lang.Short) " + variableName + ").shortValue()";
150   }
151 
152   // --------------------------------------------------------------------------
153 
154   /**
155    * {@inheritDoc}
156    */
157   public void validationCode(final JSourceCode jsc, final String fixedValue,
158       final String validatorInstanceName) {
159     jsc.add("org.exolab.castor.xml.validators.ShortValidator typeValidator;\n"
160         + "typeValidator = new org.exolab.castor.xml.validators.ShortValidator();\n"
161         + "{0}.setValidator(typeValidator);", validatorInstanceName);
162 
163     if (fixedValue != null) {
164       jsc.add("typeValidator.setFixed((short) " + fixedValue + ");");
165     }
166 
167     codePatternFacet(jsc, "typeValidator");
168     codeWhiteSpaceFacet(jsc, "typeValidator");
169 
170     if (getMinExclusive() != null) {
171       jsc.add("typeValidator.setMinExclusive((short) " + getMinExclusive() + ");");
172     } else if (getMinInclusive() != null) {
173       jsc.add("typeValidator.setMinInclusive((short) " + getMinInclusive() + ");");
174     }
175 
176     if (getMaxExclusive() != null) {
177       jsc.add("typeValidator.setMaxExclusive((short) " + getMaxExclusive() + ");");
178     } else if (getMaxInclusive() != null) {
179       jsc.add("typeValidator.setMaxInclusive((short) " + getMaxInclusive() + ");");
180     }
181 
182     codeDigitsFacet(jsc, "typeValidator");
183   }
184 
185   // --------------------------------------------------------------------------
186 }