1 | |
package org.apache.maven.plugins.shade.resource; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.maven.plugins.shade.relocation.Relocator; |
23 | |
import org.codehaus.plexus.util.StringUtils; |
24 | |
|
25 | |
import java.io.BufferedReader; |
26 | |
import java.io.IOException; |
27 | |
import java.io.InputStream; |
28 | |
import java.io.InputStreamReader; |
29 | |
import java.io.OutputStreamWriter; |
30 | |
import java.io.PrintWriter; |
31 | |
import java.io.Writer; |
32 | |
import java.text.SimpleDateFormat; |
33 | |
import java.util.Date; |
34 | |
import java.util.LinkedHashMap; |
35 | |
import java.util.LinkedHashSet; |
36 | |
import java.util.List; |
37 | |
import java.util.Map; |
38 | |
import java.util.Set; |
39 | |
import java.util.TreeSet; |
40 | |
import java.util.jar.JarEntry; |
41 | |
import java.util.jar.JarOutputStream; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | 1 | public class ApacheNoticeResourceTransformer |
47 | |
implements ResourceTransformer |
48 | |
{ |
49 | 1 | Set<String> entries = new LinkedHashSet<String>(); |
50 | |
|
51 | 1 | Map<String, Set<String>> organizationEntries = new LinkedHashMap<String, Set<String>>(); |
52 | |
|
53 | 1 | String projectName = ""; |
54 | |
|
55 | 1 | boolean addHeader = true; |
56 | |
|
57 | 1 | String preamble1 = "// ------------------------------------------------------------------\n" |
58 | |
+ "// NOTICE file corresponding to the section 4d of The Apache License,\n" |
59 | |
+ "// Version 2.0, in this case for "; |
60 | |
|
61 | 1 | String preamble2 = "\n// ------------------------------------------------------------------\n"; |
62 | |
|
63 | 1 | String preamble3 = "This product includes software developed at\n"; |
64 | |
|
65 | |
|
66 | 1 | String organizationName = "The Apache Software Foundation"; |
67 | |
|
68 | 1 | String organizationURL = "http://www.apache.org/"; |
69 | |
|
70 | 1 | String inceptionYear = "2006"; |
71 | |
|
72 | |
String copyright; |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
String encoding; |
78 | |
|
79 | |
private static final String NOTICE_PATH = "META-INF/NOTICE"; |
80 | |
|
81 | |
private static final String NOTICE_TXT_PATH = "META-INF/NOTICE.txt"; |
82 | |
|
83 | |
public boolean canTransformResource( String resource ) |
84 | |
{ |
85 | 4 | if ( NOTICE_PATH.equalsIgnoreCase( resource ) || NOTICE_TXT_PATH.equalsIgnoreCase( resource ) ) |
86 | |
{ |
87 | 3 | return true; |
88 | |
} |
89 | |
|
90 | 1 | return false; |
91 | |
} |
92 | |
|
93 | |
public void processResource( String resource, InputStream is, List<Relocator> relocators ) |
94 | |
throws IOException |
95 | |
{ |
96 | 0 | if ( entries.isEmpty() ) |
97 | |
{ |
98 | 0 | String year = new SimpleDateFormat( "yyyy" ).format( new Date() ); |
99 | 0 | if ( !inceptionYear.equals( year ) ) |
100 | |
{ |
101 | 0 | year = inceptionYear + "-" + year; |
102 | |
} |
103 | |
|
104 | |
|
105 | 0 | if ( addHeader ) |
106 | |
{ |
107 | 0 | entries.add( preamble1 + projectName + preamble2 ); |
108 | |
} |
109 | |
else |
110 | |
{ |
111 | 0 | entries.add( "" ); |
112 | |
} |
113 | |
|
114 | 0 | entries.add( projectName + "\nCopyright " + year + " " + organizationName + "\n" ); |
115 | 0 | entries.add( preamble3 + organizationName + " (" + organizationURL + ").\n" ); |
116 | |
} |
117 | |
|
118 | |
BufferedReader reader; |
119 | 0 | if ( StringUtils.isNotEmpty( encoding ) ) |
120 | |
{ |
121 | 0 | reader = new BufferedReader( new InputStreamReader( is, encoding ) ); |
122 | |
} |
123 | |
else |
124 | |
{ |
125 | 0 | reader = new BufferedReader( new InputStreamReader( is ) ); |
126 | |
} |
127 | |
|
128 | 0 | String line = reader.readLine(); |
129 | 0 | StringBuffer sb = new StringBuffer(); |
130 | 0 | Set<String> currentOrg = null; |
131 | 0 | int lineCount = 0; |
132 | 0 | while ( line != null ) |
133 | |
{ |
134 | 0 | String trimedLine = line.trim(); |
135 | |
|
136 | 0 | if ( !trimedLine.startsWith( "//" ) ) |
137 | |
{ |
138 | 0 | if ( trimedLine.length() > 0 ) |
139 | |
{ |
140 | 0 | if ( trimedLine.startsWith( "- " ) ) |
141 | |
{ |
142 | |
|
143 | 0 | if ( lineCount == 1 |
144 | |
&& sb.toString().indexOf( "This product includes/uses software(s) developed by" ) != -1 ) |
145 | |
{ |
146 | 0 | currentOrg = organizationEntries.get( sb.toString().trim() ); |
147 | 0 | if ( currentOrg == null ) |
148 | |
{ |
149 | 0 | currentOrg = new TreeSet<String>(); |
150 | 0 | organizationEntries.put( sb.toString().trim(), currentOrg ); |
151 | |
} |
152 | 0 | sb = new StringBuffer(); |
153 | |
} |
154 | 0 | else if ( sb.length() > 0 && currentOrg != null ) |
155 | |
{ |
156 | 0 | currentOrg.add( sb.toString() ); |
157 | 0 | sb = new StringBuffer(); |
158 | |
} |
159 | |
|
160 | |
} |
161 | 0 | sb.append( line ).append( "\n" ); |
162 | 0 | lineCount++; |
163 | |
} |
164 | |
else |
165 | |
{ |
166 | 0 | String ent = sb.toString(); |
167 | 0 | if ( ent.startsWith( projectName ) && ent.indexOf( "Copyright " ) != -1 ) |
168 | |
{ |
169 | 0 | copyright = ent; |
170 | |
} |
171 | 0 | if ( currentOrg == null ) |
172 | |
{ |
173 | 0 | entries.add( ent ); |
174 | |
} |
175 | |
else |
176 | |
{ |
177 | 0 | currentOrg.add( ent ); |
178 | |
} |
179 | 0 | sb = new StringBuffer(); |
180 | 0 | lineCount = 0; |
181 | 0 | currentOrg = null; |
182 | |
} |
183 | |
} |
184 | |
|
185 | 0 | line = reader.readLine(); |
186 | 0 | } |
187 | 0 | if ( sb.length() > 0 ) |
188 | |
{ |
189 | 0 | if ( currentOrg == null ) |
190 | |
{ |
191 | 0 | entries.add( sb.toString() ); |
192 | |
} |
193 | |
else |
194 | |
{ |
195 | 0 | currentOrg.add( sb.toString() ); |
196 | |
} |
197 | |
} |
198 | 0 | } |
199 | |
|
200 | |
public boolean hasTransformedResource() |
201 | |
{ |
202 | 0 | return true; |
203 | |
} |
204 | |
|
205 | |
public void modifyOutputStream( JarOutputStream jos ) |
206 | |
throws IOException |
207 | |
{ |
208 | 0 | jos.putNextEntry( new JarEntry( NOTICE_PATH ) ); |
209 | |
|
210 | |
Writer pow; |
211 | 0 | if ( StringUtils.isNotEmpty( encoding ) ) |
212 | |
{ |
213 | 0 | pow = new OutputStreamWriter( jos, encoding ); |
214 | |
} |
215 | |
else |
216 | |
{ |
217 | 0 | pow = new OutputStreamWriter( jos ); |
218 | |
} |
219 | 0 | PrintWriter writer = new PrintWriter( pow ); |
220 | |
|
221 | 0 | int count = 0; |
222 | 0 | for ( String line : entries ) |
223 | |
{ |
224 | 0 | ++count; |
225 | 0 | if ( line.equals( copyright ) && count != 2 ) |
226 | |
{ |
227 | 0 | continue; |
228 | |
} |
229 | |
|
230 | 0 | if ( count == 2 && copyright != null ) |
231 | |
{ |
232 | 0 | writer.print( copyright ); |
233 | 0 | writer.print( '\n' ); |
234 | |
} |
235 | |
else |
236 | |
{ |
237 | 0 | writer.print( line ); |
238 | 0 | writer.print( '\n' ); |
239 | |
} |
240 | 0 | if ( count == 3 ) |
241 | |
{ |
242 | |
|
243 | 0 | for ( Map.Entry<String, Set<String>> entry : organizationEntries.entrySet() ) |
244 | |
{ |
245 | 0 | writer.print( entry.getKey() ); |
246 | 0 | writer.print( '\n' ); |
247 | 0 | for ( String l : entry.getValue() ) |
248 | |
{ |
249 | 0 | writer.print( l ); |
250 | |
} |
251 | 0 | writer.print( '\n' ); |
252 | |
} |
253 | |
} |
254 | |
} |
255 | |
|
256 | 0 | writer.flush(); |
257 | |
|
258 | 0 | entries.clear(); |
259 | 0 | } |
260 | |
} |