1 ////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code for adherence to a set of rules.
3 // Copyright (C) 2001-2014 Oliver Burn
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ////////////////////////////////////////////////////////////////////////////////
19 package com.puppycrawl.tools.checkstyle.checks;
20
21 import com.puppycrawl.tools.checkstyle.api.Check;
22 import com.puppycrawl.tools.checkstyle.api.Utils;
23
24 import java.util.regex.Pattern;
25 import java.util.regex.PatternSyntaxException;
26
27 import org.apache.commons.beanutils.ConversionException;
28
29 /**
30 * <p> Abstract class for checks that verify strings using a
31 * {@link java.util.regex.Pattern regular expression}. It
32 * provides support for setting the regular
33 * expression using the property name <code>format</code>. </p>
34 *
35 * @author Oliver Burn
36 * @version 1.0
37 */
38 public abstract class AbstractFormatCheck
39 extends Check
40 {
41 /** the flags to create the regular expression with */
42 private int compileFlags;
43 /** the regexp to match against */
44 private Pattern regexp;
45 /** the format string of the regexp */
46 private String format;
47
48 /**
49 * Creates a new <code>AbstractFormatCheck</code> instance. Defaults the
50 * compile flag to 0 (the default).
51 * @param defaultFormat default format
52 * @throws ConversionException unable to parse defaultFormat
53 */
54 public AbstractFormatCheck(String defaultFormat)
55 throws ConversionException
56 {
57 this(defaultFormat, 0);
58 }
59
60 /**
61 * Creates a new <code>AbstractFormatCheck</code> instance.
62 * @param defaultFormat default format
63 * @param compileFlags the Pattern flags to compile the regexp with.
64 * See {@link Pattern#compile(java.lang.String, int)}
65 * @throws ConversionException unable to parse defaultFormat
66 */
67 public AbstractFormatCheck(String defaultFormat, int compileFlags)
68 throws ConversionException
69 {
70 updateRegexp(defaultFormat, compileFlags);
71 }
72
73 /**
74 * Set the format to the specified regular expression.
75 * @param format a <code>String</code> value
76 * @throws ConversionException unable to parse format
77 */
78 public final void setFormat(String format)
79 throws ConversionException
80 {
81 updateRegexp(format, compileFlags);
82 }
83
84 /**
85 * Set the compile flags for the regular expression.
86 * @param compileFlags the compile flags to use.
87 */
88 public final void setCompileFlags(int compileFlags)
89 {
90 updateRegexp(format, compileFlags);
91 }
92
93 /** @return the regexp to match against */
94 public final Pattern getRegexp()
95 {
96 return regexp;
97 }
98
99 /** @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 }