001: /* ****************************************************************************
002: * SimpleNode.java
003: * ****************************************************************************/
004:
005: /* J_LZ_COPYRIGHT_BEGIN *******************************************************
006: * Copyright 2001-2006 Laszlo Systems, Inc. All Rights Reserved. *
007: * Use is subject to license terms. *
008: * J_LZ_COPYRIGHT_END *********************************************************/
009:
010: package org.openlaszlo.sc.parser;
011:
012: import java.io.Serializable;
013:
014: public abstract class SimpleNode implements Node, Serializable {
015: protected final static SimpleNode[] noChildren = {};
016: protected Node parent;
017: protected SimpleNode[] children = noChildren;
018: protected int id;
019: protected Parser parser;
020: public String filename; // added
021: public int beginLine, beginColumn; // added
022: public String comment;
023:
024: public SimpleNode(int i) {
025: id = i;
026: }
027:
028: public SimpleNode(Parser p, int i) {
029: this (i);
030: parser = p;
031: }
032:
033: public void jjtOpen() {
034: }
035:
036: public void jjtClose() {
037: }
038:
039: public void jjtSetParent(Node n) {
040: parent = n;
041: }
042:
043: public Node jjtGetParent() {
044: return parent;
045: }
046:
047: public void jjtAddChild(Node n, int i) {
048: if (i >= children.length) {
049: SimpleNode c[] = new SimpleNode[i + 1];
050: System.arraycopy(children, 0, c, 0, children.length);
051: children = c;
052: }
053: children[i] = (SimpleNode) n;
054: }
055:
056: public Node jjtGetChild(int i) {
057: return children[i];
058: }
059:
060: public int jjtGetNumChildren() {
061: return (children == null) ? 0 : children.length;
062: }
063:
064: /* You can override these two methods in subclasses of SimpleNode to
065: customize the way the node appears when the tree is dumped. If
066: your output uses more than one line you should override
067: toString(String), otherwise overriding toString() is probably all
068: you need to do. */
069:
070: public String toString(String prefix) {
071: return prefix + toString();
072: }
073:
074: /* Override this method if you want to customize how the node dumps
075: out its children. */
076:
077: public void dump(String prefix) {
078: System.out.println(toString(prefix));
079: if (children != null) {
080: for (int i = 0; i < children.length; ++i) {
081: SimpleNode n = (SimpleNode) children[i];
082: if (n != null) {
083: n.dump(prefix + " ");
084: }
085: }
086: }
087: }
088:
089: public void dump() {
090: dump("");
091: }
092:
093: // Added
094:
095: public SimpleNode() {
096: this (0);
097: }
098:
099: public String toString() {
100: String name = this .getClass().getName();
101: if (name.lastIndexOf('.') >= 0) {
102: name = name.substring(name.lastIndexOf('.') + 1);
103: }
104: return name;
105: }
106:
107: public SimpleNode[] getChildren() {
108: return children;
109: }
110:
111: public void setChildren(SimpleNode[] children) {
112: this .children = children;
113: }
114:
115: // Jython interfaces
116: public Node __getitem__(int n) {
117: return jjtGetChild(n);
118: }
119:
120: public int __len__() {
121: return jjtGetNumChildren();
122: }
123:
124: public boolean __nonzero__() {
125: return true;
126: }
127:
128: // Java interfaces
129: public SimpleNode get(int n) {
130: return children[n];
131: }
132:
133: public SimpleNode set(int n, SimpleNode v) {
134: SimpleNode old = n < children.length ? get(n) : null;
135: jjtAddChild(v, n);
136: return old;
137: }
138:
139: public int size() {
140: return jjtGetNumChildren();
141: }
142:
143: //public void setLineNumber(int line) {
144: // this.beginLine = line;
145: //}
146:
147: public int getLineNumber() {
148: return beginLine;
149: }
150:
151: public int getColumnNumber() {
152: return beginColumn;
153: }
154:
155: public String getFilename() {
156: return filename;
157: }
158:
159: public void setBeginLocation(String filename, int line, int column) {
160: this .filename = filename;
161: this .beginLine = line;
162: this .beginColumn = column;
163: }
164:
165: public void setComment(String comment) {
166: this .comment = comment;
167: }
168:
169: public String getComment() {
170: return this.comment;
171: }
172: }
|