2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, 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.FileInputStream;
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.JButton;
33 import javax.swing.JOptionPane;
35 import jalview.bin.Cache;
36 import jalview.io.JalviewFileChooser;
37 import jalview.ws.jws2.dm.JabaWsParamSet;
38 import jalview.ws.params.ParamDatastoreI;
39 import jalview.ws.params.ParamManager;
40 import jalview.ws.params.WsParamSetI;
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() || unmodifiable == !p
89 add &= name == null || p.getName().equals(name);
98 } catch (IOException e)
101 .info("Failed to parse parameter file "
103 + " (Check that all JALVIEW_WSPARAMFILES entries are valid!)",
107 return params.toArray(new WsParamSetI[0]);
110 private WsParamSetI[] parseParamFile(String filename) throws IOException
112 List<WsParamSetI> psets = new ArrayList<WsParamSetI>();
113 InputStreamReader is = new InputStreamReader(
114 new java.io.FileInputStream(new File(filename)), "UTF-8");
116 jalview.schemabinding.version2.WebServiceParameterSet wspset = new jalview.schemabinding.version2.WebServiceParameterSet();
118 org.exolab.castor.xml.Unmarshaller unmar = new org.exolab.castor.xml.Unmarshaller(
120 unmar.setWhitespacePreserve(true);
123 wspset = (jalview.schemabinding.version2.WebServiceParameterSet) unmar
125 } catch (Exception ex)
127 throw new IOException(ex);
129 if (wspset != null && wspset.getParameters().length() > 0)
131 for (String url : wspset.getServiceURL())
133 ParamDatastoreI parser = paramparsers.get(url);
136 WsParamSetI pset = parser.parseServiceParameterFile(
137 wspset.getName(), wspset.getDescription(),
138 wspset.getServiceURL(), wspset.getParameters());
141 pset.setSourceFile(filename);
149 return psets.toArray(new WsParamSetI[0]);
153 public void storeParameterSet(WsParamSetI parameterSet)
155 String filename = parameterSet.getSourceFile();
159 if (filename != null && !((outfile = new File(filename)).canWrite()))
161 Cache.log.info("Can't write to " + filename
162 + " - Prompting for new file to write to.");
165 } catch (Exception e)
170 ParamDatastoreI parser = null;
171 for (String urls : parameterSet.getApplicableUrls())
175 parser = paramparsers.get(urls);
181 "Implementation error: Can't find a marshaller for the parameter set");
183 if (filename == null)
185 JalviewFileChooser chooser = new JalviewFileChooser(
186 jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
187 { "wsparams" }, new String[]
188 { "Web Service Parameter File" },
189 "Web Service Parameter File");
190 chooser.setFileView(new jalview.io.JalviewFileView());
191 chooser.setDialogTitle("Choose a filename for this parameter file");
192 chooser.setToolTipText("Save");
193 int value = chooser.showSaveDialog(Desktop.instance);
194 if (value == JalviewFileChooser.APPROVE_OPTION)
196 outfile = chooser.getSelectedFile();
198 .setProperty("LAST_DIRECTORY", outfile.getParent());
199 filename = outfile.getAbsolutePath();
200 if (!filename.endsWith(".wsparams"))
202 filename = filename.concat(".wsparams");
203 outfile = new File(filename);
209 String paramFiles = jalview.bin.Cache.getDefault("WS_PARAM_FILES",
211 if (paramFiles.indexOf(filename) == -1)
213 if (paramFiles.length() > 0)
215 paramFiles = paramFiles.concat("|");
217 paramFiles = paramFiles.concat(filename);
219 jalview.bin.Cache.setProperty("WS_PARAM_FILES", paramFiles);
221 jalview.schemabinding.version2.WebServiceParameterSet paramxml = new jalview.schemabinding.version2.WebServiceParameterSet();
223 paramxml.setName(parameterSet.getName());
224 paramxml.setDescription(parameterSet.getDescription());
225 paramxml.setServiceURL(parameterSet.getApplicableUrls().clone());
226 paramxml.setVersion("1.0");
229 paramxml.setParameters(parser
230 .generateServiceParameterFile(parameterSet));
231 PrintWriter out = new PrintWriter(new OutputStreamWriter(
232 new FileOutputStream(outfile), "UTF-8"));
233 paramxml.marshal(out);
235 parameterSet.setSourceFile(filename);
236 } catch (Exception e)
238 Cache.log.error("Couldn't write parameter file to " + outfile, e);
245 * JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache
246 * .getProperty("LAST_DIRECTORY"), new String[] { "jc" }, new String[] {
247 * "Jalview User Colours" }, "Jalview User Colours"); chooser.setFileView(new
248 * jalview.io.JalviewFileView());
249 * chooser.setDialogTitle("Load colour scheme");
250 * chooser.setToolTipText("Load");
252 * int value = chooser.showOpenDialog(this);
254 * if (value == JalviewFileChooser.APPROVE_OPTION) { File choice =
255 * chooser.getSelectedFile(); jalview.bin.Cache.setProperty("LAST_DIRECTORY",
256 * choice.getParent()); String defaultColours = jalview.bin.Cache.getDefault(
257 * "USER_DEFINED_COLOURS", choice.getPath()); if
258 * (defaultColours.indexOf(choice.getPath()) == -1) { defaultColours =
259 * defaultColours.concat("|") .concat(choice.getPath()); } (non-Javadoc)
262 * jalview.ws.params.ParamManager#deleteParameterSet(jalview.ws.params.WsParamSetI
266 public void deleteParameterSet(WsParamSetI parameterSet)
268 String filename = parameterSet.getSourceFile();
269 if (filename == null || filename.trim().length() < 1)
273 String paramFiles = jalview.bin.Cache.getDefault("WS_PARAM_FILES", "");
274 if (paramFiles.indexOf(filename) > -1)
276 String nparamFiles = new String();
277 StringTokenizer st = new StringTokenizer(paramFiles, "|");
278 while (st.hasMoreElements())
280 String fl = st.nextToken();
281 if (!fl.equals(filename))
283 nparamFiles = nparamFiles.concat("|").concat(fl);
286 jalview.bin.Cache.setProperty("WS_PARAM_FILES", nparamFiles);
291 File pfile = new File(filename);
292 if (pfile.exists() && pfile.canWrite())
294 if (JOptionPane.showConfirmDialog(Desktop.instance,
295 "Delete the preset's file, too ?", "Delete User Preset ?",
296 JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION)
301 } catch (Exception e)
304 .error("Exception when trying to delete webservice user preset: ",
310 public void registerParser(String hosturl, ParamDatastoreI paramdataStore)
312 paramparsers.put(hosturl, paramdataStore);