KickJava   Java API By Example, From Geeks To Geeks.

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


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