2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.asdc.utils;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.HashMap;
26 import java.util.List;
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;
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>>();
42 public TreePrinterListener(Parser parser) {
43 this.ruleNames = Arrays.asList(parser.getRuleNames());
46 public TreePrinterListener(List<String> ruleNames) {
47 this.ruleNames = ruleNames;
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 + "'";
56 stack.get(node.getParent()).add(text);
60 public void visitErrorNode(ErrorNode node) {
61 stack.get(node.getParent()).add(Utils.escapeWhitespace(Trees.getNodeText(node, ruleNames), false));
65 public void enterEveryRule(ParserRuleContext ctx) {
66 if (!stack.containsKey(ctx.parent)) {
67 stack.put(ctx.parent, new ArrayList<String>());
69 if (!stack.containsKey(ctx)) {
70 stack.put(ctx, new ArrayList<String>());
73 final StringBuilder sb = new StringBuilder();
74 int ruleIndex = ctx.getRuleIndex();
76 if (ruleIndex >= 0 && ruleIndex < ruleNames.size()) {
77 ruleName = ruleNames.get(ruleIndex);
79 ruleName = Integer.toString(ruleIndex);
82 stack.get(ctx).add(sb.toString());
86 public void exitEveryRule(ParserRuleContext ctx) {
87 ArrayList<String> ruleStack = stack.remove(ctx);
88 StringBuilder sb = new StringBuilder();
89 boolean brackit = ruleStack.size() > 1;
93 sb.append(ruleStack.get(0));
94 for (int i = 1; i < ruleStack.size(); i++) {
96 sb.append(ruleStack.get(i));
101 if (sb.length() < 80) {
102 stack.get(ctx.parent).add(sb.toString());
104 // Current line is too long, regenerate it using 1 line per item.
109 if (!ruleStack.isEmpty()) {
110 sb.append(ruleStack.remove(0)).append("\r\n");
112 while (!ruleStack.isEmpty()) {
113 sb.append(indent(ruleStack.remove(0))).append("\r\n");
118 stack.get(ctx.parent).add(sb.toString());
120 if (ctx.parent == null) {
121 builder.append(sb.toString());
125 static String indent(String input) {
126 return " " + input.replaceAll("\r\n(.)", "\r\n $1");
130 public String toString() {
131 return builder.toString();