001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.dependency;
034:
035: import java.io.*;
036: import java.util.*;
037:
038: public abstract class Printer extends VisitorBase {
039: private PrintWriter out;
040:
041: private String indentText = " ";
042: private int indentLevel = 0;
043: private boolean showInbounds = true;
044: private boolean showOutbounds = true;
045: private boolean showEmptyNodes = true;
046:
047: public Printer(PrintWriter out) {
048: this (new SortedTraversalStrategy(
049: new SelectiveTraversalStrategy()), out);
050: }
051:
052: public Printer(TraversalStrategy strategy, PrintWriter out) {
053: super (strategy);
054:
055: this .out = out;
056: }
057:
058: public String getIndentText() {
059: return indentText;
060: }
061:
062: public void setIndentText(String indentText) {
063: this .indentText = indentText;
064: }
065:
066: public boolean isShowInbounds() {
067: return showInbounds;
068: }
069:
070: public void setShowInbounds(boolean showInbounds) {
071: this .showInbounds = showInbounds;
072: }
073:
074: public boolean isShowOutbounds() {
075: return showOutbounds;
076: }
077:
078: public void setShowOutbounds(boolean showOutbounds) {
079: this .showOutbounds = showOutbounds;
080: }
081:
082: public boolean isShowEmptyNodes() {
083: return showEmptyNodes;
084: }
085:
086: public void setShowEmptyNodes(boolean showEmptyNodes) {
087: this .showEmptyNodes = showEmptyNodes;
088: }
089:
090: protected Printer append(boolean b) {
091: out.print(b);
092: return this ;
093: }
094:
095: protected Printer append(char c) {
096: out.print(c);
097: return this ;
098: }
099:
100: protected Printer append(char[] s) {
101: out.print(s);
102: return this ;
103: }
104:
105: protected Printer append(double d) {
106: out.print(d);
107: return this ;
108: }
109:
110: protected Printer append(float f) {
111: out.print(f);
112: return this ;
113: }
114:
115: protected Printer append(int i) {
116: out.print(i);
117: return this ;
118: }
119:
120: protected Printer append(long l) {
121: out.print(l);
122: return this ;
123: }
124:
125: protected Printer append(Object obj) {
126: out.print(obj);
127: return this ;
128: }
129:
130: protected Printer append(String s) {
131: out.print(s);
132: return this ;
133: }
134:
135: protected Printer indent() {
136: for (int i = 0; i < indentLevel; i++) {
137: append(getIndentText());
138: }
139:
140: return this ;
141: }
142:
143: protected Printer eol() {
144: out.println();
145: return this ;
146: }
147:
148: protected final Printer printScopeNodeName(Node node) {
149: return printScopeNodeName(node, node.getName());
150: }
151:
152: protected Printer printScopeNodeName(Node node, String name) {
153: return printNodeName(node, name);
154: }
155:
156: protected final Printer printDependencyNodeName(Node node) {
157: return printDependencyNodeName(node, node.getName());
158: }
159:
160: protected Printer printDependencyNodeName(Node node, String name) {
161: return printNodeName(node, name);
162: }
163:
164: protected Printer printNodeName(Node node, String name) {
165: return append(name);
166: }
167:
168: protected void raiseIndent() {
169: indentLevel++;
170: }
171:
172: protected void lowerIndent() {
173: indentLevel--;
174: }
175:
176: protected boolean shouldShowPackageNode(PackageNode node) {
177: boolean result = shouldShowNode(node);
178:
179: Iterator i = node.getClasses().iterator();
180: while (!result && i.hasNext()) {
181: result = shouldShowClassNode((ClassNode) i.next());
182: }
183:
184: return result;
185: }
186:
187: protected boolean shouldShowClassNode(ClassNode node) {
188: boolean result = shouldShowNode(node);
189:
190: Iterator i = node.getFeatures().iterator();
191: while (!result && i.hasNext()) {
192: result = shouldShowFeatureNode((FeatureNode) i.next());
193: }
194:
195: return result;
196: }
197:
198: protected boolean shouldShowFeatureNode(FeatureNode node) {
199: return shouldShowNode(node);
200: }
201:
202: protected boolean shouldShowNode(Node node) {
203: boolean result = isShowEmptyNodes();
204:
205: if (!result) {
206: result = (isShowOutbounds() && !node
207: .getOutboundDependencies().isEmpty())
208: || (isShowInbounds() && !node
209: .getInboundDependencies().isEmpty());
210: }
211:
212: return result;
213: }
214: }
|