View Javadoc
1   package org.apache.maven.plugins.enforcer.utils;
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.util.HashSet;
23  import java.util.List;
24  import java.util.Set;
25  
26  import org.apache.commons.lang3.StringUtils;
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
29  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
30  import org.apache.maven.plugins.enforcer.utils.ArtifactMatcher.Pattern;
31  import org.apache.maven.shared.dependency.graph.DependencyNode;
32  
33  /**
34   * 
35   * @author Robert Scholte
36   * @since 3.0.0
37   */
38  public final class ArtifactUtils
39  {
40      private ArtifactUtils()
41      {
42      }
43  
44      public static Set<Artifact> getAllDescendants( DependencyNode node )
45      {
46          Set<Artifact> children = null;
47          if ( node.getChildren() != null )
48          {
49              children = new HashSet<Artifact>();
50              for ( DependencyNode depNode : node.getChildren() )
51              {
52                  children.add( depNode.getArtifact() );
53                  Set<Artifact> subNodes = getAllDescendants( depNode );
54                  if ( subNodes != null )
55                  {
56                      children.addAll( subNodes );
57                  }
58              }
59          }
60          return children;
61      }
62  
63      /**
64       * Checks the set of dependencies against the list of patterns.
65       * 
66       * @param thePatterns the patterns
67       * @param dependencies the dependencies
68       * @return a set containing artifacts matching one of the patterns or <code>null</code>
69       * @throws EnforcerRuleException the enforcer rule exception
70       */
71      public static Set<Artifact> checkDependencies( Set<Artifact> dependencies, List<String> thePatterns )
72          throws EnforcerRuleException
73      {
74          Set<Artifact> foundMatches = null;
75      
76          if ( thePatterns != null && thePatterns.size() > 0 )
77          {
78      
79              for ( String pattern : thePatterns )
80              {
81                  String[] subStrings = pattern.split( ":" );
82                  subStrings = StringUtils.stripAll( subStrings );
83                  String resultPattern = StringUtils.join( subStrings, ":" );
84      
85                  for ( Artifact artifact : dependencies )
86                  {
87                      if ( compareDependency( resultPattern, artifact ) )
88                      {
89                          // only create if needed
90                          if ( foundMatches == null )
91                          {
92                              foundMatches = new HashSet<Artifact>();
93                          }
94                          foundMatches.add( artifact );
95                      }
96                  }
97              }
98          }
99          return foundMatches;
100     }
101 
102     /**
103      * Compares the given pattern against the given artifact. The pattern should follow the format
104      * <code>groupId:artifactId:version:type:scope:classifier</code>.
105      * 
106      * @param pattern The pattern to compare the artifact with.
107      * @param artifact the artifact
108      * @return <code>true</code> if the artifact matches one of the patterns
109      * @throws EnforcerRuleException the enforcer rule exception
110      */
111     private static boolean compareDependency( String pattern, Artifact artifact )
112         throws EnforcerRuleException
113     {
114     
115         ArtifactMatcher.Pattern am = new Pattern( pattern );
116         boolean result;
117         try
118         {
119             result = am.match( artifact );
120         }
121         catch ( InvalidVersionSpecificationException e )
122         {
123             throw new EnforcerRuleException( "Invalid Version Range: ", e );
124         }
125     
126         return result;
127     }
128 
129 }