Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
AbstractFormatCheck |
|
| 1.2857142857142858;1.286 |
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 mCompileFlags; | |
43 | /** the regexp to match against */ | |
44 | private Pattern mRegexp; | |
45 | /** the format string of the regexp */ | |
46 | private String mFormat; | |
47 | ||
48 | /** | |
49 | * Creates a new <code>AbstractFormatCheck</code> instance. Defaults the | |
50 | * compile flag to 0 (the default). | |
51 | * @param aDefaultFormat default format | |
52 | * @throws ConversionException unable to parse aDefaultFormat | |
53 | */ | |
54 | public AbstractFormatCheck(String aDefaultFormat) | |
55 | throws ConversionException | |
56 | { | |
57 | 124 | this(aDefaultFormat, 0); |
58 | 124 | } |
59 | ||
60 | /** | |
61 | * Creates a new <code>AbstractFormatCheck</code> instance. | |
62 | * @param aDefaultFormat default format | |
63 | * @param aCompileFlags the Pattern flags to compile the regexp with. | |
64 | * See {@link Pattern#compile(java.lang.String, int)} | |
65 | * @throws ConversionException unable to parse aDefaultFormat | |
66 | */ | |
67 | public AbstractFormatCheck(String aDefaultFormat, int aCompileFlags) | |
68 | throws ConversionException | |
69 | 146 | { |
70 | 146 | updateRegexp(aDefaultFormat, aCompileFlags); |
71 | 146 | } |
72 | ||
73 | /** | |
74 | * Set the format to the specified regular expression. | |
75 | * @param aFormat a <code>String</code> value | |
76 | * @throws ConversionException unable to parse aFormat | |
77 | */ | |
78 | public final void setFormat(String aFormat) | |
79 | throws ConversionException | |
80 | { | |
81 | 62 | updateRegexp(aFormat, mCompileFlags); |
82 | 61 | } |
83 | ||
84 | /** | |
85 | * Set the compile flags for the regular expression. | |
86 | * @param aCompileFlags the compile flags to use. | |
87 | */ | |
88 | public final void setCompileFlags(int aCompileFlags) | |
89 | { | |
90 | 1 | updateRegexp(mFormat, aCompileFlags); |
91 | 1 | } |
92 | ||
93 | /** @return the regexp to match against */ | |
94 | public final Pattern getRegexp() | |
95 | { | |
96 | 1456 | return mRegexp; |
97 | } | |
98 | ||
99 | /** @return the regexp format */ | |
100 | public final String getFormat() | |
101 | { | |
102 | 393 | 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 | 209 | mRegexp = Utils.getPattern(aFormat, aCompileFlags); |
115 | 208 | mFormat = aFormat; |
116 | 208 | mCompileFlags |= aCompileFlags; |
117 | } | |
118 | 1 | catch (final PatternSyntaxException e) { |
119 | 1 | throw new ConversionException("unable to parse " + aFormat, e); |
120 | 208 | } |
121 | 208 | } |
122 | } |