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