001: package com.opensymphony.webwork.sitegraph.model;
002:
003: import java.io.IOException;
004: import java.util.ArrayList;
005: import java.util.Iterator;
006: import java.util.List;
007:
008: /**
009: * User: plightbo
010: * Date: Jun 26, 2005
011: * Time: 4:53:57 PM
012: */
013: public class SubGraph implements Render {
014: protected String name;
015: protected SubGraph parent;
016: protected List subGraphs;
017: protected List nodes;
018:
019: public SubGraph(String name) {
020: this .name = name;
021: this .subGraphs = new ArrayList();
022: this .nodes = new ArrayList();
023: }
024:
025: public String getName() {
026: return name;
027: }
028:
029: public void addSubGraph(SubGraph subGraph) {
030: subGraph.setParent(this );
031: subGraphs.add(subGraph);
032: }
033:
034: public void setParent(SubGraph parent) {
035: this .parent = parent;
036: }
037:
038: public void addNode(SiteGraphNode node) {
039: node.setParent(this );
040: Graph.nodeMap.put(node.getFullName(), node);
041: nodes.add(node);
042: }
043:
044: public void render(IndentWriter writer) throws IOException {
045: // write the header
046: writer.write("subgraph cluster_" + getPrefix() + " {", true);
047: writer.write("color=grey;");
048: writer.write("fontcolor=grey;");
049: writer.write("label=\"" + name + "\";");
050:
051: // write out the subgraphs
052: for (Iterator iterator = subGraphs.iterator(); iterator
053: .hasNext();) {
054: SubGraph subGraph = (SubGraph) iterator.next();
055: subGraph.render(new IndentWriter(writer));
056: }
057:
058: // write out the actions
059: for (Iterator iterator = nodes.iterator(); iterator.hasNext();) {
060: SiteGraphNode siteGraphNode = (SiteGraphNode) iterator
061: .next();
062: siteGraphNode.render(writer);
063: }
064:
065: // .. footer
066: writer.write("}", true);
067: }
068:
069: public String getPrefix() {
070: if (parent == null) {
071: return name;
072: } else {
073: String prefix = parent.getPrefix();
074: if (prefix.equals("")) {
075: return name;
076: } else {
077: return prefix + "_" + name;
078: }
079: }
080: }
081:
082: public SubGraph create(String namespace) {
083: if (namespace.equals("")) {
084: return this ;
085: }
086:
087: String[] parts = namespace.split("\\/");
088: SubGraph last = this ;
089: for (int i = 0; i < parts.length; i++) {
090: String part = parts[i];
091: if (part.equals("")) {
092: continue;
093: }
094:
095: SubGraph subGraph = findSubGraph(part);
096: if (subGraph == null) {
097: subGraph = new SubGraph(part);
098: last.addSubGraph(subGraph);
099: }
100:
101: last = subGraph;
102: }
103:
104: return last;
105: }
106:
107: private SubGraph findSubGraph(String name) {
108: for (Iterator iterator = subGraphs.iterator(); iterator
109: .hasNext();) {
110: SubGraph subGraph = (SubGraph) iterator.next();
111: if (subGraph.getName().equals(name)) {
112: return subGraph;
113: }
114: }
115:
116: return null;
117: }
118: }
|