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.ByteArrayOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.jar.JarEntry;
29 import java.util.jar.JarOutputStream;
30 import java.util.regex.Pattern;
31
32 import org.apache.maven.plugins.shade.relocation.Relocator;
33 import org.codehaus.plexus.util.IOUtil;
34
35
36
37
38
39
40
41 public class ResourceBundleAppendingTransformer
42 extends AbstractCompatibilityTransformer
43 {
44 private Map<String, ByteArrayOutputStream> dataMap = new HashMap<>();
45
46 private Pattern resourceBundlePattern;
47
48 private long time = Long.MIN_VALUE;
49
50
51
52
53
54 public void setBasename( String basename )
55 {
56 resourceBundlePattern = Pattern.compile( basename + "(_[a-zA-Z]+){0,3}\\.properties" );
57 }
58
59 public boolean canTransformResource( String r )
60 {
61 return resourceBundlePattern != null && resourceBundlePattern.matcher( r ).matches();
62 }
63
64 public void processResource( String resource, InputStream is, List<Relocator> relocators, long time )
65 throws IOException
66 {
67 ByteArrayOutputStream data = dataMap.get( resource );
68 if ( data == null )
69 {
70 data = new ByteArrayOutputStream();
71 dataMap.put( resource, data );
72 }
73
74 IOUtil.copy( is, data );
75 data.write( '\n' );
76
77 if ( time > this.time )
78 {
79 this.time = time;
80 }
81 }
82
83 public boolean hasTransformedResource()
84 {
85 return !dataMap.isEmpty();
86 }
87
88 public void modifyOutputStream( JarOutputStream jos )
89 throws IOException
90 {
91 for ( Map.Entry<String, ByteArrayOutputStream> dataEntry : dataMap.entrySet() )
92 {
93 JarEntry jarEntry = new JarEntry( dataEntry.getKey() );
94 jarEntry.setTime( time );
95 jos.putNextEntry( jarEntry );
96
97 jos.write( dataEntry.getValue().toByteArray() );
98 dataEntry.getValue().reset();
99 }
100 }
101
102 }