01 /*
02 * TreeTranslator.java
03 * Copyright (c) 1998-2008, The University of Sheffield.
04 *
05 * This code is from the GATE project (http://gate.ac.uk/) and is free
06 * software licenced under the GNU General Public License version 3. It is
07 * distributed without any warranty. For more details see COPYING.txt in the
08 * top level directory (or at http://gatewiki.sf.net/COPYING.txt).
09 *
10 * Hamish Cunningham 8th May 2006
11 */
12
13 package gate.yam.translate;
14
15 import java.util.*;
16 import java.io.*;
17 import gate.util.*;
18 import gate.yam.parse.*;
19
20 /**
21 * This class prints the parse tree for a YAM document. It is intended for
22 * testing and debugging.
23 * @author Hamish Cunningham
24 */
25 public class TreeTranslator extends AbstractTranslator {
26
27 /** Construction. */
28 public TreeTranslator() {
29 } // TreeTranslator()
30
31 /** Get the path to the preamble resource. */
32 public String getPreamblePath() {
33 return "/gate/yam/translate/preamble.xhtml";
34 }
35
36 /** Array mapping node type name to start/end strings. Unused for trees. */
37 public String[][] getConstantsTable() { return null; }
38
39 /** Array mapping predicate type name to attributes. Unused for trees. */
40 public Object[][] getPredicatesTable() { return null; }
41
42 /**
43 * Translation. Results are written to the Writer.
44 */
45 public Writer translate() throws GateException {
46 SimpleNode rootNode = parseTree.getRootNode();
47 traverse("", rootNode);
48 return writer;
49 } // translate()
50
51 /** Traverse a subtree. */
52 public void traverse(String prefix, SimpleNode node) throws GateException {
53 StringBuffer line = new StringBuffer();
54
55 line.append(node.toString(prefix) + "[");
56 Token first = node.getFirstToken();
57 Token last = node.getLastToken();
58 while(first != last && first != null && last != null) {
59 line.append(subNewlines(first.image));
60 first = first.next;
61 }
62 if(last != null) line.append(subNewlines(last.image));
63 line.append("]");
64 prln(elide(line.toString()));
65
66 for(int i = 0, j = node.jjtGetNumChildren(); i < j; i++)
67 traverse(prefix + "-", (SimpleNode) node.jjtGetChild(i));
68
69 /*
70 if (node.getChildren() != null) {
71 for (int i = 0; i < node.getChildren().length; ++i) {
72 SimpleNode n = (SimpleNode)node.getChildren()[i];
73 if (n != null) {
74 traverse(prefix + "-", n);
75 }
76 }
77 }
78 */
79 } // traverse(String, SimpleNode)
80
81 /** Process URLs and Anchors. */
82 public void processURLs(SimpleNode node) throws GateException {
83 // intentionally empty
84 } // processURLs(SimpleNode)
85
86 } // TreeTranslator
|