1   package org.apache.maven.plugins.pmd;
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.List;
26  
27  import org.codehaus.plexus.util.StringUtils;
28  
29  import net.sourceforge.pmd.Report;
30  import net.sourceforge.pmd.Report.ProcessingError;
31  import net.sourceforge.pmd.RuleViolation;
32  import net.sourceforge.pmd.renderers.AbstractRenderer;
33  import net.sourceforge.pmd.util.datasource.DataSource;
34  
35  
36  
37  
38  
39  
40  
41  
42  public class PmdCollectingRenderer extends AbstractRenderer
43  {
44      private List<ProcessingError> errors = Collections.synchronizedList( new ArrayList<ProcessingError>() );
45      private List<RuleViolation> violations = Collections.synchronizedList( new ArrayList<RuleViolation>() );
46  
47      
48  
49  
50      public PmdCollectingRenderer()
51      {
52          super( PmdCollectingRenderer.class.getSimpleName(), "Collects all reports from all threads" );
53      }
54  
55      @Override
56      public void renderFileReport( Report report ) throws IOException
57      {
58          violations.addAll( report.getViolations() );
59          errors.addAll( report.getProcessingErrors() );
60      }
61  
62      
63  
64  
65  
66      public boolean hasViolations()
67      {
68          return !violations.isEmpty();
69      }
70  
71      
72  
73  
74  
75      public List<RuleViolation> getViolations()
76      {
77          return violations;
78      }
79  
80      
81  
82  
83  
84      public boolean hasErrors()
85      {
86          return !errors.isEmpty();
87      }
88  
89      
90  
91  
92  
93      public List<ProcessingError> getErrors()
94      {
95          return errors;
96      }
97  
98      
99  
100 
101 
102 
103     public String getErrorsAsString( boolean withDetails )
104     {
105         List<String> errorsAsString = new ArrayList<>( errors.size() );
106         for ( ProcessingError error : errors )
107         {
108             errorsAsString.add( error.getFile() + ": " + error.getMsg() );
109             if ( withDetails )
110             {
111                 errorsAsString.add( error.getDetail() );
112             }
113         }
114         return StringUtils.join( errorsAsString.toArray(), System.getProperty( "line.separator" ) );
115     }
116 
117     
118 
119 
120 
121     public Report asReport()
122     {
123         Report report = new Report();
124         for ( RuleViolation v : violations )
125         {
126             report.addRuleViolation( v );
127         }
128         for ( ProcessingError e : errors )
129         {
130             report.addError( e );
131         }
132         return report;
133     }
134 
135 
136     
137     @Override
138     public String defaultFileExtension()
139     {
140         return null;
141     }
142     @Override
143     public void start() throws IOException
144     {
145     }
146     @Override
147     public void startFileAnalysis( DataSource dataSource )
148     {
149     }
150     @Override
151     public void end() throws IOException
152     {
153     }
154 }