1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts2.sitegraph.model;
22
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Iterator;
26 import java.util.List;
27
28 /***
29 */
30 public class SubGraph implements Render {
31 protected String name;
32 protected SubGraph parent;
33 protected List subGraphs;
34 protected List nodes;
35
36 public SubGraph(String name) {
37 this.name = name;
38 this.subGraphs = new ArrayList();
39 this.nodes = new ArrayList();
40 }
41
42 public String getName() {
43 return name;
44 }
45
46 public void addSubGraph(SubGraph subGraph) {
47 subGraph.setParent(this);
48 subGraphs.add(subGraph);
49 }
50
51 public void setParent(SubGraph parent) {
52 this.parent = parent;
53 }
54
55 public void addNode(SiteGraphNode node) {
56 node.setParent(this);
57 Graph.nodeMap.put(node.getFullName(), node);
58 nodes.add(node);
59 }
60
61 public void render(IndentWriter writer) throws IOException {
62
63 writer.write("subgraph cluster_" + getPrefix() + " {", true);
64 writer.write("color=grey;");
65 writer.write("fontcolor=grey;");
66 writer.write("label=\"" + name + "\";");
67
68
69 for (Iterator iterator = subGraphs.iterator(); iterator.hasNext();) {
70 SubGraph subGraph = (SubGraph) iterator.next();
71 subGraph.render(new IndentWriter(writer));
72 }
73
74
75 for (Iterator iterator = nodes.iterator(); iterator.hasNext();) {
76 SiteGraphNode siteGraphNode = (SiteGraphNode) iterator.next();
77 siteGraphNode.render(writer);
78 }
79
80
81 writer.write("}", true);
82 }
83
84 public String getPrefix() {
85 if (parent == null) {
86 return name;
87 } else {
88 String prefix = parent.getPrefix();
89 if (prefix.equals("")) {
90 return name;
91 } else {
92 return prefix + "_" + name;
93 }
94 }
95 }
96
97 public SubGraph create(String namespace) {
98 if (namespace.equals("")) {
99 return this;
100 }
101
102 String[] parts = namespace.split("///");
103 SubGraph last = this;
104 for (int i = 0; i < parts.length; i++) {
105 String part = parts[i];
106 if (part.equals("")) {
107 continue;
108 }
109
110 SubGraph subGraph = findSubGraph(part);
111 if (subGraph == null) {
112 subGraph = new SubGraph(part);
113 last.addSubGraph(subGraph);
114 }
115
116 last = subGraph;
117 }
118
119 return last;
120 }
121
122 private SubGraph findSubGraph(String name) {
123 for (Iterator iterator = subGraphs.iterator(); iterator.hasNext();) {
124 SubGraph subGraph = (SubGraph) iterator.next();
125 if (subGraph.getName().equals(name)) {
126 return subGraph;
127 }
128 }
129
130 return null;
131 }
132 }