View Javadoc

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