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.FileReader;
23  import java.io.IOException;
24  import java.io.LineNumberReader;
25  import java.util.ArrayList;
26  import java.util.HashSet;
27  import java.util.List;
28  import java.util.Set;
29  
30  import org.apache.commons.lang3.StringUtils;
31  import org.apache.maven.plugin.MojoExecutionException;
32  import org.apache.maven.plugins.pmd.model.CpdFile;
33  import org.apache.maven.plugins.pmd.model.Duplication;
34  
35  import net.sourceforge.pmd.cpd.Mark;
36  import net.sourceforge.pmd.cpd.Match;
37  
38  /**
39   * This class contains utility methods to load property files which define which files
40   * should be excluded from the CPD duplication results.
41   * See property <code>pmd.excludeFromFailureFile</code>.
42   *
43   * @author Andreas Dangel
44   */
45  public class ExcludeDuplicationsFromFile implements ExcludeFromFile<Duplication>
46  {
47  
48      private final List<Set<String>> exclusionList = new ArrayList<>();
49  
50      @Override
51      public boolean isExcludedFromFailure( final Duplication errorDetail )
52      {
53          final Set<String> uniquePaths = new HashSet<>();
54          for ( final CpdFile cpdFile : errorDetail.getFiles() )
55          {
56              uniquePaths.add( cpdFile.getPath() );
57          }
58          return isExcludedFromFailure( uniquePaths );
59      }
60  
61      /**
62       * Checks whether the given {@link Match} is excluded.
63       * Note: The exclusion must have been loaded before via {@link #loadExcludeFromFailuresData(String)}.
64       * 
65       * @param errorDetail the duplication to check
66       * @return <code>true</code> if the given duplication should be excluded, <code>false</code> otherwise.
67       */
68      public boolean isExcludedFromFailure( final Match errorDetail )
69      {
70          final Set<String> uniquePaths = new HashSet<>();
71          for ( Mark mark : errorDetail.getMarkSet() )
72          {
73              uniquePaths.add( mark.getFilename() );
74          }
75          return isExcludedFromFailure( uniquePaths );
76      }
77  
78      private boolean isExcludedFromFailure( Set<String> uniquePaths )
79      {
80          for ( final Set<String> singleExclusionGroup : exclusionList )
81          {
82              if ( uniquePaths.size() == singleExclusionGroup.size()
83                  && duplicationExcludedByGroup( uniquePaths, singleExclusionGroup ) )
84              {
85                  return true;
86              }
87          }
88          return false;
89      }
90  
91      private boolean duplicationExcludedByGroup( final Set<String> uniquePaths, final Set<String> singleExclusionGroup )
92      {
93          for ( final String path : uniquePaths )
94          {
95              if ( !fileExcludedByGroup( path, singleExclusionGroup ) )
96              {
97                  return false;
98              }
99          }
100         return true;
101     }
102 
103     private boolean fileExcludedByGroup( final String path, final Set<String> singleExclusionGroup )
104     {
105         final String formattedPath = path.replace( '\\', '.' ).replace( '/', '.' );
106         for ( final String className : singleExclusionGroup )
107         {
108             if ( formattedPath.contains( className ) )
109             {
110                 return true;
111             }
112         }
113         return false;
114     }
115 
116     @Override
117     public void loadExcludeFromFailuresData( final String excludeFromFailureFile )
118             throws MojoExecutionException
119     {
120         if ( StringUtils.isEmpty( excludeFromFailureFile ) )
121         {
122             return;
123         }
124 
125         try ( LineNumberReader reader = new LineNumberReader( new FileReader( excludeFromFailureFile ) ) )
126         {
127             String line;
128             while ( ( line = reader.readLine() ) != null )
129             {
130                 if ( !line.startsWith( "#" ) )
131                 {
132                     exclusionList.add( createSetFromExclusionLine( line ) );
133                 }
134             }
135         }
136         catch ( final IOException e )
137         {
138             throw new MojoExecutionException( "Cannot load file " + excludeFromFailureFile, e );
139         }
140     }
141 
142     private Set<String> createSetFromExclusionLine( final String line )
143     {
144         final Set<String> result = new HashSet<>();
145         for ( final String className : line.split( "," ) )
146         {
147             result.add( className.trim() );
148         }
149         return result;
150     }
151 
152     @Override
153     public int countExclusions()
154     {
155         return exclusionList.size();
156     }
157 }