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

changeset 113
24f32dbd88cd
parent 92
5a32534f2604
--- a/src/main/java/de/unixwork/uwproj/Target.java	Sun Jan 28 13:26:47 2024 +0100
+++ b/src/main/java/de/unixwork/uwproj/Target.java	Sun Jan 28 14:02:41 2024 +0100
@@ -1,19 +1,18 @@
 package de.unixwork.uwproj;
 
 import org.w3c.dom.Element;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
 
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.LinkedList;
 import java.util.List;
 
-import static de.unixwork.uwproj.Util.*;
+import static de.unixwork.uwproj.Util.shId;
 import static java.util.function.Predicate.not;
 
-public class Target {
-    private String name = "";
-    private String prefix;
+public final class Target {
+    private final String name;
+    private final String prefix;
 
     private final List<String> dependencies = new LinkedList<>();
     private final List<Define> defines = new LinkedList<>();
@@ -22,67 +21,51 @@
 
     private boolean allDependencies = false;
 
-    public static Target parse(Project project, Element element) throws Exception {
-        var target = new Target();
+    public static Target defaultTarget() {
+        return new Target();
+    }
 
-        String name = element.getAttribute("name");
-        if (!name.isBlank()) {
-            target.setName(name);
-        }
+    private Target() {
+        name = "default";
+        prefix = "";
+        allDependencies = true;
+    }
 
-        NodeList nodes = element.getChildNodes();
-        for (int i = 0; i < nodes.getLength(); i++) {
-            Node node = nodes.item(i);
-            if (node.getNodeType() == Node.ELEMENT_NODE) {
-                Element elm = (Element) node;
-                String n = elm.getNodeName();
-                if (n.equals("feature")) {
-                    target.addFeature(Feature.parse(project, elm));
-                } else if (n.equals("define")) {
-                    target.addDefine(
-                            elm.getAttribute("name"),
-                            elm.getAttribute("value")
-                    );
-                } else if (n.equals("dependencies")) {
-                    Arrays.stream(Util.getContent(elm).split(","))
-                            .map(String::trim)
-                            .filter(not(String::isBlank))
-                            .forEach(target::addDependency);
-                } else if (n.equals("alldependencies")) {
-                    target.setAllDependencies(true);
-                } else if (n.equals("option")) {
-                    var opt = Option.parse(elm);
-                    target.addOption(opt);
-                    project.addOption(opt);
-                }
+    public Target(Element element) {
+        this.name = element.getAttribute("name").trim();
+        if (this.name.isEmpty()) {
+            prefix = "";
+        } else {
+            prefix = shId(name.toUpperCase());
+        }
+        Util.getChildElements(element).forEach(elm -> {
+            switch (elm.getNodeName()) {
+                case "feature" -> features.add(new Feature(elm));
+                case "define" -> defines.add(new Define(
+                        elm.getAttribute("name"),
+                        elm.getAttribute("value")
+                ));
+                case "dependencies" -> Arrays.stream(Util.getContent(elm).split(","))
+                        .map(String::trim)
+                        .filter(not(String::isBlank))
+                        .map(Util::shId)
+                        .forEach(dependencies::add);
+                case "alldependencies" -> this.allDependencies = true;
+                case "option" -> options.add(new Option(elm));
             }
-        }
-
-        if (isNullOrBlank(target.prefix) && isNotNullOrBlank(target.name)) {
-            target.setPrefix(name.toUpperCase());
-        }
-
-        return target;
+        });
     }
 
     public String getName() {
         return name;
     }
 
-    public void setName(String name) {
-        this.name = name;
-    }
-
     public String getPrefix() {
         return prefix;
     }
 
-    public void setPrefix(String prefix) {
-        this.prefix = prefix;
-    }
-
     private String withPrefix(String id) {
-        return prefix == null ? id : String.format("%s_%s", prefix, id);
+        return prefix.isEmpty() ? id : String.format("%s_%s", prefix, id);
     }
 
     public String getCFlags() {
@@ -97,50 +80,27 @@
         return withPrefix("CXXFLAGS");
     }
 
-    public void clearDependencies() {
-        dependencies.clear();
-    }
-
     public List<String> getDependencies() {
         return dependencies;
     }
 
-    public void addDependency(String dependency) {
-        // we have to add the sanitized identifier to the list of dependencies
-        dependencies.add(shId(dependency));
-    }
-
-    public boolean isAllDependencies() {
-        return allDependencies;
+    public void replaceAllDependencies(Collection<String> deps) {
+        dependencies.clear();
+        dependencies.addAll(deps);
     }
 
-    public void setAllDependencies(boolean allDependencies) {
-        this.allDependencies = allDependencies;
-    }
-
-    public void addDefine(String name, String value) {
-        if (name.isBlank()) {
-            throw new IllegalArgumentException("define element requires name attribute");
-        }
-        defines.add(new Define(name, value));
+    public boolean wantsAllDependencies() {
+        return allDependencies;
     }
 
     public List<Define> getDefines() {
         return defines;
     }
 
-    public void addFeature(Feature f) {
-        features.add(f);
-    }
-
     public List<Feature> getFeatures() {
         return features;
     }
 
-    public void addOption(Option o) {
-        options.add(o);
-    }
-
     public List<Option> getOptions() {
         return options;
     }

mercurial