Replaced all tabs with spaces in java and pom.xml
[so.git] / asdc-controller / src / test / java / org / onap / so / asdc / utils / TreePrinterListener.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.asdc.utils;
22
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import org.antlr.v4.runtime.Parser;
29 import org.antlr.v4.runtime.ParserRuleContext;
30 import org.antlr.v4.runtime.RuleContext;
31 import org.antlr.v4.runtime.misc.Utils;
32 import org.antlr.v4.runtime.tree.ErrorNode;
33 import org.antlr.v4.runtime.tree.ParseTreeListener;
34 import org.antlr.v4.runtime.tree.TerminalNode;
35 import org.antlr.v4.runtime.tree.Trees;
36
37 public class TreePrinterListener implements ParseTreeListener {
38     private final List<String> ruleNames;
39     private final StringBuilder builder = new StringBuilder();
40     Map<RuleContext, ArrayList<String>> stack = new HashMap<RuleContext, ArrayList<String>>();
41
42     public TreePrinterListener(Parser parser) {
43         this.ruleNames = Arrays.asList(parser.getRuleNames());
44     }
45
46     public TreePrinterListener(List<String> ruleNames) {
47         this.ruleNames = ruleNames;
48     }
49
50     @Override
51     public void visitTerminal(TerminalNode node) {
52         String text = Utils.escapeWhitespace(Trees.getNodeText(node, ruleNames), false);
53         if (text.startsWith(" ") || text.endsWith(" ")) {
54             text = "'" + text + "'";
55         }
56         stack.get(node.getParent()).add(text);
57     }
58
59     @Override
60     public void visitErrorNode(ErrorNode node) {
61         stack.get(node.getParent()).add(Utils.escapeWhitespace(Trees.getNodeText(node, ruleNames), false));
62     }
63
64     @Override
65     public void enterEveryRule(ParserRuleContext ctx) {
66         if (!stack.containsKey(ctx.parent)) {
67             stack.put(ctx.parent, new ArrayList<String>());
68         }
69         if (!stack.containsKey(ctx)) {
70             stack.put(ctx, new ArrayList<String>());
71         }
72
73         final StringBuilder sb = new StringBuilder();
74         int ruleIndex = ctx.getRuleIndex();
75         String ruleName;
76         if (ruleIndex >= 0 && ruleIndex < ruleNames.size()) {
77             ruleName = ruleNames.get(ruleIndex);
78         } else {
79             ruleName = Integer.toString(ruleIndex);
80         }
81         sb.append(ruleName);
82         stack.get(ctx).add(sb.toString());
83     }
84
85     @Override
86     public void exitEveryRule(ParserRuleContext ctx) {
87         ArrayList<String> ruleStack = stack.remove(ctx);
88         StringBuilder sb = new StringBuilder();
89         boolean brackit = ruleStack.size() > 1;
90         if (brackit) {
91             sb.append("(");
92         }
93         sb.append(ruleStack.get(0));
94         for (int i = 1; i < ruleStack.size(); i++) {
95             sb.append(" ");
96             sb.append(ruleStack.get(i));
97         }
98         if (brackit) {
99             sb.append(")");
100         }
101         if (sb.length() < 80) {
102             stack.get(ctx.parent).add(sb.toString());
103         } else {
104             // Current line is too long, regenerate it using 1 line per item.
105             sb.setLength(0);
106             if (brackit) {
107                 sb.append("(");
108             }
109             if (!ruleStack.isEmpty()) {
110                 sb.append(ruleStack.remove(0)).append("\r\n");
111             }
112             while (!ruleStack.isEmpty()) {
113                 sb.append(indent(ruleStack.remove(0))).append("\r\n");
114             }
115             if (brackit) {
116                 sb.append(")");
117             }
118             stack.get(ctx.parent).add(sb.toString());
119         }
120         if (ctx.parent == null) {
121             builder.append(sb.toString());
122         }
123     }
124
125     static String indent(String input) {
126         return "  " + input.replaceAll("\r\n(.)", "\r\n  $1");
127     }
128
129     @Override
130     public String toString() {
131         return builder.toString();
132     }
133
134
135 }