2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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.
23 import jalview.bin.Cache;
24 import jalview.io.JalviewFileChooser;
25 import jalview.io.JalviewFileView;
26 import jalview.util.MessageManager;
27 import jalview.ws.params.ParamDatastoreI;
28 import jalview.ws.params.ParamManager;
29 import jalview.ws.params.WsParamSetI;
32 import java.io.FileOutputStream;
33 import java.io.IOException;
34 import java.io.InputStreamReader;
35 import java.io.OutputStreamWriter;
36 import java.io.PrintWriter;
37 import java.util.ArrayList;
38 import java.util.Hashtable;
39 import java.util.List;
40 import java.util.StringTokenizer;
42 import javax.swing.JOptionPane;
45 * store and retrieve web service parameter sets.
50 public class WsParamSetManager implements ParamManager
52 Hashtable<String, ParamDatastoreI> paramparsers = new Hashtable<String, ParamDatastoreI>();
55 public WsParamSetI[] getParameterSet(String name, String serviceUrl,
56 boolean modifiable, boolean unmodifiable)
58 String files = Cache.getProperty("WS_PARAM_FILES");
63 StringTokenizer st = new StringTokenizer(files, "|");
65 ArrayList<WsParamSetI> params = new ArrayList<WsParamSetI>();
66 while (st.hasMoreTokens())
68 pfile = st.nextToken();
71 WsParamSetI[] pset = parseParamFile(pfile);
72 for (WsParamSetI p : pset)
75 if (serviceUrl != null)
77 for (String url : p.getApplicableUrls())
79 if (url.equals(serviceUrl))
89 add &= (modifiable == p.isModifiable() || unmodifiable == !p
91 add &= name == null || p.getName().equals(name);
100 } catch (IOException e)
103 .info("Failed to parse parameter file "
105 + " (Check that all JALVIEW_WSPARAMFILES entries are valid!)",
109 return params.toArray(new WsParamSetI[0]);
112 private WsParamSetI[] parseParamFile(String filename) throws IOException
114 List<WsParamSetI> psets = new ArrayList<WsParamSetI>();
115 InputStreamReader is = new InputStreamReader(
116 new java.io.FileInputStream(new File(filename)), "UTF-8");
118 jalview.schemabinding.version2.WebServiceParameterSet wspset = new jalview.schemabinding.version2.WebServiceParameterSet();
120 org.exolab.castor.xml.Unmarshaller unmar = new org.exolab.castor.xml.Unmarshaller(
122 unmar.setWhitespacePreserve(true);
125 wspset = (jalview.schemabinding.version2.WebServiceParameterSet) unmar
127 } catch (Exception ex)
129 throw new IOException(ex);
131 if (wspset != null && wspset.getParameters().length() > 0)
133 for (String url : wspset.getServiceURL())
135 ParamDatastoreI parser = paramparsers.get(url);
138 WsParamSetI pset = parser.parseServiceParameterFile(
139 wspset.getName(), wspset.getDescription(),
140 wspset.getServiceURL(), wspset.getParameters());
143 pset.setSourceFile(filename);
151 return psets.toArray(new WsParamSetI[0]);
155 public void storeParameterSet(WsParamSetI parameterSet)
157 String filename = parameterSet.getSourceFile();
161 if (filename != null && !((outfile = new File(filename)).canWrite()))
163 Cache.log.warn("Can't write to " + filename
164 + " - Prompting for new file to write to.");
167 } catch (Exception e)
172 ParamDatastoreI parser = null;
173 for (String urls : parameterSet.getApplicableUrls())
177 parser = paramparsers.get(urls);
184 .getString("error.implementation_error_cannot_find_marshaller_for_param_set"));
186 if (filename == null)
188 JalviewFileChooser chooser = new JalviewFileChooser(
189 Cache.getProperty("LAST_DIRECTORY"), "wsparams",
190 "Web Service Parameter File", "Web Service Parameter File");
191 chooser.setFileView(new JalviewFileView());
192 chooser.setDialogTitle(MessageManager
193 .getString("label.choose_filename_for_param_file"));
194 chooser.setToolTipText(MessageManager.getString("action.save"));
195 int value = chooser.showSaveDialog(Desktop.instance);
196 if (value == JalviewFileChooser.APPROVE_OPTION)
198 outfile = chooser.getSelectedFile();
200 .setProperty("LAST_DIRECTORY", outfile.getParent());
201 filename = outfile.getAbsolutePath();
202 if (!filename.endsWith(".wsparams"))
204 filename = filename.concat(".wsparams");
205 outfile = new File(filename);
211 String paramFiles = jalview.bin.Cache.getDefault("WS_PARAM_FILES",
213 if (paramFiles.indexOf(filename) == -1)
215 if (paramFiles.length() > 0)
217 paramFiles = paramFiles.concat("|");
219 paramFiles = paramFiles.concat(filename);
221 jalview.bin.Cache.setProperty("WS_PARAM_FILES", paramFiles);
223 jalview.schemabinding.version2.WebServiceParameterSet paramxml = new jalview.schemabinding.version2.WebServiceParameterSet();
225 paramxml.setName(parameterSet.getName());
226 paramxml.setDescription(parameterSet.getDescription());
227 paramxml.setServiceURL(parameterSet.getApplicableUrls().clone());
228 paramxml.setVersion("1.0");
231 paramxml.setParameters(parser
232 .generateServiceParameterFile(parameterSet));
233 PrintWriter out = new PrintWriter(new OutputStreamWriter(
234 new FileOutputStream(outfile), "UTF-8"));
235 paramxml.marshal(out);
237 parameterSet.setSourceFile(filename);
238 } catch (Exception e)
240 Cache.log.error("Couldn't write parameter file to " + outfile, e);
247 * JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache
248 * .getProperty("LAST_DIRECTORY"), new String[] { "jc" }, new String[] {
249 * "Jalview User Colours" }, "Jalview User Colours"); chooser.setFileView(new
250 * jalview.io.JalviewFileView());
251 * chooser.setDialogTitle("Load colour scheme");
252 * chooser.setToolTipText("Load");
254 * int value = chooser.showOpenDialog(this);
256 * if (value == JalviewFileChooser.APPROVE_OPTION) { File choice =
257 * chooser.getSelectedFile(); jalview.bin.Cache.setProperty("LAST_DIRECTORY",
258 * choice.getParent()); String defaultColours = jalview.bin.Cache.getDefault(
259 * "USER_DEFINED_COLOURS", choice.getPath()); if
260 * (defaultColours.indexOf(choice.getPath()) == -1) { defaultColours =
261 * defaultColours.concat("|") .concat(choice.getPath()); } (non-Javadoc)
264 * jalview.ws.params.ParamManager#deleteParameterSet(jalview.ws.params.WsParamSetI
268 public void deleteParameterSet(WsParamSetI parameterSet)
270 String filename = parameterSet.getSourceFile();
271 if (filename == null || filename.trim().length() < 1)
275 String paramFiles = jalview.bin.Cache.getDefault("WS_PARAM_FILES", "");
276 if (paramFiles.indexOf(filename) > -1)
278 String nparamFiles = new String();
279 StringTokenizer st = new StringTokenizer(paramFiles, "|");
280 while (st.hasMoreElements())
282 String fl = st.nextToken();
283 if (!fl.equals(filename))
285 nparamFiles = nparamFiles.concat("|").concat(fl);
288 jalview.bin.Cache.setProperty("WS_PARAM_FILES", nparamFiles);
293 File pfile = new File(filename);
294 if (pfile.exists() && pfile.canWrite())
296 if (JvOptionPane.showConfirmDialog(Desktop.instance,
297 "Delete the preset's file, too ?", "Delete User Preset ?",
298 JvOptionPane.OK_CANCEL_OPTION) == JvOptionPane.OK_OPTION)
303 } catch (Exception e)
306 .error("Exception when trying to delete webservice user preset: ",
312 public void registerParser(String hosturl, ParamDatastoreI paramdataStore)
314 paramparsers.put(hosturl, paramdataStore);