Blame view

src/main/java/eu/omp/irap/vespa/votable/utils/XMLUtils.java 2.7 KB
5a90c3cf   Nathanael Jourdane   Add a generic cla...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
 * 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;
5a90c3cf   Nathanael Jourdane   Add a generic cla...
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

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 {

a36c05d6   Nathanael Jourdane   Fix Sonar issues.
41
42
	private XMLUtils() {
	}
5a90c3cf   Nathanael Jourdane   Add a generic cla...
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

	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);
		}
	}
}