View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.assembly.archive.archiver;
20  
21  import java.io.File;
22  
23  import org.codehaus.plexus.archiver.FileSet;
24  import org.codehaus.plexus.components.io.filemappers.FileMapper;
25  import org.codehaus.plexus.components.io.fileselectors.FileSelector;
26  import org.codehaus.plexus.components.io.functions.InputStreamTransformer;
27  
28  /**
29   *
30   */
31  class PrefixedFileSet implements FileSet {
32      private static final FileMapper[] EMPTY_FILE_MAPPERS_ARRAY = new FileMapper[0];
33  
34      private final String rootPrefix;
35  
36      private final FileSet fileSet;
37  
38      private final FileSelector[] selectors;
39  
40      /**
41       * @param fileSet    The file set.
42       * @param rootPrefix The root prefix
43       * @param selectors  The file selectors.
44       */
45      PrefixedFileSet(final FileSet fileSet, final String rootPrefix, final FileSelector[] selectors) {
46          this.fileSet = fileSet;
47          this.selectors = selectors;
48  
49          if (rootPrefix.length() > 0 && !rootPrefix.endsWith("/")) {
50              this.rootPrefix = rootPrefix + "/";
51          } else {
52              this.rootPrefix = rootPrefix;
53          }
54      }
55  
56      /**
57       * {@inheritDoc}
58       */
59      static FileSelector[] combineSelectors(FileSelector[] first, FileSelector[] second) {
60          if ((first != null) && (second != null)) {
61              final FileSelector[] temp = new FileSelector[first.length + second.length];
62  
63              System.arraycopy(first, 0, temp, 0, first.length);
64              System.arraycopy(second, 0, temp, first.length, second.length);
65  
66              first = temp;
67          } else if ((first == null) && (second != null)) {
68              first = second;
69          }
70  
71          return first;
72      }
73  
74      /**
75       * {@inheritDoc}
76       */
77      @Override
78      public String[] getExcludes() {
79          return fileSet.getExcludes();
80      }
81  
82      /**
83       * {@inheritDoc}
84       */
85      @Override
86      public FileSelector[] getFileSelectors() {
87          FileSelector[] sel = fileSet.getFileSelectors();
88          return combineSelectors(sel, selectors);
89      }
90  
91      /**
92       * {@inheritDoc}
93       */
94      @Override
95      public String[] getIncludes() {
96          return fileSet.getIncludes();
97      }
98  
99      /**
100      * {@inheritDoc}
101      */
102     @Override
103     public String getPrefix() {
104         String prefix = fileSet.getPrefix();
105         if (prefix == null) {
106             return rootPrefix;
107         }
108 
109         if (prefix.startsWith("/")) {
110             if (prefix.length() > 1) {
111                 prefix = prefix.substring(1);
112             } else {
113                 prefix = "";
114             }
115         }
116 
117         return rootPrefix + prefix;
118     }
119 
120     /**
121      * {@inheritDoc}
122      */
123     @Override
124     public boolean isCaseSensitive() {
125         return fileSet.isCaseSensitive();
126     }
127 
128     /**
129      * {@inheritDoc}
130      */
131     @Override
132     public boolean isIncludingEmptyDirectories() {
133         return fileSet.isIncludingEmptyDirectories();
134     }
135 
136     /**
137      * {@inheritDoc}
138      */
139     @Override
140     public boolean isUsingDefaultExcludes() {
141         return fileSet.isUsingDefaultExcludes();
142     }
143 
144     /**
145      * {@inheritDoc}
146      */
147     @Override
148     public File getDirectory() {
149         return fileSet.getDirectory();
150     }
151 
152     @Override
153     public InputStreamTransformer getStreamTransformer() {
154         return fileSet.getStreamTransformer();
155     }
156 
157     @Override
158     public FileMapper[] getFileMappers() {
159         return EMPTY_FILE_MAPPERS_ARRAY;
160     }
161 }