2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.InputStreamReader;
25 import java.io.OutputStreamWriter;
26 import java.io.PrintWriter;
27 import java.util.ArrayList;
28 import java.util.Hashtable;
29 import java.util.List;
30 import java.util.StringTokenizer;
32 import javax.swing.JOptionPane;
34 import jalview.bin.Cache;
35 import jalview.io.JalviewFileChooser;
36 import jalview.ws.params.ParamDatastoreI;
37 import jalview.ws.params.ParamManager;
38 import jalview.ws.params.WsParamSetI;
41 * store and retrieve web service parameter sets.
46 public class WsParamSetManager implements ParamManager
48 Hashtable<String, ParamDatastoreI> paramparsers = new Hashtable<String, ParamDatastoreI>();
51 public WsParamSetI[] getParameterSet(String name, String serviceUrl,
52 boolean modifiable, boolean unmodifiable)
54 String files = Cache.getProperty("WS_PARAM_FILES");
59 StringTokenizer st = new StringTokenizer(files, "|");
61 ArrayList<WsParamSetI> params = new ArrayList<WsParamSetI>();
62 while (st.hasMoreTokens())
64 pfile = st.nextToken();
67 WsParamSetI[] pset = parseParamFile(pfile);
68 for (WsParamSetI p : pset)
71 if (serviceUrl != null)
73 for (String url : p.getApplicableUrls())
75 if (url.equals(serviceUrl))
85 add &= (modifiable == p.isModifiable() || unmodifiable == !p
87 add &= name == null || p.getName().equals(name);
96 } catch (IOException e)
99 .info("Failed to parse parameter file "
101 + " (Check that all JALVIEW_WSPARAMFILES entries are valid!)",
105 return params.toArray(new WsParamSetI[0]);
108 private WsParamSetI[] parseParamFile(String filename) throws IOException
110 List<WsParamSetI> psets = new ArrayList<WsParamSetI>();
111 InputStreamReader is = new InputStreamReader(
112 new java.io.FileInputStream(new File(filename)), "UTF-8");
114 jalview.schemabinding.version2.WebServiceParameterSet wspset = new jalview.schemabinding.version2.WebServiceParameterSet();
116 org.exolab.castor.xml.Unmarshaller unmar = new org.exolab.castor.xml.Unmarshaller(
118 unmar.setWhitespacePreserve(true);
121 wspset = (jalview.schemabinding.version2.WebServiceParameterSet) unmar
123 } catch (Exception ex)
125 throw new IOException(ex);
127 if (wspset != null && wspset.getParameters().length() > 0)
129 for (String url : wspset.getServiceURL())
131 ParamDatastoreI parser = paramparsers.get(url);
134 WsParamSetI pset = parser.parseServiceParameterFile(
135 wspset.getName(), wspset.getDescription(),
136 wspset.getServiceURL(), wspset.getParameters());
139 pset.setSourceFile(filename);
147 return psets.toArray(new WsParamSetI[0]);
151 public void storeParameterSet(WsParamSetI parameterSet)
153 String filename = parameterSet.getSourceFile();
157 if (filename != null && !((outfile = new File(filename)).canWrite()))
159 Cache.log.info("Can't write to " + filename
160 + " - Prompting for new file to write to.");
163 } catch (Exception e)
168 ParamDatastoreI parser = null;
169 for (String urls : parameterSet.getApplicableUrls())
173 parser = paramparsers.get(urls);
179 "Implementation error: Can't find a marshaller for the parameter set");
181 if (filename == null)
183 JalviewFileChooser chooser = new JalviewFileChooser(
184 jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
185 { "wsparams" }, new String[]
186 { "Web Service Parameter File" },
187 "Web Service Parameter File");
188 chooser.setFileView(new jalview.io.JalviewFileView());
189 chooser.setDialogTitle("Choose a filename for this parameter file");
190 chooser.setToolTipText("Save");
191 int value = chooser.showSaveDialog(Desktop.instance);
192 if (value == JalviewFileChooser.APPROVE_OPTION)
194 outfile = chooser.getSelectedFile();
196 .setProperty("LAST_DIRECTORY", outfile.getParent());
197 filename = outfile.getAbsolutePath();
198 if (!filename.endsWith(".wsparams"))
200 filename = filename.concat(".wsparams");
201 outfile = new File(filename);
207 String paramFiles = jalview.bin.Cache.getDefault("WS_PARAM_FILES",
209 if (paramFiles.indexOf(filename) == -1)
211 if (paramFiles.length() > 0)
213 paramFiles = paramFiles.concat("|");
215 paramFiles = paramFiles.concat(filename);
217 jalview.bin.Cache.setProperty("WS_PARAM_FILES", paramFiles);
219 jalview.schemabinding.version2.WebServiceParameterSet paramxml = new jalview.schemabinding.version2.WebServiceParameterSet();
221 paramxml.setName(parameterSet.getName());
222 paramxml.setDescription(parameterSet.getDescription());
223 paramxml.setServiceURL(parameterSet.getApplicableUrls().clone());
224 paramxml.setVersion("1.0");
227 paramxml.setParameters(parser
228 .generateServiceParameterFile(parameterSet));
229 PrintWriter out = new PrintWriter(new OutputStreamWriter(
230 new FileOutputStream(outfile), "UTF-8"));
231 paramxml.marshal(out);
233 parameterSet.setSourceFile(filename);
234 } catch (Exception e)
236 Cache.log.error("Couldn't write parameter file to " + outfile, e);
243 * JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache
244 * .getProperty("LAST_DIRECTORY"), new String[] { "jc" }, new String[] {
245 * "Jalview User Colours" }, "Jalview User Colours"); chooser.setFileView(new
246 * jalview.io.JalviewFileView());
247 * chooser.setDialogTitle("Load colour scheme");
248 * chooser.setToolTipText("Load");
250 * int value = chooser.showOpenDialog(this);
252 * if (value == JalviewFileChooser.APPROVE_OPTION) { File choice =
253 * chooser.getSelectedFile(); jalview.bin.Cache.setProperty("LAST_DIRECTORY",
254 * choice.getParent()); String defaultColours = jalview.bin.Cache.getDefault(
255 * "USER_DEFINED_COLOURS", choice.getPath()); if
256 * (defaultColours.indexOf(choice.getPath()) == -1) { defaultColours =
257 * defaultColours.concat("|") .concat(choice.getPath()); } (non-Javadoc)
260 * jalview.ws.params.ParamManager#deleteParameterSet(jalview.ws.params.WsParamSetI
264 public void deleteParameterSet(WsParamSetI parameterSet)
266 String filename = parameterSet.getSourceFile();
267 if (filename == null || filename.trim().length() < 1)
271 String paramFiles = jalview.bin.Cache.getDefault("WS_PARAM_FILES", "");
272 if (paramFiles.indexOf(filename) > -1)
274 String nparamFiles = new String();
275 StringTokenizer st = new StringTokenizer(paramFiles, "|");
276 while (st.hasMoreElements())
278 String fl = st.nextToken();
279 if (!fl.equals(filename))
281 nparamFiles = nparamFiles.concat("|").concat(fl);
284 jalview.bin.Cache.setProperty("WS_PARAM_FILES", nparamFiles);
289 File pfile = new File(filename);
290 if (pfile.exists() && pfile.canWrite())
292 if (JOptionPane.showConfirmDialog(Desktop.instance,
293 "Delete the preset's file, too ?", "Delete User Preset ?",
294 JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION)
299 } catch (Exception e)
302 .error("Exception when trying to delete webservice user preset: ",
308 public void registerParser(String hosturl, ParamDatastoreI paramdataStore)
310 paramparsers.put(hosturl, paramdataStore);