View Javadoc
1   package org.apache.maven.plugins.shade.resource;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * An appending transformer for resource bundles
37   * 
38   * @author Robert Scholte
39   * @since 3.0.0
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       * the base name of the resource bundle, a fully qualified class name
52       * @param basename The basename.
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 }