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