4 import org.w3c.dom.Node; |
4 import org.w3c.dom.Node; |
5 import org.w3c.dom.NodeList; |
5 import org.w3c.dom.NodeList; |
6 |
6 |
7 import java.util.LinkedList; |
7 import java.util.LinkedList; |
8 import java.util.List; |
8 import java.util.List; |
|
9 import java.util.stream.Collectors; |
|
10 |
|
11 import static de.unixwork.uwproj.Util.shId; |
9 |
12 |
10 public class Option { |
13 public class Option { |
11 private String arg; |
14 private String arg; |
12 |
15 |
13 LinkedList<OptionValue> values = new LinkedList<>(); |
16 private final LinkedList<OptionValue> values = new LinkedList<>(); |
14 LinkedList<OptionDefault> defaults = new LinkedList<>(); |
17 private final LinkedList<OptionDefault> defaults = new LinkedList<>(); |
15 |
18 |
16 public static Option parse(Project project, Element element) throws Exception { |
19 public static Option parse(Element element) throws Exception { |
17 Option opt = new Option(); |
20 var opt = new Option(); |
18 String arg = element.getAttribute("arg"); |
21 String arg = element.getAttribute("arg"); |
19 if(arg.isEmpty()) { |
22 if (arg.isBlank()) { |
20 throw new Exception("Option has no argument string"); |
23 throw new Exception("Option has no argument string"); |
21 } |
24 } |
22 opt.setArgument(arg); |
25 opt.setArgument(arg); |
23 |
26 |
24 NodeList nodes = element.getChildNodes(); |
27 NodeList nodes = element.getChildNodes(); |
25 for(int i=0;i<nodes.getLength();i++) { |
28 for (int i = 0; i < nodes.getLength(); i++) { |
26 Node node = nodes.item(i); |
29 Node node = nodes.item(i); |
27 if (node.getNodeType() == Node.ELEMENT_NODE) { |
30 if (node.getNodeType() == Node.ELEMENT_NODE) { |
28 Element elm = (Element)node; |
31 Element elm = (Element) node; |
29 String n = elm.getNodeName(); |
32 String n = elm.getNodeName(); |
30 if(n.equals("value")) { |
33 if (n.equals("value")) { |
31 OptionValue value = new OptionValue(opt); |
34 opt.values.add(new OptionValue(opt, |
32 value.setValue(elm.getAttribute("str")); |
35 elm.getAttribute("str"), |
33 value.setTargetData(TargetData.parse(elm)); |
36 TargetData.parse(elm) |
34 opt.values.add(value); |
37 )); |
35 } else if(n.equals("default")) { |
38 } else if (n.equals("default")) { |
36 String defValue = elm.getAttribute("value"); |
39 var def = new OptionDefault(opt); |
|
40 def.setValueName(elm.getAttribute("value")); |
37 String defPlatform = elm.getAttribute(("platform")); |
41 String defPlatform = elm.getAttribute(("platform")); |
38 OptionDefault def = new OptionDefault(opt); |
42 if (!defPlatform.isBlank()) { |
39 def.setValueName(defValue); |
|
40 if(!defPlatform.isEmpty()) { |
|
41 def.setPlatform(defPlatform); |
43 def.setPlatform(defPlatform); |
42 } |
44 } |
43 opt.defaults.add(def); |
45 opt.defaults.add(def); |
44 } |
46 } |
45 } |
47 } |
46 } |
48 } |
47 |
49 |
48 return opt; |
50 return opt; |
49 } |
51 } |
50 |
52 |
51 public String getArgument() { |
53 public String getArgument() { |
52 return arg; |
54 return arg; |
53 } |
55 } |
54 |
56 |
55 public void setArgument(String arg) { |
57 public void setArgument(String arg) { |
56 this.arg = arg; |
58 this.arg = arg; |
57 } |
59 } |
58 |
60 |
59 public String getVarName() { |
61 public String getVarName() { |
60 return "OPT_" + arg.toUpperCase(); |
62 return shId("OPT_" + arg.toUpperCase()); |
61 } |
63 } |
62 |
64 |
63 public String getArgValue() { |
65 public String getArgValue() { |
64 return "${ARG:"+(3+arg.length())+"}"; |
66 return "${ARG:" + (3 + arg.length()) + "}"; |
65 } |
67 } |
66 |
68 |
67 public List<OptionValue> getValues() { |
69 public List<OptionValue> getValues() { |
68 return values; |
70 return values; |
69 } |
71 } |
70 |
72 |
71 public List<OptionDefault> getDefaults() { |
73 public List<OptionDefault> getDefaults() { |
72 return defaults; |
74 return defaults; |
73 } |
75 } |
74 |
76 |
75 public String getValueFunc(String value) { |
77 public String getValueFunc(String value) { |
76 return "checkopt_"+arg+"_"+value; |
78 return shId("checkopt_" + arg + "_" + value); |
77 } |
79 } |
78 |
80 |
79 public String getValuesString() { |
81 public String getValuesString() { |
80 if(values.isEmpty()) { |
82 if (values.size() == 1) { |
81 return "()"; |
|
82 } else if(values.size() == 1) { |
|
83 return values.get(0).getValue(); |
83 return values.get(0).getValue(); |
84 } else { |
84 } else { |
85 StringBuilder vs = new StringBuilder(); |
85 return "(" + values.stream().map(OptionValue::getValue).collect(Collectors.joining("|")) + ")"; |
86 vs.append('('); |
|
87 for(int i=0;i<values.size();i++) { |
|
88 if(i < values.size()-1) { |
|
89 vs.append(values.get(i).getValue()); |
|
90 vs.append('|'); |
|
91 } else { |
|
92 vs.append(values.get(i).getValue()); |
|
93 } |
|
94 } |
|
95 vs.append(')'); |
|
96 return vs.toString(); |
|
97 } |
86 } |
98 } |
87 } |
99 } |
88 } |