2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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/>.
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.InputStreamReader;
24 import java.io.OutputStreamWriter;
25 import java.io.PrintWriter;
26 import java.util.ArrayList;
27 import java.util.Hashtable;
28 import java.util.List;
29 import java.util.StringTokenizer;
31 import javax.swing.JOptionPane;
33 import jalview.bin.Cache;
34 import jalview.io.JalviewFileChooser;
35 import jalview.ws.params.ParamDatastoreI;
36 import jalview.ws.params.ParamManager;
37 import jalview.ws.params.WsParamSetI;
40 * store and retrieve web service parameter sets.
45 public class WsParamSetManager implements ParamManager
47 Hashtable<String, ParamDatastoreI> paramparsers = new Hashtable<String, ParamDatastoreI>();
50 public WsParamSetI[] getParameterSet(String name, String serviceUrl,
51 boolean modifiable, boolean unmodifiable)
53 String files = Cache.getProperty("WS_PARAM_FILES");
58 StringTokenizer st = new StringTokenizer(files, "|");
60 ArrayList<WsParamSetI> params = new ArrayList<WsParamSetI>();
61 while (st.hasMoreTokens())
63 pfile = st.nextToken();
66 WsParamSetI[] pset = parseParamFile(pfile);
67 for (WsParamSetI p : pset)
70 if (serviceUrl != null)
72 for (String url : p.getApplicableUrls())
74 if (url.equals(serviceUrl))
84 add &= (modifiable == p.isModifiable() || unmodifiable == !p
86 add &= name == null || p.getName().equals(name);
95 } catch (IOException e)
98 .info("Failed to parse parameter file "
100 + " (Check that all JALVIEW_WSPARAMFILES entries are valid!)",
104 return params.toArray(new WsParamSetI[0]);
107 private WsParamSetI[] parseParamFile(String filename) throws IOException
109 List<WsParamSetI> psets = new ArrayList<WsParamSetI>();
110 InputStreamReader is = new InputStreamReader(
111 new java.io.FileInputStream(new File(filename)), "UTF-8");
113 jalview.schemabinding.version2.WebServiceParameterSet wspset = new jalview.schemabinding.version2.WebServiceParameterSet();
115 org.exolab.castor.xml.Unmarshaller unmar = new org.exolab.castor.xml.Unmarshaller(
117 unmar.setWhitespacePreserve(true);
120 wspset = (jalview.schemabinding.version2.WebServiceParameterSet) unmar
122 } catch (Exception ex)
124 throw new IOException(ex);
126 if (wspset != null && wspset.getParameters().length() > 0)
128 for (String url : wspset.getServiceURL())
130 ParamDatastoreI parser = paramparsers.get(url);
133 WsParamSetI pset = parser.parseServiceParameterFile(
134 wspset.getName(), wspset.getDescription(),
135 wspset.getServiceURL(), wspset.getParameters());
138 pset.setSourceFile(filename);
146 return psets.toArray(new WsParamSetI[0]);
150 public void storeParameterSet(WsParamSetI parameterSet)
152 String filename = parameterSet.getSourceFile();
156 if (filename != null && !((outfile = new File(filename)).canWrite()))
158 Cache.log.info("Can't write to " + filename
159 + " - Prompting for new file to write to.");
162 } catch (Exception e)
167 ParamDatastoreI parser = null;
168 for (String urls : parameterSet.getApplicableUrls())
172 parser = paramparsers.get(urls);
178 "Implementation error: Can't find a marshaller for the parameter set");
180 if (filename == null)
182 JalviewFileChooser chooser = new JalviewFileChooser(
183 jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
184 { "wsparams" }, new String[]
185 { "Web Service Parameter File" },
186 "Web Service Parameter File");
187 chooser.setFileView(new jalview.io.JalviewFileView());
188 chooser.setDialogTitle("Choose a filename for this parameter file");
189 chooser.setToolTipText("Save");
190 int value = chooser.showSaveDialog(Desktop.instance);
191 if (value == JalviewFileChooser.APPROVE_OPTION)
193 outfile = chooser.getSelectedFile();
195 .setProperty("LAST_DIRECTORY", outfile.getParent());
196 filename = outfile.getAbsolutePath();
197 if (!filename.endsWith(".wsparams"))
199 filename = filename.concat(".wsparams");
200 outfile = new File(filename);
206 String paramFiles = jalview.bin.Cache.getDefault("WS_PARAM_FILES",
208 if (paramFiles.indexOf(filename) == -1)
210 if (paramFiles.length() > 0)
212 paramFiles = paramFiles.concat("|");
214 paramFiles = paramFiles.concat(filename);
216 jalview.bin.Cache.setProperty("WS_PARAM_FILES", paramFiles);
218 jalview.schemabinding.version2.WebServiceParameterSet paramxml = new jalview.schemabinding.version2.WebServiceParameterSet();
220 paramxml.setName(parameterSet.getName());
221 paramxml.setDescription(parameterSet.getDescription());
222 paramxml.setServiceURL(parameterSet.getApplicableUrls().clone());
223 paramxml.setVersion("1.0");
226 paramxml.setParameters(parser
227 .generateServiceParameterFile(parameterSet));
228 PrintWriter out = new PrintWriter(new OutputStreamWriter(
229 new FileOutputStream(outfile), "UTF-8"));
230 paramxml.marshal(out);
232 parameterSet.setSourceFile(filename);
233 } catch (Exception e)
235 Cache.log.error("Couldn't write parameter file to " + outfile, e);
242 * JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache
243 * .getProperty("LAST_DIRECTORY"), new String[] { "jc" }, new String[] {
244 * "Jalview User Colours" }, "Jalview User Colours"); chooser.setFileView(new
245 * jalview.io.JalviewFileView());
246 * chooser.setDialogTitle("Load colour scheme");
247 * chooser.setToolTipText("Load");
249 * int value = chooser.showOpenDialog(this);
251 * if (value == JalviewFileChooser.APPROVE_OPTION) { File choice =
252 * chooser.getSelectedFile(); jalview.bin.Cache.setProperty("LAST_DIRECTORY",
253 * choice.getParent()); String defaultColours = jalview.bin.Cache.getDefault(
254 * "USER_DEFINED_COLOURS", choice.getPath()); if
255 * (defaultColours.indexOf(choice.getPath()) == -1) { defaultColours =
256 * defaultColours.concat("|") .concat(choice.getPath()); } (non-Javadoc)
259 * jalview.ws.params.ParamManager#deleteParameterSet(jalview.ws.params.WsParamSetI
263 public void deleteParameterSet(WsParamSetI parameterSet)
265 String filename = parameterSet.getSourceFile();
266 if (filename == null || filename.trim().length() < 1)
270 String paramFiles = jalview.bin.Cache.getDefault("WS_PARAM_FILES", "");
271 if (paramFiles.indexOf(filename) > -1)
273 String nparamFiles = new String();
274 StringTokenizer st = new StringTokenizer(paramFiles, "|");
275 while (st.hasMoreElements())
277 String fl = st.nextToken();
278 if (!fl.equals(filename))
280 nparamFiles = nparamFiles.concat("|").concat(fl);
283 jalview.bin.Cache.setProperty("WS_PARAM_FILES", nparamFiles);
288 File pfile = new File(filename);
289 if (pfile.exists() && pfile.canWrite())
291 if (JOptionPane.showConfirmDialog(Desktop.instance,
292 "Delete the preset's file, too ?", "Delete User Preset ?",
293 JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION)
298 } catch (Exception e)
301 .error("Exception when trying to delete webservice user preset: ",
307 public void registerParser(String hosturl, ParamDatastoreI paramdataStore)
309 paramparsers.put(hosturl, paramdataStore);