1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. 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 org.apache.commons.cli2.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 org.apache.commons.cli2.resource.ResourceConstants; 26 import org.apache.commons.cli2.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 Rob Oxspring 50 * @author John Keyes 51 */ 52 public class UrlValidator implements Validator { 53 /** allowed protocol */ 54 private String protocol = null; 55 56 /** 57 * Creates a UrlValidator. 58 */ 59 public UrlValidator() { 60 } 61 62 /** 63 * Creates a UrlValidator for the specified protocol. 64 * @param protocol the protocol to be used 65 */ 66 public UrlValidator(final String protocol) { 67 setProtocol(protocol); 68 } 69 70 /** 71 * Validate the list of values against the list of permitted values. 72 * If a value is valid, replace the string in the <code>values</code> 73 * {@link java.util.List} with the { java.net.URL} instance. 74 * 75 * @see org.apache.commons.cli2.validation.Validator#validate(java.util.List) 76 */ 77 public void validate(final List values) 78 throws InvalidArgumentException { 79 for (final ListIterator i = values.listIterator(); i.hasNext();) { 80 final String name = (String) i.next(); 81 82 try { 83 final URL url = new URL(name); 84 85 if ((protocol != null) && !protocol.equals(url.getProtocol())) { 86 throw new InvalidArgumentException(name); 87 } 88 89 i.set(url); 90 } catch (final MalformedURLException mue) { 91 throw new InvalidArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.URLVALIDATOR_MALFORMED_URL, 92 new Object[] { 93 name 94 })); 95 } 96 } 97 } 98 99 /** 100 * Returns the protocol that must be used by a valid URL. 101 * 102 * @return the protocol that must be used by a valid URL. 103 */ 104 public String getProtocol() { 105 return protocol; 106 } 107 108 /** 109 * Specifies the protocol that a URL must have to be valid. 110 * 111 * @param protocol the protocol that a URL must have to be valid. 112 */ 113 public void setProtocol(String protocol) { 114 this.protocol = protocol; 115 } 116 }