1 package de.matthias_burbach.deputy.core;
2
3 import java.io.InputStream;
4 import java.util.HashMap;
5 import java.util.Iterator;
6 import java.util.Map;
7
8 import org.jdom.Document;
9
10 import de.matthias_burbach.deputy.core.project.Project;
11 import de.matthias_burbach.deputy.core.util.XmlUtils;
12
13 /***
14 * Is the abstract base class for the two dependency graph generators.
15 *
16 * @author Matthias Burbach
17 */
18 public abstract class AbstractDependencyGraphGenerator {
19
20 /***
21 * @param project The project to export.
22 * @param outputFileName The absolute file path and name to store the export
23 * under.
24 * @throws Exception if anything goes unexpectedly wrong
25 */
26 public void generateDependencyGraph(
27 final Project project,
28 final String outputFileName)
29 throws Exception {
30 InputStream is =
31 getClass().getResourceAsStream(getTemplateFileName());
32 Document document = XmlUtils.loadXmlDocument(is);
33
34 generateProjectElements(project, document);
35 generateDependencyElements(project, document);
36
37 XmlUtils.saveXmlDocument(outputFileName, document);
38 }
39
40 /***
41 * @return The name of the template XML file to use.
42 */
43 protected abstract String getTemplateFileName();
44
45 /***
46 * @param topProject The top project which is the root of the graph.
47 * @param document The XML document to generate project nodes into.
48 * @throws Exception if anything goes unexpectedly wrong
49 */
50 protected abstract void generateProjectElements(
51 final Project topProject,
52 final Document document)
53 throws Exception;
54
55 /***
56 * @param topProject The top project which is the root of the graph.
57 * @param document The XML document to generate dependency nodes into.
58 * @throws Exception if anything goes unexpectedly wrong
59 */
60 protected abstract void generateDependencyElements(
61 final Project topProject,
62 final Document document)
63 throws Exception;
64
65 /***
66 * @param project The top project which is the root of the graph.
67 * @return The iterator over all projects of type {@link Project} that can
68 * be reached from the top project.
69 */
70 protected Iterator getNodeIterator(final Project project) {
71 Map nodeMap = new HashMap();
72 fillNodeMap(project, nodeMap);
73 return nodeMap.values().iterator();
74 }
75
76 /***
77 * Adds all projects that can be reached from project to the map
78 * if they are not already contained in the map.
79 *
80 * @param project The top project which is the root of the (sub) graph.
81 * @param map The map of project keys to projects to be filled.
82 */
83 protected void fillNodeMap(final Project project, final Map map) {
84 String key =
85 getProjectHashKey(project.getArtifactId(), project.getVersion());
86 if (map.get(key) == null) {
87 map.put(key, project);
88 Iterator iterator = project.getDependencies();
89 while (iterator.hasNext()) {
90 Project dependencyProject = (Project) iterator.next();
91 fillNodeMap(dependencyProject, map);
92 }
93 }
94 }
95
96 /***
97 * @param artifactId The artifact id of the project to create a key for.
98 * @param version The version of the project to create a key for.
99 * @return The key to store a project under in a map.
100 */
101 protected String getProjectHashKey(
102 final String artifactId,
103 final String version) {
104 return artifactId + "-" + version;
105 }
106 }