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

changeset 40
b42bfc9e9983
parent 39
3ca85da78515
child 41
75ee588d5d9e
equal deleted inserted replaced
39:3ca85da78515 40:b42bfc9e9983
1 /*
2 * To change this license header, choose License Headers in Project Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 */
6 package de.unixwork.uwproj; 1 package de.unixwork.uwproj;
7 2
8 import org.w3c.dom.Document;
9 import org.w3c.dom.Element; 3 import org.w3c.dom.Element;
10 import org.w3c.dom.Node; 4 import org.w3c.dom.Node;
11 import org.w3c.dom.NodeList; 5 import org.w3c.dom.NodeList;
12 6
13 import javax.xml.parsers.DocumentBuilder;
14 import javax.xml.parsers.DocumentBuilderFactory; 7 import javax.xml.parsers.DocumentBuilderFactory;
15 import java.io.File; 8 import java.io.File;
16 import java.util.*; 9 import java.util.*;
17 10
18 /**
19 *
20 * @author olaf
21 */
22 public class Project { 11 public class Project {
23 private String path; 12 private final Properties properties = new Properties();
24 private Properties properties = new Properties(); 13
25 14 private final HashMap<String, List<Dependency>> namedDependencies = new HashMap<>();
26 private HashMap<String,List<Dependency>> namedDependencies = new HashMap<>(); 15 private final List<Dependency> dependencies = new LinkedList<>();
27 private List<Dependency> dependencies = new LinkedList<>(); 16 private final List<Target> targets = new LinkedList<>();
28 private List<Target> targets = new LinkedList<>(); 17 private final List<Option> options = new LinkedList<>();
29 private List<Option> options = new LinkedList<>(); 18 private final List<Feature> features = new LinkedList<>();
30 private List<Feature> features = new LinkedList<>(); 19
31 20 private final HashMap<String, ConfigVar> configVars = new HashMap<>();
32 private HashMap<String,ConfigVar> configVars = new HashMap<>(); 21 private final List<ConfigVar> configVarsList = new LinkedList<>();
33 private List<ConfigVar> configVarsList = new LinkedList<>(); 22
34
35 public Project(File file) throws Exception { 23 public Project(File file) throws Exception {
36 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 24 var root = DocumentBuilderFactory.
37 factory.setNamespaceAware(true); 25 newDefaultNSInstance().
38 DocumentBuilder builder = factory.newDocumentBuilder(); 26 newDocumentBuilder().
39 Document doc = builder.parse(file); 27 parse(file).
40 28 getDocumentElement();
41 Element root = doc.getDocumentElement();
42 NodeList dependency_nodes = root.getElementsByTagName("dependency"); 29 NodeList dependency_nodes = root.getElementsByTagName("dependency");
43 NodeList target_nodes = root.getElementsByTagName("target"); 30 NodeList target_nodes = root.getElementsByTagName("target");
44 NodeList property_nodes = root.getElementsByTagName("property"); 31 NodeList property_nodes = root.getElementsByTagName("property");
45
46 NodeList config_nodes = root.getElementsByTagName("config"); 32 NodeList config_nodes = root.getElementsByTagName("config");
47 for(int i=0;i<config_nodes.getLength();i++) { 33
48 Element p = (Element)config_nodes.item(i); 34 for (int i = 0; i < config_nodes.getLength(); i++) {
35 Element p = (Element) config_nodes.item(i);
49 NodeList cfgs = p.getChildNodes(); 36 NodeList cfgs = p.getChildNodes();
50 for(int c=0;c<cfgs.getLength();c++) { 37 for (int c = 0; c < cfgs.getLength(); c++) {
51 Node node = cfgs.item(c); 38 Node node = cfgs.item(c);
52 if (node.getNodeType() == Node.ELEMENT_NODE) { 39 if (node.getNodeType() == Node.ELEMENT_NODE) {
53 if(node.getNodeName().equals("var")) { 40 if (node.getNodeName().equals("var")) {
54 ConfigVar var = ConfigVar.parse((Element)node); 41 Optional.ofNullable(ConfigVar.parse((Element) node)).ifPresent(v -> {
55 if(var != null) { 42 configVars.put(v.getVarName(), v);
56 configVarsList.add(var); 43 configVarsList.add(v);
57 configVars.put(var.getName(), var); 44 });
58 }
59 } 45 }
60 } 46 }
61 } 47 }
62 } 48 }
63 49
64 for(int i=0;i<dependency_nodes.getLength();i++) { 50 for (int i = 0; i < dependency_nodes.getLength(); i++) {
65 addDependency(Dependency.parse((Element)dependency_nodes.item(i))); 51 addDependency(Dependency.parse((Element) dependency_nodes.item(i)));
66 } 52 }
67 for(int i=0;i<target_nodes.getLength();i++) { 53 for (int i = 0; i < target_nodes.getLength(); i++) {
68 targets.add(Target.parse(this, (Element)target_nodes.item(i))); 54 targets.add(Target.parse(this, (Element) target_nodes.item(i)));
69 } 55 }
70 for(int i=0;i<property_nodes.getLength();i++) { 56 for (int i = 0; i < property_nodes.getLength(); i++) {
71 Element p = (Element)property_nodes.item(i); 57 var p = (Element) property_nodes.item(i);
72 String name = p.getAttribute("name"); 58 String name = p.getAttribute("name");
73 String value = p.getAttribute("value"); 59 String value = p.getAttribute("value");
74 if(name != null && value != null) { 60 if (!name.isBlank() && !value.isBlank()) {
75 properties.setProperty(name, value); 61 properties.setProperty(name, value);
76 } else { 62 } else {
77 throw new Exception("Missing property name or value"); 63 throw new Exception("Missing property name or value");
78 } 64 }
79 } 65 }
80 66
81 if(targets.size() == 0) { 67 if (targets.isEmpty()) {
82 createDefaultTarget(); 68 createDefaultTarget();
83 } 69 }
84 } 70 }
85 71
86 private void createDefaultTarget() { 72 private void createDefaultTarget() {
87 Target t = new Target(); 73 var t = new Target();
88 t.setAllDependencies(true); 74 t.setAllDependencies(true);
89 t.setName("default"); 75 t.setName("default");
90 addTarget(t); 76 addTarget(t);
91 } 77 }
92
93 public String getPath() {
94 return path;
95 }
96 78
97 public void setPath(String path) {
98 this.path = path;
99 }
100
101 public List<NamedDependency> getNamedDependencies() { 79 public List<NamedDependency> getNamedDependencies() {
102 List<NamedDependency> ret = new LinkedList<>(); 80 var ret = new LinkedList<NamedDependency>();
103 81
104 for(Map.Entry<String,List<Dependency>> entry : namedDependencies.entrySet()) { 82 for (var entry : namedDependencies.entrySet()) {
105 NamedDependency d = new NamedDependency(); 83 var d = new NamedDependency();
106 d.setName(entry.getKey()); 84 d.setName(entry.getKey());
107 d.setSubdependencies(entry.getValue()); 85 d.setSubdependencies(entry.getValue());
108 ret.add(d); 86 ret.add(d);
109 } 87 }
110 return ret; 88 return ret;
111 } 89 }
112 90
113 public List<Dependency> getDependencies() { 91 public List<Dependency> getDependencies() {
114 return dependencies; 92 return dependencies;
115 } 93 }
116 94
117 public List<Target> getTargets() { 95 public List<Target> getTargets() {
118 return targets; 96 return targets;
119 } 97 }
120 98
121 public void addDependency(Dependency dependency) { 99 public void addDependency(Dependency dependency) {
122 if(dependency.getName() != null) { 100 if(dependency.getName() != null) {
123 List<Dependency> l = namedDependencies.get(dependency.getName()); 101 List<Dependency> l = namedDependencies.computeIfAbsent(dependency.getName(), k -> new LinkedList<>());
124 if(l == null) {
125 l = new LinkedList<>();
126 namedDependencies.put(dependency.getName(), l);
127 }
128 dependency.setNum(l.size()); 102 dependency.setNum(l.size());
129 l.add(dependency); 103 l.add(dependency);
130 } else { 104 } else {
131 dependencies.add(dependency); 105 dependencies.add(dependency);
132 } 106 }
133 } 107 }
134 108
135 public void addTarget(Target target) { 109 public void addTarget(Target target) {
136 targets.add(target); 110 targets.add(target);
137 } 111 }
138 112
139 public void addOption(Option o) { 113 public void addOption(Option o) {
140 options.add(o); 114 options.add(o);
141 } 115 }
142 116
143 public List<Option> getOptions() { 117 public List<Option> getOptions() {
144 return options; 118 return options;
145 } 119 }
146 120
147 public void addFeature(Feature feature) { 121 public void addFeature(Feature feature) {
148 features.add(feature); 122 features.add(feature);
149 } 123 }
150 124
151 public List<Feature> getFeatures() { 125 public List<Feature> getFeatures() {
152 return features; 126 return features;
153 } 127 }
154 128
155 public List<ConfigVar> getVars() { 129 public List<ConfigVar> getVars() {
156 return configVarsList; 130 return configVarsList;
157 } 131 }
158 132
159 public boolean hasVar(String varName) { 133 public boolean hasVar(String varName) {
160 return configVars.containsKey(varName); 134 return configVars.containsKey(varName);
161 } 135 }
162 } 136 }

mercurial