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