1
2
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
41
42
43
44 writer.print(prefix);
45
46
47 writer.print(node.toString());
48
49
50
51
52
53
54
55
56
57 String image = node.getImage();
58
59
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
67
68
69
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
88 List<String> extras = new ArrayList<String>();
89
90
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 }