KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > core > client > PruneFolderVisitor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ccvs.core.client;
12
13
14 import java.util.HashSet JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Set JavaDoc;
17
18 import org.eclipse.team.internal.ccvs.core.CVSException;
19 import org.eclipse.team.internal.ccvs.core.ICVSFile;
20 import org.eclipse.team.internal.ccvs.core.ICVSFolder;
21 import org.eclipse.team.internal.ccvs.core.ICVSResource;
22 import org.eclipse.team.internal.ccvs.core.ICVSResourceVisitor;
23
24 /**
25  * Goes recursivly through the folders checks if they are empyty
26  * and deletes them. Of course it is starting at the leaves of the
27  * recusion (the folders that do not have subfolders).
28  */

29 public class PruneFolderVisitor implements ICVSResourceVisitor {
30     
31     private ICVSFolder localRoot;
32     
33     public PruneFolderVisitor() {
34     }
35     
36     /**
37      * This method is used to visit a set of ICVSResources.
38      */

39     public void visit(Session s, ICVSResource[] resources) throws CVSException {
40         visit(s.getLocalRoot(), resources);
41     }
42     
43     /**
44      * This method is used to visit a set of ICVSResources.
45      */

46     public void visit(ICVSFolder root, ICVSResource[] resources) throws CVSException {
47         localRoot = root;
48         
49         // Visit the resources
50
Set JavaDoc prunableParents = new HashSet JavaDoc();
51         for (int i = 0; i < resources.length; i++) {
52             ICVSResource cvsResource = resources[i];
53             // prune the resource and it's children when appropriate
54
cvsResource.accept(this);
55             // if the resource doesn't exists, attempt to prune it's parent
56
if (!cvsResource.exists())
57                 prunableParents.add(cvsResource.getParent());
58         }
59         for (Iterator JavaDoc iter = prunableParents.iterator(); iter.hasNext();) {
60             ICVSFolder cvsFolder = (ICVSFolder)iter.next();
61             pruneFolderAndParentsIfAppropriate(cvsFolder);
62         }
63     }
64     /**
65      * @see ICVSResourceVisitor#visitFile(IManagedFile)
66      */

67     public void visitFile(ICVSFile file) throws CVSException {
68         // nothing to do here
69
}
70
71     /**
72      * @see ICVSResourceVisitor#visitFolder(ICVSFolder)
73      */

74     public void visitFolder(ICVSFolder folder) throws CVSException {
75         // First prune any empty children
76
folder.acceptChildren(this);
77         // Then prune the folder if it is empty
78
pruneFolderIfAppropriate(folder);
79     }
80     
81     private void pruneFolderIfAppropriate(ICVSFolder folder) throws CVSException {
82         // Only prune managed folders that are not the root of the operation
83
if (folder.exists() && folder.isManaged()
84             && ! folder.equals(getLocalRoot())
85             && folder.members(ICVSFolder.ALL_EXISTING_MEMBERS).length == 0) {
86             
87             // Delete the folder but keep a phantom for local folders
88
folder.delete();
89         }
90     }
91     
92     private ICVSFolder getLocalRoot() {
93         return localRoot;
94     }
95
96     /**
97      * Attempt to prune the given folder. If the folder is pruned, attempt to prune it's parent.
98      */

99     private void pruneFolderAndParentsIfAppropriate(ICVSFolder folder) throws CVSException {
100         pruneFolderIfAppropriate(folder);
101         if (!folder.exists()) {
102             ICVSFolder parent = folder.getParent();
103             pruneFolderAndParentsIfAppropriate(parent);
104         }
105     }
106 }
107
Popular Tags