View Javadoc

    1   package net.sourceforge.pmd.lang.jsp.ast;
    2
    3   import java.io.IOException;
    4   import java.io.PrintWriter;
    5   import java.io.Writer;
    6   import java.util.ArrayList;
    7   import java.util.List;
    8
    9   import net.sourceforge.pmd.lang.ast.Node;
    10
    11  public class DumpFacade extends JspParserVisitorAdapter {
    12
    13  	private PrintWriter writer;
    14  	private boolean recurse;
    15
    16  	public void initializeWith(Writer writer, String prefix, boolean recurse, JspNode node) {
    17  		this.writer = (writer instanceof PrintWriter) ? (PrintWriter) writer : new PrintWriter(writer);
    18  		this.recurse = recurse;
    19  		this.visit(node, prefix);
    20  		try {
    21  			writer.flush();
    22  		} catch (IOException e) {
    23  			throw new RuntimeException("Problem flushing PrintWriter.", e);
    24  		}
    25  	}
    26
    27  	@Override
    28  	public Object visit(JspNode node, Object data) {
    29  		dump(node, (String) data);
    30  		if (recurse) {
    31  			return super.visit(node, data + " ");
    32  		} else {
    33  			return data;
    34  		}
    35  	}
    36
    37  	private void dump(Node node, String prefix) {
    38  		//
    39  		// Dump format is generally composed of the following items...
    40  		//
    41
    42  		// 1) Dump prefix
    43  		writer.print(prefix);
    44
    45  		// 2) JJT Name of the Node
    46  		writer.print(node.toString());
    47
    48  		//
    49  		// If there are any additional details, then:
    50  		// 1) A colon
    51  		// 2) The Node.getImage() if it is non-empty
    52  		// 3) Extras in parentheses
    53  		//
    54
    55  		// Standard image handling
    56  		String image = node.getImage();
    57
    58  		// Extras
    59  		List<String> extras = new ArrayList<String>();
    60
    61  		// Other extras
    62  		if (node instanceof ASTAttribute) {
    63  			extras.add("name=[" + ((ASTAttribute) node).getName() + "]");
    64  		} else if (node instanceof ASTDeclaration) {
    65  			extras.add("name=[" + ((ASTDeclaration) node).getName() + "]");
    66  		} else if (node instanceof ASTDoctypeDeclaration) {
    67  			extras.add("name=[" + ((ASTDoctypeDeclaration) node).getName() + "]");
    68  		} else if (node instanceof ASTDoctypeExternalId) {
    69  			extras.add("uri=[" + ((ASTDoctypeExternalId) node).getUri() + "]");
    70  			if (((ASTDoctypeExternalId) node).getPublicId().length() > 0) {
    71  				extras.add("publicId=[" + ((ASTDoctypeExternalId) node).getPublicId() + "]");
    72  			}
    73  		} else if (node instanceof ASTElement) {
    74  			extras.add("name=[" + ((ASTElement) node).getName() + "]");
    75  			if (((ASTElement) node).isEmpty()) {
    76  				extras.add("empty");
    77  			}
    78  		} else if (node instanceof ASTJspDirective) {
    79  			extras.add("name=[" + ((ASTJspDirective) node).getName() + "]");
    80  		} else if (node instanceof ASTJspDirectiveAttribute) {
    81  			extras.add("name=[" + ((ASTJspDirectiveAttribute) node).getName() + "]");
    82  			extras.add("value=[" + ((ASTJspDirectiveAttribute) node).getValue() + "]");
    83  		}
    84
    85  		// Output image and extras
    86  		if (image != null || !extras.isEmpty()) {
    87  			writer.print(':');
    88  			if (image != null) {
    89  				writer.print(image);
    90  			}
    91  			for (String extra : extras) {
    92  				writer.print('(');
    93  				writer.print(extra);
    94  				writer.print(')');
    95  			}
    96  		}
    97
    98  		writer.println();
    99  	}
    100 }