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 1639422 2014-11-13 18:08:07Z 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      /**
62       * {@inheritDoc}
63       */
64      public String[] getExcludes()
65      {
66          return fileSet.getExcludes();
67      }
68  
69      /**
70       * {@inheritDoc}
71       */
72      public FileSelector[] getFileSelectors()
73      {
74          FileSelector[] sel = fileSet.getFileSelectors();
75          final FileSelector[] selectors1 = selectors;
76          return combineSelectors( sel, selectors1 );
77      }
78  
79      /**
80       * {@inheritDoc}
81       */
82      static FileSelector[] combineSelectors( FileSelector[] first, FileSelector[] second )
83      {
84          if ( ( first != null ) && ( second != null ) )
85          {
86              final FileSelector[] temp = new FileSelector[first.length + second.length];
87  
88              System.arraycopy( first, 0, temp, 0, first.length );
89              System.arraycopy( second, 0, temp, first.length, second.length );
90  
91              first = temp;
92          }
93          else if ( ( first == null ) && ( second != null ) )
94          {
95              first = second;
96          }
97  
98          return first;
99      }
100 
101     /**
102      * {@inheritDoc}
103      */
104     public String[] getIncludes()
105     {
106         return fileSet.getIncludes();
107     }
108 
109     /**
110      * {@inheritDoc}
111      */
112     public String getPrefix()
113     {
114         String prefix = fileSet.getPrefix();
115         if ( prefix == null )
116         {
117             return rootPrefix;
118         }
119 
120         if ( prefix.startsWith( "/" ) )
121         {
122             if ( prefix.length() > 1 )
123             {
124                 prefix = prefix.substring( 1 );
125             }
126             else
127             {
128                 prefix = "";
129             }
130         }
131 
132         return rootPrefix + prefix;
133     }
134 
135     /**
136      * {@inheritDoc}
137      */
138     public boolean isCaseSensitive()
139     {
140         return fileSet.isCaseSensitive();
141     }
142 
143     /**
144      * {@inheritDoc}
145      */
146     public boolean isIncludingEmptyDirectories()
147     {
148         return fileSet.isIncludingEmptyDirectories();
149     }
150 
151     /**
152      * {@inheritDoc}
153      */
154     public boolean isUsingDefaultExcludes()
155     {
156         return fileSet.isUsingDefaultExcludes();
157     }
158 
159     /**
160      * {@inheritDoc}
161      */
162     public File getDirectory()
163     {
164         return fileSet.getDirectory();
165     }
166 
167     public InputStreamTransformer getStreamTransformer()
168     {
169         return fileSet.getStreamTransformer();
170     }
171 }