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 String result = null;
64
65
66
67
68
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
87 List<String> extras = new ArrayList<String>();
88
89
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 }