View Javadoc
1   package org.apache.maven.plugins.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  import org.codehaus.plexus.components.io.filemappers.FileMapper;
28  
29  /**
30   *
31   */
32  class PrefixedFileSet
33      implements FileSet
34  {
35      private final static FileMapper[] EMPTY_FILE_MAPPERS_ARRAY = new FileMapper[0];
36  
37      private final String rootPrefix;
38  
39      private final FileSet fileSet;
40  
41      private final FileSelector[] selectors;
42  
43      /**
44       * @param fileSet    The file set.
45       * @param rootPrefix The root prefix
46       * @param selectors  The file selectors.
47       */
48      PrefixedFileSet( final FileSet fileSet, final String rootPrefix, final FileSelector[] selectors )
49      {
50          this.fileSet = fileSet;
51          this.selectors = selectors;
52  
53          if ( rootPrefix.length() > 0 && !rootPrefix.endsWith( "/" ) )
54          {
55              this.rootPrefix = rootPrefix + "/";
56          }
57          else
58          {
59              this.rootPrefix = rootPrefix;
60          }
61      }
62  
63      /**
64       * {@inheritDoc}
65       */
66      static FileSelector[] combineSelectors( FileSelector[] first, FileSelector[] second )
67      {
68          if ( ( first != null ) && ( second != null ) )
69          {
70              final FileSelector[] temp = new FileSelector[first.length + second.length];
71  
72              System.arraycopy( first, 0, temp, 0, first.length );
73              System.arraycopy( second, 0, temp, first.length, second.length );
74  
75              first = temp;
76          }
77          else if ( ( first == null ) && ( second != null ) )
78          {
79              first = second;
80          }
81  
82          return first;
83      }
84  
85      /**
86       * {@inheritDoc}
87       */
88      @Override
89      public String[] getExcludes()
90      {
91          return fileSet.getExcludes();
92      }
93  
94      /**
95       * {@inheritDoc}
96       */
97      @Override
98      public FileSelector[] getFileSelectors()
99      {
100         FileSelector[] sel = fileSet.getFileSelectors();
101         final FileSelector[] selectors1 = selectors;
102         return combineSelectors( sel, selectors1 );
103     }
104 
105     /**
106      * {@inheritDoc}
107      */
108     @Override
109     public String[] getIncludes()
110     {
111         return fileSet.getIncludes();
112     }
113 
114     /**
115      * {@inheritDoc}
116      */
117     @Override
118     public String getPrefix()
119     {
120         String prefix = fileSet.getPrefix();
121         if ( prefix == null )
122         {
123             return rootPrefix;
124         }
125 
126         if ( prefix.startsWith( "/" ) )
127         {
128             if ( prefix.length() > 1 )
129             {
130                 prefix = prefix.substring( 1 );
131             }
132             else
133             {
134                 prefix = "";
135             }
136         }
137 
138         return rootPrefix + prefix;
139     }
140 
141     /**
142      * {@inheritDoc}
143      */
144     @Override
145     public boolean isCaseSensitive()
146     {
147         return fileSet.isCaseSensitive();
148     }
149 
150     /**
151      * {@inheritDoc}
152      */
153     @Override
154     public boolean isIncludingEmptyDirectories()
155     {
156         return fileSet.isIncludingEmptyDirectories();
157     }
158 
159     /**
160      * {@inheritDoc}
161      */
162     @Override
163     public boolean isUsingDefaultExcludes()
164     {
165         return fileSet.isUsingDefaultExcludes();
166     }
167 
168     /**
169      * {@inheritDoc}
170      */
171     @Override
172     public File getDirectory()
173     {
174         return fileSet.getDirectory();
175     }
176 
177     @Override
178     public InputStreamTransformer getStreamTransformer()
179     {
180         return fileSet.getStreamTransformer();
181     }
182 
183     @Override
184     public FileMapper[] getFileMappers()
185     {
186         return EMPTY_FILE_MAPPERS_ARRAY;
187     }
188 }