001: /*
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 1999 The Apache Software Foundation. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution, if
020: * any, must include the following acknowlegement:
021: * "This product includes software developed by the
022: * Apache Software Foundation (http://www.apache.org/)."
023: * Alternately, this acknowlegement may appear in the software itself,
024: * if and wherever such third-party acknowlegements normally appear.
025: *
026: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
027: * Foundation" must not be used to endorse or promote products derived
028: * from this software without prior written permission. For written
029: * permission, please contact apache@apache.org.
030: *
031: * 5. Products derived from this software may not be called "Apache"
032: * nor may "Apache" appear in their names without prior written
033: * permission of the Apache Group.
034: *
035: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: *
049: * This software consists of voluntary contributions made by many
050: * individuals on behalf of the Apache Software Foundation. For more
051: * information on the Apache Software Foundation, please see
052: * <http://www.apache.org/>.
053: *
054: */
055:
056: package com.rimfaxe.webserver.compiler.jsp;
057:
058: import org.xml.sax.Attributes;
059: import com.rimfaxe.webserver.compiler.JspToJavaException;
060:
061: public class Dumper {
062:
063: static class DumpVisitor extends Node.Visitor {
064: private int indent = 0;
065:
066: private String getAttributes(Attributes attrs) {
067: if (attrs == null)
068: return "";
069:
070: StringBuffer buf = new StringBuffer();
071: for (int i = 0; i < attrs.getLength(); i++) {
072: buf.append(" " + attrs.getQName(i) + "=\""
073: + attrs.getValue(i) + "\"");
074: }
075: return buf.toString();
076: }
077:
078: private void printString(String str) {
079: printIndent();
080: System.out.print(str);
081: }
082:
083: private void printString(String prefix, char[] chars,
084: String suffix) {
085: String str = null;
086: if (chars != null) {
087: str = new String(chars);
088: }
089: printString(prefix, str, suffix);
090: }
091:
092: private void printString(String prefix, String str,
093: String suffix) {
094: printIndent();
095: if (str != null) {
096: System.out.print(prefix + str + suffix);
097: } else {
098: System.out.print(prefix + suffix);
099: }
100: }
101:
102: private void printAttributes(String prefix, Attributes attrs,
103: String suffix) {
104: printString(prefix, getAttributes(attrs), suffix);
105: }
106:
107: private void dumpBody(Node n) throws JasperException,
108: JspToJavaException {
109: Node.Nodes page = n.getBody();
110: if (page != null) {
111: // indent++;
112: page.visit(this );
113: // indent--;
114: }
115: }
116:
117: public void visit(Node.PageDirective n) throws JasperException,
118: JspToJavaException {
119: printAttributes("<%@ page", n.getAttributes(), "%>");
120: }
121:
122: public void visit(Node.TaglibDirective n)
123: throws JasperException, JspToJavaException {
124: printAttributes("<%@ taglib", n.getAttributes(), "%>");
125: }
126:
127: public void visit(Node.IncludeDirective n)
128: throws JasperException, JspToJavaException {
129: printAttributes("<%@ include", n.getAttributes(), "%>");
130: dumpBody(n);
131: }
132:
133: public void visit(Node.Comment n) throws JasperException,
134: JspToJavaException {
135: printString("<%--", n.getText(), "--%>");
136: }
137:
138: public void visit(Node.Declaration n) throws JasperException,
139: JspToJavaException {
140: printString("<%!", n.getText(), "%>");
141: }
142:
143: public void visit(Node.Expression n) throws JasperException,
144: JspToJavaException {
145: printString("<%=", n.getText(), "%>");
146: }
147:
148: public void visit(Node.Scriptlet n) throws JasperException,
149: JspToJavaException {
150: printString("<%", n.getText(), "%>");
151: }
152:
153: public void visit(Node.IncludeAction n) throws JasperException,
154: JspToJavaException {
155: printAttributes("<jsp:include", n.getAttributes(), "/>");
156: dumpBody(n);
157: }
158:
159: public void visit(Node.ForwardAction n) throws JasperException,
160: JspToJavaException {
161: printAttributes("<jsp:forward", n.getAttributes(), ">");
162: dumpBody(n);
163: printString("</jsp:forward>");
164: }
165:
166: public void visit(Node.GetProperty n) throws JasperException,
167: JspToJavaException {
168: printAttributes("<jsp:getProperty", n.getAttributes(), "/>");
169: }
170:
171: public void visit(Node.SetProperty n) throws JasperException,
172: JspToJavaException {
173: printAttributes("<jsp:setProperty", n.getAttributes(), "/>");
174: }
175:
176: public void visit(Node.UseBean n) throws JasperException,
177: JspToJavaException {
178: printAttributes("<jsp:usebean", n.getAttributes(), ">");
179: dumpBody(n);
180: printString("</jsp:usebean>");
181: }
182:
183: public void visit(Node.PlugIn n) throws JasperException,
184: JspToJavaException {
185: printAttributes("<jsp:plugin", n.getAttributes(), ">");
186: dumpBody(n);
187: printString("</jsp:plugin>");
188: }
189:
190: public void visit(Node.CustomTag n) throws JasperException,
191: JspToJavaException {
192: printAttributes("<" + n.getName(), n.getAttributes(), ">");
193: dumpBody(n);
194: printString("</" + n.getName() + ">");
195: }
196:
197: public void visit(Node.UninterpretedTag n)
198: throws JasperException, JspToJavaException {
199: String tag = n.getName();
200: printAttributes("<" + tag, n.getAttributes(), ">");
201: dumpBody(n);
202: printString("</" + tag + ">");
203: }
204:
205: public void visit(Node.TemplateText n) throws JasperException,
206: JspToJavaException {
207: printString(new String(n.getText()));
208: }
209:
210: private void printIndent() {
211: for (int i = 0; i < indent; i++) {
212: System.out.print(" ");
213: }
214: }
215: }
216:
217: public static void dump(Node n) {
218: try {
219: n.accept(new DumpVisitor());
220: } catch (JasperException e) {
221: e.printStackTrace();
222: } catch (JspToJavaException e) {
223: e.printStackTrace();
224: }
225: }
226:
227: public static void dump(Node.Nodes page) {
228: try {
229: page.visit(new DumpVisitor());
230: } catch (JasperException e) {
231: e.printStackTrace();
232: } catch (JspToJavaException e) {
233: e.printStackTrace();
234: }
235: }
236: }
|