JAL-1378 JWS-71 update to from-source build of JABA v2.1 client for use with JABA...
[jalview.git] / src / jalview / ws / jws2 / ParameterUtils.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3  * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
10  *  
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.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.ws.jws2;
19
20 import java.net.MalformedURLException;
21 import java.util.ArrayList;
22 import java.util.HashSet;
23 import java.util.List;
24
25 import compbio.metadata.*;
26
27 public class ParameterUtils
28 {
29   public static List<String> writeParameterSet(List<Option> optSet,
30           String pseparator)
31   {
32     List<String> pset = new ArrayList<String>();
33     for (Option o : optSet)
34     {
35       pset.add(o.toCommand(pseparator));
36     }
37     return pset;
38   }
39
40   /**
41    * Converts options supplied via parameters file into {@code Option} objects
42    * (Refactored from compbio.ws.client.Jws2Client)
43    * 
44    * @param <T>
45    *          web service type
46    * @param params
47    * @param options
48    * @return List of Options of type T
49    * 
50    */
51   /*
52    * @SuppressWarnings(value = { "true" }) public static <T> List<Option<T>>
53    * processParameters(List<String> params, RunnerConfig<T> options, String
54    * pseparator)
55    */
56   public static List<Option> processParameters(List<String> params,
57           RunnerConfig options, String pseparator)
58   {
59     List<Option> chosenOptions = new ArrayList<Option>();
60     for (String param : params)
61     {
62       String oname = null;
63       if (isParameter(param, pseparator))
64       {
65         oname = getParamName(param, pseparator);
66       }
67       else
68       {
69         oname = param;
70       }
71       Option o = options.getArgumentByOptionName(oname);
72       if (o == null)
73       {
74         System.out.println("WARN ignoring unsuppoted parameter: " + oname);
75         continue;
76       }
77       if (o instanceof Parameter)
78       {
79         o = copyParameter((Parameter) o);
80       }
81       else
82       {
83         o = copyOption(o);
84       }
85       {
86         try
87         {
88           o.setDefaultValue(isParameter(param, pseparator) ? getParamValue(
89                   param, pseparator) : param);
90         } catch (WrongParameterException e)
91         {
92           System.out.println("Problem setting value for the parameter: "
93                   + param);
94           e.printStackTrace();
95         }
96       }
97       chosenOptions.add(o);
98     }
99     return chosenOptions;
100   }
101
102   static String getParamName(String fullName, String pseparator)
103   {
104     assert isParameter(fullName, pseparator);
105     return fullName.substring(0, fullName.indexOf(pseparator));
106   }
107
108   static String getParamValue(String fullName, String pseparator)
109   {
110     assert isParameter(fullName, pseparator);
111     return fullName.substring(fullName.indexOf(pseparator) + 1);
112   }
113
114   static boolean isParameter(String param, String pseparator)
115   {
116     return param.contains(pseparator);
117   }
118
119   public static Option copyOption(Option option)
120   {
121     Option copy = new Option(option.getName(), option.getDescription());
122     setOptionFrom(copy, option);
123     return copy;
124   }
125
126   public static void setOptionFrom(Option copy, Option option)
127   {
128     copy.setName(option.getName());
129     copy.setDescription(option.getDescription());
130     copy.setBasicURL(option.getBasicURL());
131     copy.setFurtherDetails(option.getFurtherDetails());
132     copy.setRequired(option.isRequired());
133     List<String> names = option.getOptionNames();
134     if (names != null)
135     {
136       if (names.size() == 1)
137       {
138         HashSet<String> st = new HashSet();
139         st.add(names.get(0));
140         copy.setOptionNames(st);
141       }
142       else
143       {
144         copy.addOptionNames(names.toArray(new String[]
145         {}));
146       }
147     }
148     try
149     {
150       if (option.getDefaultValue() != null)
151       {
152         copy.setDefaultValue(option.getDefaultValue());
153       }
154     } catch (Exception ex)
155     {
156       ex.printStackTrace();
157     }
158   }
159
160   public static ValueConstrain copyValueConstrain(ValueConstrain vc)
161   {
162     try
163     {
164       ValueConstrain copy = new ValueConstrain();
165       if (vc.getMax() != null)
166       {
167         copy.setMax(vc.getMax().toString());
168       }
169       if (vc.getMin() != null)
170       {
171         copy.setMin(vc.getMin().toString());
172       }
173       if (vc.getType() != null)
174       {
175         copy.setType(vc.getType());
176       }
177       return copy;
178     } catch (Exception e)
179     {
180       e.printStackTrace();
181       throw new Error(
182               "Implementation error: could not copy ValueConstrain!");
183     }
184   }
185
186   public static Parameter copyParameter(Parameter parameter)
187   {
188     Parameter copy = new Parameter(parameter.getName(),
189             parameter.getDescription());
190     if (parameter.getValidValue() != null)
191     {
192       copy.setValidValue(copyValueConstrain(parameter.getValidValue()));
193     }
194     List<String> pv = parameter.getPossibleValues();
195     if (pv != null)
196     {
197       copy.addPossibleValues(pv.toArray(new String[]
198       {}));
199     }
200     setOptionFrom(copy, parameter);
201     return copy;
202   }
203
204 }