001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2015 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.checks;
021
022import java.util.regex.Pattern;
023import java.util.regex.PatternSyntaxException;
024
025import org.apache.commons.beanutils.ConversionException;
026
027import com.puppycrawl.tools.checkstyle.api.Check;
028
029/**
030 * <p> Abstract class for checks that verify strings using a
031 * {@link Pattern regular expression}.  It
032 * provides support for setting the regular
033 * expression using the property name {@code format}.  </p>
034 *
035 * @author Oliver Burn
036 */
037public abstract class AbstractFormatCheck
038    extends Check {
039    /** The flags to create the regular expression with. */
040    private int compileFlags;
041    /** The regexp to match against. */
042    private Pattern regexp;
043    /** The format string of the regexp. */
044    private String format;
045
046    /**
047     * Creates a new {@code AbstractFormatCheck} instance. Defaults the
048     * compile flag to 0 (the default).
049     * @param defaultFormat default format
050     * @throws ConversionException unable to parse defaultFormat
051     */
052    protected AbstractFormatCheck(String defaultFormat) {
053        this(defaultFormat, 0);
054    }
055
056    /**
057     * Creates a new {@code AbstractFormatCheck} instance.
058     * @param defaultFormat default format
059     * @param compileFlags the Pattern flags to compile the regexp with.
060     *     See {@link Pattern#compile(String, int)}
061     * @throws ConversionException unable to parse defaultFormat
062     */
063    protected AbstractFormatCheck(String defaultFormat, int compileFlags) {
064        updateRegexp(defaultFormat, compileFlags);
065    }
066
067    /**
068     * Set the format to the specified regular expression.
069     * @param format a {@code String} value
070     * @throws ConversionException unable to parse format
071     */
072    public final void setFormat(String format) {
073        updateRegexp(format, compileFlags);
074    }
075
076    /**
077     * Set the compile flags for the regular expression.
078     * @param compileFlags the compile flags to use.
079     */
080    public final void setCompileFlags(int compileFlags) {
081        updateRegexp(format, compileFlags);
082    }
083
084    /**
085     * Gets the regexp.
086     * @return the regexp to match against
087     */
088    public final Pattern getRegexp() {
089        return regexp;
090    }
091
092    /**
093     * Gets the regexp format.
094     * @return the regexp format
095     */
096    public final String getFormat() {
097        return format;
098    }
099
100    /**
101     * Updates the regular expression using the supplied format and compiler
102     * flags. Will also update the member variables.
103     * @param regexpFormat the format of the regular expression.
104     * @param compileFlagsParam the compiler flags to use.
105     */
106    private void updateRegexp(String regexpFormat, int compileFlagsParam) {
107        try {
108            regexp = Pattern.compile(regexpFormat, compileFlagsParam);
109            format = regexpFormat;
110            compileFlags |= compileFlagsParam;
111        }
112        catch (final PatternSyntaxException e) {
113            throw new ConversionException("unable to parse " + regexpFormat, e);
114        }
115    }
116}