1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 package org.apache.myfaces.commons.validator; 20 21 import javax.faces.application.FacesMessage; 22 import javax.faces.component.UIComponent; 23 import javax.faces.context.FacesContext; 24 import javax.faces.validator.ValidatorException; 25 26 import org.apache.myfaces.commons.util.MessageUtils; 27 28 /** 29 * A custom validator for isbn codes, based upons Jakarta Commons. 30 * 31 * @JSFValidator 32 * name = "mcv:validateISBN" 33 * tagClass = "org.apache.myfaces.commons.validator.ValidateISBNTag" 34 * serialuidtag = "5230653358032218656L" 35 * 36 * @author <a href="mailto:matzew@apache.org">Matthias Weßendorf</a> (latest modification by $Author: skitching $) 37 * @version $Revision: 673801 $ $Date: 2008-07-03 15:58:48 -0500 (Thu, 03 Jul 2008) $ 38 */ 39 40 public class ISBNValidator extends ValidatorBase { 41 42 /** 43 * <p>The standard converter id for this converter.</p> 44 */ 45 public static final String VALIDATOR_ID = "org.apache.myfaces.commons.validator.ISBN"; 46 /** 47 * <p>The message identifier of the {@link FacesMessage} to be created if 48 * the maximum length check fails.</p> 49 */ 50 public static final String ISBN_MESSAGE_ID = "org.apache.myfaces.commons.validator.ISBN.INVALID"; 51 52 /** 53 * <p>isbnValidator</p> 54 */ 55 private org.apache.commons.validator.ISBNValidator isbnValidator; 56 57 public ISBNValidator(){ 58 isbnValidator = new org.apache.commons.validator.ISBNValidator(); 59 } 60 61 /** 62 * methode that validates isbn codes. 63 * it uses the commons-validator 64 */ 65 public void validate( 66 FacesContext facesContext, 67 UIComponent uiComponent, 68 Object value) 69 throws ValidatorException { 70 71 72 if (facesContext == null) throw new NullPointerException("facesContext"); 73 if (uiComponent == null) throw new NullPointerException("uiComponent"); 74 75 if (value == null) 76 { 77 return; 78 } 79 80 if (!isbnValidator.isValid( value.toString())) { 81 Object[] args = {value.toString()}; 82 String message = getMessage(); 83 if (null == message) message = ISBN_MESSAGE_ID; 84 85 throw new ValidatorException(MessageUtils.getMessage(FacesMessage.SEVERITY_ERROR, message, args)); 86 } 87 88 89 } 90 91 }