KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > dependency > DeletingVisitor


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.jeantessier.dependency;
34
35 import java.util.*;
36
37 import org.apache.log4j.*;
38
39 import com.jeantessier.classreader.*;
40
41 public class DeletingVisitor implements Visitor, RemoveVisitor {
42     private NodeFactory factory;
43     
44     public DeletingVisitor(NodeFactory factory) {
45         this.factory = factory;
46     }
47
48     public NodeFactory getFactory() {
49         return factory;
50     }
51     
52     public void traverseNodes(Collection nodes) {
53 // Iterator i = nodes.iterator();
54
// while (i.hasNext()) {
55
// ((Node) i.next()).accept(this);
56
// }
57
}
58
59     /*
60      * Regular visits are used to completely delete sections
61      */

62     
63     public void visitPackageNode(PackageNode node) {
64         Logger.getLogger(getClass()).debug("visitPackageNode(" + node + ")");
65         
66         Iterator i = new ArrayList(node.getClasses()).iterator();
67         while (i.hasNext()) {
68             ((Node) i.next()).accept(this);
69         }
70
71         visitNode(node);
72     }
73
74     public void visitClassNode(ClassNode node) {
75         Logger.getLogger(getClass()).debug("visitClassNode(" + node + ")");
76         
77         Iterator i = new ArrayList(node.getFeatures()).iterator();
78         while (i.hasNext()) {
79             ((Node) i.next()).accept(this);
80         }
81
82         visitNode(node);
83     }
84
85     public void visitFeatureNode(FeatureNode node) {
86         Logger.getLogger(getClass()).debug("visitFeatureNode(" + node + ")");
87         
88         visitNode(node);
89     }
90
91     private void visitNode(Node node) {
92         node.setConfirmed(false);
93
94         Iterator i = new ArrayList(node.getOutboundDependencies()).iterator();
95         while (i.hasNext()) {
96             Node outbound = (Node) i.next();
97             node.removeDependency(outbound);
98             outbound.acceptOutbound(this);
99         }
100         
101         node.acceptOutbound(this);
102     }
103
104     /*
105      * Outbound visits are used to clean up sections
106      */

107     
108     public void visitOutboundPackageNode(PackageNode node) {
109         Logger.getLogger(getClass()).debug("visitOutboundPackageNode(" + node + ")");
110         
111         if (canDeletePackage(node)) {
112             factory.deletePackage(node);
113         }
114     }
115     
116     public void visitOutboundClassNode(ClassNode node) {
117         Logger.getLogger(getClass()).debug("visitOutboundClassNode(" + node + ")");
118         
119         if (canDeleteClass(node)) {
120             factory.deleteClass(node);
121         }
122
123         node.getPackageNode().acceptOutbound(this);
124     }
125     
126     public void visitOutboundFeatureNode(FeatureNode node) {
127         Logger.getLogger(getClass()).debug("visitOutboundFeatureNode(" + node + ")");
128         
129         if (canDeleteFeature(node)) {
130             factory.deleteFeature(node);
131         }
132
133         node.getClassNode().acceptOutbound(this);
134     }
135
136     private boolean canDelete(Node node) {
137         return !node.isConfirmed() && node.getInboundDependencies().isEmpty();
138     }
139
140     private boolean canDeletePackage(PackageNode node) {
141         return canDelete(node) && node.getClasses().isEmpty();
142     }
143
144     private boolean canDeleteClass(ClassNode node) {
145         return canDelete(node) && node.getFeatures().isEmpty();
146     }
147
148     private boolean canDeleteFeature(FeatureNode node) {
149         return canDelete(node);
150     }
151
152     /*
153      * Inbound visits are not used
154      */

155     
156     public void visitInboundPackageNode(PackageNode node) {
157         // Do nothing
158
}
159     
160     public void visitInboundClassNode(ClassNode node) {
161         // Do nothing
162
}
163     
164     public void visitInboundFeatureNode(FeatureNode node) {
165         // Do nothing
166
}
167
168     /*
169      * RemoveVisitor methods
170      */

171     
172     public void removeClass(String JavaDoc classname) {
173         Node node = (Node) factory.getClasses().get(classname);
174         if (node != null) {
175             node.accept(this);
176         }
177     }
178 }
179
Popular Tags