1 package org.apache.maven.plugin.checkstyle;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import com.puppycrawl.tools.checkstyle.api.AuditEvent;
23
24
25
26
27
28
29
30 public final class RuleUtil
31 {
32 private RuleUtil()
33 {
34
35 }
36
37 private static final String CHECKSTYLE_PACKAGE = "com.puppycrawl.tools.checkstyle.checks";
38
39
40
41
42
43
44
45 public static String getName( AuditEvent event )
46 {
47 return getName( event.getSourceName() );
48 }
49
50
51
52
53
54
55 public static String getName( String eventSrcName )
56 {
57 if ( eventSrcName == null )
58 {
59 return null;
60 }
61
62 if ( eventSrcName.endsWith( "Check" ) )
63 {
64 eventSrcName = eventSrcName.substring( 0, eventSrcName.length() - 5 );
65 }
66
67 return eventSrcName.substring( eventSrcName.lastIndexOf( '.' ) + 1 );
68 }
69
70
71
72
73
74
75
76 public static String getCategory( AuditEvent event )
77 {
78 return getCategory( event.getSourceName() );
79 }
80
81
82
83
84
85
86
87 public static String getCategory( String eventSrcName )
88 {
89 if ( eventSrcName == null )
90 {
91 return null;
92 }
93
94 int end = eventSrcName.lastIndexOf( '.' );
95 eventSrcName = eventSrcName.substring( 0, end );
96
97 if ( CHECKSTYLE_PACKAGE.equals( eventSrcName ) )
98 {
99 return "misc";
100 }
101 else if ( !eventSrcName.startsWith( CHECKSTYLE_PACKAGE ) )
102 {
103 return "extension";
104 }
105
106 return eventSrcName.substring( eventSrcName.lastIndexOf( '.' ) + 1 );
107 }
108
109 public static Matcher[] parseMatchers( String[] specs )
110 {
111 Matcher[] matchers = new Matcher[specs.length];
112 int i = 0;
113 for ( String spec: specs )
114 {
115 spec = spec.trim();
116 Matcher matcher;
117 if ( Character.isUpperCase( spec.charAt( 0 ) ) )
118 {
119
120 matcher = new RuleMatcher( spec );
121 }
122 else if ( "misc".equals( spec ) )
123 {
124
125 matcher = new PackageMatcher( CHECKSTYLE_PACKAGE );
126 }
127 else if ( "extension".equals( spec ) )
128 {
129
130 matcher = new ExtensionMatcher();
131 }
132 else if ( !spec.contains( "." ) )
133 {
134 matcher = new PackageMatcher( CHECKSTYLE_PACKAGE + '.' + spec );
135 }
136 else
137 {
138
139 matcher = new PackageMatcher( spec );
140 }
141 matchers[i++] = matcher;
142 }
143 return matchers;
144 }
145
146
147
148
149 public interface Matcher
150 {
151
152
153
154
155
156 boolean match( String eventSrcName );
157 }
158
159 private static class RuleMatcher
160 implements Matcher
161 {
162 private final String rule;
163
164 public RuleMatcher( String rule )
165 {
166 this.rule = rule;
167 }
168
169 public boolean match( String eventSrcName )
170 {
171 return rule.equals( getName( eventSrcName ) );
172 }
173 }
174
175 private static class PackageMatcher
176 implements Matcher
177 {
178 private final String packageName;
179
180 public PackageMatcher( String packageName )
181 {
182 this.packageName = packageName;
183 }
184
185 public boolean match( String eventSrcName )
186 {
187 return eventSrcName.startsWith( packageName )
188 && !eventSrcName.substring( packageName.length() + 1 ).contains( "." );
189 }
190 }
191
192
193
194
195 private static class ExtensionMatcher
196 implements Matcher
197 {
198 public boolean match( String eventSrcName )
199 {
200 return !eventSrcName.startsWith( CHECKSTYLE_PACKAGE );
201 }
202 }
203 }