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.ArrayDeque;
20 import java.util.Deque;
21 import java.util.HashSet;
22 import java.util.Set;
23 import java.util.Stack;
24
25 /**
26 * A list of directories that have been added to an archive.
27 */
28 public class AddedDirs {
29
30 private final Set<String> addedDirs = new HashSet<String>();
31
32 /**
33 * @deprecated use {@link #asStringDeque(String)} instead.
34 */
35 @Deprecated
36 public Stack<String> asStringStack(String entry) {
37 Stack<String> directories = new Stack<>();
38
39 // Don't include the last entry itself if it's
40 // a dir; it will be added on its own.
41 int slashPos = entry.length() - (entry.endsWith("/") ? 1 : 0);
42
43 while ((slashPos = entry.lastIndexOf('/', slashPos - 1)) != -1) {
44 String dir = entry.substring(0, slashPos + 1);
45
46 if (addedDirs.contains(dir)) {
47 break;
48 }
49
50 directories.push(dir);
51 }
52 return directories;
53 }
54
55 public Deque<String> asStringDeque(String entry) {
56 Deque<String> directories = new ArrayDeque<>();
57
58 // Don't include the last entry itself if it's
59 // a dir; it will be added on its own.
60 int slashPos = entry.length() - (entry.endsWith("/") ? 1 : 0);
61
62 while ((slashPos = entry.lastIndexOf('/', slashPos - 1)) != -1) {
63 String dir = entry.substring(0, slashPos + 1);
64
65 if (addedDirs.contains(dir)) {
66 break;
67 }
68
69 directories.push(dir);
70 }
71 return directories;
72 }
73
74 public void clear() {
75 addedDirs.clear();
76 }
77
78 /**
79 * Adds the path to this list.
80 *
81 * @param vPath The path to add.
82 *
83 * @return true if the path was already present, false if it has been added.
84 */
85 public boolean update(String vPath) {
86 return !addedDirs.add(vPath);
87 }
88
89 public Set<String> allAddedDirs() {
90 return new HashSet<String>(addedDirs);
91 }
92 }