Ticket #680 (new enhancement)

Opened 21 months ago

Last modified 5 months ago

Validator: Regular expression functionality

Reported by: Jani Laakso Owned by: Jani Laakso
Priority: major Milestone: IT Mill Sponsored Backlog
Component: Server-side framework Version: 5.2.0-rc
Keywords: Cc: jukka.viitala@…
Known Issue description:
Hours estimate: 2 Deadline (dd.mm.yyyy):
Known Issue version (since): Known Issue title:
Hours done: Depends to:
Affects documentation: no
Known Issue workaround:
Affects release notes: yes Contract:

Description

Jukka has created following validator with his customer project

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.millstone.base.data.Validator;

/**
 * Validates a string against a given regular expression.
 * 
 * @author Oy IT Mill Ltd
 * 
 */
public class RegularExpressionValidator implements Validator {

    private boolean allowNull;

    private String message;

    private String reqularExpression;

    private Pattern pattern;

    /**
     * 
     * @param message
     *            to put on exception if value is not valid.
     * @param regularExpression
     *            that is compiled to Pattern just before validation.
     * @param allowNull,
     *            if <code>true</code> the null or empty value is valid.
     */
    public RegularExpressionValidator(String message, String regularExpression, boolean allowNull) {
        this.allowNull = allowNull;
        this.message = message;
        if (regularExpression == null)
            throw new IllegalArgumentException("Regular expression must not be null.");
        this.reqularExpression = regularExpression;
    }

    /**
     * 
     * @param message
     *            to put on exception if value is not valid.
     * @param pattern
     * @param allowNull,
     *            if <code>true</code> the null or empty value is valid.
     */
    public RegularExpressionValidator(String message, Pattern pattern, boolean allowNull) {
        this.allowNull = allowNull;

        if (pattern == null)
            throw new IllegalArgumentException("Pattern must not be null.");
        this.pattern = pattern;
        this.message = message;
    }

    public boolean isValid(Object value) {

        String str = value.toString();
        if (allowNull && (str == null || "".equals(str.toString())))
            return true;

        // Pattern compilation is postponed to here
        if (this.pattern == null)
            pattern = Pattern.compile(this.reqularExpression);
        Matcher matcher = pattern.matcher((CharSequence) str);
        return matcher.matches();
    }

    public void validate(Object value) throws InvalidValueException {
        if (!isValid(value))
            throw new InvalidValueException(message);
    }
}

Change History

Changed 21 months ago by Jani Laakso

  • milestone changed from 4.1 to 4.2.0-rc

Changed 17 months ago by Jani Laakso

  • milestone deleted

Milestone 4.2.0-rc deleted

Changed 10 months ago by Jani Laakso

  • summary changed from Validator: RegExp functionality to Validator: Regular expression functionality

Changed 8 months ago by Joonas Lehtinen

  • version set to 5.2.0-rc
  • component changed from undefined to Server-side framework
  • milestone set to User Interface Library 5.3.0 RC1

Changed 5 months ago by Joonas Lehtinen

  • milestone changed from User Interface Library 5.3.0 RC to IT Mill Sponsored Backlog
Note: See TracTickets for help on using tickets.