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.Iterator;
23  import java.util.LinkedHashSet;
24  import java.util.List;
25  import java.util.Set;
26  
27  import org.codehaus.plexus.util.SelectorUtils;
28  
29  /**
30   * @author Jason van Zyl
31   * @author Mauro Talevi
32   */
33  public class SimpleRelocator
34      implements Relocator
35  {
36      private String pattern;
37  
38      private String pathPattern;
39  
40      private String shadedPattern;
41  
42      private String shadedPathPattern;
43  
44      private Set excludes;
45  
46      public SimpleRelocator( String patt, String shadedPattern, List excludes )
47      {
48          this.pattern = patt.replace( '/', '.' );
49          this.pathPattern = patt.replace( '.', '/' );
50  
51          if ( shadedPattern != null )
52          {
53              this.shadedPattern = shadedPattern.replace( '/', '.' );
54              this.shadedPathPattern = shadedPattern.replace( '.', '/' );
55          }
56          else
57          {
58              this.shadedPattern = "hidden." + this.pattern;
59              this.shadedPathPattern = "hidden/" + this.pathPattern;
60          }
61  
62          if ( excludes != null && !excludes.isEmpty() )
63          {
64              this.excludes = new LinkedHashSet();
65  
66              for ( Iterator i = excludes.iterator(); i.hasNext(); )
67              {
68                  String e = (String) i.next();
69  
70                  String classExclude = e.replace( '.', '/' );
71                  this.excludes.add( classExclude );
72  
73                  if ( classExclude.endsWith( "/*" ) )
74                  {
75                      String packageExclude = classExclude.substring( 0, classExclude.lastIndexOf( '/' ) );
76                      this.excludes.add( packageExclude );
77                  }
78              }
79          }
80      }
81  
82      public boolean canRelocatePath( String path )
83      {
84          if ( path.endsWith( ".class" ) )
85          {
86              path = path.substring( 0, path.length() - 6 );
87          }
88          if ( excludes != null )
89          {
90              for ( Iterator i = excludes.iterator(); i.hasNext(); )
91              {
92                  String exclude = (String) i.next();
93  
94                  if ( SelectorUtils.matchPath( exclude, path, true ) )
95                  {
96                      return false;
97                  }
98              }
99          }
100 
101         return path.startsWith( pathPattern );
102     }
103 
104     public boolean canRelocateClass( String clazz )
105     {
106         return clazz.indexOf( '/' ) < 0 && canRelocatePath( clazz.replace( '.', '/' ) );
107     }
108 
109     public String relocatePath( String path )
110     {
111         return path.replaceFirst( pathPattern, shadedPathPattern );
112     }
113 
114     public String relocateClass( String clazz )
115     {
116         return clazz.replaceFirst( pattern, shadedPattern );
117     }
118 }