1 4 package com.tc.text; 5 6 import java.io.PrintWriter ; 7 import java.util.ArrayList ; 8 import java.util.Collection ; 9 import java.util.IdentityHashMap ; 10 import java.util.Iterator ; 11 import java.util.Map ; 12 13 public class PrettyPrinter { 14 15 private static final String INDENT = "--> "; 16 17 private final StringBuffer prefix; 18 private final PrintWriter out; 19 private final IdentityHashMap visited; 20 21 private final PrintPolicy defaultPolicy = new BasicPrintPolicy(); 22 private final Collection policies; 23 24 private boolean autoflush = true; 25 26 public PrettyPrinter(PrintWriter out) { 27 this(INDENT, out, new IdentityHashMap ()); 28 } 29 30 private PrettyPrinter(String prefix, PrintWriter out, IdentityHashMap visited) { 31 this.prefix = new StringBuffer (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 49 private boolean accountFor(Object 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 o) { 62 this.out.print(o); 63 if (autoflush()) this.out.flush(); 64 return this; 65 } 66 67 public PrettyPrinter println(Object 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 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 o) { 108 PrintPolicy policy = findPolicyFor(o); 109 return policy.visit(this, o); 110 } 111 112 private PrintPolicy findPolicyFor(Object o) { 113 if (o == null) return defaultPolicy; 114 for (Iterator 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 124 private Collection initPolicies() { 125 Collection rv = new ArrayList (); 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 o); 135 136 public boolean accepts(Object o); 137 } 138 139 private static class PrettyPrintablePrintPolicy implements PrintPolicy { 140 141 public PrettyPrinter visit(PrettyPrinter pp, Object o) { 142 return ((PrettyPrintable) o).prettyPrint(pp); 143 } 144 145 public boolean accepts(Object 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 o) { 154 return pp.print(o.getClass().getName()).print(".size()=").print(((Map ) o).size() + ""); 155 } 156 157 public boolean accepts(Object o) { 158 return o != null && o instanceof Map ; 159 } 160 161 } 162 163 private static class ShallowCollectionPrintPolicy implements PrintPolicy { 164 165 public PrettyPrinter visit(PrettyPrinter pp, Object o) { 166 return pp.print(o.getClass().getName()).print(".size()=").print(((Collection ) o).size() + ""); 167 } 168 169 public boolean accepts(Object o) { 170 return o != null && o instanceof Collection ; 171 } 172 173 } 174 175 private static class BasicPrintPolicy implements PrintPolicy { 176 177 public PrettyPrinter visit(PrettyPrinter pp, Object o) { 178 return pp.print(o); 179 } 180 181 public boolean accepts(Object o) { 182 return true; 183 } 184 185 } 186 } 187 | Popular Tags |