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