KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > share > configbean > customizers > common > ValidationSupport


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  * ValidationSupport.java
21  *
22  * Created on November 11, 2003, 10:59 AM
23  */

24
25 package org.netbeans.modules.j2ee.sun.share.configbean.customizers.common;
26
27 import java.text.MessageFormat JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.ResourceBundle JavaDoc;
32
33 import org.netbeans.modules.j2ee.sun.validation.constraints.ConstraintFailure;
34 import org.netbeans.modules.j2ee.sun.validation.ValidationManager;
35 import org.netbeans.modules.j2ee.sun.validation.ValidationManagerFactory;
36
37 /**
38  *
39  * @author Rajeshwar Patil
40  * @version %I%, %G%
41  */

42 public class ValidationSupport {
43
44     static final ResourceBundle JavaDoc bundle =
45         ResourceBundle.getBundle(
46             "org.netbeans.modules.j2ee.sun.share.configbean.customizers.common.Bundle"); // NOI18N
47

48     private ValidationManager validationManager;
49
50
51     /** Creates a new instance of ValidationSupport */
52     public ValidationSupport() {
53         ValidationManagerFactory validationManagerFactory =
54             new ValidationManagerFactory();
55         validationManager = validationManagerFactory.getValidationManager();
56     }
57
58
59     public Collection JavaDoc validate(String JavaDoc value, String JavaDoc xpath, String JavaDoc label){
60         ArrayList JavaDoc errors = new ArrayList JavaDoc();
61
62         Collection JavaDoc failures =
63             validationManager.validateIndividualProperty(value,xpath,label);
64
65         if(failures != null){
66             Iterator JavaDoc iterator = failures.iterator();
67             ConstraintFailure failure;
68             String JavaDoc error;
69
70             while(iterator.hasNext()){
71                 Object JavaDoc object = iterator.next();
72
73                 if(object instanceof ConstraintFailure){
74                     failure = (ConstraintFailure)object;
75                     error = failure.getName() + ": " + //NOI18N
76
failure.getGenericfailureMessage();
77                     errors.add(error);
78                 }
79             }
80        }
81         return errors;
82     }
83
84
85     /**
86      * Returns true if the given xpath represents mandatory field
87      *
88      * @param xpath the given xpath.
89      *
90      * @return <code>boolean</code> <code>true</code> if the given xpath is
91      * of mandatory field; else returns <code>false</code>
92      */

93     public boolean isRequiredProperty(String JavaDoc xpath){
94         boolean isRequired = false;
95         String JavaDoc property = ""; //NOI18N
96
java.util.Collection JavaDoc errors = validate(property, xpath, null);
97         if(!errors.isEmpty()){
98             isRequired = true;
99         }
100         return isRequired;
101     }
102
103
104     /**
105      * Returns marked-label for the given label. Marked labels are used in case
106      * of madatory fields.
107      *
108      * @param label the given label
109      *
110      * @return <code>String</code> the marked label. Marked label is formed by
111      * appending "* " to the given field.
112      */

113     public String JavaDoc getMarkedLabel(String JavaDoc label){
114         String JavaDoc format = bundle.getString("FMT_Required_Field_Label"); //NOI18N
115
String JavaDoc requiedMark = bundle.getString("LBL_RequiredMark"); //NOI18N
116
Object JavaDoc[] arguments = new Object JavaDoc[]{requiedMark, label};
117         return MessageFormat.format(format, arguments);
118     }
119 }
120
Popular Tags