KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > jsps > parserapi > DumpVisitor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.web.jsps.parserapi;
21
22 import javax.servlet.jsp.JspException JavaDoc;
23 import org.openide.ErrorManager;
24 import org.xml.sax.Attributes JavaDoc;
25
26 class DumpVisitor extends Node.Visitor {
27
28     private int indent = 0;
29
30     private StringBuffer JavaDoc buf;
31
32     private DumpVisitor() {
33         super();
34         buf = new StringBuffer JavaDoc();
35     }
36
37     /**
38      * This method provides a place to put actions that are common to
39      * all nodes. Override this in the child visitor class if need to.
40      */

41     protected void visitCommon(Node n) throws JspException JavaDoc {
42         printString("\nNode [" + n.getStart() + ", " + getDisplayClassName(n.getClass().getName()) + "] ");
43     }
44     
45     private String JavaDoc getDisplayClassName(String JavaDoc cn) {
46         int amp = cn.indexOf('$');
47         return cn.substring(amp + 1);
48     }
49
50     private String JavaDoc getAttributes(Attributes JavaDoc attrs) {
51         if (attrs == null)
52             return "";
53
54         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
55         for (int i=0; i < attrs.getLength(); i++) {
56             buf.append(" " + attrs.getQName(i) + "=\""
57                        + attrs.getValue(i) + "\"");
58         }
59         return buf.toString();
60     }
61
62     private void printString(String JavaDoc str) {
63         printIndent();
64         buf.append(str);
65     }
66
67     private void printString(String JavaDoc prefix, char[] chars, String JavaDoc suffix) {
68         String JavaDoc str = null;
69         if (chars != null) {
70             str = new String JavaDoc(chars);
71         }
72         printString(prefix, str, suffix);
73     }
74
75     private void printString(String JavaDoc prefix, String JavaDoc str, String JavaDoc suffix) {
76         printIndent();
77         if (str != null) {
78             buf.append(prefix);
79             buf.append(str);
80             buf.append(suffix);
81         } else {
82             buf.append(prefix);
83             buf.append(suffix);
84         }
85     }
86
87     private void printAttributes(String JavaDoc prefix, Attributes JavaDoc attrs,
88                                  String JavaDoc suffix) {
89         printString(prefix, getAttributes(attrs), suffix);
90     }
91
92     private void dumpBody(Node n) throws JspException JavaDoc {
93         Node.Nodes page = n.getBody();
94         if (page != null) {
95         indent++;
96             page.visit(this);
97         indent--;
98         }
99     }
100
101     public void visit(Node.TagDirective n) throws JspException JavaDoc {
102         visitCommon(n);
103         printAttributes("<%@ tag", n.getAttributes(), "%>");
104     }
105
106     public void visit(Node.PageDirective n) throws JspException JavaDoc {
107         visitCommon(n);
108         printAttributes("<%@ page", n.getAttributes(), "%>");
109     }
110
111     public void visit(Node.TaglibDirective n) throws JspException JavaDoc {
112         visitCommon(n);
113         printAttributes("<%@ taglib", n.getAttributes(), "%>");
114     }
115
116     public void visit(Node.IncludeDirective n) throws JspException JavaDoc {
117         visitCommon(n);
118         printAttributes("<%@ include", n.getAttributes(), "%>");
119         dumpBody(n);
120     }
121
122     public void visit(Node.Comment n) throws JspException JavaDoc {
123         visitCommon(n);
124         printString("<%--", n.getText(), "--%>");
125     }
126
127     public void visit(Node.Declaration n) throws JspException JavaDoc {
128         visitCommon(n);
129         printString("<%!", n.getText(), "%>");
130     }
131
132     public void visit(Node.Expression n) throws JspException JavaDoc {
133         visitCommon(n);
134         printString("<%=", n.getText(), "%>");
135     }
136
137     public void visit(Node.Scriptlet n) throws JspException JavaDoc {
138         visitCommon(n);
139         printString("<%", n.getText(), "%>");
140     }
141
142     public void visit(Node.IncludeAction n) throws JspException JavaDoc {
143         visitCommon(n);
144         printAttributes("<jsp:include", n.getAttributes(), ">");
145         dumpBody(n);
146         printString("</jsp:include>");
147     }
148
149     public void visit(Node.ForwardAction n) throws JspException JavaDoc {
150         visitCommon(n);
151         printAttributes("<jsp:forward", n.getAttributes(), ">");
152         dumpBody(n);
153         printString("</jsp:forward>");
154     }
155
156     public void visit(Node.GetProperty n) throws JspException JavaDoc {
157         visitCommon(n);
158         printAttributes("<jsp:getProperty", n.getAttributes(), "/>");
159     }
160
161     public void visit(Node.SetProperty n) throws JspException JavaDoc {
162         visitCommon(n);
163         printAttributes("<jsp:setProperty", n.getAttributes(), ">");
164         dumpBody(n);
165         printString("</jsp:setProperty>");
166     }
167
168     public void visit(Node.UseBean n) throws JspException JavaDoc {
169         visitCommon(n);
170         printAttributes("<jsp:useBean", n.getAttributes(), ">");
171         dumpBody(n);
172         printString("</jsp:useBean>");
173     }
174
175     public void visit(Node.PlugIn n) throws JspException JavaDoc {
176         visitCommon(n);
177         printAttributes("<jsp:plugin", n.getAttributes(), ">");
178         dumpBody(n);
179         printString("</jsp:plugin>");
180     }
181
182     public void visit(Node.ParamsAction n) throws JspException JavaDoc {
183         visitCommon(n);
184         printAttributes("<jsp:params", n.getAttributes(), ">");
185         dumpBody(n);
186         printString("</jsp:params>");
187     }
188
189     public void visit(Node.ParamAction n) throws JspException JavaDoc {
190         visitCommon(n);
191         printAttributes("<jsp:param", n.getAttributes(), ">");
192         dumpBody(n);
193         printString("</jsp:param>");
194     }
195
196     public void visit(Node.NamedAttribute n) throws JspException JavaDoc {
197         visitCommon(n);
198         printAttributes("<jsp:attribute", n.getAttributes(), ">");
199         dumpBody(n);
200         printString("</jsp:attribute>");
201     }
202
203     public void visit(Node.JspBody n) throws JspException JavaDoc {
204         visitCommon(n);
205         printAttributes("<jsp:body", n.getAttributes(), ">");
206         dumpBody(n);
207         printString("</jsp:body>");
208     }
209
210     public void visit(Node.ELExpression n) throws JspException JavaDoc {
211         visitCommon(n);
212         printString(n.getText());
213     }
214
215     public void visit(Node.CustomTag n) throws JspException JavaDoc {
216         visitCommon(n);
217         printAttributes("<" + n.getQName(), n.getAttributes(), ">");
218         dumpBody(n);
219         printString("</" + n.getQName() + ">");
220     }
221
222     public void visit(Node.UninterpretedTag n) throws JspException JavaDoc {
223         visitCommon(n);
224         String JavaDoc tag = n.getQName();
225         printAttributes("<"+tag, n.getAttributes(), ">");
226         dumpBody(n);
227         printString("</" + tag + ">");
228     }
229
230     public void visit(Node.InvokeAction n) throws JspException JavaDoc {
231         visitCommon(n);
232         printAttributes("<jsp:invoke", n.getAttributes(), ">");
233         dumpBody(n);
234         printString("</jsp:invoke>");
235     }
236
237     public void visit(Node.DoBodyAction n) throws JspException JavaDoc {
238         visitCommon(n);
239         printAttributes("<jsp:doBody", n.getAttributes(), ">");
240         dumpBody(n);
241         printString("</jsp:doBody>");
242     }
243
244     public void visit(Node.TemplateText n) throws JspException JavaDoc {
245         visitCommon(n);
246         printString(new String JavaDoc(n.getText()));
247     }
248
249     private void printIndent() {
250         for (int i=0; i < indent; i++) {
251             buf.append(" ");
252         }
253     }
254     
255     private String JavaDoc getString() {
256         return buf.toString();
257     }
258
259     public static String JavaDoc dump(Node n) {
260     try {
261             DumpVisitor dv = new DumpVisitor();
262         n.accept(dv);
263             return dv.getString();
264     } catch (JspException JavaDoc e) {
265             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
266             return e.getMessage();
267     }
268     }
269
270     public static String JavaDoc dump(Node.Nodes page) {
271     try {
272             DumpVisitor dv = new DumpVisitor();
273         page.visit(dv);
274             return dv.getString();
275     } catch (JspException JavaDoc e) {
276             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
277             return e.getMessage();
278     }
279     }
280 }
281
282
Popular Tags