1 package de.matthias_burbach.deputy.core;
2
3 import java.awt.Font;
4 import java.awt.FontMetrics;
5 import java.util.Iterator;
6
7 import javax.swing.JLabel;
8
9 import org.jdom.Document;
10 import org.jdom.Element;
11 import org.jdom.Namespace;
12 import org.jdom.xpath.XPath;
13
14 import de.matthias_burbach.deputy.core.project.Project;
15
16 /***
17 * Exports the dependency graph of the project being loaded into Deputy in the
18 * GraphML format that can be imported into the yEd Graph Editor which is
19 * available for free from http://www.yworks.com.
20 *
21 * @author Matthias Burbach
22 */
23 public class DependencyGraphMLGenerator
24 extends AbstractDependencyGraphGenerator {
25
26
27
28
29
30
31 /***
32 * {@inheritDoc}
33 */
34 protected String getTemplateFileName() {
35 return "dependency-graph-ml-template.xml";
36 }
37
38
39
40
41
42
43
44
45 /***
46 * {@inheritDoc}
47 */
48 protected void generateProjectElements(
49 final Project topProject,
50 final Document document)
51 throws Exception {
52 Element graphElement =
53 (Element) XPath.newInstance(
54 "graphml/graph").selectSingleNode(
55 document);
56 Iterator iter = getNodeIterator(topProject);
57 while (iter.hasNext()) {
58 Project project = (Project) iter.next();
59 String labelText =
60 project.getArtifactId() + "-" + project.getVersion();
61
62
63
64
65 Element nodeElement = new Element("node");
66 nodeElement.setAttribute(
67 "id",
68 getProjectHashKey(
69 project.getArtifactId(),
70 project.getVersion()));
71
72
73
74
75 Element dataElement = new Element("data");
76 dataElement.setAttribute("key", "d0");
77
78 Namespace ns = Namespace.getNamespace(
79 "y", "http://www.yworks.com/xml/graphml");
80
81
82
83
84 Element shapeNodeElement = new Element("ShapeNode");
85 shapeNodeElement.setNamespace(ns);
86
87
88
89 Element geometryElement = new Element("Geometry");
90 geometryElement.setNamespace(ns);
91 geometryElement.setAttribute("x", "10.0");
92 geometryElement.setAttribute("y", "10.0");
93
94 final int fontSize = 12;
95 final String fontFamily = "Dialog";
96 Font font = new Font(fontFamily, Font.PLAIN, fontSize);
97 FontMetrics fontMetrics = new JLabel().getFontMetrics(font);
98 final int margin = 4;
99 int width = fontMetrics.stringWidth(labelText) + margin;
100 int height = fontMetrics.getHeight() + 2;
101 geometryElement.setAttribute("width", width + ".0");
102 geometryElement.setAttribute("height", height + ".0");
103
104
105
106
107 Element fillElement = new Element("Fill");
108 fillElement.setNamespace(ns);
109 fillElement.setAttribute("color", "#CCCCFF");
110 fillElement.setAttribute("transparent", "false");
111
112
113
114
115 Element borderStyleElement = new Element("BorderStyle");
116 borderStyleElement.setNamespace(ns);
117 borderStyleElement.setAttribute("type", "line");
118 borderStyleElement.setAttribute("width", "1.0");
119 borderStyleElement.setAttribute("color", "#000000");
120
121
122
123
124 Element nodeLabelElement = new Element("NodeLabel");
125 nodeLabelElement.setNamespace(ns);
126 nodeLabelElement.setAttribute("visible", "true");
127 nodeLabelElement.setAttribute("alignment", "center");
128 nodeLabelElement.setAttribute("fontFamily", fontFamily);
129 nodeLabelElement.setAttribute("fontSize", fontSize + "");
130 nodeLabelElement.setAttribute("fontFamily", fontFamily);
131 nodeLabelElement.setAttribute("fontStyle", "plain");
132 nodeLabelElement.setAttribute("textColor", "#000000");
133 nodeLabelElement.setAttribute("modelName", "internal");
134 nodeLabelElement.setAttribute("modelPosition", "c");
135 nodeLabelElement.setAttribute("autoSizePolicy", "content");
136 nodeLabelElement.setText(labelText);
137
138
139
140
141 Element shapeElement = new Element("Shape");
142 shapeElement.setNamespace(ns);
143 shapeElement.setAttribute("type", "rectangle");
144
145
146
147
148 shapeNodeElement.addContent(geometryElement);
149 shapeNodeElement.addContent(fillElement);
150 shapeNodeElement.addContent(borderStyleElement);
151 shapeNodeElement.addContent(nodeLabelElement);
152 shapeNodeElement.addContent(shapeElement);
153 dataElement.addContent(shapeNodeElement);
154 nodeElement.addContent(dataElement);
155 graphElement.addContent(nodeElement);
156 }
157 }
158
159
160
161
162
163
164
165
166 /***
167 * {@inheritDoc}
168 */
169 protected void generateDependencyElements(
170 final Project topProject,
171 final Document document)
172 throws Exception {
173 Element graphElement =
174 (Element) XPath.newInstance(
175 "graphml/graph").selectSingleNode(
176 document);
177 Iterator iter = getNodeIterator(topProject);
178 while (iter.hasNext()) {
179 Project project = (Project) iter.next();
180 Iterator iterator = project.getDependencies();
181 while (iterator.hasNext()) {
182 Project dependencyProject = (Project) iterator.next();
183
184
185
186 Element edgeElement = new Element("edge");
187 String source =
188 getProjectHashKey(
189 project.getArtifactId(),
190 project.getVersion());
191 String target =
192 getProjectHashKey(
193 dependencyProject.getArtifactId(),
194 dependencyProject.getVersion());
195 edgeElement.setAttribute("source", source);
196 edgeElement.setAttribute("target", target);
197
198
199
200
201 Element dataElement = new Element("data");
202 dataElement.setAttribute("key", "d1");
203
204 Namespace ns = Namespace.getNamespace(
205 "y", "http://www.yworks.com/xml/graphml");
206
207
208
209
210 Element polyLineEdgeElement = new Element("PolyLineEdge");
211 polyLineEdgeElement.setNamespace(ns);
212
213
214
215
216 Element lineStyleElement = new Element("LineStyle");
217 lineStyleElement.setNamespace(ns);
218 lineStyleElement.setAttribute("type", "line");
219 lineStyleElement.setAttribute("width", "1.0");
220 lineStyleElement.setAttribute("color", "#000000");
221
222
223
224
225 Element arrowsElement = new Element("Arrows");
226 arrowsElement.setNamespace(ns);
227 arrowsElement.setAttribute("source", "none");
228 arrowsElement.setAttribute("target", "standard");
229
230
231
232
233 Element bendStyleElement = new Element("BendStyle");
234 bendStyleElement.setNamespace(ns);
235 bendStyleElement.setAttribute("smoothed", "false");
236
237
238
239
240 polyLineEdgeElement.addContent(lineStyleElement);
241 polyLineEdgeElement.addContent(arrowsElement);
242 polyLineEdgeElement.addContent(bendStyleElement);
243 dataElement.addContent(polyLineEdgeElement);
244 edgeElement.addContent(dataElement);
245 graphElement.addContent(edgeElement);
246 }
247 }
248 }
249 }