diff options
Diffstat (limited to 'asdc-controller/src/test/java/org/onap/so/asdc/utils/TreePrinterListener.java')
-rw-r--r-- | asdc-controller/src/test/java/org/onap/so/asdc/utils/TreePrinterListener.java | 112 |
1 files changed, 55 insertions, 57 deletions
diff --git a/asdc-controller/src/test/java/org/onap/so/asdc/utils/TreePrinterListener.java b/asdc-controller/src/test/java/org/onap/so/asdc/utils/TreePrinterListener.java index c4a84c0540..39119c7dca 100644 --- a/asdc-controller/src/test/java/org/onap/so/asdc/utils/TreePrinterListener.java +++ b/asdc-controller/src/test/java/org/onap/so/asdc/utils/TreePrinterListener.java @@ -25,7 +25,6 @@ import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; - import org.antlr.v4.runtime.Parser; import org.antlr.v4.runtime.ParserRuleContext; import org.antlr.v4.runtime.RuleContext; @@ -38,7 +37,7 @@ import org.antlr.v4.runtime.tree.Trees; public class TreePrinterListener implements ParseTreeListener { private final List<String> ruleNames; private final StringBuilder builder = new StringBuilder(); - Map<RuleContext,ArrayList<String>> stack = new HashMap<RuleContext,ArrayList<String>>(); + Map<RuleContext, ArrayList<String>> stack = new HashMap<RuleContext, ArrayList<String>>(); public TreePrinterListener(Parser parser) { this.ruleNames = Arrays.asList(parser.getRuleNames()); @@ -50,10 +49,10 @@ public class TreePrinterListener implements ParseTreeListener { @Override public void visitTerminal(TerminalNode node) { - String text = Utils.escapeWhitespace(Trees.getNodeText(node, ruleNames), false); - if(text.startsWith(" ") || text.endsWith(" ")){ - text = "'" + text + "'"; - } + String text = Utils.escapeWhitespace(Trees.getNodeText(node, ruleNames), false); + if (text.startsWith(" ") || text.endsWith(" ")) { + text = "'" + text + "'"; + } stack.get(node.getParent()).add(text); } @@ -64,20 +63,19 @@ public class TreePrinterListener implements ParseTreeListener { @Override public void enterEveryRule(ParserRuleContext ctx) { - if(!stack.containsKey(ctx.parent)){ - stack.put(ctx.parent, new ArrayList<String>()); - } - if(!stack.containsKey(ctx)){ - stack.put(ctx, new ArrayList<String>()); - } + if (!stack.containsKey(ctx.parent)) { + stack.put(ctx.parent, new ArrayList<String>()); + } + if (!stack.containsKey(ctx)) { + stack.put(ctx, new ArrayList<String>()); + } - final StringBuilder sb = new StringBuilder(); - int ruleIndex = ctx.getRuleIndex(); + final StringBuilder sb = new StringBuilder(); + int ruleIndex = ctx.getRuleIndex(); String ruleName; if (ruleIndex >= 0 && ruleIndex < ruleNames.size()) { ruleName = ruleNames.get(ruleIndex); - } - else { + } else { ruleName = Integer.toString(ruleIndex); } sb.append(ruleName); @@ -86,52 +84,52 @@ public class TreePrinterListener implements ParseTreeListener { @Override public void exitEveryRule(ParserRuleContext ctx) { - ArrayList<String> ruleStack = stack.remove(ctx); - StringBuilder sb = new StringBuilder(); - boolean brackit =ruleStack.size()>1; - if(brackit){ - sb.append("("); - } - sb.append(ruleStack.get(0)); - for(int i=1; i<ruleStack.size(); i++){ - sb.append(" "); - sb.append(ruleStack.get(i)); - } - if(brackit){ - sb.append(")"); - } - if(sb.length() < 80){ - stack.get(ctx.parent).add(sb.toString()); - }else{ - // Current line is too long, regenerate it using 1 line per item. - sb.setLength(0); - if(brackit){ - sb.append("("); - } - if(!ruleStack.isEmpty()){ - sb.append(ruleStack.remove(0)).append("\r\n"); - } - while(!ruleStack.isEmpty()){ - sb.append(indent(ruleStack.remove(0))).append("\r\n"); - } - if(brackit){ - sb.append(")"); - } - stack.get(ctx.parent).add(sb.toString()); - } - if(ctx.parent == null){ - builder.append(sb.toString()); - } + ArrayList<String> ruleStack = stack.remove(ctx); + StringBuilder sb = new StringBuilder(); + boolean brackit = ruleStack.size() > 1; + if (brackit) { + sb.append("("); + } + sb.append(ruleStack.get(0)); + for (int i = 1; i < ruleStack.size(); i++) { + sb.append(" "); + sb.append(ruleStack.get(i)); + } + if (brackit) { + sb.append(")"); + } + if (sb.length() < 80) { + stack.get(ctx.parent).add(sb.toString()); + } else { + // Current line is too long, regenerate it using 1 line per item. + sb.setLength(0); + if (brackit) { + sb.append("("); + } + if (!ruleStack.isEmpty()) { + sb.append(ruleStack.remove(0)).append("\r\n"); + } + while (!ruleStack.isEmpty()) { + sb.append(indent(ruleStack.remove(0))).append("\r\n"); + } + if (brackit) { + sb.append(")"); + } + stack.get(ctx.parent).add(sb.toString()); + } + if (ctx.parent == null) { + builder.append(sb.toString()); + } } - - static String indent(String input){ - return " " + input.replaceAll("\r\n(.)","\r\n $1"); + + static String indent(String input) { + return " " + input.replaceAll("\r\n(.)", "\r\n $1"); } @Override public String toString() { return builder.toString(); } - - + + } |