View Javadoc

1   package org.apache.maven.plugins.shade.mojo;
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 org.apache.maven.artifact.Artifact;
23  
24  import java.util.Collection;
25  import java.util.HashSet;
26  
27  /**
28   * @author Benjamin Bentmann
29   */
30  class ArtifactSelector
31  {
32  
33      private Collection<ArtifactId> includes;
34  
35      private Collection<ArtifactId> excludes;
36  
37      public ArtifactSelector( Artifact projectArtifact, ArtifactSet artifactSet, String groupPrefix )
38      {
39          this( ( artifactSet != null ) ? artifactSet.getIncludes() : null,
40                ( artifactSet != null ) ? artifactSet.getExcludes() : null, groupPrefix );
41  
42          if ( projectArtifact != null && !this.includes.isEmpty() )
43          {
44              this.includes.add( new ArtifactId( projectArtifact ) );
45          }
46      }
47  
48      public ArtifactSelector( Collection<String> includes, Collection<String> excludes, String groupPrefix )
49      {
50          this.includes = toIds( includes );
51          this.excludes = toIds( excludes );
52  
53          if ( groupPrefix != null && groupPrefix.length() > 0 )
54          {
55              this.includes.add( new ArtifactId( groupPrefix + "*", "*", "*", "*" ) );
56          }
57      }
58  
59      private static Collection<ArtifactId> toIds( Collection<String> patterns )
60      {
61          Collection<ArtifactId> result = new HashSet<ArtifactId>();
62  
63          if ( patterns != null )
64          {
65              for ( String pattern : patterns )
66              {
67                  result.add( new ArtifactId( pattern ) );
68              }
69          }
70  
71          return result;
72      }
73  
74      public boolean isSelected( Artifact artifact )
75      {
76          return ( artifact != null ) ? isSelected( new ArtifactId( artifact ) ) : false;
77      }
78  
79      boolean isSelected( ArtifactId id )
80      {
81          return ( includes.isEmpty() || matches( includes, id ) ) && !matches( excludes, id );
82      }
83  
84      private boolean matches( Collection<ArtifactId> patterns, ArtifactId id )
85      {
86          for ( ArtifactId pattern : patterns )
87          {
88              if ( id.matches( pattern ) )
89              {
90                  return true;
91              }
92          }
93          return false;
94      }
95  
96  }