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