All Downloads are FREE. Search and download functionalities are using the official Maven repository.

at.spardat.enterprise.fmt.AStringFmtRangeRegEx Maven / Gradle / Ivy

There is a newer version: 6.0.2
Show newest version
/*******************************************************************************
 * Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     s IT Solutions AT Spardat GmbH - initial API and implementation
 *******************************************************************************/

/*
 * @(#) $Id: AStringFmtRangeRegEx.java 2093 2007-11-28 14:23:36Z s3460 $
 *
 * Copyright 2004/2005 by SPARDAT Sparkassen-Datendienst Ges.m.b.H.,
 * A-1110 Wien, Geiselbergstr.21-25.
 * All rights reserved.
 *
 */
package at.spardat.enterprise.fmt;

import java.util.regex.Pattern;

/**
 * A formatter based at java.util.regex.
 *
 * This class defines a string formatter which accepts, min and max lenght,
 * valid range of allowed characters, style 'mandatory' and a regular expression
 * pattern which is validated.
 * This class is extended from AStringFmtRange, see there for the definition of valid characters (the range).
 * Additional the input is validated by a regular expression pattern, for this the
 * java.util.regex classes are used, see there for regular expression usage.
 * For the error message shown in the case of a negative evaluation of the regular expression
 * an resource bundle and key have to be specified.
 *
 * The class FmtFactory (used by the XMA GUI Designer) constructs a AStringFmtRangeRegEx
 * from a String like this:
 * sre,(minLen),(maxLen),(range),(m&ul&lc),bundleKey,resBundle,regex
 * The values in brackets can be empty strings, all commas are mandatory.
 *
 * An example for email validation:
 * "sre,,15,a-e@.,m,NoEmail,at.spardat.enterprise.fmt.test.FmtTestErrors,\\w+(\\.\\w+)*@\\w+\\.\\w+"
 *
 * @author s3460
 * @since version_number
 */
public class AStringFmtRangeRegEx extends AStringFmtRange {

    /**
     * The error message key
     */
    private String bundleKey;
    /**
     * The error message ressource bundel
     */
    private String resBundle;
    /**
     * The regex pattern
     */
    private Pattern pattern;

    /**
     * Returns a String formatter to limit the input length, the range of valid characters
     * and a regular expression (according to the java.util.regex packet) against which the input is vlaidated.
     *
     * @param maxLen the maximum length of an accepted string. May be -1, then
     *                the length is unlimited.
     * @param range  a string denoting one or more character ranges.
     *                See {@link AStringFmtRange} for a syntax description.
     * @param style  may be MANDATORY which forces the input not be empty or
     *                UPPER_CASE or LOWER_CASE.
     * @param regex a regular expression according to the java.util.regex packet.
     *                The input string is validated agianst this expression.
     * @param bundleKey the resource bundle key of the error message,
     *                 which is shown in the case of a negative evalution of the regular expression.
     * @param resBundle the resource bundle name (without locale) from which the error message
     *                  for negative evalution of the regular expression is taken.
     */
    public AStringFmtRangeRegEx(int maxLen, String range, int style, String regex, String bundleKey, String resBundle) {
        super(maxLen, range, style);
        this.pattern = Pattern.compile(regex);
        this.bundleKey = bundleKey;
        this.resBundle = resBundle;

    }

    /**
     * Returns a String formatter to limit the input length, the range of valid characters
     * and a regular expression (according to the java.util.regex packet) against which the input is vlaidated.
     *
     * @param maxLen the maximum length of an accepted string. May be -1, then
     *                the length is unlimited.
     * @param range  a string denoting one or more character ranges.
     *                See {@link AStringFmtRange} for a syntax description.     *
     * @param regex a regular expression according to the java.util.regex packet.
     *                The input string is validated agianst this expression.
     * @param bundleKey the resource bundle key of the error message,
     *                 which is shown in the case of a negative evalution of the regular expression.
     * @param resBundle the resource bundle name from which the error message
     *                  for negative evalution of the regular expression is taken.
     */
    public AStringFmtRangeRegEx(int maxLen, String range, String regex, String bundleKey, String resBundle) {
        super(maxLen, range);
        this.pattern = Pattern.compile(regex);
        this.bundleKey = bundleKey;
        this.resBundle = resBundle;
    }

    /**
     * @see at.spardat.enterprise.fmt.IFmt#isLegalExternalChar(char)
     */
    public boolean isLegalExternalChar (char aChar) {
        if(getNumRanges() == 0){
            return true;
        } else{
            return super.isLegalExternalChar(aChar);
        }
    }

    /**
     * @see at.spardat.enterprise.fmt.IFmt#parse(String)
     */
    public String parse (String external) throws FmtParseException {
        super.parse (external);  // throws an exception on violations of superclass restrictions
        if (external == null || external.length() == 0) return "";

        if(!this.pattern.matcher(external).matches()){
            FmtParseException ex = new FmtParseException (bundleKey);
            ex.setResBundle(resBundle);
            throw ex;
        }

        return external;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy