View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.processor;
5   
6   import java.io.BufferedInputStream;
7   import java.io.IOException;
8   import java.io.InputStream;
9   import java.util.List;
10  import java.util.logging.Level;
11  import java.util.logging.Logger;
12  
13  import net.sourceforge.pmd.PMDConfiguration;
14  import net.sourceforge.pmd.PMD;
15  import net.sourceforge.pmd.PMDException;
16  import net.sourceforge.pmd.Report;
17  import net.sourceforge.pmd.RuleContext;
18  import net.sourceforge.pmd.RuleSetFactory;
19  import net.sourceforge.pmd.RuleSets;
20  import net.sourceforge.pmd.SourceCodeProcessor;
21  import net.sourceforge.pmd.renderers.Renderer;
22  import net.sourceforge.pmd.util.datasource.DataSource;
23  
24  /**
25   * @author Romain Pelisse <belaran@gmail.com>
26   *
27   */
28  public final class MonoThreadProcessor extends AbstractPMDProcessor {
29  
30  	public MonoThreadProcessor(PMDConfiguration configuration) {
31  		super(configuration);
32  	}
33  
34  	private static final Logger LOG = Logger.getLogger(MonoThreadProcessor.class.getName());
35  
36  	public void processFiles(RuleSetFactory ruleSetFactory, List<DataSource> files,
37  			RuleContext ctx, List<Renderer> renderers) {
38  
39  		// single threaded execution
40  
41  		RuleSets rs = createRuleSets(ruleSetFactory);
42  		SourceCodeProcessor processor = new SourceCodeProcessor(configuration);
43  		
44  		for (DataSource dataSource : files) {
45  			String niceFileName = filenameFrom(dataSource);
46  					
47  			Report report = PMD.setupReport(rs, ctx, niceFileName);
48  			
49  			if (LOG.isLoggable(Level.FINE)) {
50  				LOG.fine("Processing " + ctx.getSourceCodeFilename());
51  			}
52  			rs.start(ctx);
53  
54  			for (Renderer r : renderers) {
55  				r.startFileAnalysis(dataSource);
56  			}
57  
58  			try {
59  				InputStream stream = new BufferedInputStream(dataSource.getInputStream());
60  				ctx.setLanguageVersion(null);
61  				processor.processSourceCode(stream, rs, ctx);
62  			} catch (PMDException pmde) {
63  				LOG.log(Level.FINE, "Error while processing file: "+niceFileName, pmde.getCause());
64  
65  				report.addError(new Report.ProcessingError(pmde.getMessage(), niceFileName));
66  			} catch (IOException ioe) {
67  				// unexpected exception: log and stop executor service
68  				addError(report, "Unable to read source file", ioe, niceFileName);
69  			} catch (RuntimeException re) {
70  				// unexpected exception: log and stop executor service
71  				addError(report, "RuntimeException while processing file", re, niceFileName);
72  			}
73  
74  			rs.end(ctx);
75  			super.renderReports(renderers, ctx.getReport());
76  		}
77  	}
78  
79  	private void addError(Report report, String msg, Exception ex, String fileName) {
80  		LOG.log(Level.FINE,	msg, ex);
81  		report.addError(
82  				new Report.ProcessingError(ex.getMessage(),
83  				fileName)
84  				);
85  	}
86  }