View Javadoc

1   /*
2    * $Id: SubGraph.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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          // write the header
64          writer.write("subgraph cluster_" + getPrefix() + " {", true);
65          writer.write("color=grey;");
66          writer.write("fontcolor=grey;");
67          writer.write("label=\"" + name + "\";");
68  
69          // write out the subgraphs
70          for (Iterator iterator = subGraphs.iterator(); iterator.hasNext();) {
71              SubGraph subGraph = (SubGraph) iterator.next();
72              subGraph.render(new IndentWriter(writer));
73          }
74  
75          // write out the actions
76          for (Iterator iterator = nodes.iterator(); iterator.hasNext();) {
77              SiteGraphNode siteGraphNode = (SiteGraphNode) iterator.next();
78              siteGraphNode.render(writer);
79          }
80  
81          // .. footer
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 }