package ws.prova;

/*
 * Copyright (C) 2002-2007 <a href="mailto:a.kozlenkov@city.ac.uk">Alex Kozlenkov</a>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import org.w3c.dom.Attr;
import org.w3c.dom.DOMException;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 * Class encapsulating XML Node
 * <p>Title: Prova</p>
 * <p>Description: Class encapsulating XML nodes</p>
 * <p>Copyright: Copyright (C) 2002-2007</p>
 * <p>Company: City University, London, United Kingdom</p>
 *
 * @author Alex Kozlenkov
 * @version 2.0 <25 March 2007>
 */

public class XML_Node {
	protected Node node;

	public XML_Node(Node node) {
		this.node = node;
	}

	public static XML_Node createNode(Node node) {
		if (node == null) {
			return null;
		}
		if (node instanceof Element) {
			return new XML_Element((Element) node);
		} else if (node instanceof Attr) {
			return new XML_Attr((Attr) node);
		}
		return new XML_Node(node);
	}

	public String toString() {
		return node.toString();
	}

// Difficult so far
//	public XML getOwnerDocument() {
//		return node.getOwnerDocument();
//
//	}

	public XML_Node appendChild(XML_Node newChild)
			throws DOMException {
		Node n = node.appendChild(newChild.node);
		return createNode(n);
//		if( n instanceof Element ) {
//			return new XML_Element( (Element)n );
//		}
//		return new XML_Node( n );
	}

	public XML_Node cloneNode(boolean deep) {
		Node n = node.cloneNode(deep);
		return createNode(n);
//		if( n instanceof Element ) {
//			return new XML_Element( (Element)n );
//		}
//		return new XML_Node( n );
	}

	public String getLocalName() {
		return node.getLocalName();
	}

	public String getNodeName() {
		return node.getNodeName();
	}

	public String getNodeValue() {
		return node.getNodeValue();
	}

	public void setNodeValue(String nodeValue)
			throws DOMException {
		node.setNodeValue(nodeValue);
	}

	public short getNodeType() {
		return node.getNodeType();
	}

	public String getNamespaceURI() {
		return node.getNamespaceURI();
	}

	public String getPrefix() {
		return node.getPrefix();
	}

	public void setPrefix(String prefix)
			throws DOMException {
		node.setPrefix(prefix);
	}

	public boolean isSupported(String feature,
							   String version) {
		return node.isSupported(feature, version);
	}

	public void normalize() {
		node.normalize();
	}

	public boolean hasAttributes() {
		return node.hasAttributes();
	}

	public boolean hasChildNodes() {
		return node.hasChildNodes();
	}

	public XML_NodeList getChildNodes() {
		return new XML_NodeList(node.getChildNodes());
	}

	public XML_Node getFirstChild() {
		Node n = node.getFirstChild();
		return createNode(n);
//		if( n instanceof Element ) {
//			return new XML_Element( (Element)n );
//		}
//		return new XML_Node( n );
	}

	public XML_Node getLastChild() {
		Node n = node.getLastChild();
		return createNode(n);
//		if( n instanceof Element ) {
//			return new XML_Element( (Element)n );
//		}
//		return new XML_Node( n );
	}

	public XML_Node getPreviousSibling() {
		Node n = node.getPreviousSibling();
		return createNode(n);
//		if( n instanceof Element ) {
//			return new XML_Element( (Element)n );
//		}
//		return new XML_Node( n );
	}

	public XML_Node getNextSibling() {
		Node n = node.getNextSibling();
		return createNode(n);
//		if( n instanceof Element ) {
//			return new XML_Element( (Element)n );
//		}
//		return new XML_Node( n );
	}

	public XML_Node getParentNode() {
		Node n = node.getParentNode();
		return createNode(n);
//		if( n instanceof Element ) {
//			return new XML_Element( (Element)n );
//		}
//		return new XML_Node( n );
	}

//	public String getTagName() {
//		if( node.getNodeType()==Node.ELEMENT_NODE ) {
//			return ((Element)node).getTagName();
//		}
//		return null;
//	}

	public XML_NamedNodeMap getAttributes() {
		return new XML_NamedNodeMap(node.getAttributes());
	}

//	public XML_NodeList getElementsByTagName( String name ) {
//		if( node.getNodeType()==Node.ELEMENT_NODE ) {
//			NodeList nl = ((Element)node).getElementsByTagName( name );
//			return new XML_NodeList(nl);
//		}
//		return null;
//	}

//	public String getAttributeNS(String namespaceURI,
//										  String localName) {
//		if( node.getNodeType()==Node.ELEMENT_NODE ) {
//			return ((Element)node).getAttributeNS(namespaceURI,localName);
//		}
//		return null;
//	}


}
