KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > validation > constraints > BooleanConstraint


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.sun.validation.constraints;
21
22 import java.text.MessageFormat JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25
26 import org.netbeans.modules.j2ee.sun.validation.Constants;
27 import org.netbeans.modules.j2ee.sun.validation.constraints.ConstraintFailure;
28 import org.netbeans.modules.j2ee.sun.validation.util.BundleReader;
29
30
31 /**
32  * BooleanConstraint is a {@link Constraint} to validate <code>boolean</code>
33  * values. It implements <code>Constraint</code> interface and extends
34  * {@link ConstraintUtils} class.
35  * <code>match</code> method of this object returns empty
36  * <code>Collection</code> if the value being validated is <code>boolean</code>;
37  * else it returns a <code>Collection</code> with a {@link ConstraintFailure}
38  * object in it.
39  * <code>ConstraintUtils</code> class provides utility methods for formating
40  * failure messages and a <code>print<method> method to print this object.
41  *
42  * @author Rajeshwar Patil
43  * @version %I%, %G%
44  */

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

67     public java.util.Collection JavaDoc match(String JavaDoc value, String JavaDoc name) {
68         Collection JavaDoc failed_constrained_list = new ArrayList JavaDoc();
69         if((value != null) && (value.length() != 0)) {
70             if (!( (value.equalsIgnoreCase("true")) || //NOI18N
71
(value.equalsIgnoreCase("false")))){ //NOI18N
72
String JavaDoc failureMessage = formatFailureMessage(toString(), value,
73                     name);
74
75                 String JavaDoc format = BundleReader.getValue(
76                     "MSG_BooleanConstraint_Failure"); //NOI18N
77
Object JavaDoc[] arguments = new Object JavaDoc[]{"True", "False"}; //NOI18N
78

79                 String JavaDoc genericFailureMessage =
80                     MessageFormat.format(format, arguments);
81
82                 failed_constrained_list.add(new ConstraintFailure(toString(),
83                     value, name, failureMessage, genericFailureMessage));
84             }
85         }
86         return failed_constrained_list;
87     }
88 }
89
Popular Tags