View Javadoc

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.custom.isbnvalidator;
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.shared_tomahawk.util.MessageUtils;
27  import org.apache.myfaces.tomahawk.util.Constants;
28  import org.apache.myfaces.validator.ValidatorBase;
29  
30  /**
31   * A custom validator for isbn codes, based upons Jakarta Commons.
32   * 
33   * @JSFValidator
34   *   name = "s:validateISBN"
35   *   tagClass = "org.apache.myfaces.custom.isbnvalidator.ValidateISBNTag"
36   *   serialuidtag = "5230653358032218656L"
37   *   
38   * @author <a href="mailto:matzew@apache.org">Matthias We&szlig;endorf</a> (latest modification by $Author$)
39   * @version $Revision$ $Date$
40   */
41  
42  public class ISBNValidator extends ValidatorBase {
43  
44      /**
45       * <p>The standard converter id for this converter.</p>
46       */
47      public static final String     VALIDATOR_ID        = "org.apache.myfaces.validator.ISBN";
48      /**
49       * <p>The message identifier of the {@link FacesMessage} to be created if
50       * the maximum length check fails.</p>
51       */
52      public static final String ISBN_MESSAGE_ID = "org.apache.myfaces.ISBN.INVALID";
53  
54      /**
55       * <p>isbnValidator</p>
56       */
57      private org.apache.commons.validator.ISBNValidator isbnValidator;
58  
59      public ISBNValidator(){
60          isbnValidator = new org.apache.commons.validator.ISBNValidator();
61      }
62  
63      /**
64       * methode that validates isbn codes.
65       * it uses the commons-validator
66       */
67      public void validate(
68          FacesContext facesContext,
69          UIComponent uiComponent,
70          Object value)
71          throws ValidatorException {
72  
73  
74              if (facesContext == null) throw new NullPointerException("facesContext");
75              if (uiComponent == null) throw new NullPointerException("uiComponent");
76  
77              if (value == null)
78              {
79                  return;
80              }
81          
82              if (!isbnValidator.isValid( value.toString())) {
83                  Object[] args = {value.toString()};
84                  String message = getMessage();
85                  if (null == message)  message = ISBN_MESSAGE_ID;
86  
87                  throw new ValidatorException(MessageUtils.getMessage(Constants.TOMAHAWK_DEFAULT_BUNDLE, FacesMessage.SEVERITY_ERROR, message, args, facesContext));
88              }
89              
90  
91      }
92  
93  }