1 | |
package org.apache.maven.plugins.shade.relocation; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
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 | |
|
31 | |
|
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 | 15 | { |
48 | 15 | this.pattern = patt.replace( '/', '.' ); |
49 | 15 | this.pathPattern = patt.replace( '.', '/' ); |
50 | |
|
51 | 15 | if ( shadedPattern != null ) |
52 | |
{ |
53 | 6 | this.shadedPattern = shadedPattern.replace( '/', '.' ); |
54 | 6 | this.shadedPathPattern = shadedPattern.replace( '.', '/' ); |
55 | |
} |
56 | |
else |
57 | |
{ |
58 | 9 | this.shadedPattern = "hidden." + this.pattern; |
59 | 9 | this.shadedPathPattern = "hidden/" + this.pathPattern; |
60 | |
} |
61 | |
|
62 | 15 | if ( excludes != null && !excludes.isEmpty() ) |
63 | |
{ |
64 | 7 | this.excludes = new LinkedHashSet(); |
65 | |
|
66 | 7 | for ( Iterator i = excludes.iterator(); i.hasNext(); ) |
67 | |
{ |
68 | 16 | String e = (String) i.next(); |
69 | |
|
70 | 16 | String classExclude = e.replace( '.', '/' ); |
71 | 16 | this.excludes.add( classExclude ); |
72 | |
|
73 | 16 | if ( classExclude.endsWith( "/*" ) ) |
74 | |
{ |
75 | 7 | String packageExclude = classExclude.substring( 0, classExclude.lastIndexOf( '/' ) ); |
76 | 7 | this.excludes.add( packageExclude ); |
77 | |
} |
78 | 16 | } |
79 | |
} |
80 | 15 | } |
81 | |
|
82 | |
public boolean canRelocatePath( String path ) |
83 | |
{ |
84 | 110767 | if ( path.endsWith( ".class" ) ) |
85 | |
{ |
86 | 432 | path = path.substring( 0, path.length() - 6 ); |
87 | |
} |
88 | 110767 | if ( excludes != null ) |
89 | |
{ |
90 | 88576 | for ( Iterator i = excludes.iterator(); i.hasNext(); ) |
91 | |
{ |
92 | 253819 | String exclude = (String) i.next(); |
93 | |
|
94 | 253819 | if ( SelectorUtils.matchPath( exclude, path, true ) ) |
95 | |
{ |
96 | 11016 | return false; |
97 | |
} |
98 | 242803 | } |
99 | |
} |
100 | |
|
101 | 99751 | return path.startsWith( pathPattern ); |
102 | |
} |
103 | |
|
104 | |
public boolean canRelocateClass( String clazz ) |
105 | |
{ |
106 | 4796 | return clazz.indexOf( '/' ) < 0 && canRelocatePath( clazz.replace( '.', '/' ) ); |
107 | |
} |
108 | |
|
109 | |
public String relocatePath( String path ) |
110 | |
{ |
111 | 20937 | return path.replaceFirst( pathPattern, shadedPathPattern ); |
112 | |
} |
113 | |
|
114 | |
public String relocateClass( String clazz ) |
115 | |
{ |
116 | 3 | return clazz.replaceFirst( pattern, shadedPattern ); |
117 | |
} |
118 | |
} |