Containerization feature of SO
[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
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;
37
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>>();
42
43     public TreePrinterListener(Parser parser) {
44         this.ruleNames = Arrays.asList(parser.getRuleNames());
45     }
46
47     public TreePrinterListener(List<String> ruleNames) {
48         this.ruleNames = ruleNames;
49     }
50
51     @Override
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 + "'";
56         }
57         stack.get(node.getParent()).add(text);
58     }
59
60     @Override
61     public void visitErrorNode(ErrorNode node) {
62         stack.get(node.getParent()).add(Utils.escapeWhitespace(Trees.getNodeText(node, ruleNames), false));
63     }
64
65     @Override
66     public void enterEveryRule(ParserRuleContext ctx) {
67         if(!stack.containsKey(ctx.parent)){
68                 stack.put(ctx.parent, new ArrayList<String>());
69         }
70         if(!stack.containsKey(ctx)){
71                 stack.put(ctx, new ArrayList<String>());
72         }
73
74         final StringBuilder sb = new StringBuilder();
75         int ruleIndex = ctx.getRuleIndex();
76         String ruleName;
77         if (ruleIndex >= 0 && ruleIndex < ruleNames.size()) {
78             ruleName = ruleNames.get(ruleIndex);
79         }
80         else {
81             ruleName = Integer.toString(ruleIndex);
82         }
83         sb.append(ruleName);
84         stack.get(ctx).add(sb.toString());
85     }
86
87     @Override
88     public void exitEveryRule(ParserRuleContext ctx) {
89         ArrayList<String> ruleStack = stack.remove(ctx);
90         StringBuilder sb = new StringBuilder();
91         boolean brackit =ruleStack.size()>1; 
92         if(brackit){
93                 sb.append("(");
94         }
95         sb.append(ruleStack.get(0));
96         for(int i=1; i<ruleStack.size(); i++){
97                 sb.append(" ");
98                 sb.append(ruleStack.get(i));
99         }
100         if(brackit){
101                 sb.append(")");
102         }
103         if(sb.length() < 80){
104                 stack.get(ctx.parent).add(sb.toString());
105         }else{
106                 // Current line is too long, regenerate it using 1 line per item.
107                 sb.setLength(0);
108                 if(brackit){
109                         sb.append("(");
110                 }
111                 if(!ruleStack.isEmpty()){
112                         sb.append(ruleStack.remove(0)).append("\r\n");
113                 }
114                 while(!ruleStack.isEmpty()){
115                         sb.append(indent(ruleStack.remove(0))).append("\r\n");
116                 }
117                 if(brackit){
118                         sb.append(")");
119                 }
120                 stack.get(ctx.parent).add(sb.toString());
121         }
122         if(ctx.parent == null){
123                 builder.append(sb.toString());
124         }
125     }
126     
127     static String indent(String input){
128         return "  " + input.replaceAll("\r\n(.)","\r\n  $1");
129     }
130
131     @Override
132     public String toString() {
133         return builder.toString();
134     }
135     
136     
137 }