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 | /** |
40 | * This is a basic implementation of Visitor. |
41 | * |
42 | * @see Visitor |
43 | * @author Jean Tessier |
44 | */ |
45 | public abstract class VisitorBase implements Visitor { |
46 | private TraversalStrategy strategy; |
47 | |
48 | private LinkedList<Node> currentNodes = new LinkedList<Node>(); |
49 | |
50 | public VisitorBase() { |
51 | this(new ComprehensiveTraversalStrategy()); |
52 | } |
53 | |
54 | public VisitorBase(TraversalStrategy strategy) { |
55 | this.strategy = strategy; |
56 | } |
57 | |
58 | protected TraversalStrategy getStrategy() { |
59 | return strategy; |
60 | } |
61 | |
62 | public void traverseNodes(Collection<? extends Node> nodes) { |
63 | if (Logger.getLogger(getClass()).isDebugEnabled()) { |
64 | Logger.getLogger(getClass()).debug("nodes = " + nodes); |
65 | } |
66 | |
67 | for (Node node : getStrategy().order(nodes)) { |
68 | node.accept(this); |
69 | } |
70 | } |
71 | |
72 | public void traverseInbound(Collection<? extends Node> nodes) { |
73 | for (Node node : getStrategy().order(nodes)) { |
74 | node.acceptInbound(this); |
75 | } |
76 | } |
77 | |
78 | public void traverseOutbound(Collection<? extends Node> nodes) { |
79 | for (Node node : getStrategy().order(nodes)) { |
80 | node.acceptOutbound(this); |
81 | } |
82 | } |
83 | |
84 | protected Node getCurrentNode() { |
85 | Node result = null; |
86 | |
87 | if (!currentNodes.isEmpty()) { |
88 | result = currentNodes.getLast(); |
89 | } |
90 | |
91 | if (Logger.getLogger(getClass()).isDebugEnabled()) { |
92 | Logger.getLogger(getClass()).debug(currentNodes + ": " + result); |
93 | } |
94 | |
95 | return result; |
96 | } |
97 | |
98 | protected void pushNode(Node currentNode) { |
99 | if (Logger.getLogger(getClass()).isDebugEnabled()) { |
100 | Logger.getLogger(getClass()).debug(currentNodes + " + " + currentNode); |
101 | } |
102 | |
103 | currentNodes.addLast(currentNode); |
104 | } |
105 | |
106 | protected Node popNode() { |
107 | Node result = currentNodes.removeLast(); |
108 | |
109 | if (Logger.getLogger(getClass()).isDebugEnabled()) { |
110 | Logger.getLogger(getClass()).debug(currentNodes + " -> " + result); |
111 | } |
112 | |
113 | return result; |
114 | } |
115 | |
116 | public void visitPackageNode(PackageNode node) { |
117 | boolean inScope = isInScope(node); |
118 | |
119 | if (inScope) { |
120 | preprocessPackageNode(node); |
121 | |
122 | if (getStrategy().doPreOutboundTraversal()) { |
123 | traverseOutbound(node.getOutboundDependencies()); |
124 | } |
125 | |
126 | if (getStrategy().doPreInboundTraversal()) { |
127 | traverseInbound(node.getInboundDependencies()); |
128 | } |
129 | |
130 | preprocessAfterDependenciesPackageNode(node); |
131 | } |
132 | |
133 | traverseNodes(node.getClasses()); |
134 | |
135 | if (inScope) { |
136 | postprocessBeforeDependenciesPackageNode(node); |
137 | |
138 | if (getStrategy().doPostOutboundTraversal()) { |
139 | traverseOutbound(node.getOutboundDependencies()); |
140 | } |
141 | |
142 | if (getStrategy().doPostInboundTraversal()) { |
143 | traverseInbound(node.getInboundDependencies()); |
144 | } |
145 | |
146 | postprocessPackageNode(node); |
147 | } |
148 | } |
149 | |
150 | protected boolean isInScope(PackageNode node) { |
151 | return getStrategy().isInScope(node); |
152 | } |
153 | |
154 | protected void preprocessPackageNode(PackageNode node) { |
155 | pushNode(node); |
156 | } |
157 | |
158 | protected void preprocessAfterDependenciesPackageNode(PackageNode node) { |
159 | // Do nothing |
160 | } |
161 | |
162 | protected void postprocessBeforeDependenciesPackageNode(PackageNode node) { |
163 | // Do nothing |
164 | } |
165 | |
166 | protected void postprocessPackageNode(PackageNode node) { |
167 | if (node.equals(getCurrentNode())) { |
168 | popNode(); |
169 | } |
170 | } |
171 | |
172 | public void visitInboundPackageNode(PackageNode node) { |
173 | // Do nothing |
174 | } |
175 | |
176 | public void visitOutboundPackageNode(PackageNode node) { |
177 | // Do nothing |
178 | } |
179 | |
180 | public void visitClassNode(ClassNode node) { |
181 | boolean inScope = isInScope(node); |
182 | |
183 | if (inScope) { |
184 | preprocessClassNode(node); |
185 | |
186 | if (getStrategy().doPreOutboundTraversal()) { |
187 | traverseOutbound(node.getOutboundDependencies()); |
188 | } |
189 | |
190 | if (getStrategy().doPreInboundTraversal()) { |
191 | traverseInbound(node.getInboundDependencies()); |
192 | } |
193 | |
194 | preprocessAfterDependenciesClassNode(node); |
195 | } |
196 | |
197 | traverseNodes(node.getFeatures()); |
198 | |
199 | if (inScope) { |
200 | postprocessBeforeDependenciesClassNode(node); |
201 | |
202 | if (getStrategy().doPostOutboundTraversal()) { |
203 | traverseOutbound(node.getOutboundDependencies()); |
204 | } |
205 | |
206 | if (getStrategy().doPostInboundTraversal()) { |
207 | traverseInbound(node.getInboundDependencies()); |
208 | } |
209 | |
210 | postprocessClassNode(node); |
211 | } |
212 | } |
213 | |
214 | protected boolean isInScope(ClassNode node) { |
215 | return getStrategy().isInScope(node); |
216 | } |
217 | |
218 | protected void preprocessClassNode(ClassNode node) { |
219 | pushNode(node); |
220 | } |
221 | |
222 | protected void preprocessAfterDependenciesClassNode(ClassNode node) { |
223 | // Do nothing |
224 | } |
225 | |
226 | protected void postprocessBeforeDependenciesClassNode(ClassNode node) { |
227 | // Do nothing |
228 | } |
229 | |
230 | protected void postprocessClassNode(ClassNode node) { |
231 | if (node.equals(getCurrentNode())) { |
232 | popNode(); |
233 | } |
234 | } |
235 | |
236 | public void visitInboundClassNode(ClassNode node) { |
237 | // Do nothing |
238 | } |
239 | |
240 | public void visitOutboundClassNode(ClassNode node) { |
241 | // Do nothing |
242 | } |
243 | |
244 | public void visitFeatureNode(FeatureNode node) { |
245 | if (isInScope(node)) { |
246 | preprocessFeatureNode(node); |
247 | |
248 | if (getStrategy().doPreOutboundTraversal()) { |
249 | traverseOutbound(node.getOutboundDependencies()); |
250 | } |
251 | |
252 | if (getStrategy().doPreInboundTraversal()) { |
253 | traverseInbound(node.getInboundDependencies()); |
254 | } |
255 | |
256 | if (getStrategy().doPostOutboundTraversal()) { |
257 | traverseOutbound(node.getOutboundDependencies()); |
258 | } |
259 | |
260 | if (getStrategy().doPostInboundTraversal()) { |
261 | traverseInbound(node.getInboundDependencies()); |
262 | } |
263 | |
264 | postprocessFeatureNode(node); |
265 | } |
266 | } |
267 | |
268 | protected boolean isInScope(FeatureNode node) { |
269 | return getStrategy().isInScope(node); |
270 | } |
271 | |
272 | protected void preprocessFeatureNode(FeatureNode node) { |
273 | pushNode(node); |
274 | } |
275 | |
276 | protected void postprocessFeatureNode(FeatureNode node) { |
277 | if (node.equals(getCurrentNode())) { |
278 | popNode(); |
279 | } |
280 | } |
281 | |
282 | public void visitInboundFeatureNode(FeatureNode node) { |
283 | // Do nothing |
284 | } |
285 | |
286 | public void visitOutboundFeatureNode(FeatureNode node) { |
287 | // Do nothing |
288 | } |
289 | } |