1- /**
2- * Copyright 2003-2004 The Apache Software Foundation
1+ /*
2+ * Copyright 2003-2005 The Apache Software Foundation
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
2121import java .util .ListIterator ;
2222
2323/**
24- * A Validator instance that accepts urls
24+ * The <code>UrlValidator</code> validates the string argument
25+ * values are URLs. If the value is a URL, the string value in
26+ * the {@link java.util.List} of values is replaced with the
27+ * {@link java.net.URL} instance.
28+ *
29+ * URLs can also be validated based on their scheme by using
30+ * the {@link #setProtocol setProtocol} method, or by using the specified
31+ * {@link UrlValidator(java.lang.String) constructor}.
32+ *
33+ * The following example shows how to limit the valid values
34+ * for the site argument to 'https' URLs.
35+ *
36+ * <pre>
37+ * ...
38+ * ArgumentBuilder builder = new ArgumentBuilder();
39+ * Argument site =
40+ * builder.withName("site");
41+ * .withValidator(new URLValidator("https"));
42+ * </pre>
43+ *
44+ * @author Rob Oxspring
45+ * @author John Keyes
2546 */
2647public class UrlValidator implements Validator {
2748
49+ /** allowed protocol */
2850 private String protocol = null ;
2951
52+ /**
53+ * Creates a UrlValidator.
54+ */
55+ public UrlValidator () {
56+ }
57+
58+ /**
59+ * Creates a UrlValidator for the specified protocol.
60+ */
61+ public UrlValidator (final String protocol ) {
62+ setProtocol (protocol );
63+ }
64+
65+ /**
66+ * Validate the list of values against the list of permitted values.
67+ * If a value is valid, replace the string in the <code>values</code>
68+ * {@link java.util.List} with the {@link java.net.URL}.
69+ *
70+ * @see org.apache.commons.cli2.validation.Validator#validate(java.util.List)
71+ */
3072 public void validate (final List values ) throws InvalidArgumentException {
3173 for (final ListIterator i = values .listIterator (); i .hasNext ();) {
3274 final String name = (String )i .next ();
@@ -47,16 +89,21 @@ public void validate(final List values) throws InvalidArgumentException {
4789 }
4890
4991 /**
50- * @return the protocol that must be used by a valid url
92+ * Returns the protocol that must be used by a valid URL.
93+ *
94+ * @return the protocol that must be used by a valid URL.
5195 */
5296 public String getProtocol () {
5397 return protocol ;
5498 }
5599
56100 /**
57- * @param protocol the protocol that a valid url must use
101+ * Specifies the protocol that a URL must have to be valid.
102+ *
103+ * @param protocol the protocol that a URL must have to be valid.
58104 */
59105 public void setProtocol (String protocol ) {
60106 this .protocol = protocol ;
61107 }
108+
62109}
0 commit comments