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.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  
29  import org.codehaus.plexus.util.StringUtils;
30  
31  import net.sourceforge.pmd.Report;
32  import net.sourceforge.pmd.Report.ProcessingError;
33  import net.sourceforge.pmd.Report.SuppressedViolation;
34  import net.sourceforge.pmd.RuleViolation;
35  import net.sourceforge.pmd.renderers.AbstractRenderer;
36  import net.sourceforge.pmd.util.datasource.DataSource;
37  
38  
39  /**
40   * A PMD renderer, that collects all violations and processing errors
41   * from a pmd execution.
42   * 
43   * @author Andreas Dangel
44   * @deprecated not used anymore
45   */
46  @Deprecated
47  public class PmdCollectingRenderer extends AbstractRenderer
48  {
49      private List<ProcessingError> errors = Collections.synchronizedList( new ArrayList<>() );
50      private List<RuleViolation> violations = Collections.synchronizedList( new ArrayList<>() );
51      private List<SuppressedViolation> suppressed = Collections.synchronizedList( new ArrayList<> () );
52  
53      /**
54       * Collects all reports from all threads.
55       */
56      public PmdCollectingRenderer()
57      {
58          super( PmdCollectingRenderer.class.getSimpleName(), "Collects all reports from all threads" );
59      }
60  
61      @Override
62      public void renderFileReport( Report report ) throws IOException
63      {
64          violations.addAll( report.getViolations() );
65          errors.addAll( report.getProcessingErrors() );
66          suppressed.addAll( report.getSuppressedViolations() );
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         Map<Integer, String> suppressedLines = new HashMap<Integer, String>();
140         for ( SuppressedViolation s : suppressed )
141         {
142             if ( s.suppressedByNOPMD() )
143             {
144                 suppressedLines.put( s.getRuleViolation().getBeginLine(), s.getUserMessage() );
145             }
146         }
147         report.suppress( suppressedLines );
148         for ( SuppressedViolation s : suppressed )
149         {
150             report.addRuleViolation( s.getRuleViolation() );
151         }
152         return report;
153     }
154 
155 
156     // stubs need to fulfill the Renderer interface
157     @Override
158     public String defaultFileExtension()
159     {
160         return null;
161     }
162     @Override
163     public void start() throws IOException
164     {
165     }
166     @Override
167     public void startFileAnalysis( DataSource dataSource )
168     {
169     }
170     @Override
171     public void end() throws IOException
172     {
173     }
174 }