Clover icon

Coverage Report

  1. Project Clover database Thu Nov 28 2024 11:45:30 GMT
  2. Package jalview.gui

File WsParamSetManager.java

 

Coverage histogram

../../img/srcFileCovDistChart1.png
59% of files have more coverage

Code metrics

46
112
5
1
335
264
37
0.33
22.4
5
7.4

Classes

Class Line # Actions
WsParamSetManager 58 112 37
0.0429447854.3%
 

Contributing tests

This file is covered by 95 tests. .

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
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.
11    *
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.
16    *
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.
20    */
21    package jalview.gui;
22   
23    import jalview.bin.Cache;
24    import jalview.bin.Console;
25    import jalview.io.JalviewFileChooser;
26    import jalview.io.JalviewFileView;
27    import jalview.util.MessageManager;
28    import jalview.ws.params.ParamDatastoreI;
29    import jalview.ws.params.ParamManager;
30    import jalview.ws.params.WsParamSetI;
31    import jalview.xml.binding.jalview.ObjectFactory;
32    import jalview.xml.binding.jalview.WebServiceParameterSet;
33   
34    import java.io.File;
35    import java.io.FileInputStream;
36    import java.io.FileOutputStream;
37    import java.io.IOException;
38    import java.io.InputStreamReader;
39    import java.io.OutputStreamWriter;
40    import java.io.PrintWriter;
41    import java.util.ArrayList;
42    import java.util.Hashtable;
43    import java.util.List;
44    import java.util.StringTokenizer;
45   
46    import javax.xml.bind.JAXBContext;
47    import javax.xml.bind.JAXBElement;
48    import javax.xml.bind.Marshaller;
49    import javax.xml.stream.XMLInputFactory;
50    import javax.xml.stream.XMLStreamReader;
51   
52    /**
53    * store and retrieve web service parameter sets.
54    *
55    * @author JimP
56    *
57    */
 
58    public class WsParamSetManager implements ParamManager
59    {
60    Hashtable<String, ParamDatastoreI> paramparsers = new Hashtable<>();
61   
 
62  726 toggle @Override
63    public WsParamSetI[] getParameterSet(String name, String serviceUrl,
64    boolean modifiable, boolean unmodifiable)
65    {
66  726 String files = Cache.getProperty("WS_PARAM_FILES");
67  726 if (files == null)
68    {
69  726 return null;
70    }
71  0 StringTokenizer st = new StringTokenizer(files, "|");
72  0 String pfile = null;
73  0 List<WsParamSetI> params = new ArrayList<>();
74  0 while (st.hasMoreTokens())
75    {
76  0 pfile = st.nextToken();
77  0 try
78    {
79  0 WsParamSetI[] pset = parseParamFile(pfile);
80  0 for (WsParamSetI p : pset)
81    {
82  0 boolean add = false;
83  0 if (serviceUrl != null)
84    {
85  0 for (String url : p.getApplicableUrls())
86    {
87  0 if (url.equals(serviceUrl))
88    {
89  0 add = true;
90    }
91    }
92    }
93    else
94    {
95  0 add = true;
96    }
97  0 add &= (modifiable == p.isModifiable()
98    || unmodifiable == !p.isModifiable());
99  0 add &= name == null || p.getName().equals(name);
100   
101  0 if (add)
102    {
103   
104  0 params.add(p);
105    }
106   
107    }
108    } catch (IOException e)
109    {
110  0 Console.info("Failed to parse parameter file " + pfile
111    + " (Check that all JALVIEW_WSPARAMFILES entries are valid!)",
112    e);
113    }
114    }
115  0 return params.toArray(new WsParamSetI[0]);
116    }
117   
 
118  0 toggle private WsParamSetI[] parseParamFile(String filename) throws IOException
119    {
120  0 List<WsParamSetI> psets = new ArrayList<>();
121  0 InputStreamReader is = new InputStreamReader(
122    new FileInputStream(new File(filename)), "UTF-8");
123   
124  0 WebServiceParameterSet wspset = null;
125  0 try
126    {
127  0 JAXBContext jc = JAXBContext
128    .newInstance("jalview.xml.binding.jalview");
129  0 javax.xml.bind.Unmarshaller um = jc.createUnmarshaller();
130  0 XMLStreamReader streamReader = XMLInputFactory.newInstance()
131    .createXMLStreamReader(is);
132  0 JAXBElement<WebServiceParameterSet> jbe = um.unmarshal(streamReader,
133    WebServiceParameterSet.class);
134  0 wspset = jbe.getValue();
135    } catch (Exception ex)
136    {
137  0 throw new IOException(ex);
138    }
139   
140  0 if (wspset != null && wspset.getParameters().length() > 0)
141    {
142  0 List<String> urls = wspset.getServiceURL();
143  0 final String[] urlArray = urls.toArray(new String[urls.size()]);
144   
145  0 for (String url : urls)
146    {
147  0 ParamDatastoreI parser = paramparsers.get(url);
148  0 if (parser != null)
149    {
150  0 WsParamSetI pset = parser.parseServiceParameterFile(
151    wspset.getName(), wspset.getDescription(), urlArray,
152    wspset.getParameters());
153  0 if (pset != null)
154    {
155  0 pset.setSourceFile(filename);
156  0 psets.add(pset);
157  0 break;
158    }
159    }
160    }
161    }
162   
163  0 return psets.toArray(new WsParamSetI[0]);
164    }
165   
 
166  0 toggle @Override
167    public void storeParameterSet(WsParamSetI parameterSet)
168    {
169  0 String filename = parameterSet.getSourceFile();
170  0 File outfile = null;
171  0 try
172    {
173  0 if (filename != null && !((outfile = new File(filename)).canWrite()))
174    {
175  0 Console.warn("Can't write to " + filename
176    + " - Prompting for new file to write to.");
177  0 filename = null;
178    }
179    } catch (Exception e)
180    {
181  0 filename = null;
182    }
183   
184  0 ParamDatastoreI parser = null;
185  0 for (String urls : parameterSet.getApplicableUrls())
186    {
187  0 if (parser == null)
188    {
189  0 parser = paramparsers.get(urls);
190    }
191    }
192  0 if (parser == null)
193    {
194  0 throw new Error(MessageManager.getString(
195    "error.implementation_error_cannot_find_marshaller_for_param_set"));
196    }
197  0 if (filename == null)
198    {
199    // TODO: JAL-3048 webservice - not required for Jalview-JS
200   
201  0 JalviewFileChooser chooser = new JalviewFileChooser("wsparams",
202    "Web Service Parameter File");
203  0 chooser.setFileView(new JalviewFileView());
204  0 chooser.setDialogTitle(MessageManager
205    .getString("label.choose_filename_for_param_file"));
206  0 chooser.setToolTipText(MessageManager.getString("action.save"));
207  0 int value = chooser.showSaveDialog(Desktop.instance);
208  0 if (value == JalviewFileChooser.APPROVE_OPTION)
209    {
210  0 outfile = chooser.getSelectedFile();
211  0 Cache.setProperty("LAST_DIRECTORY", outfile.getParent());
212  0 filename = outfile.getAbsolutePath();
213  0 if (!filename.endsWith(".wsparams"))
214    {
215  0 filename = filename.concat(".wsparams");
216  0 outfile = new File(filename);
217    }
218    }
219    }
220  0 if (outfile != null)
221    {
222  0 String paramFiles = Cache.getDefault("WS_PARAM_FILES", filename);
223  0 if (paramFiles.indexOf(filename) == -1)
224    {
225  0 if (paramFiles.length() > 0)
226    {
227  0 paramFiles = paramFiles.concat("|");
228    }
229  0 paramFiles = paramFiles.concat(filename);
230    }
231  0 Cache.setProperty("WS_PARAM_FILES", paramFiles);
232   
233  0 WebServiceParameterSet paramxml = new WebServiceParameterSet();
234   
235  0 paramxml.setName(parameterSet.getName());
236  0 paramxml.setDescription(parameterSet.getDescription());
237  0 for (String url : parameterSet.getApplicableUrls())
238    {
239  0 paramxml.getServiceURL().add(url);
240    }
241  0 paramxml.setVersion("1.0");
242  0 try
243    {
244  0 paramxml.setParameters(
245    parser.generateServiceParameterFile(parameterSet));
246  0 PrintWriter out = new PrintWriter(new OutputStreamWriter(
247    new FileOutputStream(outfile), "UTF-8"));
248  0 JAXBContext jaxbContext = JAXBContext
249    .newInstance(WebServiceParameterSet.class);
250  0 Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
251  0 jaxbMarshaller.marshal(
252    new ObjectFactory().createWebServiceParameterSet(paramxml),
253    out);
254  0 out.close();
255  0 parameterSet.setSourceFile(filename);
256    } catch (Exception e)
257    {
258  0 Console.error("Couldn't write parameter file to " + outfile, e);
259    }
260    }
261    }
262   
263    /*
264    *
265    * JalviewFileChooser chooser = new JalviewFileChooser(Cache
266    * .getProperty("LAST_DIRECTORY"), new String[] { "jc" }, new String[] {
267    * "Jalview User Colours" }, "Jalview User Colours"); chooser.setFileView(new
268    * jalview.io.JalviewFileView());
269    * chooser.setDialogTitle("Load colour scheme");
270    * chooser.setToolTipText("Load");
271    *
272    * int value = chooser.showOpenDialog(this);
273    *
274    * if (value == JalviewFileChooser.APPROVE_OPTION) { File choice =
275    * chooser.getSelectedFile(); Cache.setProperty("LAST_DIRECTORY",
276    * choice.getParent()); String defaultColours = Cache.getDefault(
277    * "USER_DEFINED_COLOURS", choice.getPath()); if
278    * (defaultColours.indexOf(choice.getPath()) == -1) { defaultColours =
279    * defaultColours.concat("|") .concat(choice.getPath()); } (non-Javadoc)
280    *
281    * @see
282    * jalview.ws.params.ParamManager#deleteParameterSet(jalview.ws.params.WsParamSetI
283    * )
284    */
 
285  0 toggle @Override
286    public void deleteParameterSet(WsParamSetI parameterSet)
287    {
288  0 String filename = parameterSet.getSourceFile();
289  0 if (filename == null || filename.trim().length() < 1)
290    {
291  0 return;
292    }
293  0 String paramFiles = Cache.getDefault("WS_PARAM_FILES", "");
294  0 if (paramFiles.indexOf(filename) > -1)
295    {
296  0 String nparamFiles = new String();
297  0 StringTokenizer st = new StringTokenizer(paramFiles, "|");
298  0 while (st.hasMoreElements())
299    {
300  0 String fl = st.nextToken();
301  0 if (!fl.equals(filename))
302    {
303  0 nparamFiles = nparamFiles.concat("|").concat(fl);
304    }
305    }
306  0 Cache.setProperty("WS_PARAM_FILES", nparamFiles);
307    }
308   
309  0 try
310    {
311  0 File pfile = new File(filename);
312  0 if (pfile.exists() && pfile.canWrite())
313    {
314  0 if (JvOptionPane.showConfirmDialog(Desktop.instance,
315    "Delete the preset's file, too ?", "Delete User Preset ?",
316    JvOptionPane.OK_CANCEL_OPTION) == JvOptionPane.OK_OPTION)
317    {
318  0 pfile.delete();
319    }
320    }
321    } catch (Exception e)
322    {
323  0 Console.error(
324    "Exception when trying to delete webservice user preset: ",
325    e);
326    }
327    }
328   
 
329  726 toggle @Override
330    public void registerParser(String hosturl, ParamDatastoreI paramdataStore)
331    {
332  726 paramparsers.put(hosturl, paramdataStore);
333    }
334   
335    }