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 java.io.BufferedReader; |
23 | |
import java.io.IOException; |
24 | |
import java.io.InputStream; |
25 | |
import java.io.InputStreamReader; |
26 | |
import java.io.OutputStreamWriter; |
27 | |
import java.io.PrintWriter; |
28 | |
import java.io.Writer; |
29 | |
import java.text.SimpleDateFormat; |
30 | |
import java.util.Date; |
31 | |
import java.util.Iterator; |
32 | |
import java.util.LinkedHashMap; |
33 | |
import java.util.LinkedHashSet; |
34 | |
import java.util.List; |
35 | |
import java.util.Map; |
36 | |
import java.util.Set; |
37 | |
import java.util.TreeSet; |
38 | |
import java.util.jar.JarEntry; |
39 | |
import java.util.jar.JarOutputStream; |
40 | |
|
41 | |
import org.codehaus.plexus.util.StringUtils; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | 1 | public class ApacheNoticeResourceTransformer |
47 | |
implements ResourceTransformer |
48 | |
{ |
49 | 1 | Set entries = new LinkedHashSet(); |
50 | 1 | Map organizationEntries = new LinkedHashMap(); |
51 | |
|
52 | 1 | String projectName = ""; |
53 | 1 | boolean addHeader = true; |
54 | |
|
55 | 1 | String preamble1 = |
56 | |
"// ------------------------------------------------------------------\n" |
57 | |
+ "// NOTICE file corresponding to the section 4d of The Apache License,\n" |
58 | |
+ "// Version 2.0, in this case for "; |
59 | |
|
60 | 1 | String preamble2 = "\n// ------------------------------------------------------------------\n"; |
61 | |
|
62 | 1 | String preamble3 = "This product includes software developed at\n"; |
63 | |
|
64 | |
|
65 | 1 | String organizationName = "The Apache Software Foundation"; |
66 | 1 | String organizationURL = "http://www.apache.org/"; |
67 | 1 | String inceptionYear = "2006"; |
68 | |
|
69 | |
String copyright; |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
String encoding; |
75 | |
|
76 | |
private static final String NOTICE_PATH = "META-INF/NOTICE"; |
77 | |
|
78 | |
private static final String NOTICE_TXT_PATH = "META-INF/NOTICE.txt"; |
79 | |
|
80 | |
public boolean canTransformResource( String resource ) |
81 | |
{ |
82 | 4 | if ( NOTICE_PATH.equalsIgnoreCase( resource ) || NOTICE_TXT_PATH.equalsIgnoreCase( resource ) ) |
83 | |
{ |
84 | 3 | return true; |
85 | |
} |
86 | |
|
87 | 1 | return false; |
88 | |
} |
89 | |
|
90 | |
public void processResource( String resource, InputStream is, List relocators ) |
91 | |
throws IOException |
92 | |
{ |
93 | 0 | if ( entries.isEmpty() ) |
94 | |
{ |
95 | 0 | String year = new SimpleDateFormat( "yyyy" ).format( new Date() ); |
96 | 0 | if ( !inceptionYear.equals( year ) ) |
97 | |
{ |
98 | 0 | year = inceptionYear + "-" + year; |
99 | |
} |
100 | |
|
101 | |
|
102 | |
|
103 | 0 | if ( addHeader ) |
104 | |
{ |
105 | 0 | entries.add( preamble1 + projectName + preamble2 ); |
106 | |
} |
107 | |
else |
108 | |
{ |
109 | 0 | entries.add( "" ); |
110 | |
} |
111 | |
|
112 | 0 | entries.add( projectName + "\nCopyright " + year + " " + organizationName + "\n" ); |
113 | 0 | entries.add( preamble3 + organizationName + " (" + organizationURL + ").\n" ); |
114 | |
} |
115 | |
|
116 | |
|
117 | |
BufferedReader reader; |
118 | 0 | if ( StringUtils.isNotEmpty( encoding ) ) |
119 | |
{ |
120 | 0 | reader = new BufferedReader( new InputStreamReader( is, encoding ) ); |
121 | |
} |
122 | |
else |
123 | |
{ |
124 | 0 | reader = new BufferedReader( new InputStreamReader( is ) ); |
125 | |
} |
126 | |
|
127 | 0 | String line = reader.readLine(); |
128 | 0 | StringBuffer sb = new StringBuffer(); |
129 | 0 | Set currentOrg = null; |
130 | 0 | int lineCount = 0; |
131 | 0 | while ( line != null ) |
132 | |
{ |
133 | 0 | String trimedLine = line.trim(); |
134 | |
|
135 | 0 | if ( !trimedLine.startsWith( "//" ) ) |
136 | |
{ |
137 | 0 | if ( trimedLine.length() > 0 ) |
138 | |
{ |
139 | 0 | if ( trimedLine.startsWith( "- " ) ) |
140 | |
{ |
141 | |
|
142 | 0 | if ( lineCount == 1 |
143 | |
&& sb.toString().indexOf( "This product includes/uses software(s) developed by" ) != -1 ) |
144 | |
{ |
145 | 0 | currentOrg = (Set) organizationEntries.get( sb.toString().trim() ); |
146 | 0 | if ( currentOrg == null ) |
147 | |
{ |
148 | 0 | currentOrg = new TreeSet(); |
149 | 0 | organizationEntries.put( sb.toString().trim(), currentOrg ); |
150 | |
} |
151 | 0 | sb = new StringBuffer(); |
152 | |
} |
153 | 0 | else if ( sb.length() > 0 && currentOrg != null ) |
154 | |
{ |
155 | 0 | currentOrg.add( sb.toString() ); |
156 | 0 | sb = new StringBuffer(); |
157 | |
} |
158 | |
|
159 | |
} |
160 | 0 | sb.append( line ).append( "\n" ); |
161 | 0 | lineCount++; |
162 | |
} |
163 | |
else |
164 | |
{ |
165 | 0 | String ent = sb.toString(); |
166 | 0 | if ( ent.startsWith( projectName ) |
167 | |
&& 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 ( Iterator itr = entries.iterator() ; itr.hasNext() ; ) |
223 | |
{ |
224 | 0 | ++count; |
225 | 0 | String line = (String) itr.next(); |
226 | 0 | if ( line.equals( copyright ) && count != 2 ) |
227 | |
{ |
228 | 0 | continue; |
229 | |
} |
230 | |
|
231 | 0 | if ( count == 2 && copyright != null ) |
232 | |
{ |
233 | 0 | writer.print( copyright ); |
234 | 0 | writer.print( '\n' ); |
235 | |
} |
236 | |
else |
237 | |
{ |
238 | 0 | writer.print( line ); |
239 | 0 | writer.print( '\n' ); |
240 | |
} |
241 | 0 | if ( count == 3 ) |
242 | |
{ |
243 | |
|
244 | 0 | for ( Iterator oit = organizationEntries.entrySet().iterator(); oit.hasNext(); ) |
245 | |
{ |
246 | 0 | Map.Entry entry = (Map.Entry) oit.next(); |
247 | 0 | writer.print( entry.getKey().toString() ); |
248 | 0 | writer.print( '\n' ); |
249 | 0 | Set entrySet = (Set) entry.getValue(); |
250 | 0 | for ( Iterator eit = entrySet.iterator(); eit.hasNext(); ) |
251 | |
{ |
252 | 0 | writer.print( eit.next().toString() ); |
253 | |
} |
254 | 0 | writer.print( '\n' ); |
255 | 0 | } |
256 | |
} |
257 | 0 | } |
258 | |
|
259 | 0 | writer.flush(); |
260 | |
|
261 | 0 | entries.clear(); |
262 | 0 | } |
263 | |
} |