1 package org.apache.maven.plugins.enforcer.utils;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
36
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
65
66
67
68
69
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
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
104
105
106
107
108
109
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 }