alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.web.jsps.parserapi;

import javax.servlet.jsp.JspException;
import org.openide.ErrorManager;
import org.xml.sax.Attributes;

class DumpVisitor extends Node.Visitor {
    
    private int indent = 0;
    
    private StringBuffer buf;

    private DumpVisitor() {
        super();
        buf = new StringBuffer();
    }

    /**
     * This method provides a place to put actions that are common to
     * all nodes. Override this in the child visitor class if need to.
     */
    protected void visitCommon(Node n) throws JspException {
        printString("\nNode [" + n.getStart() + ", " + getDisplayClassName(n.getClass().getName()) + "] ");
    }
    
    private String getDisplayClassName(String cn) {
        int amp = cn.indexOf('$');
        return cn.substring(amp + 1);
    }

    private String getAttributes(Attributes attrs) {
        if (attrs == null)
            return "";

        StringBuffer buf = new StringBuffer();
        for (int i=0; i < attrs.getLength(); i++) {
            buf.append(" " + attrs.getQName(i) + "=\""
                       + attrs.getValue(i) + "\"");
        }
        return buf.toString();
    }

    private void printString(String str) {
        printIndent();
        buf.append(str);
    }

    private void printString(String prefix, char[] chars, String suffix) {
        String str = null;
        if (chars != null) {
            str = new String(chars);
        }
        printString(prefix, str, suffix);
    }

    private void printString(String prefix, String str, String suffix) {
        printIndent();
        if (str != null) {
            buf.append(prefix);
            buf.append(str);
            buf.append(suffix);
        } else {
            buf.append(prefix);
            buf.append(suffix);
        }
    }

    private void printAttributes(String prefix, Attributes attrs,
                                 String suffix) {
        printString(prefix, getAttributes(attrs), suffix);
    }

    private void dumpBody(Node n) throws JspException {
        Node.Nodes page = n.getBody();
        if (page != null) {
		indent++;
            page.visit(this);
		indent--;
        }
    }

    public void visit(Node.TagDirective n) throws JspException {
        visitCommon(n);
        printAttributes("<%@ tag", n.getAttributes(), "%>");
    }

    public void visit(Node.PageDirective n) throws JspException {
        visitCommon(n);
        printAttributes("<%@ page", n.getAttributes(), "%>");
    }

    public void visit(Node.TaglibDirective n) throws JspException {
        visitCommon(n);
        printAttributes("<%@ taglib", n.getAttributes(), "%>");
    }

    public void visit(Node.IncludeDirective n) throws JspException {
        visitCommon(n);
        printAttributes("<%@ include", n.getAttributes(), "%>");
        dumpBody(n);
    }

    public void visit(Node.Comment n) throws JspException {
        visitCommon(n);
        printString("<%--", n.getText(), "--%>");
    }

    public void visit(Node.Declaration n) throws JspException {
        visitCommon(n);
        printString("<%!", n.getText(), "%>");
    }

    public void visit(Node.Expression n) throws JspException {
        visitCommon(n);
        printString("<%=", n.getText(), "%>");
    }

    public void visit(Node.Scriptlet n) throws JspException {
        visitCommon(n);
        printString("<%", n.getText(), "%>");
    }

    public void visit(Node.IncludeAction n) throws JspException {
        visitCommon(n);
        printAttributes("");
        dumpBody(n);
        printString("");
    }

    public void visit(Node.ForwardAction n) throws JspException {
        visitCommon(n);
        printAttributes("");
        dumpBody(n);
        printString("");
    }

    public void visit(Node.GetProperty n) throws JspException {
        visitCommon(n);
        printAttributes("");
    }

    public void visit(Node.SetProperty n) throws JspException {
        visitCommon(n);
        printAttributes("");
        dumpBody(n);
        printString("");
    }

    public void visit(Node.UseBean n) throws JspException {
        visitCommon(n);
        printAttributes("");
        dumpBody(n);
        printString("");
    }

    public void visit(Node.PlugIn n) throws JspException {
        visitCommon(n);
        printAttributes("");
        dumpBody(n);
        printString("");
    }

    public void visit(Node.ParamsAction n) throws JspException {
        visitCommon(n);
        printAttributes("");
        dumpBody(n);
        printString("");
    }

    public void visit(Node.ParamAction n) throws JspException {
        visitCommon(n);
        printAttributes("");
        dumpBody(n);
        printString("");
    }

    public void visit(Node.NamedAttribute n) throws JspException {
        visitCommon(n);
        printAttributes("");
        dumpBody(n);
        printString("");
    }

    public void visit(Node.JspBody n) throws JspException {
        visitCommon(n);
        printAttributes("");
        dumpBody(n);
        printString("");
    }

    public void visit(Node.ELExpression n) throws JspException {
        visitCommon(n);
        printString( "${" + new String( n.getText() ) + "}" );
    }

    public void visit(Node.CustomTag n) throws JspException {
        visitCommon(n);
        printAttributes("<" + n.getQName(), n.getAttributes(), ">");
        dumpBody(n);
        printString("");
    }

    public void visit(Node.UninterpretedTag n) throws JspException {
        visitCommon(n);
        String tag = n.getQName();
        printAttributes("<"+tag, n.getAttributes(), ">");
        dumpBody(n);
        printString("");
    }

    public void visit(Node.InvokeAction n) throws JspException {
        visitCommon(n);
        printAttributes("");
        dumpBody(n);
        printString("");
    }

    public void visit(Node.DoBodyAction n) throws JspException {
        visitCommon(n);
        printAttributes("");
        dumpBody(n);
        printString("");
    }

    public void visit(Node.TemplateText n) throws JspException {
        visitCommon(n);
        printString(new String(n.getText()));
    }

    private void printIndent() {
        for (int i=0; i < indent; i++) {
            buf.append("  ");
        }
    }
    
    private String getString() {
        return buf.toString();
    }

    public static String dump(Node n) {
	try {
            DumpVisitor dv = new DumpVisitor();
	    n.accept(dv);
            return dv.getString();
	} catch (JspException e) {
            ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
            return e.getMessage();
	}
    }

    public static String dump(Node.Nodes page) {
	try {
            DumpVisitor dv = new DumpVisitor();
	    page.visit(dv);
            return dv.getString();
	} catch (JspException e) {
            ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
            return e.getMessage();
	}
    }
}

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.