1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts2.jasper.compiler;
19
20 import org.apache.struts2.jasper.JasperException;
21 import org.xml.sax.Attributes;
22
23 class Dumper {
24
25 static class DumpVisitor extends Node.Visitor {
26 private int indent = 0;
27
28 private String getAttributes(Attributes attrs) {
29 if (attrs == null)
30 return "";
31
32 StringBuffer buf = new StringBuffer();
33 for (int i = 0; i < attrs.getLength(); i++) {
34 buf.append(" " + attrs.getQName(i) + "=\""
35 + attrs.getValue(i) + "\"");
36 }
37 return buf.toString();
38 }
39
40 private void printString(String str) {
41 printIndent();
42 System.out.print(str);
43 }
44
45 private void printString(String prefix, char[] chars, String suffix) {
46 String str = null;
47 if (chars != null) {
48 str = new String(chars);
49 }
50 printString(prefix, str, suffix);
51 }
52
53 private void printString(String prefix, String str, String suffix) {
54 printIndent();
55 if (str != null) {
56 System.out.print(prefix + str + suffix);
57 } else {
58 System.out.print(prefix + suffix);
59 }
60 }
61
62 private void printAttributes(String prefix, Attributes attrs,
63 String suffix) {
64 printString(prefix, getAttributes(attrs), suffix);
65 }
66
67 private void dumpBody(Node n) throws JasperException {
68 Node.Nodes page = n.getBody();
69 if (page != null) {
70
71 page.visit(this);
72
73 }
74 }
75
76 public void visit(Node.PageDirective n) throws JasperException {
77 printAttributes("<%@ page", n.getAttributes(), "%>");
78 }
79
80 public void visit(Node.TaglibDirective n) throws JasperException {
81 printAttributes("<%@ taglib", n.getAttributes(), "%>");
82 }
83
84 public void visit(Node.IncludeDirective n) throws JasperException {
85 printAttributes("<%@ include", n.getAttributes(), "%>");
86 dumpBody(n);
87 }
88
89 public void visit(Node.Comment n) throws JasperException {
90 printString("<%--", n.getText(), "--%>");
91 }
92
93 public void visit(Node.Declaration n) throws JasperException {
94 printString("<%!", n.getText(), "%>");
95 }
96
97 public void visit(Node.Expression n) throws JasperException {
98 printString("<%=", n.getText(), "%>");
99 }
100
101 public void visit(Node.Scriptlet n) throws JasperException {
102 printString("<%", n.getText(), "%>");
103 }
104
105 public void visit(Node.IncludeAction n) throws JasperException {
106 printAttributes("<jsp:include", n.getAttributes(), ">");
107 dumpBody(n);
108 printString("</jsp:include>");
109 }
110
111 public void visit(Node.ForwardAction n) throws JasperException {
112 printAttributes("<jsp:forward", n.getAttributes(), ">");
113 dumpBody(n);
114 printString("</jsp:forward>");
115 }
116
117 public void visit(Node.GetProperty n) throws JasperException {
118 printAttributes("<jsp:getProperty", n.getAttributes(), "/>");
119 }
120
121 public void visit(Node.SetProperty n) throws JasperException {
122 printAttributes("<jsp:setProperty", n.getAttributes(), ">");
123 dumpBody(n);
124 printString("</jsp:setProperty>");
125 }
126
127 public void visit(Node.UseBean n) throws JasperException {
128 printAttributes("<jsp:useBean", n.getAttributes(), ">");
129 dumpBody(n);
130 printString("</jsp:useBean>");
131 }
132
133 public void visit(Node.PlugIn n) throws JasperException {
134 printAttributes("<jsp:plugin", n.getAttributes(), ">");
135 dumpBody(n);
136 printString("</jsp:plugin>");
137 }
138
139 public void visit(Node.ParamsAction n) throws JasperException {
140 printAttributes("<jsp:params", n.getAttributes(), ">");
141 dumpBody(n);
142 printString("</jsp:params>");
143 }
144
145 public void visit(Node.ParamAction n) throws JasperException {
146 printAttributes("<jsp:param", n.getAttributes(), ">");
147 dumpBody(n);
148 printString("</jsp:param>");
149 }
150
151 public void visit(Node.NamedAttribute n) throws JasperException {
152 printAttributes("<jsp:attribute", n.getAttributes(), ">");
153 dumpBody(n);
154 printString("</jsp:attribute>");
155 }
156
157 public void visit(Node.JspBody n) throws JasperException {
158 printAttributes("<jsp:body", n.getAttributes(), ">");
159 dumpBody(n);
160 printString("</jsp:body>");
161 }
162
163 public void visit(Node.ELExpression n) throws JasperException {
164 printString("${" + new String(n.getText()) + "}");
165 }
166
167 public void visit(Node.CustomTag n) throws JasperException {
168 printAttributes("<" + n.getQName(), n.getAttributes(), ">");
169 dumpBody(n);
170 printString("</" + n.getQName() + ">");
171 }
172
173 public void visit(Node.UninterpretedTag n) throws JasperException {
174 String tag = n.getQName();
175 printAttributes("<" + tag, n.getAttributes(), ">");
176 dumpBody(n);
177 printString("</" + tag + ">");
178 }
179
180 public void visit(Node.TemplateText n) throws JasperException {
181 printString(new String(n.getText()));
182 }
183
184 private void printIndent() {
185 for (int i = 0; i < indent; i++) {
186 System.out.print(" ");
187 }
188 }
189 }
190
191 public static void dump(Node n) {
192 try {
193 n.accept(new DumpVisitor());
194 } catch (JasperException e) {
195 e.printStackTrace();
196 }
197 }
198
199 public static void dump(Node.Nodes page) {
200 try {
201 page.visit(new DumpVisitor());
202 } catch (JasperException e) {
203 e.printStackTrace();
204 }
205 }
206 }
207