01: /*
02: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2006.
03: *
04: * Licensed under the Aduna BSD-style license.
05: */
06: package org.openrdf.query.algebra.helpers;
07:
08: import org.openrdf.query.algebra.QueryModelNode;
09:
10: /**
11: * QueryModelVisitor implementation that "prints" a tree representation of a
12: * query model. The tree representations is printed to an internal character
13: * buffer and can be retrieved using {@link #getTreeString()}. As an
14: * alternative, the static utility method {@link #printTree(QueryModelNode)} can
15: * be used.
16: */
17: public class QueryModelTreePrinter extends
18: QueryModelVisitorBase<RuntimeException> {
19:
20: /*-----------*
21: * Constants *
22: *-----------*/
23:
24: private static final String LINE_SEPARATOR = System
25: .getProperty("line.separator");
26:
27: /*-----------*
28: * Constants *
29: *-----------*/
30:
31: public static String printTree(QueryModelNode node) {
32: QueryModelTreePrinter treePrinter = new QueryModelTreePrinter();
33: node.visit(treePrinter);
34: return treePrinter.getTreeString();
35: }
36:
37: /*-----------*
38: * Variables *
39: *-----------*/
40:
41: private String indentString = " ";
42:
43: private StringBuilder buf;
44:
45: private int indentLevel = 0;
46:
47: /*--------------*
48: * Constructors *
49: *--------------*/
50:
51: public QueryModelTreePrinter() {
52: buf = new StringBuilder(256);
53: }
54:
55: /*---------*
56: * Methods *
57: *---------*/
58:
59: public String getTreeString() {
60: return buf.toString();
61: }
62:
63: @Override
64: protected void meetNode(QueryModelNode node) {
65: for (int i = 0; i < indentLevel; i++) {
66: buf.append(indentString);
67: }
68:
69: buf.append(node.getSignature());
70: buf.append(LINE_SEPARATOR);
71:
72: indentLevel++;
73:
74: super.meetNode(node);
75:
76: indentLevel--;
77: }
78: }
|