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:boolean XML Schema datatype.
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 XSBoolean extends AbstractWhiteSpaceFacet {
28    // --------------------------------------------------------------------------
29  
30    /** Name of this XSType. */
31    public static final String NAME = "boolean";
32  
33    /** Type number of this XSType. */
34    public static final short TYPE = XSType.BOOLEAN_TYPE;
35  
36    // --------------------------------------------------------------------------
37  
38    /** The JType represented by this XSType. */
39    private final JType _jType;
40  
41    /** True if this type is implemented using the wrapper class. */
42    private final boolean _asWrapper;
43  
44    // --------------------------------------------------------------------------
45  
46    /**
47     * No-arg constructor.
48     */
49    public XSBoolean() {
50      this(false);
51    }
52  
53    /**
54     * Constructs a new XSBoolean.
55     * 
56     * @param asWrapper If true, use the java.lang wrapper class.
57     */
58    public XSBoolean(final boolean asWrapper) {
59      super();
60  
61      _asWrapper = asWrapper;
62      if (_asWrapper) {
63        _jType = new JClass("java.lang.Boolean");
64      } else {
65        _jType = JType.BOOLEAN;
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.Boolean(false);";
111   }
112 
113   /**
114    * {@inheritDoc}
115    */
116   public String createDefaultValueWithString(final String variableName) {
117     if (_asWrapper) {
118       return "new java.lang.Boolean(" + variableName + ").booleanValue()";
119     }
120     return "new java.lang.Boolean(" + variableName + ").booleanValue()";
121   }
122 
123   /**
124    * {@inheritDoc}
125    */
126   public String createToJavaObjectCode(final String variableName) {
127     if (_asWrapper) {
128       return variableName;
129     }
130     return "(" + variableName + " ? java.lang.Boolean.TRUE : java.lang.Boolean.FALSE)";
131   }
132 
133   /**
134    * {@inheritDoc}
135    */
136   public String createFromJavaObjectCode(final String variableName) {
137     if (_asWrapper) {
138       return "((java.lang.Boolean) " + variableName + ")";
139     }
140     return "((java.lang.Boolean) " + variableName + ").booleanValue()";
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.BooleanValidator typeValidator;\n"
151         + "typeValidator = new org.exolab.castor.xml.validators.BooleanValidator();\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 
162   // --------------------------------------------------------------------------
163 }