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;
43 * store and retrieve web service parameter sets.
48 public class WsParamSetManager implements ParamManager
50 Hashtable<String, ParamDatastoreI> paramparsers = new Hashtable<String, ParamDatastoreI>();
53 public WsParamSetI[] getParameterSet(String name, String serviceUrl,
54 boolean modifiable, boolean unmodifiable)
56 String files = Cache.getProperty("WS_PARAM_FILES");
61 StringTokenizer st = new StringTokenizer(files, "|");
63 ArrayList<WsParamSetI> params = new ArrayList<WsParamSetI>();
64 while (st.hasMoreTokens())
66 pfile = st.nextToken();
69 WsParamSetI[] pset = parseParamFile(pfile);
70 for (WsParamSetI p : pset)
73 if (serviceUrl != null)
75 for (String url : p.getApplicableUrls())
77 if (url.equals(serviceUrl))
87 add &= (modifiable == p.isModifiable()
88 || unmodifiable == !p.isModifiable());
89 add &= name == null || p.getName().equals(name);
98 } catch (IOException e)
100 Cache.log.info("Failed to parse parameter file " + pfile
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.warn("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);
178 throw new Error(MessageManager.getString(
179 "error.implementation_error_cannot_find_marshaller_for_param_set"));
181 if (filename == null)
183 JalviewFileChooser chooser = new JalviewFileChooser("wsparams",
184 "Web Service Parameter File");
185 chooser.setFileView(new JalviewFileView());
186 chooser.setDialogTitle(MessageManager
187 .getString("label.choose_filename_for_param_file"));
188 chooser.setToolTipText(MessageManager.getString("action.save"));
189 int value = chooser.showSaveDialog(Desktop.instance);
190 if (value == JalviewFileChooser.APPROVE_OPTION)
192 outfile = chooser.getSelectedFile();
193 jalview.bin.Cache.setProperty("LAST_DIRECTORY",
194 outfile.getParent());
195 filename = outfile.getAbsolutePath();
196 if (!filename.endsWith(".wsparams"))
198 filename = filename.concat(".wsparams");
199 outfile = new File(filename);
205 String paramFiles = jalview.bin.Cache.getDefault("WS_PARAM_FILES",
207 if (paramFiles.indexOf(filename) == -1)
209 if (paramFiles.length() > 0)
211 paramFiles = paramFiles.concat("|");
213 paramFiles = paramFiles.concat(filename);
215 jalview.bin.Cache.setProperty("WS_PARAM_FILES", paramFiles);
217 jalview.schemabinding.version2.WebServiceParameterSet paramxml = new jalview.schemabinding.version2.WebServiceParameterSet();
219 paramxml.setName(parameterSet.getName());
220 paramxml.setDescription(parameterSet.getDescription());
221 paramxml.setServiceURL(parameterSet.getApplicableUrls().clone());
222 paramxml.setVersion("1.0");
225 paramxml.setParameters(
226 parser.generateServiceParameterFile(parameterSet));
227 PrintWriter out = new PrintWriter(new OutputStreamWriter(
228 new FileOutputStream(outfile), "UTF-8"));
229 paramxml.marshal(out);
231 parameterSet.setSourceFile(filename);
232 } catch (Exception e)
234 Cache.log.error("Couldn't write parameter file to " + outfile, e);
241 * JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache
242 * .getProperty("LAST_DIRECTORY"), new String[] { "jc" }, new String[] {
243 * "Jalview User Colours" }, "Jalview User Colours"); chooser.setFileView(new
244 * jalview.io.JalviewFileView());
245 * chooser.setDialogTitle("Load colour scheme");
246 * chooser.setToolTipText("Load");
248 * int value = chooser.showOpenDialog(this);
250 * if (value == JalviewFileChooser.APPROVE_OPTION) { File choice =
251 * chooser.getSelectedFile(); jalview.bin.Cache.setProperty("LAST_DIRECTORY",
252 * choice.getParent()); String defaultColours = jalview.bin.Cache.getDefault(
253 * "USER_DEFINED_COLOURS", choice.getPath()); if
254 * (defaultColours.indexOf(choice.getPath()) == -1) { defaultColours =
255 * defaultColours.concat("|") .concat(choice.getPath()); } (non-Javadoc)
258 * jalview.ws.params.ParamManager#deleteParameterSet(jalview.ws.params.WsParamSetI
262 public void deleteParameterSet(WsParamSetI parameterSet)
264 String filename = parameterSet.getSourceFile();
265 if (filename == null || filename.trim().length() < 1)
269 String paramFiles = jalview.bin.Cache.getDefault("WS_PARAM_FILES", "");
270 if (paramFiles.indexOf(filename) > -1)
272 String nparamFiles = new String();
273 StringTokenizer st = new StringTokenizer(paramFiles, "|");
274 while (st.hasMoreElements())
276 String fl = st.nextToken();
277 if (!fl.equals(filename))
279 nparamFiles = nparamFiles.concat("|").concat(fl);
282 jalview.bin.Cache.setProperty("WS_PARAM_FILES", nparamFiles);
287 File pfile = new File(filename);
288 if (pfile.exists() && pfile.canWrite())
290 if (JvOptionPane.showConfirmDialog(Desktop.instance,
291 "Delete the preset's file, too ?", "Delete User Preset ?",
292 JvOptionPane.OK_CANCEL_OPTION) == JvOptionPane.OK_OPTION)
297 } catch (Exception e)
300 "Exception when trying to delete webservice user preset: ",
306 public void registerParser(String hosturl, ParamDatastoreI paramdataStore)
308 paramparsers.put(hosturl, paramdataStore);