KickJava   Java API By Example, From Geeks To Geeks.

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


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 public abstract class Printer extends VisitorBase {
39     private PrintWriter out;
40
41     private String JavaDoc indentText = " ";
42     private int indentLevel = 0;
43     private boolean showInbounds = true;
44     private boolean showOutbounds = true;
45     private boolean showEmptyNodes = true;
46
47     public Printer(PrintWriter out) {
48         this(new SortedTraversalStrategy(new SelectiveTraversalStrategy()), out);
49     }
50
51     public Printer(TraversalStrategy strategy, PrintWriter out) {
52         super(strategy);
53
54         this.out = out;
55     }
56
57     public String JavaDoc getIndentText() {
58         return indentText;
59     }
60
61     public void setIndentText(String JavaDoc indentText) {
62         this.indentText = indentText;
63     }
64
65     public boolean isShowInbounds() {
66         return showInbounds;
67     }
68
69     public void setShowInbounds(boolean showInbounds) {
70         this.showInbounds = showInbounds;
71     }
72     
73     public boolean isShowOutbounds() {
74         return showOutbounds;
75     }
76
77     public void setShowOutbounds(boolean showOutbounds) {
78         this.showOutbounds = showOutbounds;
79     }
80     
81     public boolean isShowEmptyNodes() {
82         return showEmptyNodes;
83     }
84
85     public void setShowEmptyNodes(boolean showEmptyNodes) {
86         this.showEmptyNodes = showEmptyNodes;
87     }
88     
89     protected Printer append(boolean b) {
90         out.print(b);
91         return this;
92     }
93
94     protected Printer append(char c) {
95         out.print(c);
96         return this;
97     }
98
99     protected Printer append(char[] s) {
100         out.print(s);
101         return this;
102     }
103
104     protected Printer append(double d) {
105         out.print(d);
106         return this;
107     }
108
109     protected Printer append(float f) {
110         out.print(f);
111         return this;
112     }
113
114     protected Printer append(int i) {
115         out.print(i);
116         return this;
117     }
118
119     protected Printer append(long l) {
120         out.print(l);
121         return this;
122     }
123
124     protected Printer append(Object JavaDoc obj) {
125         out.print(obj);
126         return this;
127     }
128
129     protected Printer append(String JavaDoc s) {
130         out.print(s);
131         return this;
132     }
133
134     protected Printer indent() {
135         for (int i=0; i<indentLevel; i++) {
136             append(getIndentText());
137         }
138
139         return this;
140     }
141
142     protected Printer eol() {
143         out.println();
144         return this;
145     }
146
147     protected Printer printNodeName(Node node) {
148         return printNodeName(node, node.getName());
149     }
150
151     protected Printer printNodeName(Node node, String JavaDoc name) {
152         return append(name);
153     }
154     
155     protected void raiseIndent() {
156         indentLevel++;
157     }
158
159     protected void lowerIndent() {
160         indentLevel--;
161     }
162
163     protected boolean shouldShowPackageNode(PackageNode node) {
164         boolean result = shouldShowNode(node);
165
166         Iterator i = node.getClasses().iterator();
167         while (!result && i.hasNext()) {
168             result = shouldShowClassNode((ClassNode) i.next());
169         }
170         
171         return result;
172     }
173
174     protected boolean shouldShowClassNode(ClassNode node) {
175         boolean result = shouldShowNode(node);
176
177         Iterator i = node.getFeatures().iterator();
178         while (!result && i.hasNext()) {
179             result = shouldShowFeatureNode((FeatureNode) i.next());
180         }
181         
182         return result;
183     }
184     
185     protected boolean shouldShowFeatureNode(FeatureNode node) {
186         return shouldShowNode(node);
187     }
188     
189     protected boolean shouldShowNode(Node node) {
190         boolean result = isShowEmptyNodes();
191
192         if (!result) {
193             result = (isShowOutbounds() && !node.getOutboundDependencies().isEmpty()) || (isShowInbounds() && !node.getInboundDependencies().isEmpty());
194         }
195
196         return result;
197     }
198 }
199
Popular Tags