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