View Javadoc
1   /*
2    * Redistribution and use of this software and associated documentation ("Software"), with or
3    * without modification, are permitted provided that the following conditions are met:
4    *
5    * 1. Redistributions of source code must retain copyright statements and notices. Redistributions
6    * must also contain a copy of this document.
7    *
8    * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
9    * conditions and the following disclaimer in the documentation and/or other materials provided with
10   * the distribution.
11   *
12   * 3. The name "Exolab" must not be used to endorse or promote products derived from this Software
13   * without prior written permission of Intalio, Inc. For written permission, please contact
14   * info@exolab.org.
15   *
16   * 4. Products derived from this Software may not be called "Exolab" nor may "Exolab" appear in
17   * their names without prior written permission of Intalio, Inc. Exolab is a registered trademark of
18   * Intalio, Inc.
19   *
20   * 5. Due credit should be given to the Exolab Project (http://www.exolab.org/).
21   *
22   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR
23   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
24   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTALIO, INC. OR ITS
25   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
29   * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30   *
31   * Copyright 2001-2003 (C) Intalio, Inc. All Rights Reserved.
32   *
33   * $Id$
34   */
35  package org.exolab.castor.xml.validators;
36  
37  import org.exolab.castor.xml.TypeValidator;
38  import org.exolab.castor.xml.ValidationContext;
39  import org.exolab.castor.xml.ValidationException;
40  
41  /**
42   * The Boolean Validation class. Handles validation for the primitive boolean and java.lang.Boolean
43   * types.
44   *
45   * @author <a href="mailto:blandin@intalio.com">Arnaud Blandin</a>
46   * @version $Revision$ $Date: 2003-03-03 02:57:21 -0700 (Mon, 03 Mar 2003) $
47   */
48  public class BooleanValidator extends PatternValidator implements TypeValidator {
49  
50    /** If true, we perform "fixed" validation. */
51    private boolean _useFixed = false;
52    /** Fixed value of this boolean. (Not used unless _useFixed == true.) */
53    private boolean _fixed = false;
54  
55    /**
56     * Creates a new BooleanValidator with no restrictions.
57     */
58    public BooleanValidator() {
59      super();
60    } // -- ByteValidator
61  
62    /**
63     * Clears the fixed value for this BooleanValidator.
64     */
65    public void clearFixed() {
66      _useFixed = false;
67    } // -- clearFixed
68  
69    /**
70     * Returns the fixed value that booleans validated with this validator must be equal to. If no
71     * fixed value has been specified, returns null.
72     *
73     * @return the fixed value to validate against.
74     */
75    public Boolean getFixed() {
76      if (_useFixed) {
77        return Boolean.valueOf(_fixed);
78      }
79      return null;
80    } // -- getFixed
81  
82    /**
83     * Returns true if a fixed value to validate against has been set.
84     *
85     * @return true if a fixed value has been set.
86     */
87    public boolean hasFixed() {
88      return _useFixed;
89    } // -- hasFixed
90  
91    /**
92     * Sets the fixed value for boolean validation.
93     *
94     * @param fixedValue the fixed value that a boolean validated with this validator must be equal
95     *        to.
96     */
97    public void setFixed(final boolean fixedValue) {
98      _useFixed = true;
99      _fixed = fixedValue;
100   } // -- setFixed
101 
102   /**
103    * Sets the fixed value for boolean validation.
104    *
105    * @param fixedValue the fixed value that a boolean validated with this validator must be equal
106    *        to.
107    */
108   public void setFixed(final Boolean fixedValue) {
109     _useFixed = true;
110     _fixed = fixedValue.booleanValue();
111   }
112 
113   /**
114    * Validates the given Object.
115    *
116    * @param b the boolean to validate
117    * @param context the ValidationContext
118    * @throws ValidationException if the object fails validation.
119    */
120   public void validate(final boolean b, final ValidationContext context)
121       throws ValidationException {
122     if (_useFixed && b != _fixed) {
123       String err = "boolean " + b + " is not equal to the fixed value: " + _fixed;
124       throw new ValidationException(err);
125     }
126 
127     if (hasPattern()) {
128       super.validate(String.valueOf(b), context);
129     }
130   } // -- validate
131 
132   /**
133    * Validates the given Object.
134    *
135    * @param object the Object to validate
136    * @throws ValidationException if the object fails validation.
137    */
138   public void validate(final Object object) throws ValidationException {
139     validate(object, (ValidationContext) null);
140   } // -- validate
141 
142   /**
143    * Validates the given Object.
144    *
145    * @param object the Object to validate
146    * @param context the ValidationContext
147    * @throws ValidationException if the object fails validation.
148    */
149   public void validate(final Object object, final ValidationContext context)
150       throws ValidationException {
151     if (object == null) {
152       String err = "BooleanValidator cannot validate a null object.";
153       throw new ValidationException(err);
154     }
155 
156     boolean value = false;
157     try {
158       value = ((Boolean) object).booleanValue();
159     } catch (Exception ex) {
160       String err = "Expecting a Boolean, received instead: ";
161       err += object.getClass().getName();
162       throw new ValidationException(err);
163     }
164     validate(value, context);
165   } // -- validate
166 
167 } // -- BooleanValidator