View Javadoc
1   package org.apache.maven.plugins.war.util;
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.util.DirectoryScanner;
23  
24  import java.io.File;
25  import java.util.Collection;
26  import java.util.HashSet;
27  import java.util.Iterator;
28  import java.util.LinkedHashSet;
29  import java.util.Set;
30  
31  /**
32   * Set of file's paths.
33   * 
34   * The class extends functionality of a "normal" set of strings by a process of the paths normalization. All paths are
35   * converted to unix form (slashes) and they don't start with starting /.
36   *
37   * @author Piotr Tabor
38   * @version $Id$
39   */
40  
41  public class PathSet
42      implements Iterable<String>
43  {
44      private static final String SEPARATOR = "/";
45      private static final char SEPARATOR_CHAR = SEPARATOR.charAt( 0 );
46      /**
47       * Set of normalized paths
48       */
49      private Set<String> pathsSet = new LinkedHashSet<>();
50  
51      static String normalizeSubPath( String path )
52      {
53          if ( path.isEmpty() )
54          {
55              return path;
56          }
57          String cleanPath = path.replaceAll( "[\\\\]+", SEPARATOR )
58                  .replaceAll( "[/]+" , SEPARATOR );
59          cleanPath = cleanPath.charAt( 0 ) == SEPARATOR_CHAR ? cleanPath.substring( 1 ) : cleanPath;
60          if ( cleanPath.isEmpty() )
61          {
62              return cleanPath;
63          }
64          if ( cleanPath.charAt( cleanPath.length() - 1 ) == SEPARATOR_CHAR )
65          {
66              return cleanPath.substring( 0, cleanPath.length() - 1 );
67          }
68          return cleanPath;
69      }
70  
71      /*-------------------- Business interface ------------------------------*/
72  
73      /**
74       * Creates an empty paths set
75       */
76      public PathSet()
77      {
78          /* Empty default constructor */
79      }
80  
81      /**
82       * Creates paths set and normalizate and adds all 'paths'. The source 'paths' will not be changed
83       *
84       * @param paths to be added
85       */
86      public PathSet( Collection<String> paths )
87      {
88          addAll( paths );
89      }
90  
91      /**
92       * Creates paths set and normalizate and adds all 'paths'. The source 'paths' will not be changed
93       *
94       * @param paths to be added
95       */
96      public PathSet( String[] paths )
97      {
98          addAll( paths );
99      }
100 
101     /**
102      * Normalizes and adds given path to the set.
103      *
104      * @param path to be added
105      */
106     public void add( String path )
107     {
108         pathsSet.add( normalizeSubPath( path ) );
109     }
110 
111     /**
112      * Normalizes and adds given paths (collection of strings) to the set. The source collection will not be changed
113      *
114      * @param paths - collection of strings to be added
115      * @param prefix added to all given paths
116      */
117     public void addAll( Collection<String> paths, String prefix )
118     {
119         for ( String val : paths )
120         {
121             add( prefix + SEPARATOR +  val );
122         }
123     }
124 
125     /**
126      * Normalizes and adds given paths to the set. The source collection will not be changed
127      *
128      * @param paths to be added
129      * @param prefix added to all given paths
130      */
131     public void addAll( String[] paths, String prefix )
132     {
133         for ( String val : paths )
134         {
135             add( prefix + SEPARATOR + val );
136         }
137     }
138 
139     /**
140      * Adds given paths to the set. The source collection will not be changed
141      *
142      * @param paths to be added
143      * @param prefix added to all given paths
144      */
145     public void addAll( PathSet paths, String prefix )
146     {
147         for ( String path : paths )
148         {
149             add( prefix + SEPARATOR + path );
150         }
151     }
152 
153     /**
154      * Normalizes and adds given paths (collection of strings) to the set. The source collection will not be changed
155      *
156      * @param paths - collection of strings to be added
157      */
158     public void addAll( Collection<String> paths )
159     {
160         addAll( paths, "" );
161     }
162 
163     /**
164      * Normalizes and adds given paths to the set. The source collection will not be changed
165      *
166      * @param paths to be added
167      */
168     public void addAll( String[] paths )
169     {
170         addAll( paths, "" );
171     }
172 
173     /**
174      * Adds given paths to the set. The source collection will not be changed
175      *
176      * @param paths to be added
177      */
178     public void addAll( PathSet paths )
179     {
180         addAll( paths, "" );
181     }
182 
183     /**
184      * Checks if the set constains given path. The path is normalized before check.
185      *
186      * @param path we are looking for in the set.
187      * @return information if the set constains the path.
188      */
189     public boolean contains( String path )
190     {
191         return pathsSet.contains( normalizeSubPath( path ) );
192     }
193 
194     /**
195      * Removes the specified path if it exists.
196      *
197      * @param path the path to remove
198      * @return true if the path was removed, false if it did not existed
199      */
200     boolean remove( String path )
201     {
202         return pathsSet.remove( normalizeSubPath( path ) );
203     }
204 
205     /**
206      * Returns iterator of normalized paths (strings)
207      *
208      * @return iterator of normalized paths (strings)
209      */
210     public Iterator<String> iterator()
211     {
212         return pathsSet.iterator();
213     }
214 
215     /**
216      * @return {@link #pathsSet}
217      */
218     public Collection<String> paths()
219     {
220         return pathsSet;
221     }
222 
223     /**
224      * Adds given prefix to all paths in the set.
225      * 
226      * The prefix should be ended by '/'. The generated paths are normalized.
227      *
228      * @param prefix to be added to all items
229      */
230     public void addPrefix( String prefix )
231     {
232         final Set<String> newSet = new HashSet<>();
233         for ( String path : pathsSet )
234         {
235             newSet.add( normalizeSubPath( prefix + path ) );
236         }
237         pathsSet = newSet;
238     }
239 
240     /**
241      * Returns count of the paths in the set
242      *
243      * @return count of the paths in the set
244      */
245     public int size()
246     {
247         return pathsSet.size();
248     }
249 
250     /**
251      * Adds to the set all files in the given directory
252      *
253      * @param directory that will be searched for file's paths to add
254      * @param prefix to be added to all found files
255      */
256     public void addAllFilesInDirectory( File directory, String prefix )
257     {
258         DirectoryScanner scanner = new DirectoryScanner();
259         scanner.setBasedir( directory );
260         scanner.scan();
261         addAll( scanner.getIncludedFiles(), prefix );
262     }
263 
264 }