@@ -50,7 +50,8 @@ public class FilesParser implements FileCallable<ParserResult> {
50
50
51
51
private final boolean canResolveRelativePaths ;
52
52
53
- private FilesParser (final String filePattern , final AnnotationParser parser , final boolean isMavenBuild , final String moduleName ) {
53
+ private FilesParser (final String filePattern , final AnnotationParser parser ,
54
+ final boolean isMavenBuild , final String moduleName ) {
54
55
this .filePattern = filePattern ;
55
56
this .parser = parser ;
56
57
this .isMavenBuild = isMavenBuild ;
@@ -59,8 +60,10 @@ private FilesParser(final String filePattern, final AnnotationParser parser, fin
59
60
canResolveRelativePaths = true ;
60
61
}
61
62
62
- private FilesParser (final String pluginId , final String filePattern , final AnnotationParser parser , final boolean shouldDetectModules ,
63
- final boolean isMavenBuild , final String moduleName , final boolean canResolveRelativePaths ) {
63
+ private FilesParser (final String pluginId , final String filePattern ,
64
+ final AnnotationParser parser , final boolean shouldDetectModules ,
65
+ final boolean isMavenBuild , final String moduleName ,
66
+ final boolean canResolveRelativePaths ) {
64
67
this .pluginId = pluginId ;
65
68
this .filePattern = filePattern ;
66
69
this .parser = parser ;
@@ -70,6 +73,22 @@ private FilesParser(final String pluginId, final String filePattern, final Annot
70
73
this .canResolveRelativePaths = canResolveRelativePaths ;
71
74
}
72
75
76
+ /**
77
+ * Creates a new instance of {@link FilesParser}. Since no file pattern is
78
+ * given, this parser assumes that it is invoked on a file rather than on a
79
+ * directory.
80
+ *
81
+ * @param pluginId
82
+ * the ID of the plug-in that uses this parser
83
+ * @param parser
84
+ * the parser to apply on the found files
85
+ * @param moduleName
86
+ * the name of the module to use for all files
87
+ */
88
+ public FilesParser (final String pluginId , final AnnotationParser parser , final String moduleName ) {
89
+ this (pluginId , "" , parser , true , true , moduleName , true );
90
+ }
91
+
73
92
/**
74
93
* Creates a new instance of {@link FilesParser}.
75
94
*
@@ -82,8 +101,8 @@ private FilesParser(final String pluginId, final String filePattern, final Annot
82
101
* @param moduleName
83
102
* the name of the module to use for all files
84
103
*/
85
- public FilesParser (final String pluginId , final String filePattern , final AnnotationParser parser ,
86
- final String moduleName ) {
104
+ public FilesParser (final String pluginId , final String filePattern ,
105
+ final AnnotationParser parser , final String moduleName ) {
87
106
this (pluginId , filePattern , parser , true , true , moduleName , true );
88
107
}
89
108
@@ -102,8 +121,9 @@ public FilesParser(final String pluginId, final String filePattern, final Annota
102
121
* @param isMavenBuild
103
122
* determines whether this build uses maven
104
123
*/
105
- public FilesParser (final String pluginId , final String filePattern , final AnnotationParser parser ,
106
- final boolean shouldDetectModules , final boolean isMavenBuild ) {
124
+ public FilesParser (final String pluginId , final String filePattern ,
125
+ final AnnotationParser parser , final boolean shouldDetectModules ,
126
+ final boolean isMavenBuild ) {
107
127
this (pluginId , filePattern , parser , shouldDetectModules , isMavenBuild , true );
108
128
}
109
129
@@ -126,15 +146,18 @@ public FilesParser(final String pluginId, final String filePattern, final Annota
126
146
* resolved using a time expensive operation that scans the whole
127
147
* workspace for matching files.
128
148
*/
129
- public FilesParser (final String pluginId , final String filePattern , final AnnotationParser parser ,
130
- final boolean shouldDetectModules , final boolean isMavenBuild , final boolean canResolveRelativePaths ) {
131
- this (pluginId , filePattern , parser , shouldDetectModules , isMavenBuild , StringUtils .EMPTY , canResolveRelativePaths );
149
+ public FilesParser (final String pluginId , final String filePattern ,
150
+ final AnnotationParser parser , final boolean shouldDetectModules ,
151
+ final boolean isMavenBuild , final boolean canResolveRelativePaths ) {
152
+ this (pluginId , filePattern , parser , shouldDetectModules , isMavenBuild , StringUtils .EMPTY ,
153
+ canResolveRelativePaths );
132
154
}
133
155
134
156
/**
135
157
* Logs the specified message.
136
158
*
137
- * @param message the message
159
+ * @param message
160
+ * the message
138
161
*/
139
162
protected void log (final String message ) {
140
163
if (stringLogger == null ) {
@@ -144,7 +167,8 @@ protected void log(final String message) {
144
167
}
145
168
146
169
/** {@inheritDoc} */
147
- public ParserResult invoke (final File workspace , final VirtualChannel channel ) throws IOException {
170
+ public ParserResult invoke (final File workspace , final VirtualChannel channel )
171
+ throws IOException {
148
172
ParserResult result ;
149
173
if (canResolveRelativePaths ) {
150
174
result = new ParserResult (new FilePath (workspace ));
@@ -153,20 +177,11 @@ public ParserResult invoke(final File workspace, final VirtualChannel channel) t
153
177
result = new ParserResult ();
154
178
}
155
179
try {
156
- log ("Finding all files that match the pattern " + filePattern );
157
- String [] fileNames = new FileFinder (filePattern ).find (workspace );
158
-
159
- if (fileNames .length == 0 ) {
160
- if (isMavenBuild ) {
161
- log ("No files found in " + workspace .getAbsolutePath () + " for pattern: " + filePattern );
162
- }
163
- else {
164
- result .addErrorMessage (Messages .FilesParser_Error_NoFiles ());
165
- }
180
+ if (StringUtils .isBlank (filePattern )) {
181
+ parseSingleFile (workspace , result );
166
182
}
167
183
else {
168
- log ("Parsing " + fileNames .length + " files in " + workspace .getAbsolutePath ());
169
- parseFiles (workspace , fileNames , result );
184
+ parserCollectionOfFiles (workspace , result );
170
185
}
171
186
}
172
187
catch (InterruptedException exception ) {
@@ -183,6 +198,30 @@ public ParserResult invoke(final File workspace, final VirtualChannel channel) t
183
198
return result ;
184
199
}
185
200
201
+ private void parserCollectionOfFiles (final File workspace , final ParserResult result ) throws InterruptedException {
202
+ log ("Finding all files that match the pattern " + filePattern );
203
+ String [] fileNames = new FileFinder (filePattern ).find (workspace );
204
+
205
+ if (fileNames .length == 0 ) {
206
+ if (isMavenBuild ) {
207
+ log ("No files found in " + workspace .getAbsolutePath () + " for pattern: " + filePattern );
208
+ }
209
+ else {
210
+ result .addErrorMessage (Messages .FilesParser_Error_NoFiles ());
211
+ }
212
+ }
213
+ else {
214
+ log ("Parsing " + fileNames .length + " files in " + workspace .getAbsolutePath ());
215
+ parseFiles (workspace , fileNames , result );
216
+ }
217
+ }
218
+
219
+ private void parseSingleFile (final File workspace , final ParserResult result ) throws InterruptedException {
220
+ String [] fileNames = new String [] {workspace .getAbsolutePath ()};
221
+ log ("Parsing file " + workspace .getAbsolutePath ());
222
+ parseFiles (workspace , fileNames , result );
223
+ }
224
+
186
225
/**
187
226
* Parses the specified collection of files and appends the results to the
188
227
* provided container.
@@ -196,11 +235,16 @@ public ParserResult invoke(final File workspace, final VirtualChannel channel) t
196
235
* @throws InterruptedException
197
236
* if the user cancels the parsing
198
237
*/
199
- private void parseFiles (final File workspace , final String [] fileNames , final ParserResult result ) throws InterruptedException {
238
+ private void parseFiles (final File workspace , final String [] fileNames ,
239
+ final ParserResult result ) throws InterruptedException {
200
240
ModuleDetector detector = createModuleDetector (workspace );
201
241
202
242
for (String fileName : fileNames ) {
203
- File file = new File (workspace , fileName );
243
+ File file = new File (fileName );
244
+
245
+ if (!file .isAbsolute ()) {
246
+ file = new File (workspace , fileName );
247
+ }
204
248
205
249
String module = getModuleName (detector , file );
206
250
@@ -256,21 +300,26 @@ private String getModuleName(final ModuleDetector detector, final File file) {
256
300
* @throws InterruptedException
257
301
* if the user cancels the parsing
258
302
*/
259
- private void parseFile (final File file , final String module , final ParserResult result ) throws InterruptedException {
303
+ private void parseFile (final File file , final String module , final ParserResult result )
304
+ throws InterruptedException {
260
305
try {
261
306
Collection <FileAnnotation > annotations = parser .parse (file , module );
262
307
result .addAnnotations (annotations );
263
308
264
- log ("Successfully parsed file " + file + " of module " + module + " with " + annotations .size () + " warnings." );
309
+ log ("Successfully parsed file " + file + " of module " + module + " with "
310
+ + annotations .size () + " warnings." );
265
311
}
266
312
catch (InvocationTargetException exception ) {
267
- String errorMessage = Messages .FilesParser_Error_Exception (file ) + "\n \n "
268
- + ExceptionUtils .getStackTrace ((Throwable )ObjectUtils .defaultIfNull (exception .getCause (), exception ));
313
+ String errorMessage = Messages .FilesParser_Error_Exception (file )
314
+ + "\n \n "
315
+ + ExceptionUtils .getStackTrace ((Throwable )ObjectUtils .defaultIfNull (
316
+ exception .getCause (), exception ));
269
317
result .addErrorMessage (module , errorMessage );
270
318
271
319
log (errorMessage );
272
320
}
273
321
}
322
+
274
323
/**
275
324
* Creates a new instance of {@link FilesParser}.
276
325
*
@@ -287,7 +336,8 @@ private void parseFile(final File file, final String module, final ParserResult
287
336
*/
288
337
@ Deprecated
289
338
@ SuppressWarnings ("PMD" )
290
- public FilesParser (final PluginLogger logger , final String filePattern , final AnnotationParser parser , final boolean isMavenBuild ) {
339
+ public FilesParser (final PluginLogger logger , final String filePattern ,
340
+ final AnnotationParser parser , final boolean isMavenBuild ) {
291
341
this (filePattern , parser , isMavenBuild , StringUtils .EMPTY );
292
342
}
293
343
@@ -308,7 +358,8 @@ public FilesParser(final PluginLogger logger, final String filePattern, final An
308
358
*/
309
359
@ Deprecated
310
360
@ SuppressWarnings ("PMD" )
311
- public FilesParser (final PluginLogger logger , final String filePattern , final AnnotationParser parser , final String moduleName ) {
361
+ public FilesParser (final PluginLogger logger , final String filePattern ,
362
+ final AnnotationParser parser , final String moduleName ) {
312
363
this (filePattern , parser , true , moduleName );
313
364
}
314
365
@@ -327,7 +378,8 @@ public FilesParser(final PluginLogger logger, final String filePattern, final An
327
378
*/
328
379
@ Deprecated
329
380
@ SuppressWarnings ("PMD" )
330
- public FilesParser (final PluginLogger logger , final String filePattern , final AnnotationParser parser ) {
381
+ public FilesParser (final PluginLogger logger , final String filePattern ,
382
+ final AnnotationParser parser ) {
331
383
this (filePattern , parser , true , StringUtils .EMPTY );
332
384
333
385
shouldDetectModules = false ;
@@ -374,8 +426,8 @@ public FilesParser(final StringPluginLogger logger, final String filePattern,
374
426
@ Deprecated
375
427
@ SuppressWarnings ("PMD" )
376
428
public FilesParser (final StringPluginLogger logger , final String filePattern ,
377
- final AnnotationParser parser ,
378
- final boolean shouldDetectModules , final boolean isMavenBuild ) {
429
+ final AnnotationParser parser , final boolean shouldDetectModules ,
430
+ final boolean isMavenBuild ) {
379
431
this (filePattern , parser , isMavenBuild , StringUtils .EMPTY );
380
432
}
381
433
}
0 commit comments