001: /*
002: * Copyright 1999,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.jasper.compiler;
018:
019: import org.xml.sax.Attributes;
020: import org.apache.jasper.JasperException;
021:
022: class Dumper {
023:
024: static class DumpVisitor extends Node.Visitor {
025: private int indent = 0;
026:
027: private String getAttributes(Attributes attrs) {
028: if (attrs == null)
029: return "";
030:
031: StringBuffer buf = new StringBuffer();
032: for (int i = 0; i < attrs.getLength(); i++) {
033: buf.append(" " + attrs.getQName(i) + "=\""
034: + attrs.getValue(i) + "\"");
035: }
036: return buf.toString();
037: }
038:
039: private void printString(String str) {
040: printIndent();
041: System.out.print(str);
042: }
043:
044: private void printString(String prefix, char[] chars,
045: String suffix) {
046: String str = null;
047: if (chars != null) {
048: str = new String(chars);
049: }
050: printString(prefix, str, suffix);
051: }
052:
053: private void printString(String prefix, String str,
054: String suffix) {
055: printIndent();
056: if (str != null) {
057: System.out.print(prefix + str + suffix);
058: } else {
059: System.out.print(prefix + suffix);
060: }
061: }
062:
063: private void printAttributes(String prefix, Attributes attrs,
064: String suffix) {
065: printString(prefix, getAttributes(attrs), suffix);
066: }
067:
068: private void dumpBody(Node n) throws JasperException {
069: Node.Nodes page = n.getBody();
070: if (page != null) {
071: // indent++;
072: page.visit(this );
073: // indent--;
074: }
075: }
076:
077: public void visit(Node.PageDirective n) throws JasperException {
078: printAttributes("<%@ page", n.getAttributes(), "%>");
079: }
080:
081: public void visit(Node.TaglibDirective n)
082: throws JasperException {
083: printAttributes("<%@ taglib", n.getAttributes(), "%>");
084: }
085:
086: public void visit(Node.IncludeDirective n)
087: throws JasperException {
088: printAttributes("<%@ include", n.getAttributes(), "%>");
089: dumpBody(n);
090: }
091:
092: public void visit(Node.Comment n) throws JasperException {
093: printString("<%--", n.getText(), "--%>");
094: }
095:
096: public void visit(Node.Declaration n) throws JasperException {
097: printString("<%!", n.getText(), "%>");
098: }
099:
100: public void visit(Node.Expression n) throws JasperException {
101: printString("<%=", n.getText(), "%>");
102: }
103:
104: public void visit(Node.Scriptlet n) throws JasperException {
105: printString("<%", n.getText(), "%>");
106: }
107:
108: public void visit(Node.IncludeAction n) throws JasperException {
109: printAttributes("<jsp:include", n.getAttributes(), ">");
110: dumpBody(n);
111: printString("</jsp:include>");
112: }
113:
114: public void visit(Node.ForwardAction n) throws JasperException {
115: printAttributes("<jsp:forward", n.getAttributes(), ">");
116: dumpBody(n);
117: printString("</jsp:forward>");
118: }
119:
120: public void visit(Node.GetProperty n) throws JasperException {
121: printAttributes("<jsp:getProperty", n.getAttributes(), "/>");
122: }
123:
124: public void visit(Node.SetProperty n) throws JasperException {
125: printAttributes("<jsp:setProperty", n.getAttributes(), ">");
126: dumpBody(n);
127: printString("</jsp:setProperty>");
128: }
129:
130: public void visit(Node.UseBean n) throws JasperException {
131: printAttributes("<jsp:useBean", n.getAttributes(), ">");
132: dumpBody(n);
133: printString("</jsp:useBean>");
134: }
135:
136: public void visit(Node.PlugIn n) throws JasperException {
137: printAttributes("<jsp:plugin", n.getAttributes(), ">");
138: dumpBody(n);
139: printString("</jsp:plugin>");
140: }
141:
142: public void visit(Node.ParamsAction n) throws JasperException {
143: printAttributes("<jsp:params", n.getAttributes(), ">");
144: dumpBody(n);
145: printString("</jsp:params>");
146: }
147:
148: public void visit(Node.ParamAction n) throws JasperException {
149: printAttributes("<jsp:param", n.getAttributes(), ">");
150: dumpBody(n);
151: printString("</jsp:param>");
152: }
153:
154: public void visit(Node.NamedAttribute n) throws JasperException {
155: printAttributes("<jsp:attribute", n.getAttributes(), ">");
156: dumpBody(n);
157: printString("</jsp:attribute>");
158: }
159:
160: public void visit(Node.JspBody n) throws JasperException {
161: printAttributes("<jsp:body", n.getAttributes(), ">");
162: dumpBody(n);
163: printString("</jsp:body>");
164: }
165:
166: public void visit(Node.ELExpression n) throws JasperException {
167: printString("${" + new String(n.getText()) + "}");
168: }
169:
170: public void visit(Node.CustomTag n) throws JasperException {
171: printAttributes("<" + n.getQName(), n.getAttributes(), ">");
172: dumpBody(n);
173: printString("</" + n.getQName() + ">");
174: }
175:
176: public void visit(Node.UninterpretedTag n)
177: throws JasperException {
178: String tag = n.getQName();
179: printAttributes("<" + tag, n.getAttributes(), ">");
180: dumpBody(n);
181: printString("</" + tag + ">");
182: }
183:
184: public void visit(Node.TemplateText n) throws JasperException {
185: printString(new String(n.getText()));
186: }
187:
188: private void printIndent() {
189: for (int i = 0; i < indent; i++) {
190: System.out.print(" ");
191: }
192: }
193: }
194:
195: public static void dump(Node n) {
196: try {
197: n.accept(new DumpVisitor());
198: } catch (JasperException e) {
199: e.printStackTrace();
200: }
201: }
202:
203: public static void dump(Node.Nodes page) {
204: try {
205: page.visit(new DumpVisitor());
206: } catch (JasperException e) {
207: e.printStackTrace();
208: }
209: }
210: }
|