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 length, whiteSpace and pattern facets.
21   * 
22   * @author <a href="mailto:ralf DOT joachim AT syscon DOT eu">Ralf Joachim</a>
23   * @version $Revision: 6662 $ $Date: 2005-03-05 06:42:06 -0700 (Sat, 05 Mar 2005) $
24   * @since 1.1
25   */
26  public abstract class AbstractLengthFacet extends AbstractWhiteSpaceFacet {
27    // --------------------------------------------------------------------------
28  
29    /** The length facet. */
30    private int _length = 0;
31  
32    /** The max length facet. */
33    private int _maxLength = -1;
34  
35    /** The min length facet. */
36    private int _minLength = 0;
37  
38    // --------------------------------------------------------------------------
39  
40    /**
41     * No-arg constructor. By default only 'collapse' is supported for the whiteSpace facet.
42     */
43    protected AbstractLengthFacet() {
44      super(true);
45    }
46  
47    /**
48     * Construct a new AbstractXSLengthFacet optionally allowing the whiteSpace facet to be set to
49     * 'replace' and 'preserve' values in addition to 'collaps'.
50     *
51     * @param whiteSpaceCollapseOnly If set to true only 'collapse' is allowed for the whiteSpace
52     *        facet. If set to false 'collaps', 'replace' and 'preserve' values are allowed for
53     *        whiteSpace facet.
54     */
55    protected AbstractLengthFacet(final boolean whiteSpaceCollapseOnly) {
56      super(whiteSpaceCollapseOnly);
57    }
58  
59    // --------------------------------------------------------------------------
60  
61    /**
62     * Returns true if a length has been set.
63     * 
64     * @return True if a length has been set.
65     */
66    public final boolean hasLength() {
67      return (_length > 0);
68    }
69  
70    /**
71     * Returns the length that this type must have.
72     * 
73     * @return The length that this type must have.
74     */
75    public final int getLength() {
76      return _length;
77    }
78  
79    /**
80     * Sets the length of this type. While setting the length, the maxLength and minLength are also
81     * set up to this length.
82     *
83     * @param length The length to set.
84     */
85    public final void setLength(final int length) {
86      _length = length;
87  
88      setMaxLength(length);
89      setMinLength(length);
90    }
91  
92    /**
93     * Returns true if a maximum length has been set.
94     * 
95     * @return True if a maximum length has been set.
96     */
97    public final boolean hasMaxLength() {
98      return (_maxLength >= 0);
99    }
100 
101   /**
102    * Returns the maximum length occurances of this type can be. A negative value denotes no maximum
103    * length.
104    * 
105    * @return The maximum length facet.
106    */
107   public final int getMaxLength() {
108     return _maxLength;
109   }
110 
111   /**
112    * Sets the maximum length of this type. To remove the max length facet, use a negative value.
113    *
114    * @param maxLength The maximum length for occurances of this type.
115    */
116   public final void setMaxLength(final int maxLength) {
117     _maxLength = maxLength;
118   }
119 
120   /**
121    * Returns true if a minimum length has been set.
122    * 
123    * @return True if a minimum length has been set.
124    */
125   public final boolean hasMinLength() {
126     return (_minLength > 0);
127   }
128 
129   /**
130    * Returns the minimum length occurances of this type can be.
131    * 
132    * @return The minimum length facet.
133    */
134   public final int getMinLength() {
135     return _minLength;
136   }
137 
138   /**
139    * Sets the minimum length of this XSString.
140    * 
141    * @param minLength The minimum length for occurances of this type.
142    */
143   public final void setMinLength(final int minLength) {
144     _minLength = minLength;
145   }
146 
147   // --------------------------------------------------------------------------
148 
149   /**
150    * {@inheritDoc}
151    */
152   protected final void setFacet(final Facet facet) {
153     super.setFacet(facet);
154     setLengthFacet(facet);
155   }
156 
157   /**
158    * Transfer given facet if it is one of length, maxLength or minLength.
159    *
160    * @param facet The facet to transfer.
161    */
162   protected final void setLengthFacet(final Facet facet) {
163     String name = facet.getName();
164     if (Facet.LENGTH.equals(name)) {
165       setLength(facet.toInt());
166     } else if (Facet.MAX_LENGTH.equals(name)) {
167       setMaxLength(facet.toInt());
168     } else if (Facet.MIN_LENGTH.equals(name)) {
169       setMinLength(facet.toInt());
170     }
171   }
172 
173   /**
174    * Generate the source code for length, maxLength or minLength facets validation.
175    *
176    * @param jsc The JSourceCode to fill in.
177    * @param validatorName The name of the TypeValidator that the patterns should be added to.
178    */
179   protected final void codeLengthFacet(final JSourceCode jsc, final String validatorName) {
180     if (hasLength()) {
181       jsc.add("{0}.setLength({1});", validatorName, Integer.toString(getLength()));
182     } else {
183       if (hasMaxLength()) {
184         jsc.add("{0}.setMaxLength({1});", validatorName, Integer.toString(getMaxLength()));
185       }
186       if (hasMinLength()) {
187         jsc.add("{0}.setMinLength({1});", validatorName, Integer.toString(getMinLength()));
188       }
189     }
190   }
191 
192   // --------------------------------------------------------------------------
193 }