KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > text > PrettyPrinter


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.text;
5
6 import java.io.PrintWriter JavaDoc;
7 import java.util.ArrayList JavaDoc;
8 import java.util.Collection JavaDoc;
9 import java.util.IdentityHashMap JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.Map JavaDoc;
12
13 public class PrettyPrinter {
14
15   private static final String JavaDoc INDENT = "--> ";
16
17   private final StringBuffer JavaDoc prefix;
18   private final PrintWriter JavaDoc out;
19   private final IdentityHashMap JavaDoc visited;
20
21   private final PrintPolicy defaultPolicy = new BasicPrintPolicy();
22   private final Collection JavaDoc policies;
23
24   private boolean autoflush = true;
25
26   public PrettyPrinter(PrintWriter JavaDoc out) {
27     this(INDENT, out, new IdentityHashMap JavaDoc());
28   }
29
30   private PrettyPrinter(String JavaDoc prefix, PrintWriter JavaDoc out, IdentityHashMap JavaDoc visited) {
31     this.prefix = new StringBuffer JavaDoc(prefix);
32     this.out = out;
33     this.visited = visited;
34     this.policies = initPolicies();
35   }
36
37   public synchronized void autoflush(boolean b) {
38     this.autoflush = b;
39   }
40
41   public synchronized boolean autoflush() {
42     return this.autoflush;
43   }
44
45   /**
46    * Returns true if the object has been visited before or if the object is null. Otherwise, it accounts for the visited
47    * object and returns false.
48    */

49   private boolean accountFor(Object JavaDoc o) {
50     if (o == null) return false;
51     synchronized (visited) {
52       if (visited.containsKey(o)) {
53         return true;
54       } else {
55         visited.put(o, "");
56         return false;
57       }
58     }
59   }
60
61   public PrettyPrinter print(Object JavaDoc o) {
62     this.out.print(o);
63     if (autoflush()) this.out.flush();
64     return this;
65   }
66
67   public PrettyPrinter println(Object JavaDoc o) {
68     this.out.println(o);
69     if (autoflush()) this.out.flush();
70     return this;
71   }
72
73   public PrettyPrinter println() {
74     this.out.println();
75     if (autoflush()) this.out.flush();
76     return this;
77   }
78
79   public PrettyPrinter indent() {
80     return print(prefix);
81   }
82
83   public PrettyPrinter duplicateAndIndent() {
84     PrettyPrinter rv = duplicate();
85     rv.indentPrefix();
86     return rv;
87   }
88
89   private void indentPrefix() {
90     if (prefix.indexOf("+") > -1) prefix.replace(prefix.indexOf("+"), prefix.indexOf("+") + 1, "|");
91     prefix.insert(prefix.indexOf("-->"), " +");
92   }
93
94   private PrettyPrinter duplicate() {
95     return new PrettyPrinter(prefix.toString(), out, this.visited);
96   }
97
98   public PrettyPrinter visit(Object JavaDoc o) {
99     if (accountFor(o)) {
100       print("ALREADY VISITED: " + o);
101       return this;
102     } else {
103       return basicVisit(o);
104     }
105   }
106
107   private PrettyPrinter basicVisit(Object JavaDoc o) {
108     PrintPolicy policy = findPolicyFor(o);
109     return policy.visit(this, o);
110   }
111
112   private PrintPolicy findPolicyFor(Object JavaDoc o) {
113     if (o == null) return defaultPolicy;
114     for (Iterator JavaDoc i = policies.iterator(); i.hasNext();) {
115       PrintPolicy policy = (PrintPolicy) i.next();
116       if (policy.accepts(o)) { return policy; }
117     }
118     return defaultPolicy;
119   }
120
121   /**
122    * Creates a policy path. Each policy is searched in order.
123    */

124   private Collection JavaDoc initPolicies() {
125     Collection JavaDoc rv = new ArrayList JavaDoc();
126     rv.add(new PrettyPrintablePrintPolicy());
127     rv.add(new ShallowMapPrintPolicy());
128     rv.add(new ShallowCollectionPrintPolicy());
129     rv.add(defaultPolicy);
130     return rv;
131   }
132
133   private static interface PrintPolicy {
134     public PrettyPrinter visit(PrettyPrinter pp, Object JavaDoc o);
135
136     public boolean accepts(Object JavaDoc o);
137   }
138
139   private static class PrettyPrintablePrintPolicy implements PrintPolicy {
140
141     public PrettyPrinter visit(PrettyPrinter pp, Object JavaDoc o) {
142       return ((PrettyPrintable) o).prettyPrint(pp);
143     }
144
145     public boolean accepts(Object JavaDoc o) {
146       return o != null && o instanceof PrettyPrintable;
147     }
148
149   }
150
151   private static class ShallowMapPrintPolicy implements PrintPolicy {
152
153     public PrettyPrinter visit(PrettyPrinter pp, Object JavaDoc o) {
154       return pp.print(o.getClass().getName()).print(".size()=").print(((Map JavaDoc) o).size() + "");
155     }
156
157     public boolean accepts(Object JavaDoc o) {
158       return o != null && o instanceof Map JavaDoc;
159     }
160
161   }
162
163   private static class ShallowCollectionPrintPolicy implements PrintPolicy {
164
165     public PrettyPrinter visit(PrettyPrinter pp, Object JavaDoc o) {
166       return pp.print(o.getClass().getName()).print(".size()=").print(((Collection JavaDoc) o).size() + "");
167     }
168
169     public boolean accepts(Object JavaDoc o) {
170       return o != null && o instanceof Collection JavaDoc;
171     }
172
173   }
174
175   private static class BasicPrintPolicy implements PrintPolicy {
176
177     public PrettyPrinter visit(PrettyPrinter pp, Object JavaDoc o) {
178       return pp.print(o);
179     }
180
181     public boolean accepts(Object JavaDoc o) {
182       return true;
183     }
184
185   }
186 }
187
Popular Tags