KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.*;
36 import java.util.*;
37
38 import org.apache.log4j.*;
39 import org.apache.oro.text.perl.*;
40
41 public class TextPrinter extends Printer {
42     private static final Perl5Util perl = new Perl5Util();
43
44     protected static Perl5Util perl() {
45         return perl;
46     }
47
48     private boolean showInferred = true;
49     private Map dependencies = new TreeMap();
50
51     public TextPrinter(PrintWriter out) {
52         super(out);
53     }
54
55     public TextPrinter(TraversalStrategy strategy, PrintWriter out) {
56         super(strategy, out);
57     }
58
59     public boolean isShowInferred() {
60         return showInferred;
61     }
62
63     public void setShowInferred(boolean showInferred) {
64         this.showInferred = showInferred;
65     }
66     
67     protected void preprocessPackageNode(PackageNode node) {
68         Logger.getLogger(getClass()).debug("Printing package \"" + node + "\" and its " + node.getInboundDependencies().size() + " inbounds and " + node.getOutboundDependencies().size() + " outbounds");
69         
70         super.preprocessPackageNode(node);
71
72         raiseIndent();
73
74         dependencies.clear();
75     }
76
77     protected void preprocessAfterDependenciesPackageNode(PackageNode node) {
78         Logger.getLogger(getClass()).debug("Package \"" + node + "\" with " + node.getInboundDependencies().size() + " inbounds and " + node.getOutboundDependencies().size() + " outbounds had " + dependencies.size() + " dependencies.");
79         
80         if (shouldShowPackageNode(node) || !dependencies.isEmpty()) {
81             lowerIndent();
82             indent().printNodeName(node).eol();
83             raiseIndent();
84         }
85         
86         printDependencies(dependencies);
87     }
88     
89     protected void postprocessPackageNode(PackageNode node) {
90         lowerIndent();
91
92         super.postprocessPackageNode(node);
93     }
94
95     public void visitInboundPackageNode(PackageNode node) {
96         if (isShowInbounds()) {
97             Logger.getLogger(getClass()).debug("Printing \"" + getCurrentNode() + "\" <-- \"" + node + "\"");
98         
99             Integer JavaDoc i = (Integer JavaDoc) dependencies.get(node);
100             if (i != null) {
101                 dependencies.put(node, new Integer JavaDoc(i.intValue() - 1));
102             } else {
103                 dependencies.put(node, new Integer JavaDoc(-1));
104             }
105         } else {
106             Logger.getLogger(getClass()).debug("Ignoring \"" + getCurrentNode() + "\" <-- \"" + node + "\"");
107         }
108     }
109
110     public void visitOutboundPackageNode(PackageNode node) {
111         if (isShowOutbounds()) {
112             Logger.getLogger(getClass()).debug("Printing \"" + getCurrentNode() + "\" --> \"" + node + "\"");
113         
114             Integer JavaDoc i = (Integer JavaDoc) dependencies.get(node);
115             if (i != null) {
116                 dependencies.put(node, new Integer JavaDoc(i.intValue() + 1));
117             } else {
118                 dependencies.put(node, new Integer JavaDoc(1));
119             }
120         } else {
121             Logger.getLogger(getClass()).debug("Ignoring \"" + getCurrentNode() + "\" --> \"" + node + "\"");
122         }
123     }
124
125     protected void preprocessClassNode(ClassNode node) {
126         Logger.getLogger(getClass()).debug("Printing class \"" + node + "\" and its " + node.getInboundDependencies().size() + " inbounds and " + node.getOutboundDependencies().size() + " outbounds");
127         
128         super.preprocessClassNode(node);
129
130         raiseIndent();
131
132         dependencies.clear();
133     }
134
135     protected void preprocessAfterDependenciesClassNode(ClassNode node) {
136         Logger.getLogger(getClass()).debug("Class \"" + node + "\" with " + node.getInboundDependencies().size() + " inbounds and " + node.getOutboundDependencies().size() + " outbounds had " + dependencies.size() + " dependencies.");
137         
138         if (shouldShowClassNode(node) || !dependencies.isEmpty()) {
139             lowerIndent();
140             indent().printNodeName(node, node.getName().substring(node.getName().lastIndexOf('.') + 1)).eol();
141             raiseIndent();
142         }
143
144         printDependencies(dependencies);
145     }
146
147     protected void postprocessClassNode(ClassNode node) {
148         lowerIndent();
149
150         super.postprocessClassNode(node);
151     }
152     
153     public void visitInboundClassNode(ClassNode node) {
154         if (isShowInbounds()) {
155             Logger.getLogger(getClass()).debug("Printing \"" + getCurrentNode() + "\" <-- \"" + node + "\"");
156         
157             Integer JavaDoc i = (Integer JavaDoc) dependencies.get(node);
158             
159             if (i != null) {
160                 dependencies.put(node, new Integer JavaDoc(i.intValue() - 1));
161             } else {
162                 dependencies.put(node, new Integer JavaDoc(-1));
163             }
164         } else {
165             Logger.getLogger(getClass()).debug("Ignoring \"" + getCurrentNode() + "\" <-- \"" + node + "\"");
166         }
167     }
168
169     public void visitOutboundClassNode(ClassNode node) {
170         if (isShowOutbounds()) {
171             Logger.getLogger(getClass()).debug("Printing \"" + getCurrentNode() + "\" --> \"" + node + "\"");
172         
173             Integer JavaDoc i = (Integer JavaDoc) dependencies.get(node);
174             
175             if (i != null) {
176                 dependencies.put(node, new Integer JavaDoc(i.intValue() + 1));
177             } else {
178                 dependencies.put(node, new Integer JavaDoc(1));
179             }
180         } else {
181             Logger.getLogger(getClass()).debug("Ignoring \"" + getCurrentNode() + "\" --> \"" + node + "\"");
182         }
183     }
184
185     protected void preprocessFeatureNode(FeatureNode node) {
186         Logger.getLogger(getClass()).debug("Printing feature \"" + node + "\" and its " + node.getInboundDependencies().size() + " inbounds and " + node.getOutboundDependencies().size() + " outbounds");
187         
188         super.preprocessFeatureNode(node);
189
190         raiseIndent();
191
192         dependencies.clear();
193     }
194
195     protected void postprocessFeatureNode(FeatureNode node) {
196         Logger.getLogger(getClass()).debug("Feature \"" + node + "\" with " + node.getInboundDependencies().size() + " inbounds and " + node.getOutboundDependencies().size() + " outbounds had " + dependencies.size() + " dependencies.");
197         
198         if (shouldShowFeatureNode(node) || !dependencies.isEmpty()) {
199             lowerIndent();
200             if (perl().match("/([^\\.]*\\(.*\\))$/", node.getName())) {
201                 indent().printNodeName(node, perl().group(1)).eol();
202             } else if (perl().match("/([^\\.]*)$/", node.getName())) {
203                 indent().printNodeName(node, perl().group(1)).eol();
204             } else {
205                 indent().printNodeName(node, node.getName().substring(node.getName().lastIndexOf('.') + 1)).eol();
206             }
207             raiseIndent();
208         }
209         
210         printDependencies(dependencies);
211
212         lowerIndent();
213
214         super.postprocessFeatureNode(node);
215     }
216
217     public void visitInboundFeatureNode(FeatureNode node) {
218         if (isShowInbounds()) {
219             Logger.getLogger(getClass()).debug("Printing \"" + getCurrentNode() + "\" <-- \"" + node + "\"");
220         
221             Integer JavaDoc i = (Integer JavaDoc) dependencies.get(node);
222             if (i != null) {
223                 dependencies.put(node, new Integer JavaDoc(i.intValue() - 1));
224             } else {
225                 dependencies.put(node, new Integer JavaDoc(-1));
226             }
227         } else {
228             Logger.getLogger(getClass()).debug("Ignoring \"" + getCurrentNode() + "\" <-- \"" + node + "\"");
229         }
230     }
231
232     public void visitOutboundFeatureNode(FeatureNode node) {
233         if (isShowOutbounds()) {
234             Logger.getLogger(getClass()).debug("Printing \"" + getCurrentNode() + "\" --> \"" + node + "\"");
235         
236             Integer JavaDoc i = (Integer JavaDoc) dependencies.get(node);
237             if (i != null) {
238                 dependencies.put(node, new Integer JavaDoc(i.intValue() + 1));
239             } else {
240                 dependencies.put(node, new Integer JavaDoc(1));
241             }
242         } else {
243             Logger.getLogger(getClass()).debug("Ignoring \"" + getCurrentNode() + "\" --> \"" + node + "\"");
244         }
245     }
246
247     protected Printer printNodeName(Node node, String JavaDoc name) {
248         super.printNodeName(node, name);
249
250         if (isShowInferred() && !node.isConfirmed()) {
251             append(" *");
252         }
253         
254         return this;
255     }
256     
257     private void printDependencies(Map dependencies) {
258         Iterator i = dependencies.entrySet().iterator();
259         while (i.hasNext()) {
260             Map.Entry entry = (Map.Entry) i.next();
261             if (((Integer JavaDoc) entry.getValue()).intValue() < 0) {
262                 indent().append("<-- ").printNodeName((Node) entry.getKey()).eol();
263             } else if (((Integer JavaDoc) entry.getValue()).intValue() > 0) {
264                 indent().append("--> ").printNodeName((Node) entry.getKey()).eol();
265             } else {
266                 indent().append("<-> ").printNodeName((Node) entry.getKey()).eol();
267             }
268         }
269     }
270 }
271
Popular Tags