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          final FileSelector[] selectors1 = selectors;
89          return combineSelectors(sel, selectors1);
90      }
91  
92      /**
93       * {@inheritDoc}
94       */
95      @Override
96      public String[] getIncludes() {
97          return fileSet.getIncludes();
98      }
99  
100     /**
101      * {@inheritDoc}
102      */
103     @Override
104     public String getPrefix() {
105         String prefix = fileSet.getPrefix();
106         if (prefix == null) {
107             return rootPrefix;
108         }
109 
110         if (prefix.startsWith("/")) {
111             if (prefix.length() > 1) {
112                 prefix = prefix.substring(1);
113             } else {
114                 prefix = "";
115             }
116         }
117 
118         return rootPrefix + prefix;
119     }
120 
121     /**
122      * {@inheritDoc}
123      */
124     @Override
125     public boolean isCaseSensitive() {
126         return fileSet.isCaseSensitive();
127     }
128 
129     /**
130      * {@inheritDoc}
131      */
132     @Override
133     public boolean isIncludingEmptyDirectories() {
134         return fileSet.isIncludingEmptyDirectories();
135     }
136 
137     /**
138      * {@inheritDoc}
139      */
140     @Override
141     public boolean isUsingDefaultExcludes() {
142         return fileSet.isUsingDefaultExcludes();
143     }
144 
145     /**
146      * {@inheritDoc}
147      */
148     @Override
149     public File getDirectory() {
150         return fileSet.getDirectory();
151     }
152 
153     @Override
154     public InputStreamTransformer getStreamTransformer() {
155         return fileSet.getStreamTransformer();
156     }
157 
158     @Override
159     public FileMapper[] getFileMappers() {
160         return EMPTY_FILE_MAPPERS_ARRAY;
161     }
162 }