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