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