1 | /* |
2 | * Copyright (c) 2001-2009, 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<? extends Node> nodes) { |
53 | throw new UnsupportedOperationException("not implemented yet."); |
54 | } |
55 | |
56 | public void traverseInbound(Collection<? extends Node> nodes) { |
57 | throw new UnsupportedOperationException("not implemented yet."); |
58 | } |
59 | |
60 | public void traverseOutbound(Collection<? extends Node> nodes) { |
61 | throw new UnsupportedOperationException("not implemented yet."); |
62 | } |
63 | /* |
64 | * Regular visits are used to completely delete sections |
65 | */ |
66 | |
67 | public void visitPackageNode(PackageNode node) { |
68 | Logger.getLogger(getClass()).debug("visitPackageNode(" + node + ")"); |
69 | |
70 | for (ClassNode classNode : new ArrayList<ClassNode>(node.getClasses())) { |
71 | classNode.accept(this); |
72 | } |
73 | |
74 | visitNode(node); |
75 | } |
76 | |
77 | public void visitClassNode(ClassNode node) { |
78 | Logger.getLogger(getClass()).debug("visitClassNode(" + node + ")"); |
79 | |
80 | for (FeatureNode featureNode : new ArrayList<FeatureNode>(node.getFeatures())) { |
81 | featureNode.accept(this); |
82 | } |
83 | |
84 | visitNode(node); |
85 | } |
86 | |
87 | public void visitFeatureNode(FeatureNode node) { |
88 | Logger.getLogger(getClass()).debug("visitFeatureNode(" + node + ")"); |
89 | |
90 | visitNode(node); |
91 | } |
92 | |
93 | private void visitNode(Node node) { |
94 | node.setConfirmed(false); |
95 | |
96 | for (Node outbound : new ArrayList<Node>(node.getOutboundDependencies())) { |
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 classname) { |
173 | Node node = factory.getClasses().get(classname); |
174 | if (node != null) { |
175 | node.accept(this); |
176 | } |
177 | } |
178 | } |