View Javadoc
1   package org.apache.maven.plugin.assembly.archive.archiver;
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 org.codehaus.plexus.archiver.FileSet;
23  import org.codehaus.plexus.components.io.fileselectors.FileSelector;
24  import org.codehaus.plexus.components.io.functions.InputStreamTransformer;
25  
26  import java.io.File;
27  
28  /**
29   * @version $Id: PrefixedFileSet.java 1633832 2014-10-23 15:03:47Z krosenvold $
30   */
31  class PrefixedFileSet
32      implements FileSet
33  {
34  
35      private final String rootPrefix;
36  
37      private final FileSet fileSet;
38  
39      private final FileSelector[] selectors;
40  
41      /**
42       * @param fileSet The file set.
43       * @param rootPrefix The root prefix
44       * @param selectors The file selectors.
45       */
46      public PrefixedFileSet( final FileSet fileSet, final String rootPrefix, final FileSelector[] selectors )
47      {
48          this.fileSet = fileSet;
49          this.selectors = selectors;
50  
51          if ( rootPrefix.length() > 0 && !rootPrefix.endsWith( "/" ) )
52          {
53              this.rootPrefix = rootPrefix + "/";
54          }
55          else
56          {
57              this.rootPrefix = rootPrefix;
58          }
59      }
60  
61      /** {@inheritDoc} */
62      public String[] getExcludes()
63      {
64          return fileSet.getExcludes();
65      }
66  
67      /** {@inheritDoc} */
68      public FileSelector[] getFileSelectors()
69      {
70          FileSelector[] sel = fileSet.getFileSelectors();
71          final FileSelector[] selectors1 = selectors;
72          return combineSelectors( sel, selectors1 );
73      }
74  
75      /** {@inheritDoc} */
76      static FileSelector[] combineSelectors( FileSelector[] first, FileSelector[] second )
77      {
78          if ( ( first != null ) && ( second != null ) )
79          {
80              final FileSelector[] temp = new FileSelector[first.length + second.length];
81  
82              System.arraycopy( first, 0, temp, 0, first.length );
83              System.arraycopy( second, 0, temp, first.length, second.length );
84  
85              first = temp;
86          }
87          else if ( ( first == null ) && ( second != null ) )
88          {
89              first = second;
90          }
91  
92          return first;
93      }
94  
95      /** {@inheritDoc} */
96      public String[] getIncludes()
97      {
98          return fileSet.getIncludes();
99      }
100 
101     /** {@inheritDoc} */
102     public String getPrefix()
103     {
104         String prefix = fileSet.getPrefix();
105         if ( prefix == null )
106         {
107             return rootPrefix;
108         }
109 
110         if ( prefix.startsWith( "/" ) )
111         {
112             if ( prefix.length() > 1 )
113             {
114                 prefix = prefix.substring( 1 );
115             }
116             else
117             {
118                 prefix = "";
119             }
120         }
121 
122         return rootPrefix + prefix;
123     }
124 
125     /** {@inheritDoc} */
126     public boolean isCaseSensitive()
127     {
128         return fileSet.isCaseSensitive();
129     }
130 
131     /** {@inheritDoc} */
132     public boolean isIncludingEmptyDirectories()
133     {
134         return fileSet.isIncludingEmptyDirectories();
135     }
136 
137     /** {@inheritDoc} */
138     public boolean isUsingDefaultExcludes()
139     {
140         return fileSet.isUsingDefaultExcludes();
141     }
142 
143     /** {@inheritDoc} */
144     public File getDirectory()
145     {
146         return fileSet.getDirectory();
147     }
148 
149     public InputStreamTransformer getStreamTransformer()
150     {
151         return fileSet.getStreamTransformer();
152     }
153 }