1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
package org.webmacro.engine; |
24 | |
|
25 | |
import org.webmacro.Macro; |
26 | |
import org.webmacro.TemplateVisitor; |
27 | |
import org.webmacro.WM; |
28 | |
|
29 | |
import java.io.InputStreamReader; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | 0 | public class TemplateDumper extends TemplateVisitor |
35 | |
{ |
36 | |
|
37 | 0 | private boolean atBol = true; |
38 | |
|
39 | |
private void print (String s) |
40 | |
{ |
41 | 0 | System.out.print(s); |
42 | 0 | atBol = false; |
43 | 0 | } |
44 | |
|
45 | |
private void println (String s) |
46 | |
{ |
47 | 0 | System.out.println(s); |
48 | 0 | atBol = true; |
49 | 0 | } |
50 | |
|
51 | |
private void println () |
52 | |
{ |
53 | 0 | System.out.println(); |
54 | 0 | atBol = true; |
55 | 0 | } |
56 | |
|
57 | |
private void newLine () |
58 | |
{ |
59 | 0 | if (!atBol) println(); |
60 | 0 | } |
61 | |
|
62 | |
|
63 | |
public void visitString (String s) |
64 | |
{ |
65 | 0 | print(s); |
66 | 0 | } |
67 | |
|
68 | |
public void beginBlock () |
69 | |
{ |
70 | 0 | newLine(); |
71 | 0 | println("#begin"); |
72 | 0 | } |
73 | |
|
74 | |
public void endBlock () |
75 | |
{ |
76 | 0 | newLine(); |
77 | 0 | println("#end"); |
78 | 0 | } |
79 | |
|
80 | |
public void beginDirective (String directiveName) |
81 | |
{ |
82 | 0 | newLine(); |
83 | 0 | print("#" + directiveName + " "); |
84 | 0 | } |
85 | |
|
86 | |
public void visitDirectiveArg (String argName, Object o) |
87 | |
{ |
88 | 0 | print(":" + argName + ":"); |
89 | 0 | if (o instanceof Macro) |
90 | 0 | visitMacro((Macro) o); |
91 | 0 | else if (o == null) |
92 | 0 | print("<NULL>"); |
93 | |
else |
94 | 0 | print(o.toString()); |
95 | 0 | } |
96 | |
|
97 | |
public void visitBinaryOperation (String opType, Object l, Object r) |
98 | |
{ |
99 | 0 | print(opType + "("); |
100 | 0 | if (l instanceof Macro) |
101 | 0 | visitMacro((Macro) l); |
102 | |
else |
103 | 0 | print((l != null) ? l.toString() : "<NULL>"); |
104 | 0 | print(", "); |
105 | 0 | if (r instanceof Macro) |
106 | 0 | visitMacro((Macro) r); |
107 | |
else |
108 | 0 | print((r != null) ? r.toString() : "<NULL>"); |
109 | 0 | print(")"); |
110 | 0 | } |
111 | |
|
112 | |
public void visitUnaryOperation (String opType, Object o) |
113 | |
{ |
114 | 0 | print(opType + "(" + ((o != null) ? o.toString() : "<NULL>") |
115 | |
+ ")"); |
116 | 0 | } |
117 | |
|
118 | |
public void visitVariable (Variable v, Object[] names) |
119 | |
{ |
120 | 0 | print("$" + v.toString()); |
121 | 0 | } |
122 | |
|
123 | |
public void visitUnknownMacro (String macroType, Macro m) |
124 | |
{ |
125 | 0 | print("[Unknown macro type " + macroType + "]"); |
126 | 0 | } |
127 | |
|
128 | |
public static void main (String args[]) throws Exception |
129 | |
{ |
130 | 0 | WM wm = new WM(); |
131 | 0 | WMTemplate t = new StreamTemplate(wm.getBroker(), |
132 | |
new InputStreamReader(System.in)); |
133 | 0 | t.parse(); |
134 | 0 | System.out.println("--------"); |
135 | 0 | TemplateDumper td = new TemplateDumper(); |
136 | 0 | t.accept(td); |
137 | 0 | System.out.println("--------"); |
138 | 0 | } |
139 | |
} |