View Javadoc

1   package org.apache.maven.plugins.shade.relocation;
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.Collection;
23  import java.util.Iterator;
24  import java.util.LinkedHashSet;
25  import java.util.List;
26  import java.util.Set;
27  
28  import org.codehaus.plexus.util.SelectorUtils;
29  
30  /**
31   * @author Jason van Zyl
32   * @author Mauro Talevi
33   */
34  public class SimpleRelocator
35      implements Relocator
36  {
37  
38      private final String pattern;
39  
40      private final String pathPattern;
41  
42      private final String shadedPattern;
43  
44      private final String shadedPathPattern;
45  
46      private final Set includes;
47  
48      private final Set excludes;
49  
50      public SimpleRelocator( String patt, String shadedPattern, List includes, List excludes )
51      {
52          this.pattern = patt.replace( '/', '.' );
53          this.pathPattern = patt.replace( '.', '/' );
54  
55          if ( shadedPattern != null )
56          {
57              this.shadedPattern = shadedPattern.replace( '/', '.' );
58              this.shadedPathPattern = shadedPattern.replace( '.', '/' );
59          }
60          else
61          {
62              this.shadedPattern = "hidden." + this.pattern;
63              this.shadedPathPattern = "hidden/" + this.pathPattern;
64          }
65  
66          this.includes = normalizePatterns( includes );
67          this.excludes = normalizePatterns( excludes );
68      }
69  
70      private static Set normalizePatterns( Collection patterns )
71      {
72          Set normalized = null;
73  
74          if ( patterns != null && !patterns.isEmpty() )
75          {
76              normalized = new LinkedHashSet();
77  
78              for ( Iterator i = patterns.iterator(); i.hasNext(); )
79              {
80                  String pattern = (String) i.next();
81  
82                  String classPattern = pattern.replace( '.', '/' );
83  
84                  normalized.add( classPattern );
85  
86                  if ( classPattern.endsWith( "/*" ) )
87                  {
88                      String packagePattern = classPattern.substring( 0, classPattern.lastIndexOf( '/' ) );
89                      normalized.add( packagePattern );
90                  }
91              }
92          }
93  
94          return normalized;
95      }
96  
97      private boolean isIncluded( String path )
98      {
99          if ( includes != null && !includes.isEmpty() )
100         {
101             for ( Iterator i = includes.iterator(); i.hasNext(); )
102             {
103                 String include = (String) i.next();
104 
105                 if ( SelectorUtils.matchPath( include, path, true ) )
106                 {
107                     return true;
108                 }
109             }
110             return false;
111         }
112         return true;
113     }
114 
115     private boolean isExcluded( String path )
116     {
117         if ( excludes != null && !excludes.isEmpty() )
118         {
119             for ( Iterator i = excludes.iterator(); i.hasNext(); )
120             {
121                 String exclude = (String) i.next();
122 
123                 if ( SelectorUtils.matchPath( exclude, path, true ) )
124                 {
125                     return true;
126                 }
127             }
128         }
129         return false;
130     }
131 
132     public boolean canRelocatePath( String path )
133     {
134         if ( path.endsWith( ".class" ) )
135         {
136             path = path.substring( 0, path.length() - 6 );
137         }
138 
139         if ( !isIncluded( path ) || isExcluded( path ) )
140         {
141             return false;
142         }
143 
144         return path.startsWith( pathPattern );
145     }
146 
147     public boolean canRelocateClass( String clazz )
148     {
149         return clazz.indexOf( '/' ) < 0 && canRelocatePath( clazz.replace( '.', '/' ) );
150     }
151 
152     public String relocatePath( String path )
153     {
154         return path.replaceFirst( pathPattern, shadedPathPattern );
155     }
156 
157     public String relocateClass( String clazz )
158     {
159         return clazz.replaceFirst( pattern, shadedPattern );
160     }
161 }