1 package org.apache.maven.shared.artifact.filter.collection;
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.Iterator;
24 import java.util.Set;
25
26 import org.apache.maven.artifact.Artifact;
27 import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
28 import org.codehaus.plexus.util.StringUtils;
29
30
31
32
33
34 public class ScopeFilter
35 extends AbstractArtifactsFilter
36 {
37
38 private String includeScope;
39
40 private String excludeScope;
41
42 public ScopeFilter( String includeScope, String excludeScope )
43 {
44 this.includeScope = includeScope;
45 this.excludeScope = excludeScope;
46 }
47
48
49
50
51
52
53
54
55
56
57
58 public Set filter( Set artifacts)
59 throws ArtifactFilterException
60 {
61 Set results = artifacts;
62
63 if ( StringUtils.isNotEmpty( includeScope ) )
64 {
65 if ( !Artifact.SCOPE_COMPILE.equals( includeScope ) && !Artifact.SCOPE_TEST.equals( includeScope )
66 && !Artifact.SCOPE_PROVIDED.equals( includeScope ) && !Artifact.SCOPE_RUNTIME.equals( includeScope )
67 && !Artifact.SCOPE_SYSTEM.equals( includeScope ) )
68 {
69 throw new ArtifactFilterException( "Invalid Scope in includeScope: " + includeScope );
70 }
71
72 results = new HashSet();
73
74 if ( Artifact.SCOPE_PROVIDED.equals( includeScope ) || Artifact.SCOPE_SYSTEM.equals( includeScope ) )
75 {
76 results = includeSingleScope( artifacts, includeScope );
77 }
78 else
79 {
80 ScopeArtifactFilter saf = new ScopeArtifactFilter( includeScope );
81
82 Iterator iter = artifacts.iterator();
83 while ( iter.hasNext() )
84 {
85 Artifact artifact = (Artifact) iter.next();
86 if ( saf.include( artifact ) )
87 {
88 results.add( artifact );
89 }
90 }
91 }
92 }
93 else if ( StringUtils.isNotEmpty( excludeScope ) )
94 {
95 if ( !Artifact.SCOPE_COMPILE.equals( excludeScope ) && !Artifact.SCOPE_TEST.equals( excludeScope )
96 && !Artifact.SCOPE_PROVIDED.equals( excludeScope ) && !Artifact.SCOPE_RUNTIME.equals( excludeScope )
97 && !Artifact.SCOPE_SYSTEM.equals( excludeScope ) )
98 {
99 throw new ArtifactFilterException( "Invalid Scope in excludeScope: " + excludeScope );
100 }
101 results = new HashSet();
102
103
104
105 if ( Artifact.SCOPE_TEST.equals( excludeScope ) )
106 {
107 throw new ArtifactFilterException( " Can't exclude Test scope, this will exclude everything." );
108 }
109 else if ( !Artifact.SCOPE_PROVIDED.equals( excludeScope ) && !Artifact.SCOPE_SYSTEM.equals( excludeScope ) )
110 {
111 ScopeArtifactFilter saf = new ScopeArtifactFilter( excludeScope );
112
113 Iterator iter = artifacts.iterator();
114 while ( iter.hasNext() )
115 {
116 Artifact artifact = (Artifact) iter.next();
117 if ( !saf.include( artifact ) )
118 {
119 results.add( artifact );
120 }
121 }
122 }
123 else
124 {
125 results = excludeSingleScope( artifacts, excludeScope );
126 }
127 }
128
129 return results;
130 }
131
132 private Set includeSingleScope( Set artifacts, String scope )
133 {
134 HashSet results = new HashSet();
135 Iterator iter = artifacts.iterator();
136 while ( iter.hasNext() )
137 {
138 Artifact artifact = (Artifact) iter.next();
139 if ( scope.equals( artifact.getScope() ) )
140 {
141 results.add( artifact );
142 }
143 }
144 return results;
145 }
146
147 private Set excludeSingleScope( Set artifacts, String scope )
148 {
149 HashSet results = new HashSet();
150 Iterator iter = artifacts.iterator();
151 while ( iter.hasNext() )
152 {
153 Artifact artifact = (Artifact) iter.next();
154 if ( !scope.equals( artifact.getScope() ) )
155 {
156 results.add( artifact );
157 }
158 }
159 return results;
160 }
161
162
163
164
165 public String getIncludeScope()
166 {
167 return this.includeScope;
168 }
169
170
171
172
173
174 public void setIncludeScope( String scope )
175 {
176 this.includeScope = scope;
177 }
178
179
180
181
182 public String getExcludeScope()
183 {
184 return this.excludeScope;
185 }
186
187
188
189
190
191 public void setExcludeScope( String excludeScope )
192 {
193 this.excludeScope = excludeScope;
194 }
195
196 }