/*
 * This file is a part of EpnTAPClient.
 * This program aims to provide EPN-TAP support for software clients, like CASSIS spectrum analyzer.
 * See draft specifications: https://voparis-confluence.obspm.fr/pages/viewpage.action?pageId=559861
 * Copyright (C) 2016 Institut de Recherche en Astrophysique et Planétologie.
 *
 * This program is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or (at your option) any later
 * version. This program 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 General Public License for more details. You should have received a copy of
 * the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */

package eu.omp.irap.vespa.votable.utils;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

/**
 * @author N. Jourdane
 */
public class XmlUtils {

	/** Private constructor to hide the implicit public one. */
	private XmlUtils() {
	}

	/**
	 * Update the attributes of the root node of an XML file, and overwrite the old attributes.
	 *
	 * @param xmlPath The path of the XML file to modify.
	 * @param attributes A map representing the new attributes, as <name, value>.
	 * @throws IOException If th XML file can not be read or writed.
	 */
	public static void changeRootAttributes(String xmlPath, Map<String, String> attributes)
			throws IOException {
		try {
			Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(xmlPath);
			NamedNodeMap votAttrs = doc.getFirstChild().getAttributes();

			for (Map.Entry<String, String> attr : attributes.entrySet()) {
				Node attNode = doc.createAttribute(attr.getKey());
				attNode.setNodeValue(attr.getValue());
				votAttrs.setNamedItem(attNode);
			}

			Transformer transformer = TransformerFactory.newInstance().newTransformer();
			StreamResult result = new StreamResult(new File(xmlPath));
			transformer.transform(new DOMSource(doc), result);

		} catch (ParserConfigurationException pce) {
			throw new IOException("Configuration error when parsing XML file " + xmlPath, pce);
		} catch (TransformerException tfe) {
			throw new IOException("Can not change VOTable version in XML file " + xmlPath, tfe);
		} catch (IOException ioe) {
			throw new IOException("Can read/write the XML file " + xmlPath, ioe);
		} catch (SAXException sae) {
			throw new IOException("Can not parse the XML file " + xmlPath, sae);
		}
	}
}
