KickJava   Java API By Example, From Geeks To Geeks.

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


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.text.MessageFormat 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  * CardinalConstraint is a {@link Constraint} to validate the structure.
32  * It implements <code>Constraint</code> interface and extends
33  * {@link ConstraintUtils} class.
34  * <code>match</code> method of this object returns empty
35  * <code>Collection</code> if the value being validated conforms to the
36  * structure specified by this <code>Constraint</code>; else it returns a
37  * <code>Collection</code> with a {@link ConstraintFailure} object in it.
38  * <code>ConstraintUtils</code> class provides utility methods for formating
39  * failure messages and a <code>print<method> method to print this object.
40  *
41  * @author Rajeshwar Patil
42  * @version %I%, %G%
43  */

44 public class CardinalConstraint extends ConstraintUtils
45                 implements Constraint{
46  
47     /**
48      * The structure represented by this <code>Constraint</code>.
49      */

50     private int cardinal = Constants.MANDATORY_ELEMENT;
51
52
53     /** Creates a new instance of CardinalConstraint */
54     public CardinalConstraint() {
55         cardinal = Constants.MANDATORY_ELEMENT;
56     }
57
58
59     /** Creates a new instance of <code>CardinalConstraint</code>. */
60     public CardinalConstraint(int cardinal) {
61         this.cardinal = cardinal;
62     }
63
64
65     /** Creates a new instance of <code>CardinalConstraint</code>. */
66     public CardinalConstraint(String JavaDoc cardinal) {
67         try {
68            this.cardinal = Integer.parseInt(cardinal);
69         } catch(NumberFormatException JavaDoc e) {
70             String JavaDoc format =
71                 BundleReader.getValue("Error_failed_to_create"); //NOI18N
72
Object JavaDoc[] arguments =
73                 new Object JavaDoc[]{"CardinalConstraint"}; //NOI18N
74
System.out.println(MessageFormat.format(format, arguments));
75         }
76     }
77
78
79     /**
80      * Validates the given value against this <code>Constraint</code>.
81      *
82      * @param value the value to be validated.
83      * This value must be of appropiate type, that is , either an Object or
84      * an array of Objects, in consistence with the Cardinal represented
85      * by this Object.
86      * @param name the element name, value of which is being validated.
87      * It is used only in case of <code>Constraint</code> failure, to
88      * construct the failure message.
89      *
90      * @return <code>Collection</code> the Collection of
91      * <code>ConstraintFailure</code> Objects. Collection is empty
92      * if there are no failures. This <code>Constraint</code> will
93      * fail if the value does not conform to the structure represented
94      * by this object.
95      */

96     public Collection JavaDoc match(String JavaDoc value, String JavaDoc name){
97         return match((Object JavaDoc)value, name);
98     }
99
100
101     /**
102      * Validates the given value against this <code>Constraint</code>.
103      *
104      * @param value the value to be validated.
105      * This value must be of appropriate type, that is , either an Object or
106      * an array of Objects, in consistence with the Cardinal represented
107      * by this Object.
108      * @param name the element name, value of which is being validated.
109      * It is used only in case of <code>Constraint</code> failure, to
110      * construct the failure message.
111      *
112      * @return <code>Collection</code> the Collection of
113      * <code>ConstraintFailure</code> Objects. Collection is empty
114      * if there are no failures. This method will fail if the value does
115      * not conform to the structure represented by this object.
116      */

117     public Collection JavaDoc match(Object JavaDoc value, String JavaDoc name) {
118         Collection JavaDoc failed_constrained_list =
119                 new ArrayList JavaDoc();
120
121         if(Constants.MANDATORY_ARRAY == cardinal) {
122             Object JavaDoc[] values = null;
123             try {
124                 values = (Object JavaDoc[])value;
125             } catch(ClassCastException JavaDoc exception){
126                 System.out.println(BundleReader.getValue(
127                    "Error_expects_argument_one_to_be_an_array_of_Objects"));//NOI18N
128
}
129
130             if((null == values) || (values.length < 1)){
131                 String JavaDoc failureMessage = formatFailureMessage(toString(), name);
132                 failed_constrained_list.add(new ConstraintFailure(toString(),
133                     null, name, failureMessage, BundleReader.getValue(
134                         "MSG_CardinalConstraint_Failure"))); //NOI18N
135
}
136         } else {
137             if(Constants.MANDATORY_ELEMENT == cardinal){
138                 if (null == value){
139                     String JavaDoc failureMessage =
140                         formatFailureMessage(toString(), name);
141                     failed_constrained_list.add(
142                         new ConstraintFailure(toString(), null, name,
143                             failureMessage, BundleReader.getValue(
144                                 "MSG_CardinalConstraint_Failure"))); //NOI18N
145
}
146             }
147         }
148         return failed_constrained_list;
149     }
150
151
152     /**
153      * Sets the given cardinal as the structure
154      * represented by this object.
155      *
156      * @param cardinal the cardinal to be set
157      * as the structure represented by this object.
158      */

159     public void setCardinal(int cardinal){
160         this.cardinal = cardinal;
161     }
162
163
164     /**
165      * Sets the given cardinal as the structure
166      * represented by this object.
167      *
168      * @param cardinal the cardinal to be set
169      * as the structure represented by this object.
170      */

171     public void setCardinal(String JavaDoc cardinal){
172         try {
173            this.cardinal = Integer.parseInt(cardinal);
174         } catch(NumberFormatException JavaDoc e) {
175             String JavaDoc format =
176                 BundleReader.getValue("Error_failed_to_set"); //NOI18N
177
Object JavaDoc[] arguments =
178                 new Object JavaDoc[]{this.toString(), "Cardinal"}; //NOI18N
179

180             System.out.println(MessageFormat.format(format, arguments));
181         }
182     }
183
184
185     /**
186      * Prints this <code>Constraint</code>.
187      */

188     public void print() {
189         super.print();
190         String JavaDoc format = BundleReader.getValue("Name_Value_Pair_Format");//NOI18N
191
Object JavaDoc[] arguments =
192             new Object JavaDoc[]{"Cardinal", String.valueOf(cardinal)}; //NOI18N
193
System.out.println(MessageFormat.format(format, arguments));
194     }
195 }
196
Popular Tags