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