View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.plsql.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  public class DumpFacade extends PLSQLParserVisitorAdapter {
13  
14  	private PrintWriter writer;
15  	private boolean recurse;
16  
17  	public void initializeWith(Writer writer, String prefix, boolean recurse, PLSQLNode node) {
18  		this.writer = writer instanceof PrintWriter ? (PrintWriter) writer : new PrintWriter(writer);
19  		this.recurse = recurse;
20  		this.visit(node, prefix);
21  		try {
22  			writer.flush();
23  		} catch (IOException e) {
24  			throw new RuntimeException("Problem flushing PrintWriter.", e);
25  		}
26  	}
27  
28  	@Override
29  	public Object visit(PLSQLNode node, Object data) {
30  		dump(node, (String) data);
31  		if (recurse) {
32  			return super.visit(node, data + " ");
33  		} else {
34  			return data;
35  		}
36  	}
37  
38  	private void dump(PLSQLNode node, String prefix) {
39  		//
40  		// Dump format is generally composed of the following items...
41  		//
42  
43  		// 1) Dump prefix
44  		writer.print(prefix);
45  
46  		// 2) JJT Name of the Node
47  		writer.print(node.toString());
48  
49  		//
50  		// If there are any additional details, then:
51  		// 1) A colon
52  		// 2) The Node.getImage() if it is non-empty
53  		// 3) Extras in parentheses
54  		//
55  
56  		// Standard image handling
57  		String image = node.getImage();
58  
59  		// Special image handling (e.g. Nodes with normally null images)
60  		if (node instanceof ASTBooleanLiteral) {
61  			image = node.getImage();
62  		} else if (node instanceof ASTPrimaryPrefix) {
63  			String result = null;
64  			/*
65  			if (primaryPrefix.usesSuperModifier()) {
66  				result = "super";
67  			} else if (primaryPrefix.usesThisModifier()) {
68  				result = "this";
69  			}
70  			*/
71  			if (image != null) {
72  				result += "." + image;
73  			}
74  			image = result;
75  		} else if (node instanceof ASTPrimarySuffix) {
76  			ASTPrimarySuffix primarySuffix = (ASTPrimarySuffix) node;
77  			if (primarySuffix.isArrayDereference()) {
78  				if (image == null) {
79  					image = "[";
80  				} else {
81  					image = "[" + image;
82  				}
83  			}
84  		}
85  
86  		// Extras
87  		List<String> extras = new ArrayList<>();
88  
89  		// Output image and extras
90  		if (image != null || !extras.isEmpty()) {
91  			writer.print(':');
92  			if (image != null) {
93  				writer.print(image);
94  			}
95  			for (String extra : extras) {
96  				writer.print('(');
97  				writer.print(extra);
98  				writer.print(')');
99  			}
100 		}
101 
102 		writer.println();
103 	}
104 
105 }