Skip to content
This repository was archived by the owner on Apr 6, 2022. It is now read-only.

Commit 7e79c79

Browse files
committedFeb 12, 2013
[JENKINS-16250] Introduced new constructor to parse a single file only.
1 parent ccb17f9 commit 7e79c79

File tree

1 file changed

+87
-35
lines changed

1 file changed

+87
-35
lines changed
 

‎src/main/java/hudson/plugins/analysis/core/FilesParser.java

+87-35
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ public class FilesParser implements FileCallable<ParserResult> {
5050

5151
private final boolean canResolveRelativePaths;
5252

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) {
5455
this.filePattern = filePattern;
5556
this.parser = parser;
5657
this.isMavenBuild = isMavenBuild;
@@ -59,8 +60,10 @@ private FilesParser(final String filePattern, final AnnotationParser parser, fin
5960
canResolveRelativePaths = true;
6061
}
6162

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) {
6467
this.pluginId = pluginId;
6568
this.filePattern = filePattern;
6669
this.parser = parser;
@@ -70,6 +73,22 @@ private FilesParser(final String pluginId, final String filePattern, final Annot
7073
this.canResolveRelativePaths = canResolveRelativePaths;
7174
}
7275

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+
7392
/**
7493
* Creates a new instance of {@link FilesParser}.
7594
*
@@ -82,8 +101,8 @@ private FilesParser(final String pluginId, final String filePattern, final Annot
82101
* @param moduleName
83102
* the name of the module to use for all files
84103
*/
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) {
87106
this(pluginId, filePattern, parser, true, true, moduleName, true);
88107
}
89108

@@ -102,8 +121,9 @@ public FilesParser(final String pluginId, final String filePattern, final Annota
102121
* @param isMavenBuild
103122
* determines whether this build uses maven
104123
*/
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) {
107127
this(pluginId, filePattern, parser, shouldDetectModules, isMavenBuild, true);
108128
}
109129

@@ -126,15 +146,18 @@ public FilesParser(final String pluginId, final String filePattern, final Annota
126146
* resolved using a time expensive operation that scans the whole
127147
* workspace for matching files.
128148
*/
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);
132154
}
133155

134156
/**
135157
* Logs the specified message.
136158
*
137-
* @param message the message
159+
* @param message
160+
* the message
138161
*/
139162
protected void log(final String message) {
140163
if (stringLogger == null) {
@@ -144,7 +167,8 @@ protected void log(final String message) {
144167
}
145168

146169
/** {@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 {
148172
ParserResult result;
149173
if (canResolveRelativePaths) {
150174
result = new ParserResult(new FilePath(workspace));
@@ -153,20 +177,11 @@ public ParserResult invoke(final File workspace, final VirtualChannel channel) t
153177
result = new ParserResult();
154178
}
155179
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);
166182
}
167183
else {
168-
log("Parsing " + fileNames.length + " files in " + workspace.getAbsolutePath());
169-
parseFiles(workspace, fileNames, result);
184+
parserCollectionOfFiles(workspace, result);
170185
}
171186
}
172187
catch (InterruptedException exception) {
@@ -183,6 +198,30 @@ public ParserResult invoke(final File workspace, final VirtualChannel channel) t
183198
return result;
184199
}
185200

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+
186225
/**
187226
* Parses the specified collection of files and appends the results to the
188227
* provided container.
@@ -196,11 +235,16 @@ public ParserResult invoke(final File workspace, final VirtualChannel channel) t
196235
* @throws InterruptedException
197236
* if the user cancels the parsing
198237
*/
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 {
200240
ModuleDetector detector = createModuleDetector(workspace);
201241

202242
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+
}
204248

205249
String module = getModuleName(detector, file);
206250

@@ -256,21 +300,26 @@ private String getModuleName(final ModuleDetector detector, final File file) {
256300
* @throws InterruptedException
257301
* if the user cancels the parsing
258302
*/
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 {
260305
try {
261306
Collection<FileAnnotation> annotations = parser.parse(file, module);
262307
result.addAnnotations(annotations);
263308

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.");
265311
}
266312
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));
269317
result.addErrorMessage(module, errorMessage);
270318

271319
log(errorMessage);
272320
}
273321
}
322+
274323
/**
275324
* Creates a new instance of {@link FilesParser}.
276325
*
@@ -287,7 +336,8 @@ private void parseFile(final File file, final String module, final ParserResult
287336
*/
288337
@Deprecated
289338
@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) {
291341
this(filePattern, parser, isMavenBuild, StringUtils.EMPTY);
292342
}
293343

@@ -308,7 +358,8 @@ public FilesParser(final PluginLogger logger, final String filePattern, final An
308358
*/
309359
@Deprecated
310360
@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) {
312363
this(filePattern, parser, true, moduleName);
313364
}
314365

@@ -327,7 +378,8 @@ public FilesParser(final PluginLogger logger, final String filePattern, final An
327378
*/
328379
@Deprecated
329380
@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) {
331383
this(filePattern, parser, true, StringUtils.EMPTY);
332384

333385
shouldDetectModules = false;
@@ -374,8 +426,8 @@ public FilesParser(final StringPluginLogger logger, final String filePattern,
374426
@Deprecated
375427
@SuppressWarnings("PMD")
376428
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) {
379431
this(filePattern, parser, isMavenBuild, StringUtils.EMPTY);
380432
}
381433
}

0 commit comments

Comments
 (0)
This repository has been archived.