KickJava   Java API By Example, From Geeks To Geeks.

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


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 package com.sun.enterprise.tools.common.validation.constraints;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collection JavaDoc;
28
29 import com.sun.enterprise.tools.common.validation.constraints.ConstraintFailure;
30 import com.sun.enterprise.tools.common.validation.util.BundleReader;
31
32 /**
33  * NonBlankConstraint is a {@link Constraint} to validate non spaces only
34  * value. It implements <code>Constraint</code> interface and extends
35  * {@link ConstraintUtils} class.
36  * <code>match</code> method of this object returns empty
37  * <code>Collection</code> if the value being validated is spaces only;
38  * else it returns a <code>Collection</code> with a {@link ConstraintFailure}
39  * object in it.
40  * <code>ConstraintUtils</code> class provides utility methods for formating
41  * failure messages and a <code>print<method> method to print this object.
42  *
43  * @author Rajeshwar Patil
44  * @version %I%, %G%
45  */

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

67     public 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             value = value.trim();
71             if(value.length() == 0){
72                 String JavaDoc failureMessage = formatFailureMessage(toString(), name);
73                 failed_constrained_list.add(new ConstraintFailure(toString(),
74                     value, name, failureMessage, BundleReader.getValue(
75                         "MSG_NonBlankConstraint_Failure"))); //NOI18N
76
}
77         }
78         return failed_constrained_list;
79     }
80 }
81
Popular Tags