Frames | No Frames |
1: /* 2: * Copyright 2005 Stephen J. McConnell 3: * 4: * Licensed under the Apache License, Version 2.0 (the "License"); 5: * you may not use this file except in compliance with the License. 6: * You may obtain a copy of the License at 7: * 8: * http://www.apache.org/licenses/LICENSE-2.0 9: * 10: * Unless required by applicable law or agreed to in writing, software 11: * distributed under the License is distributed on an "AS IS" BASIS, 12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13: * See the License for the specific language governing permissions and 14: * limitations under the License. 15: */ 16: package net.dpml.cli.validation; 17: 18: import java.net.URISyntaxException; 19: import java.net.URI; 20: 21: import java.util.List; 22: import java.util.ListIterator; 23: 24: /** 25: * The <code>URIValidator</code> validates the string argument 26: * values are valid URIs. If the value is a URI, the string value in 27: * the {@link java.util.List} of values is replaced with the 28: * {@link java.net.URI} instance. 29: * 30: * The following example shows how to limit the valid values 31: * for the site argument to 'artifact' URIs. 32: * 33: * <pre> 34: * ... 35: * ArgumentBuilder builder = new ArgumentBuilder(); 36: * Argument plugin = 37: * builder 38: * .withName("plugin"); 39: * .withValidator( new URIValidator( "artifact", "link" ) ); 40: * </pre> 41: * 42: * @author <a href="@PUBLISHER-URL@">@PUBLISHER-NAME@</a> 43: * @version @PROJECT-VERSION@ 44: */ 45: public class URIValidator implements Validator 46: { 47: private final String[] m_schemes; 48: 49: /** 50: * Creates a UriValidator. 51: */ 52: public URIValidator() 53: { 54: m_schemes = new String[0]; 55: } 56: 57: /** 58: * Creates a UriValidator for the specified scheme. 59: * @param scheme the uri scheme 60: */ 61: public URIValidator( final String scheme ) 62: { 63: m_schemes = new String[]{scheme}; 64: } 65: 66: /** 67: * Creates a UriValidator for the specified schemes. 68: * @param schemes an array of schemes 69: */ 70: public URIValidator( final String[] schemes ) 71: { 72: m_schemes = schemes; 73: } 74: 75: /** 76: * Validate the list of values against the list of permitted values. 77: * If a value is valid, replace the string in the <code>values</code> 78: * {@link java.util.List} with the {@link java.net.URI} instance. 79: * 80: * @param values the list of values to validate 81: * @exception InvalidArgumentException if a value is invalid 82: * @see net.dpml.cli.validation.Validator#validate(java.util.List) 83: */ 84: public void validate( final List values ) 85: throws InvalidArgumentException 86: { 87: for( final ListIterator i = values.listIterator(); i.hasNext();) 88: { 89: final Object object = i.next(); 90: if( object instanceof URI ) 91: { 92: break; 93: } 94: final String name = (String) object; 95: try 96: { 97: final URI uri = new URI( name ); 98: if( m_schemes.length == 0 ) 99: { 100: i.set( uri ); 101: } 102: else 103: { 104: if( match( uri ) ) 105: { 106: i.set( uri ); 107: } 108: else 109: { 110: throw new InvalidArgumentException( name ); 111: } 112: } 113: } 114: catch( final URISyntaxException e ) 115: { 116: final String error = 117: "Bad uri syntax in value [" + name + "]."; 118: throw new InvalidArgumentException( error ); 119: } 120: } 121: } 122: 123: private boolean match( URI uri ) 124: { 125: String scheme = uri.getScheme(); 126: for( int i=0; i<m_schemes.length; i++ ) 127: { 128: if( scheme.startsWith( m_schemes[i] ) ) 129: { 130: return true; 131: } 132: } 133: return false; 134: } 135: }