View Javadoc
1   package org.apache.maven.plugins.pmd;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * A PMD renderer, that collects all violations and processing errors
38   * from a pmd execution.
39   * 
40   * @author Andreas Dangel
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       * Collects all reports from all threads.
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       * Checks whether any violations have been found.
64       * @return <code>true</code> if at least one violations has been found
65       */
66      public boolean hasViolations()
67      {
68          return !violations.isEmpty();
69      }
70  
71      /**
72       * Gets the list of all found violations.
73       * @return the violations
74       */
75      public List<RuleViolation> getViolations()
76      {
77          return violations;
78      }
79  
80      /**
81       * Checks whether any processing errors have been found.
82       * @return <code>true</code> if any errors have been found
83       */
84      public boolean hasErrors()
85      {
86          return !errors.isEmpty();
87      }
88  
89      /**
90       * Gets all the processing errors.
91       * @return the errors
92       */
93      public List<ProcessingError> getErrors()
94      {
95          return errors;
96      }
97  
98      /**
99       * Gets the errors as a single string. Each error is in its own line.
100      * @param withDetails if <code>true</code> then add the error details additionally (contains e.g. the stacktrace)
101      * @return the errors as string
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      * Create a new single report with all violations for further rendering into other formats than HTML.
119      * @return the report
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     // stubs need to fulfill the Renderer interface
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 }