View Javadoc

1   /*
2    * $Id: SubGraph.java 454251 2006-10-09 02:10:57Z husted $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.sitegraph.model;
19  
20  import java.io.IOException;
21  import java.util.ArrayList;
22  import java.util.Iterator;
23  import java.util.List;
24  
25  /***
26   */
27  public class SubGraph implements Render {
28      protected String name;
29      protected SubGraph parent;
30      protected List subGraphs;
31      protected List nodes;
32  
33      public SubGraph(String name) {
34          this.name = name;
35          this.subGraphs = new ArrayList();
36          this.nodes = new ArrayList();
37      }
38  
39      public String getName() {
40          return name;
41      }
42  
43      public void addSubGraph(SubGraph subGraph) {
44          subGraph.setParent(this);
45          subGraphs.add(subGraph);
46      }
47  
48      public void setParent(SubGraph parent) {
49          this.parent = parent;
50      }
51  
52      public void addNode(SiteGraphNode node) {
53          node.setParent(this);
54          Graph.nodeMap.put(node.getFullName(), node);
55          nodes.add(node);
56      }
57  
58      public void render(IndentWriter writer) throws IOException {
59          // write the header
60          writer.write("subgraph cluster_" + getPrefix() + " {", true);
61          writer.write("color=grey;");
62          writer.write("fontcolor=grey;");
63          writer.write("label=\"" + name + "\";");
64  
65          // write out the subgraphs
66          for (Iterator iterator = subGraphs.iterator(); iterator.hasNext();) {
67              SubGraph subGraph = (SubGraph) iterator.next();
68              subGraph.render(new IndentWriter(writer));
69          }
70  
71          // write out the actions
72          for (Iterator iterator = nodes.iterator(); iterator.hasNext();) {
73              SiteGraphNode siteGraphNode = (SiteGraphNode) iterator.next();
74              siteGraphNode.render(writer);
75          }
76  
77          // .. footer
78          writer.write("}", true);
79      }
80  
81      public String getPrefix() {
82          if (parent == null) {
83              return name;
84          } else {
85              String prefix = parent.getPrefix();
86              if (prefix.equals("")) {
87                  return name;
88              } else {
89                  return prefix + "_" + name;
90              }
91          }
92      }
93  
94      public SubGraph create(String namespace) {
95          if (namespace.equals("")) {
96              return this;
97          }
98  
99          String[] parts = namespace.split("///");
100         SubGraph last = this;
101         for (int i = 0; i < parts.length; i++) {
102             String part = parts[i];
103             if (part.equals("")) {
104                 continue;
105             }
106 
107             SubGraph subGraph = findSubGraph(part);
108             if (subGraph == null) {
109                 subGraph = new SubGraph(part);
110                 last.addSubGraph(subGraph);
111             }
112 
113             last = subGraph;
114         }
115 
116         return last;
117     }
118 
119     private SubGraph findSubGraph(String name) {
120         for (Iterator iterator = subGraphs.iterator(); iterator.hasNext();) {
121             SubGraph subGraph = (SubGraph) iterator.next();
122             if (subGraph.getName().equals(name)) {
123                 return subGraph;
124             }
125         }
126 
127         return null;
128     }
129 }