001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2017 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.AbstractCheck;
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 * @deprecated Checkstyle will not support abstract checks anymore. Use
035 *             {@link AbstractCheck} instead.
036 * @author Oliver Burn
037 * @noinspection AbstractClassNeverImplemented
038 */
039@Deprecated
040public abstract class AbstractFormatCheck
041    extends AbstractCheck {
042    /** The flags to create the regular expression with. */
043    private int compileFlags;
044    /** The regexp to match against. */
045    private Pattern regexp;
046    /** The format string of the regexp. */
047    private String format;
048
049    /**
050     * Creates a new {@code AbstractFormatCheck} instance. Defaults the
051     * compile flag to 0 (the default).
052     * @param defaultFormat default format
053     * @throws ConversionException unable to parse defaultFormat
054     */
055    protected AbstractFormatCheck(String defaultFormat) {
056        this(defaultFormat, 0);
057    }
058
059    /**
060     * Creates a new {@code AbstractFormatCheck} instance.
061     * @param defaultFormat default format
062     * @param compileFlags the Pattern flags to compile the regexp with.
063     *     See {@link Pattern#compile(String, int)}
064     * @throws ConversionException unable to parse defaultFormat
065     */
066    protected AbstractFormatCheck(String defaultFormat, int compileFlags) {
067        updateRegexp(defaultFormat, compileFlags);
068    }
069
070    /**
071     * Set the format to the specified regular expression.
072     * @param format a {@code String} value
073     * @throws ConversionException unable to parse format
074     */
075    public final void setFormat(String format) {
076        updateRegexp(format, compileFlags);
077    }
078
079    /**
080     * Set the compile flags for the regular expression.
081     * @param compileFlags the compile flags to use.
082     */
083    public final void setCompileFlags(int compileFlags) {
084        updateRegexp(format, compileFlags);
085    }
086
087    /**
088     * Gets the regexp.
089     * @return the regexp to match against
090     */
091    public final Pattern getRegexp() {
092        return regexp;
093    }
094
095    /**
096     * Gets the regexp format.
097     * @return the regexp format
098     */
099    public final String getFormat() {
100        return format;
101    }
102
103    /**
104     * Updates the regular expression using the supplied format and compiler
105     * flags. Will also update the member variables.
106     * @param regexpFormat the format of the regular expression.
107     * @param compileFlagsParam the compiler flags to use.
108     */
109    private void updateRegexp(String regexpFormat, int compileFlagsParam) {
110        try {
111            regexp = Pattern.compile(regexpFormat, compileFlagsParam);
112            format = regexpFormat;
113            compileFlags |= compileFlagsParam;
114        }
115        catch (final PatternSyntaxException ex) {
116            throw new IllegalArgumentException("unable to parse " + regexpFormat, ex);
117        }
118    }
119}