--- a/src/main/java/de/unixwork/uwproj/Target.java Wed Sep 06 22:55:42 2023 +0200 +++ b/src/main/java/de/unixwork/uwproj/Target.java Sun Sep 10 12:52:10 2023 +0200 @@ -4,69 +4,67 @@ import org.w3c.dom.Node; import org.w3c.dom.NodeList; +import java.util.Arrays; import java.util.LinkedList; import java.util.List; +import static de.unixwork.uwproj.Util.isNotNullOrBlank; +import static de.unixwork.uwproj.Util.isNullOrBlank; + public class Target { private String name; private String prefix; - + private final List<String> dependencies = new LinkedList<>(); private final List<String> optionalDependencies = new LinkedList<>(); private final List<Define> defines = new LinkedList<>(); private final List<Feature> features = new LinkedList<>(); private final List<Option> options = new LinkedList<>(); - + private boolean allDependencies = false; - + public static Target parse(Project project, Element element) throws Exception { - Target target = new Target(); - + var target = new Target(); + String name = element.getAttribute("name"); - if(!name.isEmpty()) { + if (!name.isBlank()) { target.setName(name); } - + NodeList nodes = element.getChildNodes(); - for(int i=0;i<nodes.getLength();i++) { + for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { - Element elm = (Element)node; + Element elm = (Element) node; String n = elm.getNodeName(); - if(n.equals("feature")) { - Feature feature = Feature.parse(project, elm); - target.addFeature(feature); - } else if(n.equals("define")) { - String def = elm.getAttribute("name"); - String defval = elm.getAttribute("value"); - if (def.isBlank()) { - throw new Exception("define element requires name attribute"); - } - target.addDefine(def, defval); - } else if(n.equals("dependencies")) { - String deps = Util.getContent(elm); - String[] dependencies = deps.split(","); - for(String dependency : dependencies) { - dependency = dependency.trim(); - target.addDependency(dependency, false); - } - } else if(n.equals("alldependencies")) { + 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) + .forEach(target::addDependency); + } else if (n.equals("alldependencies")) { target.setAllDependencies(true); - } else if(n.equals("option")) { - Option opt = Option.parse(project, elm); + } else if (n.equals("option")) { + var opt = Option.parse(elm); target.addOption(opt); project.addOption(opt); } } } - - if (target.prefix == null && target.name != null) { + + if (isNullOrBlank(target.prefix) && isNotNullOrBlank(target.name)) { target.setPrefix(name.toUpperCase()); } - + return target; } - + public String getName() { return name; } @@ -82,17 +80,21 @@ public void setPrefix(String prefix) { this.prefix = prefix; } - + + private String withPrefix(String id) { + return prefix == null ? id : String.format("%s_%s", prefix, id); + } + public String getCFlags() { - return prefix == null ? "CFLAGS" : prefix + "_CFLAGS"; + return withPrefix("CFLAGS"); } - + public String getLDFlags() { - return prefix == null ? "LDFLAGS" : prefix + "_LDFLAGS"; + return withPrefix("LDFLAGS"); } - + public String getCXXFlags() { - return prefix == null ? "CXXFLAGS" : prefix + "_CXXFLAGS"; + return withPrefix("CXXFLAGS"); } public List<String> getDependencies() { @@ -100,13 +102,17 @@ } public void addDependency(String dependency, boolean optional) { - if(optional) { + if (optional) { optionalDependencies.add(dependency); } else { dependencies.add(dependency); } } + public void addDependency(String dependency) { + dependencies.add(dependency); + } + public List<String> getOptionalDependencies() { return optionalDependencies; } @@ -118,30 +124,30 @@ public void setAllDependencies(boolean allDependencies) { this.allDependencies = allDependencies; } - + public void addDefine(String name, String value) { - Define def = new Define(); - def.setName(name); - def.setValue(value); - defines.add(def); + if (name.isBlank()) { + throw new IllegalArgumentException("define element requires name attribute"); + } + defines.add(new Define(name, value)); } - + 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; }