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 if ( resourceBundlePattern != null && resourceBundlePattern.matcher( r ).matches() )
62 {
63 return true;
64 }
65
66 return false;
67 }
68
69 public void processResource( String resource, InputStream is, List<Relocator> relocators, long time )
70 throws IOException
71 {
72 ByteArrayOutputStream data = dataMap.get( resource );
73 if ( data == null )
74 {
75 data = new ByteArrayOutputStream();
76 dataMap.put( resource, data );
77 }
78
79 IOUtil.copy( is, data );
80 data.write( '\n' );
81
82 if ( time > this.time )
83 {
84 this.time = time;
85 }
86 }
87
88 public boolean hasTransformedResource()
89 {
90 return !dataMap.isEmpty();
91 }
92
93 public void modifyOutputStream( JarOutputStream jos )
94 throws IOException
95 {
96 for ( Map.Entry<String, ByteArrayOutputStream> dataEntry : dataMap.entrySet() )
97 {
98 JarEntry jarEntry = new JarEntry( dataEntry.getKey() );
99 jarEntry.setTime( time );
100 jos.putNextEntry( jarEntry );
101
102 jos.write( dataEntry.getValue().toByteArray() );
103 dataEntry.getValue().reset();
104 }
105 }
106
107 }