KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.text.MessageFormat JavaDoc;
26
27 import org.netbeans.modules.j2ee.sun.validation.constraints.ConstraintFailure;
28 import org.netbeans.modules.j2ee.sun.validation.util.BundleReader;
29
30
31 /**
32  * InConstraint is a {@link Constraint} to validate an Enumeration value.
33  * 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 belongs to the
37  * enumeration represented by this object; else it returns a
38  * <code>Collection</code> with a {@link ConstraintFailure} 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 InConstraint extends ConstraintUtils implements Constraint {
46
47     /**
48      * An enumeration represented by this <code>Constraint</code>.
49      */

50     private Collection JavaDoc enumeration = null;
51
52
53     /** Creates a new instance of <code>InConstraint</code>. */
54     public InConstraint() {
55         enumeration = new ArrayList JavaDoc();
56     }
57
58
59     /** Creates a new instance of <code>InConstraint</code>. */
60     public InConstraint(Collection JavaDoc enumeration) {
61         this.enumeration = enumeration;
62     }
63
64
65     /** Creates a new instance of <code>InConstraint</code>. */
66     public InConstraint(String JavaDoc[] enumeration) {
67         this.enumeration = new ArrayList JavaDoc();
68         int size = enumeration.length;
69         for(int i=0; i<size; i++) {
70             this.enumeration.add(enumeration[i]);
71         }
72     }
73
74
75     /**
76      * Validates the given value against this <code>Constraint</code>.
77      *
78      * @param value the value to be validated
79      * @param name the element name, value of which is being validated.
80      * It is used only in case of <code>Constraint</code> failure, to
81      * construct the failure message.
82      *
83      * @return <code>Collection</code> the Collection of
84      * <code>ConstraintFailure</code> Objects. Collection is empty
85      * if there are no failures. This method will fail, only for the values
86      * that does not belong to an enumeration represented by this object.
87      */

88     public java.util.Collection JavaDoc match(String JavaDoc value, String JavaDoc name) {
89         Collection JavaDoc failed_constrained_list = new ArrayList JavaDoc();
90         if((value != null) && (value.length() != 0)) {
91             if(!enumeration.contains(value)){
92                 String JavaDoc failureMessage = formatFailureMessage(toString(),
93                     value, name);
94
95                 String JavaDoc format = BundleReader.getValue(
96                     "MSG_InConstraint_Failure"); //NOI18N
97
String JavaDoc set = ""; //NOI18N
98
Iterator JavaDoc iterator = enumeration.iterator();
99                 String JavaDoc member;
100                 while(iterator.hasNext()){
101                     member = (String JavaDoc)iterator.next();
102                     set = set + " " + member; //NOI18N
103
}
104
105                 Object JavaDoc[] arguments = new Object JavaDoc[]{set};
106
107                 String JavaDoc genericFailureMessage =
108                     MessageFormat.format(format, arguments);
109
110                 failed_constrained_list.add(new ConstraintFailure(toString(),
111                     value, name, failureMessage, genericFailureMessage));
112             }
113         }
114         return failed_constrained_list;
115     }
116
117
118     /**
119      * Sets the given <code>Collection</code> as the enumeration
120      * represented by this object.
121      *
122      * @param enumeration the <code>Collection</code> to be set
123      * as the enumeration represented by this object.
124      */

125     public void setEnumerationValue(Collection JavaDoc enumeration){
126         this.enumeration = enumeration;
127     }
128
129
130     /**
131      * Adds the given <code>value</code> to the enumeration
132      * represented by this object.
133      *
134      * @param value the value to be added to the enumeration
135      * represented by this object.
136      */

137     public void setEnumerationValue(String JavaDoc value){
138         enumeration.add(value);
139     }
140
141
142     /**
143      * Prints this <code>Constraint</code>.
144      */

145     void print() {
146         super.print();
147         String JavaDoc format = BundleReader.getValue("Name_Value_Pair_Format");//NOI18N
148
Iterator JavaDoc iterator = enumeration.iterator();
149         String JavaDoc values = "";
150         while(iterator.hasNext()){
151             values = values + " " + (String JavaDoc)iterator.next(); //NOI18N
152
}
153
154         if(values != null){
155             Object JavaDoc[] arguments =
156                 new Object JavaDoc[]{"Enumeration Values", values}; //NOI18N
157
System.out.println(MessageFormat.format(format, arguments));
158         }
159     }
160 }
161
Popular Tags