View Javadoc
1   /*
2    * Copyright 2007 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.castor.xml.schema.Facet;
17  import org.exolab.javasource.JSourceCode;
18  
19  /**
20   * A base class for types which support the range, whiteSpace and pattern facets.
21   * 
22   * @author <a href="mailto:ralf DOT joachim AT syscon DOT eu">Ralf Joachim</a>
23   * @version $Revision: 6623 $ $Date: 2005-03-05 06:42:06 -0700 (Sat, 05 Mar 2005) $
24   * @since 1.1
25   */
26  public abstract class AbstractRangeFacet extends AbstractWhiteSpaceFacet {
27  
28    /** Maximum Date (exclusive). */
29    private String _maxExclusive;
30  
31    /** Maximum Date (inclusive). */
32    private String _maxInclusive;
33  
34    /** Minimum Date (exclusive). */
35    private String _minExclusive;
36  
37    /** Minimum Date (inclusive). */
38    private String _minInclusive;
39  
40    private String _positiveInfinity;
41  
42    private String _negativeInfinity;
43  
44    /**
45     * Returns true if a maximum (inclusive or exclusive) has been set.
46     * 
47     * @return True if a maximum (inclusive or exclusive) has been set.
48     */
49    public final boolean hasMaximum() {
50      return (_maxInclusive != null) || (_maxExclusive != null);
51    }
52  
53    /**
54     * Returns the maximum exclusive value that this XSDate can hold.
55     * 
56     * @return The maximum exclusive value that this XSDate can hold. If no maximum exclusive value
57     *         has been set, Null will be returned.
58     */
59    public final String getMaxExclusive() {
60      return _maxExclusive;
61    }
62  
63    /**
64     * Sets the maximum exclusive value that this XSDate can hold.
65     * 
66     * @param max The maximum exclusive value this XSDate can be.
67     */
68    public final void setMaxExclusive(final String max) {
69      _maxExclusive = max;
70      _maxInclusive = null;
71    }
72  
73    /**
74     * Returns the maximum inclusive value that this XSDate can hold.
75     * 
76     * @return The maximum inclusive value that this XSDate can hold. If no maximum inclusive value
77     *         has been set, Null will be returned.
78     */
79    public final String getMaxInclusive() {
80      return _maxInclusive;
81    }
82  
83    /**
84     * Sets the maximum inclusive value that this XSDate can hold.
85     * 
86     * @param max The maximum inclusive value this XSDate can be.
87     */
88    public final void setMaxInclusive(final String max) {
89      _maxInclusive = max;
90      _maxExclusive = null;
91    }
92  
93    /**
94     * Returns true if a minimum (inclusive or exclusive) has been set.
95     * 
96     * @return True if a minimum (inclusive or exclusive) has been set.
97     */
98    public final boolean hasMinimum() {
99      return (_minInclusive != null) || (_minExclusive != null);
100   }
101 
102   /**
103    * Returns the minimum exclusive value that this XSDate can hold.
104    * 
105    * @return The minimum exclusive value that this XSDate can hold. If no minimum exclusive value
106    *         has been set, Null will be returned.
107    */
108   public final String getMinExclusive() {
109     return _minExclusive;
110   }
111 
112   /**
113    * Sets the minimum exclusive value that this XSDate can hold.
114    * 
115    * @param min The minimum exclusive value this XSDate can be.
116    */
117   public final void setMinExclusive(final String min) {
118     _minExclusive = min;
119     _minInclusive = null;
120   }
121 
122   /**
123    * Returns the minimum inclusive value that this XSDate can hold.
124    * 
125    * @return The minimum inclusive value that this XSDate can be.
126    */
127   public final String getMinInclusive() {
128     return _minInclusive;
129   }
130 
131   /**
132    * Sets the minimum inclusive value that this XSDate can hold.
133    * 
134    * @param min The minimum inclusive value this XSDate can be.
135    */
136   public final void setMinInclusive(final String min) {
137     _minInclusive = min;
138     _minExclusive = null;
139   }
140 
141   public final void setPositiveInfinity(final String positiveInfity) {
142     _positiveInfinity = positiveInfity;
143   }
144 
145   public final void setNegativeInfinity(final String negativeInfity) {
146     _negativeInfinity = negativeInfity;
147   }
148 
149   public final boolean hasPositiveInfinity() {
150     return _positiveInfinity != null;
151   }
152 
153   public final boolean hasNegavtiveInfinity() {
154     return _negativeInfinity != null;
155   }
156 
157   public final String getPositiveInfinity() {
158     return _positiveInfinity;
159   }
160 
161   public final String getNegativeInfinity() {
162     return _negativeInfinity;
163   }
164 
165   /**
166    * {@inheritDoc}
167    */
168   protected void setFacet(final Facet facet) {
169     super.setFacet(facet);
170     setRangeFacet(facet);
171   }
172 
173   /**
174    * Transfer given facet if it is a range.
175    *
176    * @param facet The facet to transfer.
177    */
178   protected final void setRangeFacet(final Facet facet) {
179     String name = facet.getName();
180     if (Facet.MAX_EXCLUSIVE.equals(name)) {
181       setMaxExclusive(facet.getValue());
182     } else if (Facet.MAX_INCLUSIVE.equals(name)) {
183       setMaxInclusive(facet.getValue());
184     } else if (Facet.MIN_EXCLUSIVE.equals(name)) {
185       setMinExclusive(facet.getValue());
186     } else if (Facet.MIN_INCLUSIVE.equals(name)) {
187       setMinInclusive(facet.getValue());
188     }
189   }
190 
191   /**
192    * Generate the source code for pattern facet validation.
193    *
194    * @param jsc The JSourceCode to fill in.
195    * @param validatorName The name of the TypeValidator that the range should be added to.
196    */
197   protected final void codeRangeFacet(final JSourceCode jsc, final String validatorName) {
198     // maxInclusive / maxExclusive facets (only one or the other, never both)
199     if (_maxInclusive != null) {
200       jsc.add("{0}.setMaxInclusive(\"{1}\");", validatorName, getMaxInclusive());
201     } else if (_maxExclusive != null) {
202       jsc.add("{0}.setMaxExclusive(\"{1}\");", validatorName, getMaxExclusive());
203     }
204 
205     // minInclusive / minExclusive facets (only one or the other, never both)
206     if (_minInclusive != null) {
207       jsc.add("{0}.setMinInclusive(\"{1}\");", validatorName, getMinInclusive());
208     } else if (_minExclusive != null) {
209       jsc.add("{0}.setMinExclusive(\"{1}\");", validatorName, getMinExclusive());
210     }
211 
212     // if (_positiveInfinity != null) {
213     // jsc.add("{0}.setPositiveInfinity(\"{1}\");", validatorName, getPositiveInfinity());
214     // }
215     //
216     // if (_negativeInfinity != null) {
217     // jsc.add("{0}.setNegativeInfinity(\"{1}\");", validatorName, getNegativeInfinity());
218     // }
219   }
220 
221 }