commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject svn commit: r639943 [7/17] - in /commons/proper/cli/trunk/src: java/org/apache/commons/cli2/ java/org/apache/commons/cli2/builder/ java/org/apache/commons/cli2/commandline/ java/org/apache/commons/cli2/option/ java/org/apache/commons/cli2/resource/ jav...
Date Sat, 22 Mar 2008 03:08:35 GMT
Modified: commons/proper/cli/trunk/src/java/org/apache/commons/cli2/validation/DateValidator.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/validation/DateValidator.java?rev=639943&r1=639942&r2=639943&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/java/org/apache/commons/cli2/validation/DateValidator.java (original)
+++ commons/proper/cli/trunk/src/java/org/apache/commons/cli2/validation/DateValidator.java Fri Mar 21 20:08:23 2008
@@ -1 +1,308 @@
-/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements.  See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License.  You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.commons.cli2.validation;import java.text.DateFormat;import java.text.ParsePosition;import java.util.Date;import java.util.List;import java.util.ListIterator;import org.apache.commons.c
 li2.resource.ResourceConstants;import org.apache.commons.cli2.resource.ResourceHelper;/** * The <code>DateValidator</code> validates the argument values * are date or time value(s). * * The following example shows how to validate that * an argument value(s) is a Date of the following * type: d/M/yy (see {@link java.text.DateFormat}). * * <pre> * DateFormat date = new SimpleDateFormat("d/M/yy"); * ... * ArgumentBuilder builder = new ArgumentBuilder(); * Argument dateFormat = *     builder.withName("date"); *            .withValidator(new DateValidator(dateFormat)); * </pre> * * The following example shows how to validate that * an argument value(s) is a time of the following * type: HH:mm:ss (see {@link java.text.DateFormat}). * * <pre> * DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss"); * ... * ArgumentBuilder builder = new ArgumentBuilder(); * Argument time = *     builder.withName("time"); *            .withValidator(new DateValidator(timeFormat)); * </pre> * * @au
 thor John Keyes * * @see java.text.DateFormat */public class DateValidator implements Validator {    /** i18n */    private static final ResourceHelper resources = ResourceHelper.getResourceHelper();    /** an array of permitted DateFormats */    private DateFormat[] formats;    /** minimum Date allowed i.e: a valid date occurs later than this date */    private Date minimum;    /** maximum Date allowed i.e: a valid date occurs earlier than this date */    private Date maximum;    /** leniant parsing */    private boolean isLenient;    /**     * Creates a Validator for the default date/time format     */    public DateValidator() {        this(DateFormat.getInstance());    }    /**     * Creates a Validator for the specified DateFormat.     *     * @param format     *            a DateFormat which dates must conform to     */    public DateValidator(final DateFormat format) {        setFormat(format);    }    /**     * Creates a Validator for the List of specified DateFormat
 s.     *     * @param formats     *            a List of DateFormats which dates must conform to     */    public DateValidator(final List formats) {        setFormats(formats);    }    /**     * Creates a Validator for dates.     *     * @return DateValidator a Validator for dates     */    public static DateValidator getDateInstance() {        return new DateValidator(DateFormat.getDateInstance());    }    /**     * Creates a Validator for times.     *     * @return DateValidator a Validator for times     */    public static DateValidator getTimeInstance() {        return new DateValidator(DateFormat.getTimeInstance());    }    /**     * Creates a Validator for date/times     *     * @return DateValidator a Validator for date/times     */    public static DateValidator getDateTimeInstance() {        return new DateValidator(DateFormat.getDateTimeInstance());    }    /**     * Validate each String value in the specified List against this instances     * permitted DateFormat
 s.     *     * If a value is valid then it's <code>String</code> value in the list is     * replaced with it's <code>Date</code> value.     *     * @see org.apache.commons.cli2.validation.Validator#validate(java.util.List)     */    public void validate(final List values)        throws InvalidArgumentException {        // for each value        for (final ListIterator i = values.listIterator(); i.hasNext();) {            final String value = (String) i.next();            Date date = null;            // create a resuable ParsePosition instance            final ParsePosition pp = new ParsePosition(0);            // for each permitted DateFormat            for (int f = 0; (f < this.formats.length) && (date == null); ++f) {                // reset the parse position                pp.setIndex(0);                date = this.formats[f].parse(value, pp);                // if the wrong number of characters have been parsed                if (pp.getIndex() < value.length()) {         
            date = null;                }            }            // if date has not been set throw an InvalidArgumentException            if (date == null) {                throw new InvalidArgumentException(value);            }            // if the date is outside the bounds            if (isDateEarlier(date) || isDateLater(date)) {                throw new InvalidArgumentException(resources.getMessage(ResourceConstants.DATEVALIDATOR_DATE_OUTOFRANGE,                                                                        value));            }            // replace the value in the list with the actual Date            i.set(date);        }    }    /**     * Sets whether this validator uses lenient parsing.     *     * @param lenient whether this validator uses lenient parsing     */    public void setLenient(final boolean lenient) {        for (int i = 0; i < this.formats.length; i++) {            this.formats[i].setLenient(lenient);        }        this.isLenient = lenient; 
    }    /**     * Returns whether this validator uses lenient parsing.     *     * @return whether this validator uses lenient parsing     */    public boolean isLenient() {        return this.isLenient;    }    /**     * Returns the maximum date permitted.     *     * @return Date the maximum date permitted. If no maximum date has been     *         specified then return <code>null</code>.     */    public Date getMaximum() {        return maximum;    }    /**     * Sets the maximum Date to the specified value.     *     * @param maximum     *            the maximum Date permitted     */    public void setMaximum(final Date maximum) {        this.maximum = maximum;    }    /**     * Returns the minimum date permitted.     *     * @return Date the minimum date permitted. If no minimum date has been     *         specified then return <code>null</code>.     */    public Date getMinimum() {        return minimum;    }    /**     * Sets the minimum Date to the specified value. 
     *     * @param minimum     *            the minimum Date permitted     */    public void setMinimum(Date minimum) {        this.minimum = minimum;    }    /**     * Returns whether the specified Date is later than the maximum date.     *     * @param date     *            the Date to evaluate     *     * @return boolean whether <code>date</code> is earlier than the maximum     *         date     */    private boolean isDateLater(Date date) {        return (maximum != null) && (date.getTime() > maximum.getTime());    }    /**     * Returns whether the specified Date is earlier than the minimum date.     *     * @param date     *            the Date to evaluate     *     * @return boolean whether <code>date</code> is earlier than the minimum     *         date     */    private boolean isDateEarlier(Date date) {        return (minimum != null) && (date.getTime() < minimum.getTime());    }    /**     * Sets the date format permitted.     *     * @param format     *         
      the format to use     */    public void setFormat(final DateFormat format) {        setFormats(new DateFormat[] { format });    }    /**     * Sets the date formats permitted.     *     * @param formats     *               the List of DateFormats to use     */    public void setFormats(final List formats) {        setFormats((DateFormat[]) formats.toArray(new DateFormat[formats.size()]));    }    /**     * Sets the date formats permitted.     *     * @param formats     *               the array of DateFormats to use     */    public void setFormats(final DateFormat[] formats) {        this.formats = formats;        setLenient(this.isLenient);    }    /**     * Gets the date formats permitted.     *     * @return the permitted formats     */    public DateFormat[] getFormats() {        return this.formats;    }}
\ No newline at end of file
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.cli2.validation;
+
+import java.text.DateFormat;
+import java.text.ParsePosition;
+
+import java.util.Date;
+import java.util.List;
+import java.util.ListIterator;
+
+import org.apache.commons.cli2.resource.ResourceConstants;
+import org.apache.commons.cli2.resource.ResourceHelper;
+
+/**
+ * The <code>DateValidator</code> validates the argument values
+ * are date or time value(s).
+ *
+ * The following example shows how to validate that
+ * an argument value(s) is a Date of the following
+ * type: d/M/yy (see {@link java.text.DateFormat}).
+ *
+ * <pre>
+ * DateFormat date = new SimpleDateFormat("d/M/yy");
+ * ...
+ * ArgumentBuilder builder = new ArgumentBuilder();
+ * Argument dateFormat =
+ *     builder.withName("date");
+ *            .withValidator(new DateValidator(dateFormat));
+ * </pre>
+ *
+ * The following example shows how to validate that
+ * an argument value(s) is a time of the following
+ * type: HH:mm:ss (see {@link java.text.DateFormat}).
+ *
+ * <pre>
+ * DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
+ * ...
+ * ArgumentBuilder builder = new ArgumentBuilder();
+ * Argument time =
+ *     builder.withName("time");
+ *            .withValidator(new DateValidator(timeFormat));
+ * </pre>
+ *
+ * @author John Keyes
+ *
+ * @see java.text.DateFormat
+ */
+public class DateValidator implements Validator {
+    /** i18n */
+    private static final ResourceHelper resources = ResourceHelper.getResourceHelper();
+
+    /** an array of permitted DateFormats */
+    private DateFormat[] formats;
+
+    /** minimum Date allowed i.e: a valid date occurs later than this date */
+    private Date minimum;
+
+    /** maximum Date allowed i.e: a valid date occurs earlier than this date */
+    private Date maximum;
+
+    /** leniant parsing */
+    private boolean isLenient;
+
+    /**
+     * Creates a Validator for the default date/time format
+     */
+    public DateValidator() {
+        this(DateFormat.getInstance());
+    }
+
+    /**
+     * Creates a Validator for the specified DateFormat.
+     *
+     * @param format
+     *            a DateFormat which dates must conform to
+     */
+    public DateValidator(final DateFormat format) {
+        setFormat(format);
+    }
+
+    /**
+     * Creates a Validator for the List of specified DateFormats.
+     *
+     * @param formats
+     *            a List of DateFormats which dates must conform to
+     */
+    public DateValidator(final List formats) {
+        setFormats(formats);
+    }
+
+    /**
+     * Creates a Validator for dates.
+     *
+     * @return DateValidator a Validator for dates
+     */
+    public static DateValidator getDateInstance() {
+        return new DateValidator(DateFormat.getDateInstance());
+    }
+
+    /**
+     * Creates a Validator for times.
+     *
+     * @return DateValidator a Validator for times
+     */
+    public static DateValidator getTimeInstance() {
+        return new DateValidator(DateFormat.getTimeInstance());
+    }
+
+    /**
+     * Creates a Validator for date/times
+     *
+     * @return DateValidator a Validator for date/times
+     */
+    public static DateValidator getDateTimeInstance() {
+        return new DateValidator(DateFormat.getDateTimeInstance());
+    }
+
+    /**
+     * Validate each String value in the specified List against this instances
+     * permitted DateFormats.
+     *
+     * If a value is valid then it's <code>String</code> value in the list is
+     * replaced with it's <code>Date</code> value.
+     *
+     * @see org.apache.commons.cli2.validation.Validator#validate(java.util.List)
+     */
+    public void validate(final List values)
+        throws InvalidArgumentException {
+        // for each value
+        for (final ListIterator i = values.listIterator(); i.hasNext();) {
+            final String value = (String) i.next();
+
+            Date date = null;
+
+            // create a resuable ParsePosition instance
+            final ParsePosition pp = new ParsePosition(0);
+
+            // for each permitted DateFormat
+            for (int f = 0; (f < this.formats.length) && (date == null); ++f) {
+                // reset the parse position
+                pp.setIndex(0);
+                date = this.formats[f].parse(value, pp);
+
+                // if the wrong number of characters have been parsed
+                if (pp.getIndex() < value.length()) {
+                    date = null;
+                }
+            }
+
+            // if date has not been set throw an InvalidArgumentException
+            if (date == null) {
+                throw new InvalidArgumentException(value);
+            }
+
+            // if the date is outside the bounds
+            if (isDateEarlier(date) || isDateLater(date)) {
+                throw new InvalidArgumentException(resources.getMessage(ResourceConstants.DATEVALIDATOR_DATE_OUTOFRANGE,
+                                                                        value));
+            }
+
+            // replace the value in the list with the actual Date
+            i.set(date);
+        }
+    }
+
+    /**
+     * Sets whether this validator uses lenient parsing.
+     *
+     * @param lenient whether this validator uses lenient parsing
+     */
+    public void setLenient(final boolean lenient) {
+        for (int i = 0; i < this.formats.length; i++) {
+            this.formats[i].setLenient(lenient);
+        }
+
+        this.isLenient = lenient;
+    }
+
+    /**
+     * Returns whether this validator uses lenient parsing.
+     *
+     * @return whether this validator uses lenient parsing
+     */
+    public boolean isLenient() {
+        return this.isLenient;
+    }
+
+    /**
+     * Returns the maximum date permitted.
+     *
+     * @return Date the maximum date permitted. If no maximum date has been
+     *         specified then return <code>null</code>.
+     */
+    public Date getMaximum() {
+        return maximum;
+    }
+
+    /**
+     * Sets the maximum Date to the specified value.
+     *
+     * @param maximum
+     *            the maximum Date permitted
+     */
+    public void setMaximum(final Date maximum) {
+        this.maximum = maximum;
+    }
+
+    /**
+     * Returns the minimum date permitted.
+     *
+     * @return Date the minimum date permitted. If no minimum date has been
+     *         specified then return <code>null</code>.
+     */
+    public Date getMinimum() {
+        return minimum;
+    }
+
+    /**
+     * Sets the minimum Date to the specified value.
+     *
+     * @param minimum
+     *            the minimum Date permitted
+     */
+    public void setMinimum(Date minimum) {
+        this.minimum = minimum;
+    }
+
+    /**
+     * Returns whether the specified Date is later than the maximum date.
+     *
+     * @param date
+     *            the Date to evaluate
+     *
+     * @return boolean whether <code>date</code> is earlier than the maximum
+     *         date
+     */
+    private boolean isDateLater(Date date) {
+        return (maximum != null) && (date.getTime() > maximum.getTime());
+    }
+
+    /**
+     * Returns whether the specified Date is earlier than the minimum date.
+     *
+     * @param date
+     *            the Date to evaluate
+     *
+     * @return boolean whether <code>date</code> is earlier than the minimum
+     *         date
+     */
+    private boolean isDateEarlier(Date date) {
+        return (minimum != null) && (date.getTime() < minimum.getTime());
+    }
+
+    /**
+     * Sets the date format permitted.
+     *
+     * @param format
+     *              the format to use
+     */
+    public void setFormat(final DateFormat format) {
+        setFormats(new DateFormat[] { format });
+    }
+
+    /**
+     * Sets the date formats permitted.
+     *
+     * @param formats
+     *               the List of DateFormats to use
+     */
+    public void setFormats(final List formats) {
+        setFormats((DateFormat[]) formats.toArray(new DateFormat[formats.size()]));
+    }
+
+    /**
+     * Sets the date formats permitted.
+     *
+     * @param formats
+     *               the array of DateFormats to use
+     */
+    public void setFormats(final DateFormat[] formats) {
+        this.formats = formats;
+        setLenient(this.isLenient);
+    }
+
+    /**
+     * Gets the date formats permitted.
+     *
+     * @return the permitted formats
+     */
+    public DateFormat[] getFormats() {
+        return this.formats;
+    }
+}

Modified: commons/proper/cli/trunk/src/java/org/apache/commons/cli2/validation/EnumValidator.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/validation/EnumValidator.java?rev=639943&r1=639942&r2=639943&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/java/org/apache/commons/cli2/validation/EnumValidator.java (original)
+++ commons/proper/cli/trunk/src/java/org/apache/commons/cli2/validation/EnumValidator.java Fri Mar 21 20:08:23 2008
@@ -1 +1,120 @@
-/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements.  See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License.  You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.commons.cli2.validation;import java.util.Iterator;import java.util.List;import java.util.Set;import org.apache.commons.cli2.resource.ResourceConstants;import org.apache.commons.cli2.re
 source.ResourceHelper;/** * The <code>EnumValidator</code> validates the string argument * values are valid. * * The following example shows how to limit the valid values * for the color argument to 'red', 'green', or 'blue'. * * <pre> * Set values = new HashSet(); * values.add("red"); * values.add("green"); * values.add("blue"); * ... * ArgumentBuilder builder = new ArgumentBuilder(); * Argument color = *     builder.withName("color"); *            .withValidator(new EnumValidator(values)); * </pre> * * @author John Keyes */public class EnumValidator implements Validator {    /** List of permitted values */    private Set validValues;    /**     * Creates a new EnumValidator for the specified values.     *     * @param values The list of permitted values     */    public EnumValidator(final Set values) {        setValidValues(values);    }    /**     * Validate the list of values against the list of permitted values.     *     * @see org.apache.commons.cli2.validation.Valid
 ator#validate(java.util.List)     */    public void validate(final List values)        throws InvalidArgumentException {        for (final Iterator iter = values.iterator(); iter.hasNext();) {            final String value = (String) iter.next();            if (!this.validValues.contains(value)) {                throw new InvalidArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.ENUM_ILLEGAL_VALUE,                                                                                                 new Object[] {                                                                                                     value,                                                                                                     getValuesAsString()                                                                                                 }));            }        }    }    /**     * Returns the permitted values in a comma separated String     *     * @return 
 String formatted list of values     */    String getValuesAsString() {        final StringBuffer buff = new StringBuffer();        buff.append("[");        for (final Iterator iter = this.validValues.iterator(); iter.hasNext();) {            buff.append("'").append(iter.next()).append("'");            if (iter.hasNext()) {                buff.append(", ");            }        }        buff.append("]");        return buff.toString();    }    /**     * Returns the Set of valid argument values.     *     * @return Returns the Set of valid argument values.     */    public Set getValidValues() {        return validValues;    }    /**     * Specifies the Set of valid argument values.     *     * @param validValues The Set of valid argument values.     */    protected void setValidValues(Set validValues) {        this.validValues = validValues;    }}
\ No newline at end of file
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.cli2.validation;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.commons.cli2.resource.ResourceConstants;
+import org.apache.commons.cli2.resource.ResourceHelper;
+
+/**
+ * The <code>EnumValidator</code> validates the string argument
+ * values are valid.
+ *
+ * The following example shows how to limit the valid values
+ * for the color argument to 'red', 'green', or 'blue'.
+ *
+ * <pre>
+ * Set values = new HashSet();
+ * values.add("red");
+ * values.add("green");
+ * values.add("blue");
+ * ...
+ * ArgumentBuilder builder = new ArgumentBuilder();
+ * Argument color =
+ *     builder.withName("color");
+ *            .withValidator(new EnumValidator(values));
+ * </pre>
+ *
+ * @author John Keyes
+ */
+public class EnumValidator implements Validator {
+    /** List of permitted values */
+    private Set validValues;
+
+    /**
+     * Creates a new EnumValidator for the specified values.
+     *
+     * @param values The list of permitted values
+     */
+    public EnumValidator(final Set values) {
+        setValidValues(values);
+    }
+
+    /**
+     * Validate the list of values against the list of permitted values.
+     *
+     * @see org.apache.commons.cli2.validation.Validator#validate(java.util.List)
+     */
+    public void validate(final List values)
+        throws InvalidArgumentException {
+        for (final Iterator iter = values.iterator(); iter.hasNext();) {
+            final String value = (String) iter.next();
+
+            if (!this.validValues.contains(value)) {
+                throw new InvalidArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.ENUM_ILLEGAL_VALUE,
+                                                                                                 new Object[] {
+                                                                                                     value,
+                                                                                                     getValuesAsString()
+                                                                                                 }));
+            }
+        }
+    }
+
+    /**
+     * Returns the permitted values in a comma separated String
+     *
+     * @return String formatted list of values
+     */
+    String getValuesAsString() {
+        final StringBuffer buff = new StringBuffer();
+
+        buff.append("[");
+
+        for (final Iterator iter = this.validValues.iterator(); iter.hasNext();) {
+            buff.append("'").append(iter.next()).append("'");
+
+            if (iter.hasNext()) {
+                buff.append(", ");
+            }
+        }
+
+        buff.append("]");
+
+        return buff.toString();
+    }
+
+    /**
+     * Returns the Set of valid argument values.
+     *
+     * @return Returns the Set of valid argument values.
+     */
+    public Set getValidValues() {
+        return validValues;
+    }
+
+    /**
+     * Specifies the Set of valid argument values.
+     *
+     * @param validValues The Set of valid argument values.
+     */
+    protected void setValidValues(Set validValues) {
+        this.validValues = validValues;
+    }
+}

Modified: commons/proper/cli/trunk/src/java/org/apache/commons/cli2/validation/FileValidator.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/validation/FileValidator.java?rev=639943&r1=639942&r2=639943&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/java/org/apache/commons/cli2/validation/FileValidator.java (original)
+++ commons/proper/cli/trunk/src/java/org/apache/commons/cli2/validation/FileValidator.java Fri Mar 21 20:08:23 2008
@@ -1 +1,265 @@
-/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements.  See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License.  You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.commons.cli2.validation;import java.io.File;import java.util.List;import java.util.ListIterator;/** * The <code>FileValidator</code> validates the string argument * values are files.  
 If the value is a file, the string value in * the {@link java.util.List} of values is replaced with the * {@link java.io.File} instance. * * The following attributes can also be specified using the * appropriate settors: * <ul> *  <li>writable</li> *  <li>readable</li> *  <li>hidden</li> *  <li>existing</li> *  <li>is a file</li> *  <li>is a directory</li> * </ul> * * The following example shows how to limit the valid values * for the config attribute to files that are readable, writeable, * and that already existing. * * <pre> * ... * ArgumentBuilder builder = new ArgumentBuilder(); * FileValidator validator = FileValidator.getExistingFileInstance(); * validator.setReadable(true); * validator.setWritable(true); * * Argument age = *     builder.withName("config"); *            .withValidator(validator); * </pre> * * @author Rob Oxspring * @author John Keyes */public class FileValidator implements Validator {    /**     * Returns a <code>FileValidator</code> for existing file
 s/directories.     *     * @return a <code>FileValidator</code> for existing files/directories.     */    public static FileValidator getExistingInstance() {        final FileValidator validator = new FileValidator();        validator.setExisting(true);        return validator;    }    /**     * Returns a <code>FileValidator</code> for existing files.     *     * @return a <code>FileValidator</code> for existing files.     */    public static FileValidator getExistingFileInstance() {        final FileValidator validator = new FileValidator();        validator.setExisting(true);        validator.setFile(true);        return validator;    }    /**     * Returns a <code>FileValidator</code> for existing directories.     *     * @return a <code>FileValidator</code> for existing directories.     */    public static FileValidator getExistingDirectoryInstance() {        final FileValidator validator = new FileValidator();        validator.setExisting(true);        validator.setDire
 ctory(true);        return validator;    }    /** whether the argument value is readable */    private boolean readable = false;    /** whether the argument value is writable */    private boolean writable = false;    /** whether the argument value exists */    private boolean existing = false;    /** whether the argument value is a directory */    private boolean directory = false;    /** whether the argument value is a file */    private boolean file = false;    /** whether the argument value is a hidden file or directory */    private boolean hidden = false;    /**     * Validate the list of values against the list of permitted values.     * If a value is valid, replace the string in the <code>values</code>     * {@link java.util.List} with the {@link java.io.File} instance.     *     * @see org.apache.commons.cli2.validation.Validator#validate(java.util.List)     */    public void validate(final List values) throws InvalidArgumentException {        for (final ListIterato
 r i = values.listIterator(); i.hasNext();) {            final String name = (String)i.next();            final File f = new File(name);            if ((existing && !f.exists())                || (file && !f.isFile())                || (directory && !f.isDirectory())                || (hidden && !f.isHidden())                || (readable && !f.canRead())                || (writable && !f.canWrite())) {                throw new InvalidArgumentException(name);            }            i.set(f);        }    }    /**     * Returns whether the argument values must represent directories.     *     * @return whether the argument values must represent directories.     */    public boolean isDirectory() {        return directory;    }    /**     * Specifies whether the argument values must represent directories.     *     * @param directory specifies whether the argument values must     * represent directories.     */    public void setDirectory(boolean directory) {        this.directo
 ry = directory;    }    /**     * Returns whether the argument values must represent existing     * files/directories.     *     * @return whether the argument values must represent existing     * files/directories.     */    public boolean isExisting() {        return existing;    }    /**     * Specifies whether the argument values must represent existing     * files/directories.     *     * @param existing specifies whether the argument values must     * represent existing files/directories.     */    public void setExisting(boolean existing) {        this.existing = existing;    }    /**     * Returns whether the argument values must represent directories.     *     * @return whether the argument values must represent directories.     */    public boolean isFile() {        return file;    }    /**     * Specifies whether the argument values must represent files.     *     * @param file specifies whether the argument values must     * represent files.     */    public voi
 d setFile(boolean file) {        this.file = file;    }    /**     * Returns whether the argument values must represent hidden     * files/directories.     *     * @return whether the argument values must represent hidden     * files/directories.     */    public boolean isHidden() {        return hidden;    }    /**     * Specifies whether the argument values must represent hidden     * files/directories.     *     * @param hidden specifies whether the argument values must     * represent hidden files/directories.     */    public void setHidden(boolean hidden) {        this.hidden = hidden;    }    /**     * Returns whether the argument values must represent readable     * files/directories.     *     * @return whether the argument values must represent readable     * files/directories.     */    public boolean isReadable() {        return readable;    }    /**     * Specifies whether the argument values must represent readable     * files/directories.     *     * @param r
 eadable specifies whether the argument values must     * represent readable files/directories.     */    public void setReadable(boolean readable) {        this.readable = readable;    }    /**     * Returns whether the argument values must represent writable     * files/directories.     *     * @return whether the argument values must represent writable     * files/directories.     */    public boolean isWritable() {        return writable;    }    /**     * Specifies whether the argument values must represent writable     * files/directories.     *     * @param writable specifies whether the argument values must     * represent writable files/directories.     */    public void setWritable(boolean writable) {        this.writable = writable;    }}
\ No newline at end of file
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.cli2.validation;
+
+import java.io.File;
+import java.util.List;
+import java.util.ListIterator;
+
+/**
+ * The <code>FileValidator</code> validates the string argument
+ * values are files.  If the value is a file, the string value in
+ * the {@link java.util.List} of values is replaced with the
+ * {@link java.io.File} instance.
+ *
+ * The following attributes can also be specified using the 
+ * appropriate settors:
+ * <ul>
+ *  <li>writable</li>
+ *  <li>readable</li>
+ *  <li>hidden</li>
+ *  <li>existing</li>
+ *  <li>is a file</li>
+ *  <li>is a directory</li>
+ * </ul>
+ *
+ * The following example shows how to limit the valid values
+ * for the config attribute to files that are readable, writeable,
+ * and that already existing.
+ *
+ * <pre>
+ * ...
+ * ArgumentBuilder builder = new ArgumentBuilder();
+ * FileValidator validator = FileValidator.getExistingFileInstance();
+ * validator.setReadable(true);
+ * validator.setWritable(true);
+ * 
+ * Argument age = 
+ *     builder.withName("config");
+ *            .withValidator(validator);
+ * </pre>
+ * 
+ * @author Rob Oxspring
+ * @author John Keyes
+ */
+public class FileValidator implements Validator {
+
+    /**
+     * Returns a <code>FileValidator</code> for existing files/directories.
+     *
+     * @return a <code>FileValidator</code> for existing files/directories.
+     */
+    public static FileValidator getExistingInstance() {
+        final FileValidator validator = new FileValidator();
+        validator.setExisting(true);
+        return validator;
+    }
+
+    /**
+     * Returns a <code>FileValidator</code> for existing files.
+     *
+     * @return a <code>FileValidator</code> for existing files.
+     */
+    public static FileValidator getExistingFileInstance() {
+        final FileValidator validator = new FileValidator();
+        validator.setExisting(true);
+        validator.setFile(true);
+        return validator;
+    }
+
+    /**
+     * Returns a <code>FileValidator</code> for existing directories.
+     *
+     * @return a <code>FileValidator</code> for existing directories.
+     */
+    public static FileValidator getExistingDirectoryInstance() {
+        final FileValidator validator = new FileValidator();
+        validator.setExisting(true);
+        validator.setDirectory(true);
+        return validator;
+    }
+
+    /** whether the argument value is readable */
+    private boolean readable = false;
+    
+    /** whether the argument value is writable */
+    private boolean writable = false;
+    
+    /** whether the argument value exists */
+    private boolean existing = false;
+    
+    /** whether the argument value is a directory */
+    private boolean directory = false;
+    
+    /** whether the argument value is a file */
+    private boolean file = false;
+
+    /** whether the argument value is a hidden file or directory */
+    private boolean hidden = false;
+
+    /**
+     * Validate the list of values against the list of permitted values.
+     * If a value is valid, replace the string in the <code>values</code>
+     * {@link java.util.List} with the {@link java.io.File} instance.
+     * 
+     * @see org.apache.commons.cli2.validation.Validator#validate(java.util.List)
+     */
+    public void validate(final List values) throws InvalidArgumentException {
+        for (final ListIterator i = values.listIterator(); i.hasNext();) {
+            final String name = (String)i.next();
+            final File f = new File(name);
+
+            if ((existing && !f.exists())
+                || (file && !f.isFile())
+                || (directory && !f.isDirectory())
+                || (hidden && !f.isHidden())
+                || (readable && !f.canRead())
+                || (writable && !f.canWrite())) {
+
+                throw new InvalidArgumentException(name);
+            }
+            
+            i.set(f);
+        }
+    }
+
+    /**
+     * Returns whether the argument values must represent directories.
+     *
+     * @return whether the argument values must represent directories.
+     */
+    public boolean isDirectory() {
+        return directory;
+    }
+
+    /**
+     * Specifies whether the argument values must represent directories.
+     *
+     * @param directory specifies whether the argument values must 
+     * represent directories.
+     */
+    public void setDirectory(boolean directory) {
+        this.directory = directory;
+    }
+
+    /**
+     * Returns whether the argument values must represent existing 
+     * files/directories.
+     *
+     * @return whether the argument values must represent existing 
+     * files/directories.
+     */
+    public boolean isExisting() {
+        return existing;
+    }
+
+    /**
+     * Specifies whether the argument values must represent existing 
+     * files/directories.
+     *
+     * @param existing specifies whether the argument values must 
+     * represent existing files/directories.
+     */
+    public void setExisting(boolean existing) {
+        this.existing = existing;
+    }
+
+    /**
+     * Returns whether the argument values must represent directories.
+     *
+     * @return whether the argument values must represent directories.
+     */
+    public boolean isFile() {
+        return file;
+    }
+
+    /**
+     * Specifies whether the argument values must represent files.
+     *
+     * @param file specifies whether the argument values must 
+     * represent files.
+     */
+    public void setFile(boolean file) {
+        this.file = file;
+    }
+
+    /**
+     * Returns whether the argument values must represent hidden 
+     * files/directories.
+     *
+     * @return whether the argument values must represent hidden 
+     * files/directories.
+     */
+    public boolean isHidden() {
+        return hidden;
+    }
+
+    /**
+     * Specifies whether the argument values must represent hidden 
+     * files/directories.
+     *
+     * @param hidden specifies whether the argument values must 
+     * represent hidden files/directories.
+     */
+    public void setHidden(boolean hidden) {
+        this.hidden = hidden;
+    }
+
+    /**
+     * Returns whether the argument values must represent readable 
+     * files/directories.
+     *
+     * @return whether the argument values must represent readable 
+     * files/directories.
+     */
+    public boolean isReadable() {
+        return readable;
+    }
+
+    /**
+     * Specifies whether the argument values must represent readable 
+     * files/directories.
+     *
+     * @param readable specifies whether the argument values must 
+     * represent readable files/directories.
+     */
+    public void setReadable(boolean readable) {
+        this.readable = readable;
+    }
+
+    /**
+     * Returns whether the argument values must represent writable 
+     * files/directories.
+     *
+     * @return whether the argument values must represent writable 
+     * files/directories.
+     */
+    public boolean isWritable() {
+        return writable;
+    }
+
+    /**
+     * Specifies whether the argument values must represent writable 
+     * files/directories.
+     *
+     * @param writable specifies whether the argument values must 
+     * represent writable files/directories.
+     */
+    public void setWritable(boolean writable) {
+        this.writable = writable;
+    }
+}

Modified: commons/proper/cli/trunk/src/java/org/apache/commons/cli2/validation/InvalidArgumentException.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/validation/InvalidArgumentException.java?rev=639943&r1=639942&r2=639943&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/java/org/apache/commons/cli2/validation/InvalidArgumentException.java (original)
+++ commons/proper/cli/trunk/src/java/org/apache/commons/cli2/validation/InvalidArgumentException.java Fri Mar 21 20:08:23 2008
@@ -1 +1,34 @@
-/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements.  See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License.  You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.commons.cli2.validation;/** * An exception indicating validation failure. * * @author Rob Oxspring * @author John Keyes */public class InvalidArgumentException extends Exception {    /
 **     * Creates a new exception     * @param message the reason for failure     */    public InvalidArgumentException(final String message) {        super(message);    }}
\ No newline at end of file
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.cli2.validation;
+
+/**
+ * An exception indicating validation failure.
+ *
+ * @author Rob Oxspring
+ * @author John Keyes
+ */
+public class InvalidArgumentException extends Exception {
+
+    /**
+     * Creates a new exception
+     * @param message the reason for failure
+     */
+    public InvalidArgumentException(final String message) {
+        super(message);
+    }
+}

Modified: commons/proper/cli/trunk/src/java/org/apache/commons/cli2/validation/NumberValidator.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/validation/NumberValidator.java?rev=639943&r1=639942&r2=639943&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/java/org/apache/commons/cli2/validation/NumberValidator.java (original)
+++ commons/proper/cli/trunk/src/java/org/apache/commons/cli2/validation/NumberValidator.java Fri Mar 21 20:08:23 2008
@@ -1 +1,200 @@
-/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements.  See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License.  You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.commons.cli2.validation;import java.text.NumberFormat;import java.text.ParsePosition;import java.util.List;import java.util.ListIterator;import org.apache.commons.cli2.resource.Resourc
 eConstants;import org.apache.commons.cli2.resource.ResourceHelper;/** * The <code>NumberValidator</code> validates the string argument * values are numbers.  If the value is a number, the string value in * the {@link java.util.List} of values is replaced with the * {@link java.lang.Number} instance. * * A maximum and minimum value can also be specified using * the {@link #setMaximum setMaximum}, and the * {@link #setMinimum setMinimum} methods. * * The following example shows how to limit the valid values * for the age attribute to integers less than 100. * * <pre> * ... * ArgumentBuilder builder = new ArgumentBuilder(); * NumberValidator validator = NumberValidator.getIntegerInstance(); * validator.setMaximum(new Integer(100)); * * Argument age = *     builder.withName("age"); *            .withValidator(validator); * </pre> * * @author Rob Oxspring * @author John Keyes */public class NumberValidator implements Validator {    /** the <code>NumberFormat</code> being used. */
     private NumberFormat format;    /** the lower bound for argument values. */    private Number minimum = null;    /** the upper bound for argument values */    private Number maximum = null;    /**     * Creates a new NumberValidator based on the specified NumberFormat     * @param format the format of numbers to accept     */    public NumberValidator(final NumberFormat format) {        setFormat(format);    }    /**     * Returns a <code>NumberValidator</code> for a currency format     * for the current default locale.     * @return a <code>NumberValidator</code> for a currency format     * for the current default locale.     */    public static NumberValidator getCurrencyInstance() {        return new NumberValidator(NumberFormat.getCurrencyInstance());    }    /**     * Returns a <code>NumberValidator</code> for an integer number format     * for the current default locale.     * @return a <code>NumberValidator</code> for an integer number format     * for the current
  default locale.     */    public static NumberValidator getIntegerInstance() {        final NumberFormat format = NumberFormat.getNumberInstance();        format.setParseIntegerOnly(true);        return new NumberValidator(format);    }    /**     * Returns a <code>NumberValidator</code> for a percentage format     * for the current default locale.     * @return a <code>NumberValidator</code> for a percentage format     * for the current default locale.     */    public static NumberValidator getPercentInstance() {        return new NumberValidator(NumberFormat.getPercentInstance());    }    /**     * Returns a <code>NumberValidator</code> for a general-purpose     * number format for the current default locale.     * @return a <code>NumberValidator</code> for a general-purpose     * number format for the current default locale.     */    public static NumberValidator getNumberInstance() {        return new NumberValidator(NumberFormat.getNumberInstance());    }    /**     
 * Validate the list of values against the list of permitted values.     * If a value is valid, replace the string in the <code>values</code>     * {@link java.util.List} with the {@link java.lang.Number} instance.     *     * @see org.apache.commons.cli2.validation.Validator#validate(java.util.List)     */    public void validate(final List values)        throws InvalidArgumentException {        for (final ListIterator i = values.listIterator(); i.hasNext();) {            final String value = (String) i.next();            final ParsePosition pp = new ParsePosition(0);            final Number number = format.parse(value, pp);            if (pp.getIndex() < value.length()) {                throw new InvalidArgumentException(value);            }            if (((minimum != null) && (number.doubleValue() < minimum.doubleValue())) ||                    ((maximum != null) && (number.doubleValue() > maximum.doubleValue()))) {                throw new InvalidArgumentException(Resour
 ceHelper.getResourceHelper().getMessage(ResourceConstants.NUMBERVALIDATOR_NUMBER_OUTOFRANGE,                                                                                                 new Object[] {                                                                                                     value                                                                                                 }));            }            i.set(number);        }    }    /**     * Return the format being used to validate argument values against.     *     * @return the format being used to validate argument values against.     */    public NumberFormat getFormat() {        return format;    }    /**     * Specify the format being used to validate argument values against.     *     * @param format the format being used to validate argument values against.     */    protected void setFormat(NumberFormat format) {        this.format = format;    }    /**     * Return the maximum value a
 llowed for an argument value.     *     * @return the maximum value allowed for an argument value.     */    public Number getMaximum() {        return maximum;    }    /**     * Specify the maximum value allowed for an argument value.     *     * @param maximum the maximum value allowed for an argument value.     */    public void setMaximum(Number maximum) {        this.maximum = maximum;    }    /**     * Return the minimum value allowed for an argument value.     *     * @return the minimum value allowed for an argument value.     */    public Number getMinimum() {        return minimum;    }    /**     * Specify the minimum value allowed for an argument value.     *     * @param minimum the minimum value allowed for an argument value.     */    public void setMinimum(Number minimum) {        this.minimum = minimum;    }}
\ No newline at end of file
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.cli2.validation;
+
+import java.text.NumberFormat;
+import java.text.ParsePosition;
+
+import java.util.List;
+import java.util.ListIterator;
+
+import org.apache.commons.cli2.resource.ResourceConstants;
+import org.apache.commons.cli2.resource.ResourceHelper;
+
+/**
+ * The <code>NumberValidator</code> validates the string argument
+ * values are numbers.  If the value is a number, the string value in
+ * the {@link java.util.List} of values is replaced with the
+ * {@link java.lang.Number} instance.
+ *
+ * A maximum and minimum value can also be specified using
+ * the {@link #setMaximum setMaximum}, and the
+ * {@link #setMinimum setMinimum} methods.
+ *
+ * The following example shows how to limit the valid values
+ * for the age attribute to integers less than 100.
+ *
+ * <pre>
+ * ...
+ * ArgumentBuilder builder = new ArgumentBuilder();
+ * NumberValidator validator = NumberValidator.getIntegerInstance();
+ * validator.setMaximum(new Integer(100));
+ *
+ * Argument age =
+ *     builder.withName("age");
+ *            .withValidator(validator);
+ * </pre>
+ *
+ * @author Rob Oxspring
+ * @author John Keyes
+ */
+public class NumberValidator implements Validator {
+    /** the <code>NumberFormat</code> being used. */
+    private NumberFormat format;
+
+    /** the lower bound for argument values. */
+    private Number minimum = null;
+
+    /** the upper bound for argument values */
+    private Number maximum = null;
+
+    /**
+     * Creates a new NumberValidator based on the specified NumberFormat
+     * @param format the format of numbers to accept
+     */
+    public NumberValidator(final NumberFormat format) {
+        setFormat(format);
+    }
+
+    /**
+     * Returns a <code>NumberValidator</code> for a currency format
+     * for the current default locale.
+     * @return a <code>NumberValidator</code> for a currency format
+     * for the current default locale.
+     */
+    public static NumberValidator getCurrencyInstance() {
+        return new NumberValidator(NumberFormat.getCurrencyInstance());
+    }
+
+    /**
+     * Returns a <code>NumberValidator</code> for an integer number format
+     * for the current default locale.
+     * @return a <code>NumberValidator</code> for an integer number format
+     * for the current default locale.
+     */
+    public static NumberValidator getIntegerInstance() {
+        final NumberFormat format = NumberFormat.getNumberInstance();
+        format.setParseIntegerOnly(true);
+
+        return new NumberValidator(format);
+    }
+
+    /**
+     * Returns a <code>NumberValidator</code> for a percentage format
+     * for the current default locale.
+     * @return a <code>NumberValidator</code> for a percentage format
+     * for the current default locale.
+     */
+    public static NumberValidator getPercentInstance() {
+        return new NumberValidator(NumberFormat.getPercentInstance());
+    }
+
+    /**
+     * Returns a <code>NumberValidator</code> for a general-purpose
+     * number format for the current default locale.
+     * @return a <code>NumberValidator</code> for a general-purpose
+     * number format for the current default locale.
+     */
+    public static NumberValidator getNumberInstance() {
+        return new NumberValidator(NumberFormat.getNumberInstance());
+    }
+
+    /**
+     * Validate the list of values against the list of permitted values.
+     * If a value is valid, replace the string in the <code>values</code>
+     * {@link java.util.List} with the {@link java.lang.Number} instance.
+     *
+     * @see org.apache.commons.cli2.validation.Validator#validate(java.util.List)
+     */
+    public void validate(final List values)
+        throws InvalidArgumentException {
+        for (final ListIterator i = values.listIterator(); i.hasNext();) {
+            final String value = (String) i.next();
+
+            final ParsePosition pp = new ParsePosition(0);
+            final Number number = format.parse(value, pp);
+
+            if (pp.getIndex() < value.length()) {
+                throw new InvalidArgumentException(value);
+            }
+
+            if (((minimum != null) && (number.doubleValue() < minimum.doubleValue())) ||
+                    ((maximum != null) && (number.doubleValue() > maximum.doubleValue()))) {
+                throw new InvalidArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.NUMBERVALIDATOR_NUMBER_OUTOFRANGE,
+                                                                                                 new Object[] {
+                                                                                                     value
+                                                                                                 }));
+            }
+
+            i.set(number);
+        }
+    }
+
+    /**
+     * Return the format being used to validate argument values against.
+     *
+     * @return the format being used to validate argument values against.
+     */
+    public NumberFormat getFormat() {
+        return format;
+    }
+
+    /**
+     * Specify the format being used to validate argument values against.
+     *
+     * @param format the format being used to validate argument values against.
+     */
+    protected void setFormat(NumberFormat format) {
+        this.format = format;
+    }
+
+    /**
+     * Return the maximum value allowed for an argument value.
+     *
+     * @return the maximum value allowed for an argument value.
+     */
+    public Number getMaximum() {
+        return maximum;
+    }
+
+    /**
+     * Specify the maximum value allowed for an argument value.
+     *
+     * @param maximum the maximum value allowed for an argument value.
+     */
+    public void setMaximum(Number maximum) {
+        this.maximum = maximum;
+    }
+
+    /**
+     * Return the minimum value allowed for an argument value.
+     *
+     * @return the minimum value allowed for an argument value.
+     */
+    public Number getMinimum() {
+        return minimum;
+    }
+
+    /**
+     * Specify the minimum value allowed for an argument value.
+     *
+     * @param minimum the minimum value allowed for an argument value.
+     */
+    public void setMinimum(Number minimum) {
+        this.minimum = minimum;
+    }
+}

Modified: commons/proper/cli/trunk/src/java/org/apache/commons/cli2/validation/UrlValidator.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/validation/UrlValidator.java?rev=639943&r1=639942&r2=639943&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/java/org/apache/commons/cli2/validation/UrlValidator.java (original)
+++ commons/proper/cli/trunk/src/java/org/apache/commons/cli2/validation/UrlValidator.java Fri Mar 21 20:08:23 2008
@@ -1 +1,115 @@
-/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements.  See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License.  You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.commons.cli2.validation;import java.net.MalformedURLException;import java.net.URL;import java.util.List;import java.util.ListIterator;import org.apache.commons.cli2.resource.ResourceCo
 nstants;import org.apache.commons.cli2.resource.ResourceHelper;/** * The <code>UrlValidator</code> validates the string argument * values are URLs.  If the value is a URL, the string value in * the {@link java.util.List} of values is replaced with the * {@link java.net.URL} instance. * * URLs can also be validated based on their scheme by using * the {@link #setProtocol setProtocol} method, or by using the specified * {@link #UrlValidator(java.lang.String) constructor}. * * The following example shows how to limit the valid values * for the site argument to 'https' URLs. * * <pre> * ... * ArgumentBuilder builder = new ArgumentBuilder(); * Argument site = *     builder.withName("site"); *            .withValidator(new URLValidator("https")); * </pre> * * @author Rob Oxspring * @author John Keyes */public class UrlValidator implements Validator {    /** allowed protocol */    private String protocol = null;    /**     * Creates a UrlValidator.     */    public UrlValidator() {
     }    /**     * Creates a UrlValidator for the specified protocol.     */    public UrlValidator(final String protocol) {        setProtocol(protocol);    }    /**     * Validate the list of values against the list of permitted values.     * If a value is valid, replace the string in the <code>values</code>     * {@link java.util.List} with the { java.net.URL} instance.     *     * @see org.apache.commons.cli2.validation.Validator#validate(java.util.List)     */    public void validate(final List values)        throws InvalidArgumentException {        for (final ListIterator i = values.listIterator(); i.hasNext();) {            final String name = (String) i.next();            try {                final URL url = new URL(name);                if ((protocol != null) && !protocol.equals(url.getProtocol())) {                    throw new InvalidArgumentException(name);                }                i.set(url);            } catch (final MalformedURLException mue) {         
        throw new InvalidArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.URLVALIDATOR_MALFORMED_URL,                                                                                                 new Object[] {                                                                                                     name                                                                                                 }));            }        }    }    /**     * Returns the protocol that must be used by a valid URL.     *     * @return the protocol that must be used by a valid URL.     */    public String getProtocol() {        return protocol;    }    /**     * Specifies the protocol that a URL must have to be valid.     *     * @param protocol the protocol that a URL must have to be valid.     */    public void setProtocol(String protocol) {        this.protocol = protocol;    }}
\ No newline at end of file
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.cli2.validation;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+
+import java.util.List;
+import java.util.ListIterator;
+
+import org.apache.commons.cli2.resource.ResourceConstants;
+import org.apache.commons.cli2.resource.ResourceHelper;
+
+/**
+ * The <code>UrlValidator</code> validates the string argument
+ * values are URLs.  If the value is a URL, the string value in
+ * the {@link java.util.List} of values is replaced with the
+ * {@link java.net.URL} instance.
+ *
+ * URLs can also be validated based on their scheme by using
+ * the {@link #setProtocol setProtocol} method, or by using the specified
+ * {@link #UrlValidator(java.lang.String) constructor}.
+ *
+ * The following example shows how to limit the valid values
+ * for the site argument to 'https' URLs.
+ *
+ * <pre>
+ * ...
+ * ArgumentBuilder builder = new ArgumentBuilder();
+ * Argument site =
+ *     builder.withName("site");
+ *            .withValidator(new URLValidator("https"));
+ * </pre>
+ *
+ * @author Rob Oxspring
+ * @author John Keyes
+ */
+public class UrlValidator implements Validator {
+    /** allowed protocol */
+    private String protocol = null;
+
+    /**
+     * Creates a UrlValidator.
+     */
+    public UrlValidator() {
+    }
+
+    /**
+     * Creates a UrlValidator for the specified protocol.
+     */
+    public UrlValidator(final String protocol) {
+        setProtocol(protocol);
+    }
+
+    /**
+     * Validate the list of values against the list of permitted values.
+     * If a value is valid, replace the string in the <code>values</code>
+     * {@link java.util.List} with the { java.net.URL} instance.
+     *
+     * @see org.apache.commons.cli2.validation.Validator#validate(java.util.List)
+     */
+    public void validate(final List values)
+        throws InvalidArgumentException {
+        for (final ListIterator i = values.listIterator(); i.hasNext();) {
+            final String name = (String) i.next();
+
+            try {
+                final URL url = new URL(name);
+
+                if ((protocol != null) && !protocol.equals(url.getProtocol())) {
+                    throw new InvalidArgumentException(name);
+                }
+
+                i.set(url);
+            } catch (final MalformedURLException mue) {
+                throw new InvalidArgumentException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.URLVALIDATOR_MALFORMED_URL,
+                                                                                                 new Object[] {
+                                                                                                     name
+                                                                                                 }));
+            }
+        }
+    }
+
+    /**
+     * Returns the protocol that must be used by a valid URL.
+     *
+     * @return the protocol that must be used by a valid URL.
+     */
+    public String getProtocol() {
+        return protocol;
+    }
+
+    /**
+     * Specifies the protocol that a URL must have to be valid.
+     *
+     * @param protocol the protocol that a URL must have to be valid.
+     */
+    public void setProtocol(String protocol) {
+        this.protocol = protocol;
+    }
+}

Modified: commons/proper/cli/trunk/src/java/org/apache/commons/cli2/validation/Validator.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/validation/Validator.java?rev=639943&r1=639942&r2=639943&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/java/org/apache/commons/cli2/validation/Validator.java (original)
+++ commons/proper/cli/trunk/src/java/org/apache/commons/cli2/validation/Validator.java Fri Mar 21 20:08:23 2008
@@ -1 +1,43 @@
-/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements.  See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License.  You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.commons.cli2.validation;import java.util.List;/** * The validation interface for validating argument values(s). * * A validator can replace the argument string value with a * specific 
 class instance e.g. the {@link UrlValidator} replaces * the string value with a {@link java.net.URL} instance. * * @author Rob Oxspring * @author John Keyes */public interface Validator {    /**     * Validate the specified values (List of Strings).     *     * @param values The values to validate.     *     * @throws InvalidArgumentException If any of the     * specified values are not valid.     */    void validate(final List values) throws InvalidArgumentException;}
\ No newline at end of file
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.cli2.validation;
+
+import java.util.List;
+
+/**
+ * The validation interface for validating argument values(s).
+ *
+ * A validator can replace the argument string value with a
+ * specific class instance e.g. the {@link UrlValidator} replaces
+ * the string value with a {@link java.net.URL} instance.
+ *
+ * @author Rob Oxspring
+ * @author John Keyes
+ */
+public interface Validator {
+
+    /**
+     * Validate the specified values (List of Strings).
+     * 
+     * @param values The values to validate.
+     * 
+     * @throws InvalidArgumentException If any of the 
+     * specified values are not valid.
+     */
+    void validate(final List values) throws InvalidArgumentException;
+
+}

Modified: commons/proper/cli/trunk/src/test/org/apache/commons/cli2/CLITestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/CLITestCase.java?rev=639943&r1=639942&r2=639943&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/test/org/apache/commons/cli2/CLITestCase.java (original)
+++ commons/proper/cli/trunk/src/test/org/apache/commons/cli2/CLITestCase.java Fri Mar 21 20:08:23 2008
@@ -1 +1,85 @@
-/** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements.  See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License.  You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.commons.cli2;import java.util.Arrays;import java.util.Collection;import java.util.Collections;import java.util.Iterator;import java.util.LinkedList;import java.util.List;import junit.
 framework.TestCase;public abstract class CLITestCase extends TestCase {	public static List list() {	    return Collections.EMPTY_LIST;	}	public static List list(final Object args[]) {	    return new LinkedList(Arrays.asList(args));	}	public static List list(final Object arg0) {	    return list(new Object[] { arg0 });	}	public static List list(final Object arg0, final Object arg1) {	    return list(new Object[] { arg0, arg1 });	}	public static List list(final Object arg0, final Object arg1, final Object arg2) {	    return list(new Object[] { arg0, arg1, arg2 });	}	public static List list(final Object arg0, final Object arg1, final Object arg2, final Object arg3) {	    return list(new Object[] { arg0, arg1, arg2, arg3 });	}	public static List list(final Object arg0, final Object arg1, final Object arg2, final Object arg3, final Object arg4) {	    return list(new Object[] { arg0, arg1, arg2, arg3, arg4 });	}	public static List list(final Object arg0, final Object arg1, final Ob
 ject arg2, final Object arg3, final Object arg4, final Object arg5) {	    return list(new Object[] { arg0, arg1, arg2, arg3, arg4, arg5 });	}	public static void assertListContentsEqual(final List expected, final List found) {	    final Iterator e = expected.iterator();	    final Iterator f = found.iterator();	    while (e.hasNext() && f.hasNext()) {	        assertEquals(e.next(), f.next());	    }	    if (e.hasNext()) {	        fail("Expected more elements");	    }	    if (f.hasNext()) {	        fail("Found more elements");	    }	}	public static void assertContentsEqual(final Collection expected, final Collection found) {	    assertTrue(expected.containsAll(found));	    assertTrue(found.containsAll(expected));	    assertEquals(expected.size(), found.size());	}}
\ No newline at end of file
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.cli2;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+public abstract class CLITestCase extends TestCase {
+
+	public static List list() {
+	    return Collections.EMPTY_LIST;
+	}
+
+	public static List list(final Object args[]) {
+	    return new LinkedList(Arrays.asList(args));
+	}
+
+	public static List list(final Object arg0) {
+	    return list(new Object[] { arg0 });
+	}
+
+	public static List list(final Object arg0, final Object arg1) {
+	    return list(new Object[] { arg0, arg1 });
+	}
+
+	public static List list(final Object arg0, final Object arg1, final Object arg2) {
+	    return list(new Object[] { arg0, arg1, arg2 });
+	}
+
+	public static List list(final Object arg0, final Object arg1, final Object arg2, final Object arg3) {
+	    return list(new Object[] { arg0, arg1, arg2, arg3 });
+	}
+
+	public static List list(final Object arg0, final Object arg1, final Object arg2, final Object arg3, final Object arg4) {
+	    return list(new Object[] { arg0, arg1, arg2, arg3, arg4 });
+	}
+
+	public static List list(final Object arg0, final Object arg1, final Object arg2, final Object arg3, final Object arg4, final Object arg5) {
+	    return list(new Object[] { arg0, arg1, arg2, arg3, arg4, arg5 });
+	}
+
+	public static void assertListContentsEqual(final List expected, final List found) {
+	
+	    final Iterator e = expected.iterator();
+	    final Iterator f = found.iterator();
+	
+	    while (e.hasNext() && f.hasNext()) {
+	        assertEquals(e.next(), f.next());
+	    }
+	
+	    if (e.hasNext()) {
+	        fail("Expected more elements");
+	    }
+	
+	    if (f.hasNext()) {
+	        fail("Found more elements");
+	    }
+	}
+
+	public static void assertContentsEqual(final Collection expected, final Collection found) {
+	    assertTrue(expected.containsAll(found));
+	    assertTrue(found.containsAll(expected));
+	    assertEquals(expected.size(), found.size());
+	}
+}



Mime
View raw message