Source for net.dpml.cli.validation.URLValidator

   1: /*
   2:  * Copyright 2003-2005 The Apache Software Foundation
   3:  * Copyright 2005 Stephen McConnell
   4:  *
   5:  * Licensed under the Apache License, Version 2.0 (the "License");
   6:  * you may not use this file except in compliance with the License.
   7:  * You may obtain a copy of the License at
   8:  *
   9:  *     http://www.apache.org/licenses/LICENSE-2.0
  10:  *
  11:  * Unless required by applicable law or agreed to in writing, software
  12:  * distributed under the License is distributed on an "AS IS" BASIS,
  13:  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14:  * See the License for the specific language governing permissions and
  15:  * limitations under the License.
  16:  */
  17: package net.dpml.cli.validation;
  18: 
  19: import java.net.MalformedURLException;
  20: import java.net.URL;
  21: 
  22: import java.util.List;
  23: import java.util.ListIterator;
  24: 
  25: import net.dpml.cli.resource.ResourceConstants;
  26: import net.dpml.cli.resource.ResourceHelper;
  27: 
  28: /**
  29:  * The <code>URLValidator</code> validates the string argument
  30:  * values are URLs.  If the value is a URL, the string value in
  31:  * the {@link java.util.List} of values is replaced with the
  32:  * {@link java.net.URL} instance.
  33:  *
  34:  * URLs can also be validated based on their scheme by using
  35:  * the {@link #setProtocol setProtocol} method, or by using the specified
  36:  * {@link #URLValidator(java.lang.String) constructor}.
  37:  *
  38:  * The following example shows how to limit the valid values
  39:  * for the site argument to 'https' URLs.
  40:  *
  41:  * <pre>
  42:  * ...
  43:  * ArgumentBuilder builder = new ArgumentBuilder();
  44:  * Argument site =
  45:  *     builder.withName("site");
  46:  *            .withValidator(new URLValidator("https"));
  47:  * </pre>
  48:  *
  49:  * @author <a href="@PUBLISHER-URL@">@PUBLISHER-NAME@</a>
  50:  * @version @PROJECT-VERSION@
  51:  */
  52: public class URLValidator implements Validator 
  53: {
  54:     /** allowed protocol */
  55:     private String m_protocol = null;
  56: 
  57:     /**
  58:      * Creates a URLValidator.
  59:      */
  60:     public URLValidator() 
  61:     {
  62:     }
  63: 
  64:     /**
  65:      * Creates a URLValidator for the specified protocol.
  66:      * @param protocol the url protocol
  67:      */
  68:     public URLValidator( final String protocol ) 
  69:     {
  70:         setProtocol( protocol );
  71:     }
  72: 
  73:    /**
  74:     * Validate the list of values against the list of permitted values.
  75:     * If a value is valid, replace the string in the <code>values</code>
  76:     * {@link java.util.List} with the { java.net.URL} instance.
  77:     *
  78:     * @param values the list of values to validate 
  79:     * @exception InvalidArgumentException if a value is invalid
  80:     * @see net.dpml.cli.validation.Validator#validate(java.util.List)
  81:     */
  82:     public void validate( final List values ) throws InvalidArgumentException
  83:     {
  84:         for( final ListIterator i = values.listIterator(); i.hasNext();) 
  85:         {
  86:             final Object next = i.next();
  87:             if( next instanceof URL )
  88:             {
  89:                 return;
  90:             }
  91:             final String name = (String) next;
  92:             try
  93:             {
  94:                 final URL url = new URL( name );
  95:                 if( ( m_protocol != null ) && !m_protocol.equals( url.getProtocol() ) ) 
  96:                 {
  97:                     throw new InvalidArgumentException( name );
  98:                 }
  99:                 i.set( url );
 100:             }
 101:             catch( final MalformedURLException mue ) 
 102:             {
 103:                 throw new InvalidArgumentException(
 104:                   ResourceHelper.getResourceHelper().getMessage(
 105:                     ResourceConstants.URLVALIDATOR_MALFORMED_URL,
 106:                     new Object[]{name} ) );
 107:             }
 108:         }
 109:     }
 110: 
 111:     /**
 112:      * Returns the protocol that must be used by a valid URL.
 113:      *
 114:      * @return the protocol that must be used by a valid URL.
 115:      */
 116:     public String getProtocol()
 117:     {
 118:         return m_protocol;
 119:     }
 120: 
 121:     /**
 122:      * Specifies the protocol that a URL must have to be valid.
 123:      *
 124:      * @param protocol the protocol that a URL must have to be valid.
 125:      */
 126:     public void setProtocol( String protocol )
 127:     {
 128:         m_protocol = protocol;
 129:     }
 130: }