KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > common > validation > constraints > BooleanConstraint


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * BooleanConstraint.java April 1, 2003, 7:44 AM
26  *
27  */

28
29 package com.sun.enterprise.tools.common.validation.constraints;
30
31 import java.text.MessageFormat JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Collection JavaDoc;
34
35 import com.sun.enterprise.tools.common.validation.Constants;
36 import com.sun.enterprise.tools.common.validation.constraints.ConstraintFailure;
37 import com.sun.enterprise.tools.common.validation.util.BundleReader;
38
39
40 /**
41  * BooleanConstraint is a {@link Constraint} to validate <code>boolean</code>
42  * values. It implements <code>Constraint</code> interface and extends
43  * {@link ConstraintUtils} class.
44  * <code>match</code> method of this object returns empty
45  * <code>Collection</code> if the value being validated is <code>boolean</code>;
46  * else it returns a <code>Collection</code> with a {@link ConstraintFailure}
47  * object in it.
48  * <code>ConstraintUtils</code> class provides utility methods for formating
49  * failure messages and a <code>print<method> method to print this object.
50  *
51  * @author Rajeshwar Patil
52  * @version %I%, %G%
53  */

54 public class BooleanConstraint extends ConstraintUtils
55             implements Constraint {
56
57     /** Creates a new instance of <code>BooleanConstraint</code>. */
58     public BooleanConstraint() {
59     }
60
61
62     /**
63      * Validates the given value against this <code>Constraint</code>.
64      *
65      * @param value the value to be validated
66      * @param name the element name, value of which is being validated.
67      * It is used only in case of <code>Constraint</code> failure, to construct
68      * the failure message.
69      *
70      * @return <code>Collection</code> the Collection of
71      * <code>ConstraintFailure</code> Objects. Collection is empty if
72      * there are no failures. Failure can occur if value being validated is
73      * anything other than the following values : <code>true</code>,
74      * <code>false</code> , case ignored.
75      */

76     public java.util.Collection JavaDoc match(String JavaDoc value, String JavaDoc name) {
77         Collection JavaDoc failed_constrained_list = new ArrayList JavaDoc();
78         if((value != null) && (value.length() != 0)) {
79             if (!( (value.equalsIgnoreCase("true")) || //NOI18N
80
(value.equalsIgnoreCase("false")))){ //NOI18N
81
String JavaDoc failureMessage = formatFailureMessage(toString(), value,
82                     name);
83
84                 String JavaDoc format = BundleReader.getValue(
85                     "MSG_BooleanConstraint_Failure"); //NOI18N
86
Object JavaDoc[] arguments = new Object JavaDoc[]{"True", "False"}; //NOI18N
87

88                 String JavaDoc genericFailureMessage =
89                     MessageFormat.format(format, arguments);
90
91                 failed_constrained_list.add(new ConstraintFailure(toString(),
92                     value, name, failureMessage, genericFailureMessage));
93             }
94         }
95         return failed_constrained_list;
96     }
97 }
98
Popular Tags