--- a/src/main/java/de/unixwork/uwproj/Project.java Wed Sep 06 21:08:04 2023 +0200 +++ b/src/main/java/de/unixwork/uwproj/Project.java Wed Sep 06 22:55:42 2023 +0200 @@ -1,115 +1,93 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ package de.unixwork.uwproj; -import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; -import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import java.io.File; import java.util.*; -/** - * - * @author olaf - */ public class Project { - private String path; - private Properties properties = new Properties(); - - private HashMap<String,List<Dependency>> namedDependencies = new HashMap<>(); - private List<Dependency> dependencies = new LinkedList<>(); - private List<Target> targets = new LinkedList<>(); - private List<Option> options = new LinkedList<>(); - private List<Feature> features = new LinkedList<>(); - - private HashMap<String,ConfigVar> configVars = new HashMap<>(); - private List<ConfigVar> configVarsList = new LinkedList<>(); - + private final Properties properties = new Properties(); + + private final HashMap<String, List<Dependency>> namedDependencies = new HashMap<>(); + private final List<Dependency> dependencies = new LinkedList<>(); + private final List<Target> targets = new LinkedList<>(); + private final List<Option> options = new LinkedList<>(); + private final List<Feature> features = new LinkedList<>(); + + private final HashMap<String, ConfigVar> configVars = new HashMap<>(); + private final List<ConfigVar> configVarsList = new LinkedList<>(); + public Project(File file) throws Exception { - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - factory.setNamespaceAware(true); - DocumentBuilder builder = factory.newDocumentBuilder(); - Document doc = builder.parse(file); - - Element root = doc.getDocumentElement(); + var root = DocumentBuilderFactory. + newDefaultNSInstance(). + newDocumentBuilder(). + parse(file). + getDocumentElement(); NodeList dependency_nodes = root.getElementsByTagName("dependency"); NodeList target_nodes = root.getElementsByTagName("target"); NodeList property_nodes = root.getElementsByTagName("property"); - NodeList config_nodes = root.getElementsByTagName("config"); - for(int i=0;i<config_nodes.getLength();i++) { - Element p = (Element)config_nodes.item(i); + + for (int i = 0; i < config_nodes.getLength(); i++) { + Element p = (Element) config_nodes.item(i); NodeList cfgs = p.getChildNodes(); - for(int c=0;c<cfgs.getLength();c++) { + for (int c = 0; c < cfgs.getLength(); c++) { Node node = cfgs.item(c); if (node.getNodeType() == Node.ELEMENT_NODE) { - if(node.getNodeName().equals("var")) { - ConfigVar var = ConfigVar.parse((Element)node); - if(var != null) { - configVarsList.add(var); - configVars.put(var.getName(), var); - } + if (node.getNodeName().equals("var")) { + Optional.ofNullable(ConfigVar.parse((Element) node)).ifPresent(v -> { + configVars.put(v.getVarName(), v); + configVarsList.add(v); + }); } } } } - - for(int i=0;i<dependency_nodes.getLength();i++) { - addDependency(Dependency.parse((Element)dependency_nodes.item(i))); + + for (int i = 0; i < dependency_nodes.getLength(); i++) { + addDependency(Dependency.parse((Element) dependency_nodes.item(i))); } - for(int i=0;i<target_nodes.getLength();i++) { - targets.add(Target.parse(this, (Element)target_nodes.item(i))); + for (int i = 0; i < target_nodes.getLength(); i++) { + targets.add(Target.parse(this, (Element) target_nodes.item(i))); } - for(int i=0;i<property_nodes.getLength();i++) { - Element p = (Element)property_nodes.item(i); + for (int i = 0; i < property_nodes.getLength(); i++) { + var p = (Element) property_nodes.item(i); String name = p.getAttribute("name"); String value = p.getAttribute("value"); - if(name != null && value != null) { + if (!name.isBlank() && !value.isBlank()) { properties.setProperty(name, value); } else { throw new Exception("Missing property name or value"); } } - - if(targets.size() == 0) { + + if (targets.isEmpty()) { createDefaultTarget(); } } - + private void createDefaultTarget() { - Target t = new Target(); + var t = new Target(); t.setAllDependencies(true); t.setName("default"); addTarget(t); } - - public String getPath() { - return path; - } - public void setPath(String path) { - this.path = path; - } - public List<NamedDependency> getNamedDependencies() { - List<NamedDependency> ret = new LinkedList<>(); - - for(Map.Entry<String,List<Dependency>> entry : namedDependencies.entrySet()) { - NamedDependency d = new NamedDependency(); + var ret = new LinkedList<NamedDependency>(); + + for (var entry : namedDependencies.entrySet()) { + var d = new NamedDependency(); d.setName(entry.getKey()); d.setSubdependencies(entry.getValue()); ret.add(d); } return ret; } - + public List<Dependency> getDependencies() { return dependencies; } @@ -117,45 +95,41 @@ public List<Target> getTargets() { return targets; } - + public void addDependency(Dependency dependency) { if(dependency.getName() != null) { - List<Dependency> l = namedDependencies.get(dependency.getName()); - if(l == null) { - l = new LinkedList<>(); - namedDependencies.put(dependency.getName(), l); - } + List<Dependency> l = namedDependencies.computeIfAbsent(dependency.getName(), k -> new LinkedList<>()); dependency.setNum(l.size()); l.add(dependency); } else { dependencies.add(dependency); } } - + public void addTarget(Target target) { targets.add(target); } - + public void addOption(Option o) { options.add(o); } - + public List<Option> getOptions() { return options; } - + public void addFeature(Feature feature) { features.add(feature); } - + public List<Feature> getFeatures() { return features; } - + public List<ConfigVar> getVars() { return configVarsList; } - + public boolean hasVar(String varName) { return configVars.containsKey(varName); }