01: /*
02: * Copyright 2006, 2007 Odysseus Software GmbH
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package de.odysseus.el.tree;
17:
18: import java.io.PrintWriter;
19: import java.util.Stack;
20:
21: /**
22: * Node pretty printer for debugging purposes.
23: *
24: * @author Christoph Beck
25: */
26: public class NodePrinter {
27: private static boolean isLastSibling(Node node, Node parent) {
28: if (parent != null) {
29: return node == parent.getChild(parent.getCardinality() - 1);
30: }
31: return true;
32: }
33:
34: private static void dump(PrintWriter writer, Node node,
35: Stack<Node> predecessors) {
36: if (!predecessors.isEmpty()) {
37: Node parent = null;
38: for (Node predecessor : predecessors) {
39: if (isLastSibling(predecessor, parent)) {
40: writer.print(" ");
41: } else {
42: writer.print("| ");
43: }
44: parent = predecessor;
45: }
46: writer.println("|");
47: }
48: Node parent = null;
49: for (Node predecessor : predecessors) {
50: if (isLastSibling(predecessor, parent)) {
51: writer.print(" ");
52: } else {
53: writer.print("| ");
54: }
55: parent = predecessor;
56: }
57: writer.print("+- ");
58: writer.println(node.toString());
59:
60: predecessors.push(node);
61: for (int i = 0; i < node.getCardinality(); i++) {
62: dump(writer, node.getChild(i), predecessors);
63: }
64: predecessors.pop();
65: }
66:
67: public static void dump(PrintWriter writer, Node node) {
68: dump(writer, node, new Stack<Node>());
69: }
70: }
|