|
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; |
|
7 |
|
8 import java.io.File; |
|
9 import java.io.FileInputStream; |
|
10 import java.io.FileNotFoundException; |
|
11 import java.io.IOException; |
|
12 import java.util.HashMap; |
|
13 import java.util.LinkedList; |
|
14 import java.util.List; |
|
15 import java.util.Map; |
|
16 import java.util.Properties; |
|
17 import javax.xml.parsers.DocumentBuilder; |
|
18 import javax.xml.parsers.DocumentBuilderFactory; |
|
19 import javax.xml.parsers.ParserConfigurationException; |
|
20 import org.w3c.dom.Document; |
|
21 import org.w3c.dom.Element; |
|
22 import org.w3c.dom.NodeList; |
|
23 import org.xml.sax.SAXException; |
|
24 |
|
25 /** |
|
26 * |
|
27 * @author olaf |
|
28 */ |
|
29 public class Project { |
|
30 private String path; |
|
31 private Properties properties = new Properties(); |
|
32 |
|
33 private HashMap<String,List<Dependency>> namedDependencies = new HashMap<>(); |
|
34 private List<Dependency> dependencies = new LinkedList<>(); |
|
35 private List<Target> targets = new LinkedList<>(); |
|
36 private List<Option> options = new LinkedList<>(); |
|
37 private List<Feature> features = new LinkedList<>(); |
|
38 |
|
39 public Project(File file) throws ParserConfigurationException, SAXException, IOException, Exception { |
|
40 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
|
41 factory.setNamespaceAware(true); |
|
42 DocumentBuilder builder = factory.newDocumentBuilder(); |
|
43 Document doc = builder.parse(file); |
|
44 |
|
45 Element root = doc.getDocumentElement(); |
|
46 NodeList dependency_nodes = root.getElementsByTagName("dependency"); |
|
47 NodeList target_nodes = root.getElementsByTagName("target"); |
|
48 NodeList property_nodes = root.getElementsByTagName("property"); |
|
49 |
|
50 for(int i=0;i<dependency_nodes.getLength();i++) { |
|
51 addDependency(Dependency.parse((Element)dependency_nodes.item(i))); |
|
52 } |
|
53 for(int i=0;i<target_nodes.getLength();i++) { |
|
54 targets.add(Target.parse(this, (Element)target_nodes.item(i))); |
|
55 } |
|
56 for(int i=0;i<property_nodes.getLength();i++) { |
|
57 Element p = (Element)property_nodes.item(i); |
|
58 String name = p.getAttribute("name"); |
|
59 String value = p.getAttribute("value"); |
|
60 if(name != null && value != null) { |
|
61 properties.setProperty(name, value); |
|
62 } else { |
|
63 throw new Exception("Missing property name or value"); |
|
64 } |
|
65 } |
|
66 |
|
67 if(targets.size() == 0) { |
|
68 createDefaultTarget(); |
|
69 } |
|
70 } |
|
71 |
|
72 private void createDefaultTarget() { |
|
73 Target t = new Target(); |
|
74 t.setAllDependencies(true); |
|
75 t.setName("default"); |
|
76 addTarget(t); |
|
77 } |
|
78 |
|
79 public String getPath() { |
|
80 return path; |
|
81 } |
|
82 |
|
83 public void setPath(String path) { |
|
84 this.path = path; |
|
85 } |
|
86 |
|
87 public List<NamedDependency> getNamedDependencies() { |
|
88 List<NamedDependency> ret = new LinkedList<>(); |
|
89 |
|
90 for(Map.Entry<String,List<Dependency>> entry : namedDependencies.entrySet()) { |
|
91 NamedDependency d = new NamedDependency(); |
|
92 d.setName(entry.getKey()); |
|
93 d.setSubdependencies(entry.getValue()); |
|
94 ret.add(d); |
|
95 } |
|
96 return ret; |
|
97 } |
|
98 |
|
99 public List<Dependency> getDependencies() { |
|
100 return dependencies; |
|
101 } |
|
102 |
|
103 public List<Target> getTargets() { |
|
104 return targets; |
|
105 } |
|
106 |
|
107 public void addDependency(Dependency dependency) { |
|
108 if(dependency.getName() != null) { |
|
109 List<Dependency> l = namedDependencies.get(dependency.getName()); |
|
110 if(l == null) { |
|
111 l = new LinkedList<>(); |
|
112 namedDependencies.put(dependency.getName(), l); |
|
113 } |
|
114 dependency.setNum(l.size()); |
|
115 l.add(dependency); |
|
116 } else { |
|
117 dependencies.add(dependency); |
|
118 } |
|
119 } |
|
120 |
|
121 public void addTarget(Target target) { |
|
122 targets.add(target); |
|
123 } |
|
124 |
|
125 public void addOption(Option o) { |
|
126 options.add(o); |
|
127 } |
|
128 |
|
129 public List<Option> getOptions() { |
|
130 return options; |
|
131 } |
|
132 |
|
133 public void addFeature(Feature feature) { |
|
134 features.add(feature); |
|
135 } |
|
136 |
|
137 public List<Feature> getFeatures() { |
|
138 return features; |
|
139 } |
|
140 } |