KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > custom > isbnvalidator > ISBNValidator


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.custom.isbnvalidator;
17
18 import org.apache.myfaces.util.MessageUtils;
19
20 import javax.faces.application.FacesMessage;
21 import javax.faces.component.UIComponent;
22 import javax.faces.context.FacesContext;
23 import javax.faces.validator.Validator;
24 import javax.faces.validator.ValidatorException;
25
26 /**
27  * @author <a HREF="mailto:matzew@apache.org">Matthias We�endorf</a> (latest modification by $Author: matzew $)
28  * @version $Revision: 1.3 $ $Date: 2004/11/30 09:37:42 $
29  * $Log: ISBNValidator.java,v $
30  * Revision 1.3 2004/11/30 09:37:42 matzew
31  * changes i18n-messages for validation
32  *
33  * Revision 1.2 2004/11/23 04:59:24 svieujot
34  * Remove "unmappable character for encoding UTF8" warning.
35  *
36  * Revision 1.1 2004/11/20 20:17:05 matzew
37  * added new validator for ISBN codes
38  *
39  */

40
41 public class ISBNValidator implements Validator {
42     
43     /**
44      * <p>The standard converter id for this converter.</p>
45      */

46     public static final String JavaDoc VALIDATOR_ID = "org.apache.myfaces.validator.ISBN";
47     /**
48      * <p>The message identifier of the {@link FacesMessage} to be created if
49      * the maximum length check fails.</p>
50      */

51     public static final String JavaDoc ISBN_MESSAGE_ID = "org.apache.myfaces.ISBN.INVALID";
52     
53     /**
54      * <p>isbnValidator</p>
55      */

56     private org.apache.commons.validator.ISBNValidator isbnValidator;
57     
58     public ISBNValidator(){
59         isbnValidator = new org.apache.commons.validator.ISBNValidator();
60     }
61
62     /**
63      * methode that validates isbn codes.
64      * it uses the commons-validator
65      */

66     public void validate(
67         FacesContext facesContext,
68         UIComponent uiComponent,
69         Object JavaDoc value)
70         throws ValidatorException {
71     
72     
73             if (facesContext == null) throw new NullPointerException JavaDoc("facesContext");
74             if (uiComponent == null) throw new NullPointerException JavaDoc("uiComponent");
75
76             if (value == null)
77             {
78                 return;
79             }
80             if (!isbnValidator.isValid( value.toString())) {
81                 Object JavaDoc[] args = {value.toString()};
82                 throw new ValidatorException(MessageUtils.getMessage(FacesMessage.SEVERITY_ERROR,ISBN_MESSAGE_ID, args));
83                 
84             }
85
86     }
87
88 }
89
Popular Tags