001 package net.sourceforge.pmd.lang.jsp.ast;
002
003 import java.io.IOException;
004 import java.io.PrintWriter;
005 import java.io.Writer;
006 import java.util.ArrayList;
007 import java.util.List;
008
009 import net.sourceforge.pmd.lang.ast.Node;
010
011 public class DumpFacade extends JspParserVisitorAdapter {
012
013 private PrintWriter writer;
014 private boolean recurse;
015
016 public void initializeWith(Writer writer, String prefix, boolean recurse, JspNode node) {
017 this.writer = (writer instanceof PrintWriter) ? (PrintWriter) writer : new PrintWriter(writer);
018 this.recurse = recurse;
019 this.visit(node, prefix);
020 try {
021 writer.flush();
022 } catch (IOException e) {
023 throw new RuntimeException("Problem flushing PrintWriter.", e);
024 }
025 }
026
027 @Override
028 public Object visit(JspNode node, Object data) {
029 dump(node, (String) data);
030 if (recurse) {
031 return super.visit(node, data + " ");
032 } else {
033 return data;
034 }
035 }
036
037 private void dump(Node node, String prefix) {
038 //
039 // Dump format is generally composed of the following items...
040 //
041
042 // 1) Dump prefix
043 writer.print(prefix);
044
045 // 2) JJT Name of the Node
046 writer.print(node.toString());
047
048 //
049 // If there are any additional details, then:
050 // 1) A colon
051 // 2) The Node.getImage() if it is non-empty
052 // 3) Extras in parentheses
053 //
054
055 // Standard image handling
056 String image = node.getImage();
057
058 // Extras
059 List<String> extras = new ArrayList<String>();
060
061 // Other extras
062 if (node instanceof ASTAttribute) {
063 extras.add("name=[" + ((ASTAttribute) node).getName() + "]");
064 } else if (node instanceof ASTDeclaration) {
065 extras.add("name=[" + ((ASTDeclaration) node).getName() + "]");
066 } else if (node instanceof ASTDoctypeDeclaration) {
067 extras.add("name=[" + ((ASTDoctypeDeclaration) node).getName() + "]");
068 } else if (node instanceof ASTDoctypeExternalId) {
069 extras.add("uri=[" + ((ASTDoctypeExternalId) node).getUri() + "]");
070 if (((ASTDoctypeExternalId) node).getPublicId().length() > 0) {
071 extras.add("publicId=[" + ((ASTDoctypeExternalId) node).getPublicId() + "]");
072 }
073 } else if (node instanceof ASTElement) {
074 extras.add("name=[" + ((ASTElement) node).getName() + "]");
075 if (((ASTElement) node).isEmpty()) {
076 extras.add("empty");
077 }
078 } else if (node instanceof ASTJspDirective) {
079 extras.add("name=[" + ((ASTJspDirective) node).getName() + "]");
080 } else if (node instanceof ASTJspDirectiveAttribute) {
081 extras.add("name=[" + ((ASTJspDirectiveAttribute) node).getName() + "]");
082 extras.add("value=[" + ((ASTJspDirectiveAttribute) node).getValue() + "]");
083 }
084
085 // Output image and extras
086 if (image != null || !extras.isEmpty()) {
087 writer.print(":");
088 if (image != null) {
089 writer.print(image);
090 }
091 for (String extra : extras) {
092 writer.print("(");
093 writer.print(extra);
094 writer.print(")");
095 }
096 }
097
098 writer.println();
099 }
100 }
|