From 5b999f8fe759252318a697ac4a4d700cb51b175e Mon Sep 17 00:00:00 2001 From: Nathanael Jourdane Date: Thu, 12 May 2016 17:13:34 +0200 Subject: [PATCH] Fix #12 --- src/main/java/eu/omp/irap/vespa/epntapclient/EpnTapGet.java | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main/java/eu/omp/irap/vespa/epntapclient/gui/ParamField.java | 36 ++---------------------------------- 2 files changed, 66 insertions(+), 34 deletions(-) create mode 100644 src/main/java/eu/omp/irap/vespa/epntapclient/EpnTapGet.java diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/EpnTapGet.java b/src/main/java/eu/omp/irap/vespa/epntapclient/EpnTapGet.java new file mode 100644 index 0000000..8ebe2c4 --- /dev/null +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/EpnTapGet.java @@ -0,0 +1,64 @@ +/* + * 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 + * . + */ + +package eu.omp.irap.vespa.epntapclient; + +import java.util.HashMap; +import java.util.Map; +import java.util.logging.Logger; + +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; + +import eu.omp.irap.vespa.votable.utils.CantSendQueryException; +import eu.omp.irap.vespa.votable.utils.Network; + +/** + * @author N. Jourdane + */ +public class EpnTapGet { + + /** The logger for the class EpnTapGetQueries. */ + private static final Logger logger = Logger.getLogger(EpnTapGet.class.getName()); + + /** The URL of the resolver used for the `target name` field. */ + private static final String RESOLVER_URL = "http://voparis-registry.obspm.fr/ssodnet/1/autocomplete"; + + + /** + * The method used to get target names propositions by asking to the resolver. + * + * @param begining The beginning of the target_name. + * @return An array of Strings corresponding to the target names got. + * @throws CantSendQueryException + * @throws CantGetXMLException If the resolver do not work. + */ + public static String[] getTargetNames(String begining) throws CantSendQueryException { + Map params = new HashMap<>(); + params.put("q", "\"" + begining + "\""); + + String query = Network.buildQuery(RESOLVER_URL, params); + JsonObject root = Network.readJson(query); + int count = Integer.parseInt(root.get("count").toString()); + String[] targetNames = new String[count]; + JsonArray hits = root.getAsJsonArray("hits"); + for (int i = 0; i < count; i++) { + JsonObject elmt = hits.get(i).getAsJsonObject(); + targetNames[i] = elmt.get("name").toString().replace("\"", ""); + } + return targetNames; + } +} diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/gui/ParamField.java b/src/main/java/eu/omp/irap/vespa/epntapclient/gui/ParamField.java index 9b38eec..58cd18b 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/gui/ParamField.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/gui/ParamField.java @@ -24,10 +24,8 @@ import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Locale; -import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; @@ -40,11 +38,8 @@ import javax.swing.SwingUtilities; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; - +import eu.omp.irap.vespa.epntapclient.EpnTapGet; import eu.omp.irap.vespa.votable.utils.CantSendQueryException; -import eu.omp.irap.vespa.votable.utils.Network; /** * A field used to set a service parameter to build the query (in the parameter panel). ParamField @@ -74,9 +69,6 @@ public abstract class ParamField extends JPanel { /** The preferred label width. */ private static final int LABEL_WIDTH = 140; - /** The URL of the resolver used for the `target name` field. */ - private static final String RESOLVER_URL = "http://voparis-registry.obspm.fr/ssodnet/1/autocomplete"; - /** The date format used in the DateRange field */ private static final String DATE_FORMAT = "dd/MM/yyyy"; @@ -384,7 +376,7 @@ public abstract class ParamField extends JPanel { lastContent = content; comboBox.removeAllItems(); try { - for (String s : TargetNameField.getItems(content)) { + for (String s : EpnTapGet.getTargetNames(content)) { comboBox.addItem(s); } } catch (CantSendQueryException e) { @@ -423,30 +415,6 @@ public abstract class ParamField extends JPanel { } /** - * The method used to get target names propositions by asking to the resolver. - * - * @param begining The beginning of the target_name. - * @return An array of Strings corresponding to the target names got. - * @throws CantSendQueryException - * @throws CantGetXMLException If the resolver do not work. - */ - static String[] getItems(String begining) throws CantSendQueryException { - Map params = new HashMap<>(); - params.put("q", "\"" + begining + "\""); - - String query = Network.buildQuery(ParamField.RESOLVER_URL, params); - JsonObject root = Network.readJson(query); - int count = Integer.parseInt(root.get("count").toString()); - String[] targetNames = new String[count]; - JsonArray hits = root.getAsJsonArray("hits"); - for (int i = 0; i < count; i++) { - JsonObject elmt = hits.get(i).getAsJsonObject(); - targetNames[i] = elmt.get("name").toString().replace("\"", ""); - } - return targetNames; - } - - /** * This method is called each time the field is modified. */ @Override -- libgit2 0.21.2