001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2014  Oliver Burn
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////////////////////////////////////////////////////////////////////////////////
019package com.puppycrawl.tools.checkstyle.checks;
020
021import com.puppycrawl.tools.checkstyle.api.Check;
022import com.puppycrawl.tools.checkstyle.api.Utils;
023
024import java.util.regex.Pattern;
025import java.util.regex.PatternSyntaxException;
026
027import org.apache.commons.beanutils.ConversionException;
028
029/**
030 * <p> Abstract class for checks that verify strings using a
031 * {@link java.util.regex.Pattern regular expression}.  It
032 * provides support for setting the regular
033 * expression using the property name <code>format</code>.  </p>
034 *
035 * @author Oliver Burn
036 * @version 1.0
037 */
038public abstract class AbstractFormatCheck
039    extends Check
040{
041    /** the flags to create the regular expression with */
042    private int compileFlags;
043    /** the regexp to match against */
044    private Pattern regexp;
045    /** the format string of the regexp */
046    private String format;
047
048    /**
049     * Creates a new <code>AbstractFormatCheck</code> instance. Defaults the
050     * compile flag to 0 (the default).
051     * @param defaultFormat default format
052     * @throws ConversionException unable to parse defaultFormat
053     */
054    public AbstractFormatCheck(String defaultFormat)
055        throws ConversionException
056    {
057        this(defaultFormat, 0);
058    }
059
060    /**
061     * Creates a new <code>AbstractFormatCheck</code> instance.
062     * @param defaultFormat default format
063     * @param compileFlags the Pattern flags to compile the regexp with.
064     * See {@link Pattern#compile(java.lang.String, int)}
065     * @throws ConversionException unable to parse defaultFormat
066     */
067    public AbstractFormatCheck(String defaultFormat, int compileFlags)
068        throws ConversionException
069    {
070        updateRegexp(defaultFormat, compileFlags);
071    }
072
073    /**
074     * Set the format to the specified regular expression.
075     * @param format a <code>String</code> value
076     * @throws ConversionException unable to parse format
077     */
078    public final void setFormat(String format)
079        throws ConversionException
080    {
081        updateRegexp(format, compileFlags);
082    }
083
084    /**
085     * Set the compile flags for the regular expression.
086     * @param compileFlags the compile flags to use.
087     */
088    public final void setCompileFlags(int compileFlags)
089    {
090        updateRegexp(format, compileFlags);
091    }
092
093    /** @return the regexp to match against */
094    public final Pattern getRegexp()
095    {
096        return regexp;
097    }
098
099    /** @return the regexp format */
100    public final String getFormat()
101    {
102        return format;
103    }
104
105    /**
106     * Updates the regular expression using the supplied format and compiler
107     * flags. Will also update the member variables.
108     * @param format the format of the regular expression.
109     * @param compileFlagsParam the compiler flags to use.
110     */
111    private void updateRegexp(String format, int compileFlagsParam)
112    {
113        try {
114            regexp = Utils.getPattern(format, compileFlagsParam);
115            this.format = format;
116            compileFlags |= compileFlagsParam;
117        }
118        catch (final PatternSyntaxException e) {
119            throw new ConversionException("unable to parse " + format, e);
120        }
121    }
122}