src/main/java/de/unixwork/uwproj/Target.java

changeset 41
75ee588d5d9e
parent 40
b42bfc9e9983
child 52
afcb57893858
equal deleted inserted replaced
40:b42bfc9e9983 41:75ee588d5d9e
2 2
3 import org.w3c.dom.Element; 3 import org.w3c.dom.Element;
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.Arrays;
7 import java.util.LinkedList; 8 import java.util.LinkedList;
8 import java.util.List; 9 import java.util.List;
10
11 import static de.unixwork.uwproj.Util.isNotNullOrBlank;
12 import static de.unixwork.uwproj.Util.isNullOrBlank;
9 13
10 public class Target { 14 public class Target {
11 private String name; 15 private String name;
12 private String prefix; 16 private String prefix;
13 17
14 private final List<String> dependencies = new LinkedList<>(); 18 private final List<String> dependencies = new LinkedList<>();
15 private final List<String> optionalDependencies = new LinkedList<>(); 19 private final List<String> optionalDependencies = new LinkedList<>();
16 private final List<Define> defines = new LinkedList<>(); 20 private final List<Define> defines = new LinkedList<>();
17 private final List<Feature> features = new LinkedList<>(); 21 private final List<Feature> features = new LinkedList<>();
18 private final List<Option> options = new LinkedList<>(); 22 private final List<Option> options = new LinkedList<>();
19 23
20 private boolean allDependencies = false; 24 private boolean allDependencies = false;
21 25
22 public static Target parse(Project project, Element element) throws Exception { 26 public static Target parse(Project project, Element element) throws Exception {
23 Target target = new Target(); 27 var target = new Target();
24 28
25 String name = element.getAttribute("name"); 29 String name = element.getAttribute("name");
26 if(!name.isEmpty()) { 30 if (!name.isBlank()) {
27 target.setName(name); 31 target.setName(name);
28 } 32 }
29 33
30 NodeList nodes = element.getChildNodes(); 34 NodeList nodes = element.getChildNodes();
31 for(int i=0;i<nodes.getLength();i++) { 35 for (int i = 0; i < nodes.getLength(); i++) {
32 Node node = nodes.item(i); 36 Node node = nodes.item(i);
33 if (node.getNodeType() == Node.ELEMENT_NODE) { 37 if (node.getNodeType() == Node.ELEMENT_NODE) {
34 Element elm = (Element)node; 38 Element elm = (Element) node;
35 String n = elm.getNodeName(); 39 String n = elm.getNodeName();
36 if(n.equals("feature")) { 40 if (n.equals("feature")) {
37 Feature feature = Feature.parse(project, elm); 41 target.addFeature(Feature.parse(project, elm));
38 target.addFeature(feature); 42 } else if (n.equals("define")) {
39 } else if(n.equals("define")) { 43 target.addDefine(
40 String def = elm.getAttribute("name"); 44 elm.getAttribute("name"),
41 String defval = elm.getAttribute("value"); 45 elm.getAttribute("value")
42 if (def.isBlank()) { 46 );
43 throw new Exception("define element requires name attribute"); 47 } else if (n.equals("dependencies")) {
44 } 48 Arrays.stream(Util.getContent(elm).split(","))
45 target.addDefine(def, defval); 49 .map(String::trim)
46 } else if(n.equals("dependencies")) { 50 .forEach(target::addDependency);
47 String deps = Util.getContent(elm); 51 } else if (n.equals("alldependencies")) {
48 String[] dependencies = deps.split(",");
49 for(String dependency : dependencies) {
50 dependency = dependency.trim();
51 target.addDependency(dependency, false);
52 }
53 } else if(n.equals("alldependencies")) {
54 target.setAllDependencies(true); 52 target.setAllDependencies(true);
55 } else if(n.equals("option")) { 53 } else if (n.equals("option")) {
56 Option opt = Option.parse(project, elm); 54 var opt = Option.parse(elm);
57 target.addOption(opt); 55 target.addOption(opt);
58 project.addOption(opt); 56 project.addOption(opt);
59 } 57 }
60 } 58 }
61 } 59 }
62 60
63 if (target.prefix == null && target.name != null) { 61 if (isNullOrBlank(target.prefix) && isNotNullOrBlank(target.name)) {
64 target.setPrefix(name.toUpperCase()); 62 target.setPrefix(name.toUpperCase());
65 } 63 }
66 64
67 return target; 65 return target;
68 } 66 }
69 67
70 public String getName() { 68 public String getName() {
71 return name; 69 return name;
72 } 70 }
73 71
74 public void setName(String name) { 72 public void setName(String name) {
80 } 78 }
81 79
82 public void setPrefix(String prefix) { 80 public void setPrefix(String prefix) {
83 this.prefix = prefix; 81 this.prefix = prefix;
84 } 82 }
85 83
84 private String withPrefix(String id) {
85 return prefix == null ? id : String.format("%s_%s", prefix, id);
86 }
87
86 public String getCFlags() { 88 public String getCFlags() {
87 return prefix == null ? "CFLAGS" : prefix + "_CFLAGS"; 89 return withPrefix("CFLAGS");
88 } 90 }
89 91
90 public String getLDFlags() { 92 public String getLDFlags() {
91 return prefix == null ? "LDFLAGS" : prefix + "_LDFLAGS"; 93 return withPrefix("LDFLAGS");
92 } 94 }
93 95
94 public String getCXXFlags() { 96 public String getCXXFlags() {
95 return prefix == null ? "CXXFLAGS" : prefix + "_CXXFLAGS"; 97 return withPrefix("CXXFLAGS");
96 } 98 }
97 99
98 public List<String> getDependencies() { 100 public List<String> getDependencies() {
99 return dependencies; 101 return dependencies;
100 } 102 }
101 103
102 public void addDependency(String dependency, boolean optional) { 104 public void addDependency(String dependency, boolean optional) {
103 if(optional) { 105 if (optional) {
104 optionalDependencies.add(dependency); 106 optionalDependencies.add(dependency);
105 } else { 107 } else {
106 dependencies.add(dependency); 108 dependencies.add(dependency);
107 } 109 }
110 }
111
112 public void addDependency(String dependency) {
113 dependencies.add(dependency);
108 } 114 }
109 115
110 public List<String> getOptionalDependencies() { 116 public List<String> getOptionalDependencies() {
111 return optionalDependencies; 117 return optionalDependencies;
112 } 118 }
116 } 122 }
117 123
118 public void setAllDependencies(boolean allDependencies) { 124 public void setAllDependencies(boolean allDependencies) {
119 this.allDependencies = allDependencies; 125 this.allDependencies = allDependencies;
120 } 126 }
121 127
122 public void addDefine(String name, String value) { 128 public void addDefine(String name, String value) {
123 Define def = new Define(); 129 if (name.isBlank()) {
124 def.setName(name); 130 throw new IllegalArgumentException("define element requires name attribute");
125 def.setValue(value); 131 }
126 defines.add(def); 132 defines.add(new Define(name, value));
127 } 133 }
128 134
129 public List<Define> getDefines() { 135 public List<Define> getDefines() {
130 return defines; 136 return defines;
131 } 137 }
132 138
133 public void addFeature(Feature f) { 139 public void addFeature(Feature f) {
134 features.add(f); 140 features.add(f);
135 } 141 }
136 142
137 public List<Feature> getFeatures() { 143 public List<Feature> getFeatures() {
138 return features; 144 return features;
139 } 145 }
140 146
141 public void addOption(Option o) { 147 public void addOption(Option o) {
142 options.add(o); 148 options.add(o);
143 } 149 }
144 150
145 public List<Option> getOptions() { 151 public List<Option> getOptions() {
146 return options; 152 return options;
147 } 153 }
148 } 154 }

mercurial