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.DOMException;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.Reader;

/**
 * Class encapsulating XML DOM objects.
 * The problem lies in reflection bugs in versions 1.4.x, so
 * we need wrappers enabling useful Java calls from Prova.
 * <p>Title: Prova</p>
 * <p>Description: XML class encapsulating XML DOM objects.
 * The problem lies in reflection bugs in versions 1.4.x, so
 * we need wrappers enabling useful Java calls from Prova.</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 {
	private Document document = null;

	public XML(String filename) throws SAXException, IOException {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder parser = null;
		try {
			parser = factory.newDocumentBuilder();
		} catch (ParserConfigurationException ex) {
			return;
		}
		document = parser.parse(filename);
	}

	/**
	 * Make the wrapped DOM Document available for direct calls.
	 * This method is useful, for example, for XML serialization.
	 * test007B.prova shows how to serialize XML from Prova.
	 * Thanks to Mark Lemke for identifying this issue.
	 *
	 * @return
	 */
	public Document getDocument() {
		return document;
	}

	public XML(Reader r) throws SAXException, IOException {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder parser = null;
		try {
			parser = factory.newDocumentBuilder();
		} catch (ParserConfigurationException ex) {
			return;
		}
		InputSource is = new InputSource(r);
		document = parser.parse(is);
	}

	public XML_Element createElement(String tagName)
			throws DOMException {
		return new XML_Element(document.createElement(tagName));
	}

	public XML_Element createElementNS(String namespaceURI,
									   String qualifiedName)
			throws DOMException {
		return new XML_Element(document.createElementNS(namespaceURI, qualifiedName));
	}

	public XML_Node createAttribute(String name)
			throws DOMException {
		return new XML_Node(document.createAttribute(name));
	}

	public XML_Node createAttributeNS(String namespaceURI,
									  String qualifiedName)
			throws DOMException {
		return new XML_Node(document.createAttributeNS(namespaceURI, namespaceURI));
	}

	public XML_Node createTextNode(String data) {
		return new XML_Node(document.createTextNode(data));
	}

	public XML_Element getDocumentElement() {
		return new XML_Element(document.getDocumentElement());
	}

	public XML_Node importNode(XML_Node importedNode,
							   boolean deep)
			throws DOMException {
		return new XML_Node(document.importNode(importedNode.node, deep));
	}


}
