VOTableParser.java
3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* 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.controller;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import eu.omp.irap.vespa.epntapclient.votable.model.VOTABLE;
import eu.omp.irap.vespa.votable.utils.XMLUtils;
/**
* @author N. Jourdane
*/
public final class VOTableParser {
/** The path of the VOTable to verify the VOTable XML file. */
private static final String VOTABLE_SHEMA = "http://www.ivoa.net/xml/VOTable/v";
/**
* The version of the VOTable with which VOTables will be parsed with JAXB (ie. the last
* published VOTable version by IVOA).
*/
private static final String REQUIRED_VOTABLE_VERSION = "1.3";
/** The VOTable package where are the generated .java which represents the VOTable model. */
private static final String VOTABLE_MODEL_PACKAGE = "eu.omp.irap.vespa.epntapclient.votable.model";
/** Constructor to hide the implicit public one. */
private VOTableParser() {
}
/**
* @param voTablePath The path of the VOTable.
* @return The VOTable resulting of the XML document.
* @throws CantDisplayVOTableException VOTable is not valid or not writable.
*/
public static VOTABLE parseVOTable(String voTablePath) throws CantGetVOTableException {
VOTABLE voTable;
JAXBContext jc;
try {
changeVOTableSchemaLocation(voTablePath);
} catch (IOException e) {
throw new CantGetVOTableException("Can not modify the VOTable XML file " + voTablePath,
e);
}
try {
jc = JAXBContext.newInstance(VOTableParser.VOTABLE_MODEL_PACKAGE);
Unmarshaller unmarshaller = jc.createUnmarshaller();
voTable = (VOTABLE) unmarshaller.unmarshal(new File(voTablePath));
} catch (JAXBException e) {
throw new CantGetVOTableException("Can not get a VOTable object from the XML file.", e);
}
return voTable;
}
/**
* Change the version of the VOTable to the last supported version (1.3 as 2016/02/05). This
* allows JAXB to parse the VOTable even it's not the same namespace than the XSD. Because
* VOTables schemas are retro-compatibles, there is no problem to parse a VOTable with a XML
* schema with a higher version. In the same way, this method check if the file is a valid XML.
*
* @param voTablePath The path of the VOTable XML file.
* @throws IOException Can not read or write the XML file.
*/
private static void changeVOTableSchemaLocation(String voTablePath) throws IOException {
Map<String, String> attributes = new HashMap<>();
attributes.put("xmlns", VOTABLE_SHEMA + REQUIRED_VOTABLE_VERSION);
XMLUtils.changeRootAttributes(voTablePath, attributes);
}
}