001 /*
002 * YamParseTree.java
003 * Copyright (c) 1998-2008, The University of Sheffield.
004 *
005 * This code is from the GATE project (http://gate.ac.uk/) and is free
006 * software licenced under the GNU General Public License version 3. It is
007 * distributed without any warranty. For more details see COPYING.txt in the
008 * top level directory (or at http://gatewiki.sf.net/COPYING.txt).
009 *
010 * Hamish Cunningham, 7th May 2006
011 */
012
013 package gate.yam.parse;
014 import java.util.*;
015 import gate.util.*;
016
017 /**
018 * The parse tree and related state created by parsing a YAM file.
019 * Forms the input to YAM translators.
020 * @author Hamish Cunningham
021 */
022 public class YamParseTree {
023 /** Construction. */
024 public YamParseTree() { }
025
026 /** The root node of the tree. */
027 SimpleNode rootNode;
028
029 /** Get the root node of the tree. */
030 public SimpleNode getRootNode() { return rootNode; }
031
032 /** Set the root node of the tree. */
033 public void setRootNode(Node rootNode) {
034 this.rootNode = (SimpleNode) rootNode;
035 } // setRootNode(Node)
036
037 /** The title node of the tree. Null if there is no title node. */
038 SimpleNode titleNode = null;
039
040 /** Get the title node of the tree. */
041 public SimpleNode getTitleNode() { return titleNode; }
042
043 /** Set the title node of the tree. */
044 public void setTitleNode(Node titleNode) {
045 this.titleNode = (SimpleNode) titleNode;
046 } // setTitleNode(Node)
047
048 /** The contents node of the tree. Null if there is no contents node. */
049 SimpleNode contentsNode = null;
050
051 /** Get the contents node of the tree. */
052 public SimpleNode getContentsNode() { return contentsNode; }
053
054 /** Set the contents node of the tree. */
055 public void setContentsNode(Node contentsNode) {
056 this.contentsNode = (SimpleNode) contentsNode;
057 } // setContentsNode(Node)
058
059 /** The heading nodes of the tree. Never null. */
060 List headingNodes = new LinkedList();
061
062 /** Get the list of heading nodes. */
063 public List getHeadingNodes() { return headingNodes; }
064
065 /** Add a heading node. */
066 public void addHeadingNode(Node headingNode) {
067 headingNodes.add(headingNode);
068 }
069
070 /** A list of errors encountered during parsing. */
071 List errors = new ArrayList();
072
073 /** Get the list of errors encountered during parsing. */
074 public List getErrors() { return errors; }
075
076 /** A list of warnings encountered during parsing. */
077 List warnings = new ArrayList();
078
079 /** Get the list of warnings encountered during parsing. */
080 public List getWarnings() { return warnings; }
081
082 /** Reset the output strings on all nodes in the tree. */
083 public void reset() {
084 reset(rootNode);
085 } // reset()
086
087 /** Recursive helper method for {@link reset()}. */
088 void reset(SimpleNode n) {
089 n.reset();
090 for(int i = n.jjtGetNumChildren(); i > 0; )
091 reset((SimpleNode) n.jjtGetChild(--i));
092 } // reset(SimpleNode)
093
094 /** Count the number of nodes in a (sub)tree. */
095 public int getNumNodes(SimpleNode node) {
096 return getNumNodes(0, node);
097 } // getNumNodes(SimpleNode)
098
099 /** Helper to count the number of nodes in a (sub)tree. */
100 int getNumNodes(int n, SimpleNode node) {
101 n++;
102 for(int i=0; i<node.jjtGetNumChildren(); i++)
103 n += getNumNodes((SimpleNode) node.jjtGetChild(i));
104
105 return n;
106 } // getNumNodes(int, SimpleNode)
107
108 /**
109 * Merge another tree with this one. It is assumed that the other tree is
110 * subsequent to this one.
111 */
112 public void merge(YamParseTree other) {
113 headingNodes.addAll(other.headingNodes);
114 rootNode.addChildren(other.rootNode);
115 errors.addAll(other.errors);
116 warnings.addAll(other.warnings);
117 links.addAll(other.links);
118 includes.addAll(other.includes);
119 } // merge(YamParseTree)
120
121 /** A list of links to other local files made from the YAM source. */
122 List<String> links = new ArrayList<String>();
123
124 /**
125 * Get the list of links to other local files made from the YAM source.
126 * These will always be relative links.
127 */
128 public List<String> getLinks() { return links; }
129
130 /** A list of includes of other local files made in the YAM source. */
131 List<String> includes = new ArrayList<String>();
132
133 /** Get the list of includes of other local files made in the YAM source. */
134 public List<String> getIncludes() { return includes; }
135
136 } // YamParseTree
|