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;
29 import org.antlr.v4.runtime.Parser;
30 import org.antlr.v4.runtime.ParserRuleContext;
31 import org.antlr.v4.runtime.RuleContext;
32 import org.antlr.v4.runtime.misc.Utils;
33 import org.antlr.v4.runtime.tree.ErrorNode;
34 import org.antlr.v4.runtime.tree.ParseTreeListener;
35 import org.antlr.v4.runtime.tree.TerminalNode;
36 import org.antlr.v4.runtime.tree.Trees;
38 public class TreePrinterListener implements ParseTreeListener {
39 private final List<String> ruleNames;
40 private final StringBuilder builder = new StringBuilder();
41 Map<RuleContext,ArrayList<String>> stack = new HashMap<RuleContext,ArrayList<String>>();
43 public TreePrinterListener(Parser parser) {
44 this.ruleNames = Arrays.asList(parser.getRuleNames());
47 public TreePrinterListener(List<String> ruleNames) {
48 this.ruleNames = ruleNames;
52 public void visitTerminal(TerminalNode node) {
53 String text = Utils.escapeWhitespace(Trees.getNodeText(node, ruleNames), false);
54 if(text.startsWith(" ") || text.endsWith(" ")){
55 text = "'" + text + "'";
57 stack.get(node.getParent()).add(text);
61 public void visitErrorNode(ErrorNode node) {
62 stack.get(node.getParent()).add(Utils.escapeWhitespace(Trees.getNodeText(node, ruleNames), false));
66 public void enterEveryRule(ParserRuleContext ctx) {
67 if(!stack.containsKey(ctx.parent)){
68 stack.put(ctx.parent, new ArrayList<String>());
70 if(!stack.containsKey(ctx)){
71 stack.put(ctx, new ArrayList<String>());
74 final StringBuilder sb = new StringBuilder();
75 int ruleIndex = ctx.getRuleIndex();
77 if (ruleIndex >= 0 && ruleIndex < ruleNames.size()) {
78 ruleName = ruleNames.get(ruleIndex);
81 ruleName = Integer.toString(ruleIndex);
84 stack.get(ctx).add(sb.toString());
88 public void exitEveryRule(ParserRuleContext ctx) {
89 ArrayList<String> ruleStack = stack.remove(ctx);
90 StringBuilder sb = new StringBuilder();
91 boolean brackit =ruleStack.size()>1;
95 sb.append(ruleStack.get(0));
96 for(int i=1; i<ruleStack.size(); i++){
98 sb.append(ruleStack.get(i));
103 if(sb.length() < 80){
104 stack.get(ctx.parent).add(sb.toString());
106 // Current line is too long, regenerate it using 1 line per item.
111 if(!ruleStack.isEmpty()){
112 sb.append(ruleStack.remove(0)).append("\r\n");
114 while(!ruleStack.isEmpty()){
115 sb.append(indent(ruleStack.remove(0))).append("\r\n");
120 stack.get(ctx.parent).add(sb.toString());
122 if(ctx.parent == null){
123 builder.append(sb.toString());
127 static String indent(String input){
128 return " " + input.replaceAll("\r\n(.)","\r\n $1");
132 public String toString() {
133 return builder.toString();