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  
20  package org.apache.myfaces.commons.validator;
21  
22  import javax.faces.application.FacesMessage;
23  import javax.faces.component.UIComponent;
24  import javax.faces.context.FacesContext;
25  import javax.faces.validator.ValidatorException;
26  
27  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFProperty;
28  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFValidator;
29  
30  /**
31   * A custom validator for url format, based upons Jakarta Commons.
32   * 
33   *
34   * @author Fabian Frederick
35   *
36   * @version $Revision: 1021618 $ $Date: 2010-10-11 23:08:53 -0500 (Mon, 11 Oct 2010) $
37   */
38  @JSFValidator(
39     name = "mcv:validateUrl",
40     clazz = "org.apache.myfaces.commons.validator.UrlValidator",
41     tagClass = "org.apache.myfaces.commons.validator.ValidateUrlTag",
42     serialuidtag = "6041422002721046221L")
43  public abstract class AbstractUrlValidator extends ValidatorBase {
44  
45      /**
46       * <p>The standard converter id for this converter.</p>
47       */
48      public static final String     VALIDATOR_ID        = "org.apache.myfaces.commons.validator.Url";
49      /**
50       * <p>The message identifier of the {@link FacesMessage} to be created if
51       * the maximum length check fails.</p>
52       */
53      public static final String URL_MESSAGE_ID = "org.apache.myfaces.commons.validator.Url.INVALID";
54       
55      public AbstractUrlValidator(){
56  
57      }
58  
59      /**
60       * method that validates an url address.
61       * it uses the commons-validator
62       */
63      public void validate(
64          FacesContext facesContext,
65          UIComponent uiComponent,
66          Object value)
67          throws ValidatorException {
68  
69  
70              if (facesContext == null) throw new NullPointerException("facesContext");
71              if (uiComponent == null) throw new NullPointerException("uiComponent");
72  
73              if (value == null)
74              {
75                  return;
76              }
77              
78              org.apache.commons.validator.UrlValidator urlValidator = initValidator();
79              
80              if (!urlValidator.isValid(value.toString())) {
81                  Object[] args = {value.toString()};
82                  throw new ValidatorException(getFacesMessage(URL_MESSAGE_ID, args));
83              }
84  
85      }
86      
87      private org.apache.commons.validator.UrlValidator initValidator()
88      {
89          int options = 0;
90          
91          if (isAllow2Slashes())
92          {
93              options = options | org.apache.commons.validator.UrlValidator.ALLOW_2_SLASHES; 
94          }
95          
96          if (isAllowAllSchemas())
97          {
98              options = options | org.apache.commons.validator.UrlValidator.ALLOW_ALL_SCHEMES;
99          }
100         
101         String [] schemesList = getSchemesList(); 
102         org.apache.commons.validator.UrlValidator urlValidator = null;
103         if (schemesList == null){
104             urlValidator = new 
105                 org.apache.commons.validator.UrlValidator(options);
106         }
107         else
108         {
109             urlValidator = new 
110                 org.apache.commons.validator.UrlValidator(schemesList,options);
111         }
112         return urlValidator;
113     }
114     
115     private String[] getSchemesList(){
116         if (getSchemes() == null)
117         {
118             return null;
119         }
120         String [] list = getSchemes().split(",");
121         String [] resp = new String [list.length];
122         
123         for (int i = 0; i < list.length; i++)
124         {
125             resp[i] = list[i].trim();
126         }        
127         return resp;        
128     }
129         
130     public abstract void setSchemes(String _schemes);
131 
132     /**
133      *  CSV values that indicates the set of schemes to check this url.
134      *  
135      *  If allowAllSchemas = true, the values of this field are ignored.
136      * 
137      *  If no schemes are provided, default to this set ("http", "https", "ftp").
138      * 
139      */
140     @JSFProperty
141     public abstract String getSchemes();
142 
143     public abstract void setAllow2Slashes(boolean _allow2Slashes);
144 
145     /**
146      *  Allow two slashes in the path component of the URL.
147      * 
148      * 
149      */
150     @JSFProperty(
151        defaultValue = "false")
152     public abstract boolean isAllow2Slashes();
153 
154     public abstract void setAllowAllSchemas(boolean _allowAllSchemas);
155 
156     /**
157      *  Allows all validly formatted schemes to pass validation instead of 
158      *  supplying a set of valid schemes.
159      *  
160      */
161     @JSFProperty(
162        defaultValue = "false")
163     public abstract boolean isAllowAllSchemas();
164 
165 }