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 mCompileFlags; 043 /** the regexp to match against */ 044 private Pattern mRegexp; 045 /** the format string of the regexp */ 046 private String mFormat; 047 048 /** 049 * Creates a new <code>AbstractFormatCheck</code> instance. Defaults the 050 * compile flag to 0 (the default). 051 * @param aDefaultFormat default format 052 * @throws ConversionException unable to parse aDefaultFormat 053 */ 054 public AbstractFormatCheck(String aDefaultFormat) 055 throws ConversionException 056 { 057 this(aDefaultFormat, 0); 058 } 059 060 /** 061 * Creates a new <code>AbstractFormatCheck</code> instance. 062 * @param aDefaultFormat default format 063 * @param aCompileFlags the Pattern flags to compile the regexp with. 064 * See {@link Pattern#compile(java.lang.String, int)} 065 * @throws ConversionException unable to parse aDefaultFormat 066 */ 067 public AbstractFormatCheck(String aDefaultFormat, int aCompileFlags) 068 throws ConversionException 069 { 070 updateRegexp(aDefaultFormat, aCompileFlags); 071 } 072 073 /** 074 * Set the format to the specified regular expression. 075 * @param aFormat a <code>String</code> value 076 * @throws ConversionException unable to parse aFormat 077 */ 078 public final void setFormat(String aFormat) 079 throws ConversionException 080 { 081 updateRegexp(aFormat, mCompileFlags); 082 } 083 084 /** 085 * Set the compile flags for the regular expression. 086 * @param aCompileFlags the compile flags to use. 087 */ 088 public final void setCompileFlags(int aCompileFlags) 089 { 090 updateRegexp(mFormat, aCompileFlags); 091 } 092 093 /** @return the regexp to match against */ 094 public final Pattern getRegexp() 095 { 096 return mRegexp; 097 } 098 099 /** @return the regexp format */ 100 public final String getFormat() 101 { 102 return mFormat; 103 } 104 105 /** 106 * Updates the regular expression using the supplied format and compiler 107 * flags. Will also update the member variables. 108 * @param aFormat the format of the regular expression. 109 * @param aCompileFlags the compiler flags to use. 110 */ 111 private void updateRegexp(String aFormat, int aCompileFlags) 112 { 113 try { 114 mRegexp = Utils.getPattern(aFormat, aCompileFlags); 115 mFormat = aFormat; 116 mCompileFlags |= aCompileFlags; 117 } 118 catch (final PatternSyntaxException e) { 119 throw new ConversionException("unable to parse " + aFormat, e); 120 } 121 } 122}