Clover icon

jalviewX

  1. Project Clover database Wed Oct 31 2018 15:13:58 GMT
  2. Package jalview.gui

File WsParamSetManager.java

 

Coverage histogram

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

Code metrics

46
105
5
1
313
244
37
0.35
21
5
7.4

Classes

Class Line # Actions
WsParamSetManager 48 105 37 149
0.0448717964.5%
 

Contributing tests

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