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  			ASTPrimaryPrefix primaryPrefix = (ASTPrimaryPrefix) node;
64  			String result = null;
65  			/*
66  			if (primaryPrefix.usesSuperModifier()) {
67  				result = "super";
68  			} else if (primaryPrefix.usesThisModifier()) {
69  				result = "this";
70  			}
71  			*/
72  			if (image != null) {
73  				result += "." + image;
74  			}
75  			image = result;
76  		} else if (node instanceof ASTPrimarySuffix) {
77  			ASTPrimarySuffix primarySuffix = (ASTPrimarySuffix) node;
78  			if (primarySuffix.isArrayDereference()) {
79  				if (image == null) {
80  					image = "[";
81  				} else {
82  					image = "[" + image;
83  				}
84  			}
85  		}
86  
87  		// Extras
88  		List<String> extras = new ArrayList<String>();
89  
90  		// Output image and extras
91  		if (image != null || !extras.isEmpty()) {
92  			writer.print(':');
93  			if (image != null) {
94  				writer.print(image);
95  			}
96  			for (String extra : extras) {
97  				writer.print('(');
98  				writer.print(extra);
99  				writer.print(')');
100 			}
101 		}
102 
103 		writer.println();
104 	}
105 
106 }