--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/unixwork/uwproj/Project.java Mon Feb 04 15:09:39 2019 +0100 @@ -0,0 +1,140 @@ +/* + * 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 java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +/** + * + * @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<>(); + + public Project(File file) throws ParserConfigurationException, SAXException, IOException, Exception { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setNamespaceAware(true); + DocumentBuilder builder = factory.newDocumentBuilder(); + Document doc = builder.parse(file); + + Element root = doc.getDocumentElement(); + NodeList dependency_nodes = root.getElementsByTagName("dependency"); + NodeList target_nodes = root.getElementsByTagName("target"); + NodeList property_nodes = root.getElementsByTagName("property"); + + 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<property_nodes.getLength();i++) { + Element p = (Element)property_nodes.item(i); + String name = p.getAttribute("name"); + String value = p.getAttribute("value"); + if(name != null && value != null) { + properties.setProperty(name, value); + } else { + throw new Exception("Missing property name or value"); + } + } + + if(targets.size() == 0) { + createDefaultTarget(); + } + } + + private void createDefaultTarget() { + Target 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(); + d.setName(entry.getKey()); + d.setSubdependencies(entry.getValue()); + ret.add(d); + } + return ret; + } + + public List<Dependency> getDependencies() { + return dependencies; + } + + 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); + } + 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; + } +}