001: package org.apache.velocity.runtime.parser.node;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.io.IOException;
023: import java.io.Writer;
024:
025: import org.apache.commons.lang.builder.ToStringBuilder;
026: import org.apache.velocity.context.InternalContextAdapter;
027: import org.apache.velocity.exception.MethodInvocationException;
028: import org.apache.velocity.exception.ParseErrorException;
029: import org.apache.velocity.exception.ResourceNotFoundException;
030: import org.apache.velocity.exception.TemplateInitException;
031: import org.apache.velocity.runtime.RuntimeServices;
032: import org.apache.velocity.runtime.log.Log;
033: import org.apache.velocity.runtime.parser.Parser;
034: import org.apache.velocity.runtime.parser.ParserVisitor;
035: import org.apache.velocity.runtime.parser.Token;
036:
037: /**
038: *
039: */
040: public class SimpleNode implements Node {
041: /** */
042: protected RuntimeServices rsvc = null;
043:
044: /** */
045: protected Log log = null;
046:
047: /** */
048: protected Node parent;
049:
050: /** */
051: protected Node[] children;
052:
053: /** */
054: protected int id;
055:
056: /** */
057: protected Parser parser;
058:
059: /** */
060: protected int info; // added
061:
062: /** */
063: public boolean state;
064:
065: /** */
066: protected boolean invalid = false;
067:
068: /** */
069: protected Token first;
070:
071: /** */
072: protected Token last;
073:
074: /**
075: * @param i
076: */
077: public SimpleNode(int i) {
078: id = i;
079: }
080:
081: /**
082: * @param p
083: * @param i
084: */
085: public SimpleNode(Parser p, int i) {
086: this (i);
087: parser = p;
088: }
089:
090: /**
091: * @see org.apache.velocity.runtime.parser.node.Node#jjtOpen()
092: */
093: public void jjtOpen() {
094: first = parser.getToken(1); // added
095: }
096:
097: /**
098: * @see org.apache.velocity.runtime.parser.node.Node#jjtClose()
099: */
100: public void jjtClose() {
101: last = parser.getToken(0); // added
102: }
103:
104: /**
105: * @param t
106: */
107: public void setFirstToken(Token t) {
108: this .first = t;
109: }
110:
111: /**
112: * @see org.apache.velocity.runtime.parser.node.Node#getFirstToken()
113: */
114: public Token getFirstToken() {
115: return first;
116: }
117:
118: /**
119: * @see org.apache.velocity.runtime.parser.node.Node#getLastToken()
120: */
121: public Token getLastToken() {
122: return last;
123: }
124:
125: /**
126: * @see org.apache.velocity.runtime.parser.node.Node#jjtSetParent(org.apache.velocity.runtime.parser.node.Node)
127: */
128: public void jjtSetParent(Node n) {
129: parent = n;
130: }
131:
132: /**
133: * @see org.apache.velocity.runtime.parser.node.Node#jjtGetParent()
134: */
135: public Node jjtGetParent() {
136: return parent;
137: }
138:
139: /**
140: * @see org.apache.velocity.runtime.parser.node.Node#jjtAddChild(org.apache.velocity.runtime.parser.node.Node, int)
141: */
142: public void jjtAddChild(Node n, int i) {
143: if (children == null) {
144: children = new Node[i + 1];
145: } else if (i >= children.length) {
146: Node c[] = new Node[i + 1];
147: System.arraycopy(children, 0, c, 0, children.length);
148: children = c;
149: }
150: children[i] = n;
151: }
152:
153: /**
154: * @see org.apache.velocity.runtime.parser.node.Node#jjtGetChild(int)
155: */
156: public Node jjtGetChild(int i) {
157: return children[i];
158: }
159:
160: /**
161: * @see org.apache.velocity.runtime.parser.node.Node#jjtGetNumChildren()
162: */
163: public int jjtGetNumChildren() {
164: return (children == null) ? 0 : children.length;
165: }
166:
167: /**
168: * @see org.apache.velocity.runtime.parser.node.Node#jjtAccept(org.apache.velocity.runtime.parser.ParserVisitor, java.lang.Object)
169: */
170: public Object jjtAccept(ParserVisitor visitor, Object data) {
171: return visitor.visit(this , data);
172: }
173:
174: /**
175: * @see org.apache.velocity.runtime.parser.node.Node#childrenAccept(org.apache.velocity.runtime.parser.ParserVisitor, java.lang.Object)
176: */
177: public Object childrenAccept(ParserVisitor visitor, Object data) {
178: if (children != null) {
179: for (int i = 0; i < children.length; ++i) {
180: children[i].jjtAccept(visitor, data);
181: }
182: }
183: return data;
184: }
185:
186: /* You can override these two methods in subclasses of SimpleNode to
187: customize the way the node appears when the tree is dumped. If
188: your output uses more than one line you should override
189: toString(String), otherwise overriding toString() is probably all
190: you need to do. */
191:
192: // public String toString()
193: // {
194: // return ParserTreeConstants.jjtNodeName[id];
195: // }
196: /**
197: * @param prefix
198: * @return String representation of this node.
199: */
200: public String toString(String prefix) {
201: return prefix + toString();
202: }
203:
204: /**
205: * Override this method if you want to customize how the node dumps
206: * out its children.
207: *
208: * @param prefix
209: */
210: public void dump(String prefix) {
211: System.out.println(toString(prefix));
212: if (children != null) {
213: for (int i = 0; i < children.length; ++i) {
214: SimpleNode n = (SimpleNode) children[i];
215: if (n != null) {
216: n.dump(prefix + " ");
217: }
218: }
219: }
220: }
221:
222: // All additional methods
223:
224: /**
225: * @see org.apache.velocity.runtime.parser.node.Node#literal()
226: */
227: public String literal() {
228: Token t = first;
229: StringBuffer sb = new StringBuffer(t.image);
230:
231: while (t != last) {
232: t = t.next;
233: sb.append(t.image);
234: }
235:
236: return sb.toString();
237: }
238:
239: /**
240: * @throws TemplateInitException
241: * @see org.apache.velocity.runtime.parser.node.Node#init(org.apache.velocity.context.InternalContextAdapter, java.lang.Object)
242: */
243: public Object init(InternalContextAdapter context, Object data)
244: throws TemplateInitException {
245: /*
246: * hold onto the RuntimeServices
247: */
248:
249: rsvc = (RuntimeServices) data;
250: log = rsvc.getLog();
251:
252: int i, k = jjtGetNumChildren();
253:
254: for (i = 0; i < k; i++) {
255: jjtGetChild(i).init(context, data);
256: }
257:
258: return data;
259: }
260:
261: /**
262: * @see org.apache.velocity.runtime.parser.node.Node#evaluate(org.apache.velocity.context.InternalContextAdapter)
263: */
264: public boolean evaluate(InternalContextAdapter context)
265: throws MethodInvocationException {
266: return false;
267: }
268:
269: /**
270: * @see org.apache.velocity.runtime.parser.node.Node#value(org.apache.velocity.context.InternalContextAdapter)
271: */
272: public Object value(InternalContextAdapter context)
273: throws MethodInvocationException {
274: return null;
275: }
276:
277: /**
278: * @see org.apache.velocity.runtime.parser.node.Node#render(org.apache.velocity.context.InternalContextAdapter, java.io.Writer)
279: */
280: public boolean render(InternalContextAdapter context, Writer writer)
281: throws IOException, MethodInvocationException,
282: ParseErrorException, ResourceNotFoundException {
283: int i, k = jjtGetNumChildren();
284:
285: for (i = 0; i < k; i++)
286: jjtGetChild(i).render(context, writer);
287:
288: return true;
289: }
290:
291: /**
292: * @see org.apache.velocity.runtime.parser.node.Node#execute(java.lang.Object, org.apache.velocity.context.InternalContextAdapter)
293: */
294: public Object execute(Object o, InternalContextAdapter context)
295: throws MethodInvocationException {
296: return null;
297: }
298:
299: /**
300: * @see org.apache.velocity.runtime.parser.node.Node#getType()
301: */
302: public int getType() {
303: return id;
304: }
305:
306: /**
307: * @see org.apache.velocity.runtime.parser.node.Node#setInfo(int)
308: */
309: public void setInfo(int info) {
310: this .info = info;
311: }
312:
313: /**
314: * @see org.apache.velocity.runtime.parser.node.Node#getInfo()
315: */
316: public int getInfo() {
317: return info;
318: }
319:
320: /**
321: * @see org.apache.velocity.runtime.parser.node.Node#setInvalid()
322: */
323: public void setInvalid() {
324: invalid = true;
325: }
326:
327: /**
328: * @see org.apache.velocity.runtime.parser.node.Node#isInvalid()
329: */
330: public boolean isInvalid() {
331: return invalid;
332: }
333:
334: /**
335: * @see org.apache.velocity.runtime.parser.node.Node#getLine()
336: */
337: public int getLine() {
338: return first.beginLine;
339: }
340:
341: /**
342: * @see org.apache.velocity.runtime.parser.node.Node#getColumn()
343: */
344: public int getColumn() {
345: return first.beginColumn;
346: }
347:
348: public String toString() {
349: StringBuffer tokens = new StringBuffer();
350:
351: for (Token t = getFirstToken(); t != null;) {
352: tokens.append("[").append(t.image).append("]");
353: if (t.next != null) {
354: if (t.equals(getLastToken())) {
355: break;
356: } else {
357: tokens.append(", ");
358: }
359: }
360: t = t.next;
361: }
362:
363: return new ToStringBuilder(this ).append("id", getType())
364: .append("info", getInfo()).append("invalid",
365: isInvalid()).append("children",
366: jjtGetNumChildren()).append("tokens", tokens)
367: .toString();
368: }
369: }
|