VOTableController.java
5.4 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* 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.util.SortedMap;
import java.util.TreeMap;
import java.util.logging.Logger;
import eu.omp.irap.vespa.epntapclient.votable.model.Info;
import eu.omp.irap.vespa.epntapclient.votable.model.Table;
import eu.omp.irap.vespa.epntapclient.votable.model.VOTABLE;
import eu.omp.irap.vespa.votable.Consts;
import eu.omp.irap.vespa.votable.utils.CantSendQueryException;
import eu.omp.irap.vespa.votable.utils.Network;
import eu.omp.irap.vespa.votable.view.VOTableViewListener;
import eu.omp.irap.vespa.votable.votabledata.VOTableData;
import eu.omp.irap.vespa.votable.votabledata.VOTableDataParser;
/**
* @author N. Jourdane
*/
public class VOTableController implements VOTableViewListener {
/** The logger for the class VOTableController. */
private static final Logger logger = Logger.getLogger(VOTableController.class.getName());
/** The VOTable model */
private VOTABLE voTable;
protected String voTablePath;
protected String targetURL;
protected String query;
private VOTableData voTableData;
/**
* Constructor of VOTableController
*/
public VOTableController() {
/* Subclasses initializes their own attributes, we don't want to initialize them twice. */
}
public VOTableController(String voTablePath) {
this.voTablePath = voTablePath;
}
/**
* Method constructor
*
* @param targetURL The URL of the registry to communicate (ie. "http://reg.g-vo.org").
* @param queryLanguage The language used for the queries (ie. "ADQL").
* @param query The query to ask to the registry.
*/
public VOTableController(String targetURL, String query) {
voTablePath = null;
this.targetURL = targetURL;
this.query = query;
}
public void updateVOTable(String voTablePath) throws CantGetVOTableException {
this.voTablePath = voTablePath;
readTable();
}
public void updateVOTable(String targetURL, String query) throws CantGetVOTableException {
voTablePath = null;
this.targetURL = targetURL;
this.query = query;
readTable();
}
/**
* @param voTablePath The path of the VOTable file.
* @throws CantDisplayVOTableException If the VOTable can not be filled.
* @throws CantSendQueryException
*/
public void readTable() throws CantGetVOTableException {
if (voTablePath == null) {
voTablePath = VOTableController.downloadVOTable(targetURL, query);
}
voTable = VOTableParser.parseVOTable(voTablePath);
checkVOTable(voTable);
Table table = (Table) voTable.getRESOURCE().get(0).getLINKAndTABLEOrRESOURCE().get(0);
VOTableDataParser dataCtrl;
dataCtrl = new VOTableDataParser(table);
dataCtrl.parseData();
voTableData = dataCtrl.getData();
}
public static void checkVOTable(VOTABLE voTable) throws CantGetVOTableException {
if (voTable.getRESOURCE().isEmpty()) {
throw new CantGetVOTableException("The VOTable do not have any resource. "
+ "See the local file for more informations.");
}
if (voTable.getRESOURCE().size() > 1) {
throw new CantGetVOTableException(
"VOTable with more than one resource are not yet supported.");
}
for (Info info : voTable.getRESOURCE().get(0).getINFO()) {
if ("ERROR".equals(info.getValueAttribute())) {
throw new CantGetVOTableException("There is an error in the VOTable:\n"
+ info.getValue() + "\nPlease check your ADQL query. ");
}
}
if (voTable.getRESOURCE().get(0).getLINKAndTABLEOrRESOURCE().size() > 1) {
throw new CantGetVOTableException(
"VOTable with more than one table are not yet supported. "
+ "See the local file for more informations.");
}
}
public static String downloadVOTable(String targetURL, String query)
throws CantGetVOTableException {
String url = targetURL + "/sync";
SortedMap<String, String> parameters = new TreeMap<>();
parameters.put("REQUEST", Consts.QUERY_REQUEST);
parameters.put("LANG", Consts.QUERY_LANG);
parameters.put("FORMAT", Consts.QUERY_FORMAT);
parameters.put("QUERY", query);
String fullURL = Network.buildQuery(url, parameters);
VOTableController.logger.info("Sending query '" + fullURL + "'");
String voTablePath;
try {
voTablePath = Network.saveQuery(fullURL);
} catch (CantSendQueryException e) {
throw new CantGetVOTableException("Can not get the VOTable on " + url, e);
}
VOTableController.logger.info("VOTable downloaded in " + voTablePath);
return voTablePath;
}
public VOTableData getVOTableData() {
return voTableData;
}
public VOTABLE getVOTable() {
return voTable;
}
public String getVOTablePath() {
return voTablePath;
}
}