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