001 /* Generated By:JJTree: Do not edit this line. SimpleNode.java */
002
003 package gate.yam.parse;
004 import gate.util.*;
005
006 /**
007 * Node implementation class mostly generated by JJTree. Altered to
008 * <ul>
009 * <li>
010 * inherit from YamNode (for additional state and behaviour related to
011 * translation)
012 * <li>
013 * print / dump differently
014 * <li>
015 * add getId method
016 * <li>
017 * add addChildren method
018 * </ul>
019 */
020 public class SimpleNode extends YamNode implements Node {
021
022 /** Get the id of this node. */
023 public int getId() { return id; }
024
025 /** Does this node have children? */
026 public boolean hasChildren() { return jjtGetNumChildren() != 0; }
027
028 protected Node parent;
029 protected Node[] children;
030 protected int id;
031 protected YamParser parser;
032
033 public SimpleNode(int i) {
034 id = i;
035 }
036
037 public SimpleNode(YamParser p, int i) {
038 this(i);
039 parser = p;
040 }
041
042 public void jjtOpen() {
043 }
044
045 public void jjtClose() {
046 }
047
048 public void jjtSetParent(Node n) { parent = n; }
049 public Node jjtGetParent() { return parent; }
050
051 public void jjtAddChild(Node n, int i) {
052 if (children == null) {
053 children = new Node[i + 1];
054 } else if (i >= children.length) {
055 Node c[] = new Node[i + 1];
056 System.arraycopy(children, 0, c, 0, children.length);
057 children = c;
058 }
059 children[i] = n;
060 }
061
062 public Node jjtGetChild(int i) {
063 return children[i];
064 }
065
066 public int jjtGetNumChildren() {
067 return (children == null) ? 0 : children.length;
068 }
069
070 /* You can override these two methods in subclasses of SimpleNode to
071 customize the way the node appears when the tree is dumped. If
072 your output uses more than one line you should override
073 toString(String), otherwise overriding toString() is probably all
074 you need to do. */
075
076 public String toString() { return YamParserTreeConstants.jjtNodeName[id]; }
077 public String toString(String prefix) { return prefix + toString(); }
078
079 /* Override this method if you want to customize how the node dumps
080 out its children. */
081 /** Print the node. */
082 public void dump(String prefix) {
083 Out.pr(toString(prefix) + "[");
084 Token t = first;
085 while(t != last) {
086 Out.pr(t.image);
087 t = t.next;
088 }
089 Out.pr(last.image);
090 Out.prln("]");
091 if (children != null) {
092 for (int i = 0; i < children.length; ++i) {
093 SimpleNode n = (SimpleNode)children[i];
094 if (n != null) {
095 n.dump(prefix + " ");
096 }
097 }
098 }
099 } // dump
100
101 /**
102 * Return the image of the node, based on the concatenation of its tokens.
103 */
104 public String getImage() {
105 if(first == null) return last.image;
106 else if(last == null) return first.image;
107 else if(first == last) return first.image;
108
109 StringBuffer image = new StringBuffer();
110 Token t = first;
111 do {
112 image.append(t.image);
113 t = t.next;
114 } while(t != last && t != null);
115 image.append(last.image);
116
117 if (children != null) {
118 for (int i = 0; i < children.length; ++i) {
119 SimpleNode n = (SimpleNode) children[i];
120 if (n != null) {
121 image.append(n.getImage());
122 }
123 }
124 }
125
126 return image.toString();
127 } // getImage()
128
129 /** Add all the children of a node to this node. */
130 public void addChildren(Node other) {
131 int j = this.jjtGetNumChildren();
132 for(int i=0; i<other.jjtGetNumChildren(); i++)
133 this.jjtAddChild((SimpleNode) other.jjtGetChild(i), j++);
134 } // addChildren(Node)
135
136 } // SimpleNode
|