ASTNode.java

package edu.udel.cis.vsl.tass.ast.impl;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import edu.udel.cis.vsl.tass.ast.IF.ASTNodeIF;
import edu.udel.cis.vsl.tass.ast.IF.AbstractSyntaxTreeIF;
import edu.udel.cis.vsl.tass.model.IF.SyntaxException;
import edu.udel.cis.vsl.tass.util.Source;

public abstract class ASTNode implements ASTNodeIF {

	private Source source;
	private long id;
	private ASTNodeIF parent = null;
	private Map<String, Object> annotations = new HashMap<String, Object>();

	public ASTNode(long id) {
		this.id = id;
	}

	@Override
	public Source getSource() {
		return source;
	}

	@Override
	public void setSource(Source source) {
		this.source = source;
	}

	@Override
	public long id() {
		return id;
	}

	@Override
	public ASTNodeIF parent() {
		return parent;
	}

	@Override
	public void setParent(ASTNodeIF parent) {
		this.parent = parent;
	}

	@Override
	public Iterator<ASTNodeIF> children() {
		return new ASTIterator<ASTNodeIF>(this);
	}

	protected String nodeType() {
		return "ASTNode";
	}

	@Override
	public String toString() {
		return " ASTNode " + id() + ", " + nodeType();
	}

	@Override
	public void print(String prefix, PrintWriter out) {
		out.println(prefix + toString());
		Iterator<ASTNodeIF> it = children();
		while (it.hasNext()) {
			ASTNodeIF next = it.next();
			if (next != null) {
				next.print(prefix + "|", out);
			}
		}
	}

	@Override
	public void toXml(String prefix, PrintWriter out, AbstractSyntaxTreeIF ast) {
	}
	
	@Override
	public Map<String, Object> annotations() {
		return annotations;
	}

	@Override
	public Object annotation(String key) {
		return annotations.get(key);
	}

	@Override
	public void setAnnotation(String key, Object value) throws SyntaxException {
		if (key == null) {
			throw new SyntaxException(this, "Annotation key may not be null.");
		} else if (value == null) {
			throw new SyntaxException(this, "Annotation value may not be null.");
		}
		annotations.put(key, value);
	}
}