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