KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > walker > PrintVisitor


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

5 package com.tc.object.walker;
6
7 import com.tc.util.ClassUtils;
8
9 import java.lang.reflect.Field JavaDoc;
10
11 /**
12  * Produces a textual representation of the type visited
13  */

14 public class PrintVisitor implements Visitor {
15   private static final String JavaDoc INDENT = " ";
16   private final OutputSink out;
17   private final WalkTest walkTest;
18   private final ValueFormatter formatter;
19
20   public PrintVisitor(OutputSink sink, WalkTest walkTest, ValueFormatter formatter) {
21     this.out = sink;
22     this.walkTest = walkTest;
23     this.formatter = formatter;
24   }
25
26   public void visitRootObject(MemberValue value) {
27     outputLine(typeDisplay(value.getValueObject().getClass()) + " (id " + value.getId() + ")");
28   }
29
30   private void outputLine(String JavaDoc line) {
31     out.output(line);
32   }
33
34   public void visitValue(MemberValue value, int depth) {
35     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
36     boolean isLiteral = !walkTest.shouldTraverse(value);
37
38     indent(depth, buf);
39
40     if (value.isMapKey()) {
41       buf.append("key ");
42     } else if (value.isMapValue()) {
43       buf.append("value ");
44     }
45
46     Field JavaDoc field = value.getSourceField();
47     if (field != null) {
48       buf.append(typeDisplay(field.getType()) + " ");
49       boolean shadowed = value.isShadowed();
50       if (shadowed) {
51         buf.append(field.getDeclaringClass().getName() + ".");
52       }
53
54       buf.append(field.getName() + " ");
55     }
56
57     if (value.isElement()) {
58       buf.append("[" + value.getIndex() + "] ");
59     }
60
61     buf.append("= ");
62
63     Object JavaDoc o = value.getValueObject();
64
65     if (isLiteral || o == null) {
66       buf.append(formatter.format(value.getValueObject()));
67     } else {
68       if (value.isRepeated()) {
69         buf.append("(ref id " + value.getId() + ")");
70       } else {
71         if ((field != null) && (o.getClass().equals(field.getType()))) {
72           buf.append("(id " + value.getId() + ")");
73         } else {
74           buf.append("(" + typeDisplay(o.getClass()) + ", id " + value.getId() + ")");
75         }
76       }
77     }
78
79     String JavaDoc adorn = formatter.valueAdornment(value);
80     if (adorn != null) {
81       buf.append(adorn);
82     }
83
84     outputLine(buf.toString());
85   }
86
87   private static final String JavaDoc JAVA_LANG = "java.lang.";
88   private static final int JAVA_LANG_LEN = JAVA_LANG.length();
89
90   private static final String JavaDoc JAVA_UTIL = "java.util.";
91   private static final int JAVA_UTIL_LEN = JAVA_UTIL.length();
92
93   private static String JavaDoc typeDisplay(Class JavaDoc c) {
94     String JavaDoc type = c.getName();
95     int dim = 0;
96     if (c.isArray()) {
97       dim = ClassUtils.arrayDimensions(c);
98       type = ClassUtils.baseComponetType(c).getName();
99     }
100
101     if (type.startsWith(JAVA_LANG) && type.lastIndexOf('.') + 1 == JAVA_LANG_LEN) {
102       type = type.substring(JAVA_LANG_LEN);
103     } else if (type.startsWith(JAVA_UTIL) && type.lastIndexOf('.') + 1 == JAVA_UTIL_LEN) {
104       type = type.substring(JAVA_UTIL_LEN);
105     }
106
107     for (int i = 0; i < dim; i++) {
108       type = type + "[]";
109     }
110
111     return type;
112   }
113
114   public void visitMapEntry(int index, int depth) {
115     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
116     indent(depth, buf);
117     buf.append("[entry ").append(index).append("]");
118     outputLine(buf.toString());
119   }
120
121   private static void indent(int currentDepth, StringBuffer JavaDoc buffer) {
122     while (currentDepth-- > 0) {
123       buffer.append(INDENT);
124     }
125   }
126
127   public interface OutputSink {
128     void output(String JavaDoc line);
129   }
130
131   public interface ValueFormatter {
132     String JavaDoc format(Object JavaDoc value);
133
134     String JavaDoc valueAdornment(MemberValue value);
135   }
136
137 }
Popular Tags