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