tapestry-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Howard M. Lewis Ship" <hls...@comcast.net>
Subject RE: [VOTE] Adding a URL Validator to tapestry valid package
Date Tue, 16 Sep 2003 14:39:08 GMT
Tapestry uses Jakarata ORO internally, so there's no reason to use JDK 1.4 regexp.

--
Howard M. Lewis Ship
Creator, Tapestry: Java Web Components
http://jakarta.apache.org/tapestry
http://jakarta.apache.org/commons/sandbox/hivemind/
http://javatapestry.blogspot.com

> -----Original Message-----
> From: Harish Krishnaswamy [mailto:hkrishnaswamy@comcast.net] 
> Sent: Tuesday, September 16, 2003 10:29 AM
> To: Tapestry development
> Subject: Re: [VOTE] Adding a URL Validator to tapestry valid package
> 
> 
> Looks good. What lowest JDK version does Tapestry need? May 
> be we should 
> look into other regexp packages for that purpose? Any suggestions?
> 
> -Harish
> 
> Bill Lear wrote:
> 
> >Here's what I have used.  You can create other regexp 
> validators if you 
> >need...
> >
> >Here is its use in a .page file:
> >
> >    <bean name="timeValidator"
> >          class="org.apache.blah.blah.JavaPatternValidator"
> >          lifecycle="page">
> >        <set-property name="pattern">
> >            "(0?[0-9]|1[012]):[0-5][0-9] (am|AM|pm|PM)"
> >        </set-property>
> >        <set-property name="errorMessage">
> >"Invalid time entry.  The form is HH:MM AM|PM.  Example: 12:30 pm"
> >        </set-property>
> >    </bean>
> >
> >I have written the validator as an abstract base class using the GOF 
> >template pattern.  I have so far created one implementation 
> class, one 
> >based on Java 1.4 regexp package.
> >
> >
> >Here are the two Java classes:
> >
> >
> >package ....;
> >
> >import org.apache.tapestry.form.IFormComponent;
> >import org.apache.tapestry.valid.BaseValidator;
> >import org.apache.tapestry.valid.ValidatorException;
> >
> >/**
> > * Abstract base class for pattern (regular expression) 
> validators.  */
> >public abstract class PatternValidator extends BaseValidator {
> >    /**
> >     * The error message to display to the user if validation fails.
> >     */
> >    private String errorMessage;
> >
> >    /**
> >     * The string representation of the regular expression pattern.
> >     */
> >    private String pattern;
> >
> >    /** @return the error message */
> >    public String getErrorMessage() {
> >        return errorMessage;
> >    }
> >
> >    /**
> >     * Sets the error message.
> >     *
> >     * @param errorMessage the error message to set.
> >     */
> >    public void setErrorMessage(String errorMessage) {
> >        this.errorMessage = errorMessage;
> >    }
> >
> >    /**
> >     * @return the pattern
> >     */
> >    public String getPattern() {
> >        return pattern;
> >    }
> >
> >    /**
> >     * Sets the pattern.
> >     *
> >     * @param pattern the pattern to set.
> >     */
> >    public void setPattern(String pattern) {
> >        this.pattern = pattern;
> >    }
> >
> >    public String toString(IFormComponent field, Object value) {
> >        if (value == null) {
> >            return null;
> >        }
> >
> >        return value.toString();
> >    }
> >
> >    /**
> >     * Validates a given input field.  This is implemented as a
> >     * template method (following Gang-of-Four pattern). The template
> >     * implementation is fulfilled by the valid() method.
> >     *
> >     * @param field The form component from which the input is taken.
> >     * @param input The content of the field to validate.
> >     * @return null if the field is not required, or the 
> contents of the field
> >     * if validation is successful.
> >     * @throws ValidatorException if validation fails.
> >     */
> >    public Object toObject(IFormComponent field, String input)
> >        throws ValidatorException {
> >
> >        if (checkRequired(field, input)) {
> >            return null;
> >        }
> >
> >        if (!valid(input)) {
> >            throw new ValidatorException(getErrorMessage(), null);
> >        }
> >
> >        return input;
> >    }
> >
> >    /**
> >     * The template implementation method for the toObject template.
> >     *
> >     * @param input the content of the text field to validate
> >     * @return true if the input is valid
> >     */
> >    protected abstract boolean valid(String input);
> >}
> >
> >
> >Here is the Java 1.4 regexp version.
> >
> >
> >
> >package ...
> >
> >import java.util.regex.Pattern;
> >import java.util.regex.PatternSyntaxException;
> >
> >import org.apache.tapestry.ApplicationRuntimeException;
> >
> >/**
> > * A pattern validator that uses the Java 1.4 regular expression 
> >package.
> > *
> > * @author Bill Lear
> > */
> >public class JavaPatternValidator extends PatternValidator {
> >    /**
> >     * The pattern to use when validating input.
> >     */
> >
> >private Pattern regex;
> >
> >    /**
> >     * @see PatternValidator#valid
> >     */
> >   protected boolean valid(String input) {
> >        if (regex == null) {
> >            try {
> >                regex = Pattern.compile(getPattern());
> >            } catch (PatternSyntaxException pex) {
> >                throw new
> >                    ApplicationRuntimeException(pex.toString());
> >            }
> >        }
> >        return input == null ? false : 
> regex.matcher(input).matches();
> >    }
> >}
> >
> >
> >
> >Bill
> >
> >---------------------------------------------------------------------
> >To unsubscribe, e-mail: tapestry-dev-unsubscribe@jakarta.apache.org
> >For additional commands, e-mail: tapestry-dev-help@jakarta.apache.org
> >
> >
> >  
> >
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-dev-help@jakarta.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-dev-help@jakarta.apache.org


Mime
View raw message