KickJava   Java API By Example, From Geeks To Geeks.

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


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  * CardinalConstraint.java March 24, 2003, 5:17 PM
26  *
27  */

28
29 package com.sun.enterprise.tools.common.validation.constraints;
30
31 import java.util.ArrayList JavaDoc;
32 import java.util.Collection JavaDoc;
33 import java.text.MessageFormat 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  * CardinalConstraint is a {@link Constraint} to validate the structure.
41  * It implements <code>Constraint</code> interface and extends
42  * {@link ConstraintUtils} class.
43  * <code>match</code> method of this object returns empty
44  * <code>Collection</code> if the value being validated conforms to the
45  * structure specified by this <code>Constraint</code>; else it returns a
46  * <code>Collection</code> with a {@link ConstraintFailure} object in it.
47  * <code>ConstraintUtils</code> class provides utility methods for formating
48  * failure messages and a <code>print<method> method to print this object.
49  *
50  * @author Rajeshwar Patil
51  * @version %I%, %G%
52  */

53 public class CardinalConstraint extends ConstraintUtils
54                 implements Constraint{
55  
56     /**
57      * The structure represented by this <code>Constraint</code>.
58      */

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

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

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

168     public void setCardinal(int cardinal){
169         this.cardinal = cardinal;
170     }
171
172
173     /**
174      * Sets the given cardinal as the structure
175      * represented by this object.
176      *
177      * @param cardinal the cardinal to be set
178      * as the structure represented by this object.
179      */

180     public void setCardinal(String JavaDoc cardinal){
181         try {
182            this.cardinal = Integer.parseInt(cardinal);
183         } catch(NumberFormatException JavaDoc e) {
184             String JavaDoc format =
185                 BundleReader.getValue("Error_failed_to_set"); //NOI18N
186
Object JavaDoc[] arguments =
187                 new Object JavaDoc[]{this.toString(), "Cardinal"}; //NOI18N
188

189             System.out.println(MessageFormat.format(format, arguments));
190         }
191     }
192
193
194     /**
195      * Prints this <code>Constraint</code>.
196      */

197     public void print() {
198         super.print();
199         String JavaDoc format = BundleReader.getValue("Name_Value_Pair_Format");//NOI18N
200
Object JavaDoc[] arguments =
201             new Object JavaDoc[]{"Cardinal", String.valueOf(cardinal)}; //NOI18N
202
System.out.println(MessageFormat.format(format, arguments));
203     }
204 }
205
Popular Tags