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.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
110 public String getErrorsAsString( boolean withDetails )
111 {
112 List<String> errorsAsString = new ArrayList<>( errors.size() );
113 for ( ProcessingError error : errors )
114 {
115 errorsAsString.add( error.getFile() + ": " + error.getMsg() );
116 if ( withDetails )
117 {
118 errorsAsString.add( error.getDetail() );
119 }
120 }
121 return StringUtils.join( errorsAsString.toArray(), System.getProperty( "line.separator" ) );
122 }
123
124
125
126
127
128 public Report asReport()
129 {
130 Report report = new Report();
131 for ( RuleViolation v : violations )
132 {
133 report.addRuleViolation( v );
134 }
135 for ( ProcessingError e : errors )
136 {
137 report.addError( e );
138 }
139 return report;
140 }
141
142
143
144 @Override
145 public String defaultFileExtension()
146 {
147 return null;
148 }
149 @Override
150 public void start() throws IOException
151 {
152 }
153 @Override
154 public void startFileAnalysis( DataSource dataSource )
155 {
156 }
157 @Override
158 public void end() throws IOException
159 {
160 }
161 }