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.*;

import javax.xml.transform.TransformerException;

/**
 * Class encapsulating XML elements
 * <p>Title: Prova</p>
 * <p>Description: XML class encapsulating XML elements</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_Element extends XML_Node {
//	private Element element;

	public XML_Element(Element element) {
		super(element);
//		this.element = (Element)node;
	}

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

	public String getAttribute(String name) {
		return ((Element) node).getAttribute(name);
	}

	public XML_Attr getAttributeNode(String name) {
		return new XML_Attr(((Element) node).getAttributeNode(name));
	}

	public XML_NodeList getElementsByTagName(String name) {
		NodeList nl = ((Element) node).getElementsByTagName(name);
		return new XML_NodeList(nl);
	}

	public XML_NodeList getElementsByTagNameNS(String namespaceURI, String localName) {
		return new XML_NodeList(((Element) node).getElementsByTagNameNS(namespaceURI, localName));
	}

	public XML_NodeList selectNodeList(String expression) throws TransformerException {
		NodeList nl = null;

//		// Native Java 5.0
//		javax.xml.xpath.XPath xpath = javax.xml.xpath.XPathFactory.newInstance().
//			newXPath();
//		try {
//			nl = (com.sun.org.apache.xpath.internal.NodeSet) xpath.evaluate(
//				expression, node, javax.xml.xpath.XPathConstants.NODESET);
//		}
//		catch (javax.xml.xpath.XPathExpressionException e) {
//		}
		// Java 1.4.2
		// When compiling under Java 5.0 the following fragment requires xalan.jar
		nl = org.apache.xpath.XPathAPI.selectNodeList((Element) node, expression);
		return new XML_NodeList(nl);
	}

	/**
	 * @param expression
	 * @param namespaceNode
	 * @return
	 * @todo Would not honour namespaceNode in Java 5.0
	 */
	public XML_NodeList selectNodeList(String expression, XML_Element namespaceNode) throws TransformerException {
		NodeList nl = null;
//		// Native Java 5.0
//		XPath xpath = XPathFactory.newInstance().newXPath();
//		try {
//			nl = (NodeSet) xpath.evaluate(expression, node, XPathConstants.NODESET);
//		} catch( XPathExpressionException e ) {
//		}
		// Java 1.4.2
		// When compiling under Java 5.0 the following fragment requires xalan.jar
		nl = org.apache.xpath.XPathAPI.selectNodeList((Element) node, expression, namespaceNode.node);
		return new XML_NodeList(nl);
	}

	public String getTagName() {
		return ((Element) node).getTagName();
	}

	public boolean hasAttribute(String name) {
		return ((Element) node).hasAttribute(name);
	}

	public String getAttributeNS(String namespaceURI,
								 String localName) {
		return ((Element) node).getAttributeNS(namespaceURI, localName);
	}

	public XML_NamedNodeMap getAttributes() {
		NamedNodeMap nm = ((Element) node).getAttributes();
		return new XML_NamedNodeMap(nm);
	}

	public void setAttribute(String name,
							 Object value)
			throws DOMException {
		((Element) node).setAttribute(name, value.toString());
	}

	public void removeAttribute(String name)
			throws DOMException {
		((Element) node).removeAttribute(name);

	}

	public XML_Node setAttributeNode(XML_Node newAttr)
			throws DOMException {
		return new XML_Node(((Element) node).setAttributeNode((Attr) (newAttr.node)));
	}

}
