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 {
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 }