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