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.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   * A PMD renderer, that collects all violations and processing errors
39   * from a pmd execution.
40   * 
41   * @author Andreas Dangel
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       * Collects all reports from all threads.
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       * Checks whether any violations have been found.
71       * @return <code>true</code> if at least one violations has been found
72       */
73      public boolean hasViolations()
74      {
75          return !violations.isEmpty();
76      }
77  
78      /**
79       * Gets the list of all found violations.
80       * @return the violations
81       */
82      public List<RuleViolation> getViolations()
83      {
84          return violations;
85      }
86  
87      /**
88       * Checks whether any processing errors have been found.
89       * @return <code>true</code> if any errors have been found
90       */
91      public boolean hasErrors()
92      {
93          return !errors.isEmpty();
94      }
95  
96      /**
97       * Gets all the processing errors.
98       * @return the errors
99       */
100     public List<ProcessingError> getErrors()
101     {
102         return errors;
103     }
104 
105     /**
106      * Gets the errors as a single string. Each error is in its own line.
107      * @param withDetails if <code>true</code> then add the error details additionally (contains e.g. the stacktrace)
108      * @return the errors as string
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      * Create a new single report with all violations for further rendering into other formats than HTML.
126      * @return the report
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     // stubs need to fulfill the Renderer interface
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 }