View Javadoc
1   /**
2    *
3    * Copyright 2015 The Apache Software Foundation
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.codehaus.plexus.archiver.zip;
18  
19  import java.util.HashSet;
20  import java.util.Hashtable;
21  import java.util.Set;
22  import java.util.Stack;
23  
24  /**
25   * A list of directories that have been added to an archive.
26   */
27  public class AddedDirs
28  {
29  
30      private final Hashtable<String, String> addedDirs = new Hashtable<String, String>();
31  
32      public Stack<String> asStringStack( String entry )
33      {
34          Stack<String> directories = new Stack<String>();
35  
36          // Don't include the last entry itself if it's
37          // a dir; it will be added on its own.
38          int slashPos = entry.length() - ( entry.endsWith( "/" ) ? 1 : 0 );
39  
40          while ( ( slashPos = entry.lastIndexOf( '/', slashPos - 1 ) ) != -1 )
41          {
42              String dir = entry.substring( 0, slashPos + 1 );
43  
44              if ( addedDirs.contains( dir ) )
45              {
46                  break;
47              }
48  
49              directories.push( dir );
50          }
51          return directories;
52      }
53  
54      public void clear()
55      {
56          addedDirs.clear();
57      }
58  
59      /**
60       * Adds the path to this list.
61       *
62       * @param vPath The path to add.
63       *
64       * @return true if the path was not present, false if it already existed.
65       */
66      public boolean update( String vPath )
67      {
68          if ( addedDirs.get( vPath ) != null )
69          {
70              // don't add directories we've already added.
71              // no warning if we try, it is harmless in and of itself
72              return true;
73          }
74          addedDirs.put( vPath, vPath );
75          return false;
76      }
77  
78      public Set<String> allAddedDirs()
79      {
80          return new HashSet<String>( addedDirs.keySet() );
81      }
82  
83  }