2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.InputStreamReader;
27 import java.io.OutputStreamWriter;
28 import java.io.PrintWriter;
29 import java.util.ArrayList;
30 import java.util.Hashtable;
31 import java.util.List;
32 import java.util.StringTokenizer;
34 import javax.swing.JOptionPane;
36 import jalview.bin.Cache;
37 import jalview.io.JalviewFileChooser;
38 import jalview.util.MessageManager;
39 import jalview.ws.params.ParamDatastoreI;
40 import jalview.ws.params.ParamManager;
41 import jalview.ws.params.WsParamSetI;
44 * store and retrieve web service parameter sets.
49 public class WsParamSetManager implements ParamManager
51 Hashtable<String, ParamDatastoreI> paramparsers = new Hashtable<String, ParamDatastoreI>();
54 public WsParamSetI[] getParameterSet(String name, String serviceUrl,
55 boolean modifiable, boolean unmodifiable)
57 String files = Cache.getProperty("WS_PARAM_FILES");
62 StringTokenizer st = new StringTokenizer(files, "|");
64 ArrayList<WsParamSetI> params = new ArrayList<WsParamSetI>();
65 while (st.hasMoreTokens())
67 pfile = st.nextToken();
70 WsParamSetI[] pset = parseParamFile(pfile);
71 for (WsParamSetI p : pset)
74 if (serviceUrl != null)
76 for (String url : p.getApplicableUrls())
78 if (url.equals(serviceUrl))
88 add &= (modifiable == p.isModifiable() || unmodifiable == !p
90 add &= name == null || p.getName().equals(name);
99 } catch (IOException e)
102 .info("Failed to parse parameter file "
104 + " (Check that all JALVIEW_WSPARAMFILES entries are valid!)",
108 return params.toArray(new WsParamSetI[0]);
111 private WsParamSetI[] parseParamFile(String filename) throws IOException
113 List<WsParamSetI> psets = new ArrayList<WsParamSetI>();
114 InputStreamReader is = new InputStreamReader(
115 new java.io.FileInputStream(new File(filename)), "UTF-8");
117 jalview.schemabinding.version2.WebServiceParameterSet wspset = new jalview.schemabinding.version2.WebServiceParameterSet();
119 org.exolab.castor.xml.Unmarshaller unmar = new org.exolab.castor.xml.Unmarshaller(
121 unmar.setWhitespacePreserve(true);
124 wspset = (jalview.schemabinding.version2.WebServiceParameterSet) unmar
126 } catch (Exception ex)
128 throw new IOException(ex);
130 if (wspset != null && wspset.getParameters().length() > 0)
132 for (String url : wspset.getServiceURL())
134 ParamDatastoreI parser = paramparsers.get(url);
137 WsParamSetI pset = parser.parseServiceParameterFile(
138 wspset.getName(), wspset.getDescription(),
139 wspset.getServiceURL(), wspset.getParameters());
142 pset.setSourceFile(filename);
150 return psets.toArray(new WsParamSetI[0]);
154 public void storeParameterSet(WsParamSetI parameterSet)
156 String filename = parameterSet.getSourceFile();
160 if (filename != null && !((outfile = new File(filename)).canWrite()))
162 Cache.log.info("Can't write to " + filename
163 + " - Prompting for new file to write to.");
166 } catch (Exception e)
171 ParamDatastoreI parser = null;
172 for (String urls : parameterSet.getApplicableUrls())
176 parser = paramparsers.get(urls);
182 "Implementation error: Can't find a marshaller for the parameter set");
184 if (filename == null)
186 JalviewFileChooser chooser = new JalviewFileChooser(
187 jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
188 { "wsparams" }, new String[]
189 { "Web Service Parameter File" },
190 "Web Service Parameter File");
191 chooser.setFileView(new jalview.io.JalviewFileView());
192 chooser.setDialogTitle("Choose a filename for this parameter file");
193 chooser.setToolTipText(MessageManager.getString("action.save"));
194 int value = chooser.showSaveDialog(Desktop.instance);
195 if (value == JalviewFileChooser.APPROVE_OPTION)
197 outfile = chooser.getSelectedFile();
199 .setProperty("LAST_DIRECTORY", outfile.getParent());
200 filename = outfile.getAbsolutePath();
201 if (!filename.endsWith(".wsparams"))
203 filename = filename.concat(".wsparams");
204 outfile = new File(filename);
210 String paramFiles = jalview.bin.Cache.getDefault("WS_PARAM_FILES",
212 if (paramFiles.indexOf(filename) == -1)
214 if (paramFiles.length() > 0)
216 paramFiles = paramFiles.concat("|");
218 paramFiles = paramFiles.concat(filename);
220 jalview.bin.Cache.setProperty("WS_PARAM_FILES", paramFiles);
222 jalview.schemabinding.version2.WebServiceParameterSet paramxml = new jalview.schemabinding.version2.WebServiceParameterSet();
224 paramxml.setName(parameterSet.getName());
225 paramxml.setDescription(parameterSet.getDescription());
226 paramxml.setServiceURL(parameterSet.getApplicableUrls().clone());
227 paramxml.setVersion("1.0");
230 paramxml.setParameters(parser
231 .generateServiceParameterFile(parameterSet));
232 PrintWriter out = new PrintWriter(new OutputStreamWriter(
233 new FileOutputStream(outfile), "UTF-8"));
234 paramxml.marshal(out);
236 parameterSet.setSourceFile(filename);
237 } catch (Exception e)
239 Cache.log.error("Couldn't write parameter file to " + outfile, e);
246 * JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache
247 * .getProperty("LAST_DIRECTORY"), new String[] { "jc" }, new String[] {
248 * "Jalview User Colours" }, "Jalview User Colours"); chooser.setFileView(new
249 * jalview.io.JalviewFileView());
250 * chooser.setDialogTitle("Load colour scheme");
251 * chooser.setToolTipText("Load");
253 * int value = chooser.showOpenDialog(this);
255 * if (value == JalviewFileChooser.APPROVE_OPTION) { File choice =
256 * chooser.getSelectedFile(); jalview.bin.Cache.setProperty("LAST_DIRECTORY",
257 * choice.getParent()); String defaultColours = jalview.bin.Cache.getDefault(
258 * "USER_DEFINED_COLOURS", choice.getPath()); if
259 * (defaultColours.indexOf(choice.getPath()) == -1) { defaultColours =
260 * defaultColours.concat("|") .concat(choice.getPath()); } (non-Javadoc)
263 * jalview.ws.params.ParamManager#deleteParameterSet(jalview.ws.params.WsParamSetI
267 public void deleteParameterSet(WsParamSetI parameterSet)
269 String filename = parameterSet.getSourceFile();
270 if (filename == null || filename.trim().length() < 1)
274 String paramFiles = jalview.bin.Cache.getDefault("WS_PARAM_FILES", "");
275 if (paramFiles.indexOf(filename) > -1)
277 String nparamFiles = new String();
278 StringTokenizer st = new StringTokenizer(paramFiles, "|");
279 while (st.hasMoreElements())
281 String fl = st.nextToken();
282 if (!fl.equals(filename))
284 nparamFiles = nparamFiles.concat("|").concat(fl);
287 jalview.bin.Cache.setProperty("WS_PARAM_FILES", nparamFiles);
292 File pfile = new File(filename);
293 if (pfile.exists() && pfile.canWrite())
295 if (JOptionPane.showConfirmDialog(Desktop.instance,
296 "Delete the preset's file, too ?", "Delete User Preset ?",
297 JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION)
302 } catch (Exception e)
305 .error("Exception when trying to delete webservice user preset: ",
311 public void registerParser(String hosturl, ParamDatastoreI paramdataStore)
313 paramparsers.put(hosturl, paramdataStore);