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  		configuration.setPmdRuleSets(rs);
43  		SourceCodeProcessor processor = new SourceCodeProcessor(configuration);
44  		
45  		for (DataSource dataSource : files) {
46  			String niceFileName = filenameFrom(dataSource);
47  					
48  			Report report = PMD.setupReport(rs, ctx, niceFileName);
49  			
50  			if (LOG.isLoggable(Level.FINE)) {
51  				LOG.fine("Processing " + ctx.getSourceCodeFilename());
52  			}
53  			rs.start(ctx);
54  
55  			for (Renderer r : renderers) {
56  				r.startFileAnalysis(dataSource);
57  			}
58  
59  			try {
60  				InputStream stream = new BufferedInputStream(dataSource.getInputStream());
61  				ctx.setLanguageVersion(null);
62  				processor.processSourceCode(stream, rs, ctx);
63  			} catch (PMDException pmde) {
64  			    if (LOG.isLoggable(Level.FINE)) {
65  			        LOG.log(Level.FINE, "Error while processing file: "+niceFileName, pmde.getCause());
66  			    }
67  
68  				report.addError(new Report.ProcessingError(pmde.getMessage(), niceFileName));
69  			} catch (IOException ioe) {
70  				// unexpected exception: log and stop executor service
71  				addError(report, "Unable to read source file", ioe, niceFileName);
72  			} catch (RuntimeException re) {
73  				// unexpected exception: log and stop executor service
74  				addError(report, "RuntimeException while processing file", re, niceFileName);
75  			}
76  
77  			rs.end(ctx);
78  			super.renderReports(renderers, ctx.getReport());
79  		}
80  	}
81  
82  	private void addError(Report report, String msg, Exception ex, String fileName) {
83  		LOG.log(Level.FINE,	msg, ex);
84  		report.addError(
85  				new Report.ProcessingError(ex.getMessage(),
86  				fileName)
87  				);
88  	}
89  }