View Javadoc

1   /*
2    * $Id: Graph.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.Iterator;
22  import java.util.LinkedHashMap;
23  import java.util.Map;
24  import java.util.Set;
25  import java.util.TreeSet;
26  
27  /***
28   */
29  public class Graph extends SubGraph {
30      private Set links;
31      public static Map nodeMap = new LinkedHashMap();
32  
33      public Graph() {
34          super("");
35          this.links = new TreeSet();
36      }
37  
38      public void addLink(Link link) {
39          links.add(link);
40      }
41  
42      public void render(IndentWriter writer) throws IOException {
43          // write out the header
44          writer.write("digraph mygraph {", true);
45          writer.write("fontsize=10;");
46          writer.write("fontname=helvetica;");
47          writer.write("node [fontsize=10, fontname=helvetica, style=filled, shape=rectangle]");
48          writer.write("edge [fontsize=10, fontname=helvetica]");
49  
50          // render all the subgraphs
51          for (Iterator iterator = subGraphs.iterator(); iterator.hasNext();) {
52              SubGraph subGraph = (SubGraph) iterator.next();
53              subGraph.render(new IndentWriter(writer));
54          }
55  
56          // render all the nodes
57          for (Iterator iterator = nodes.iterator(); iterator.hasNext();) {
58              SiteGraphNode siteGraphNode = (SiteGraphNode) iterator.next();
59              siteGraphNode.render(writer);
60          }
61  
62          // finally, render the links
63          for (Iterator iterator = links.iterator(); iterator.hasNext();) {
64              Link link = (Link) iterator.next();
65              link.render(writer);
66          }
67  
68          // and now the footer
69          writer.write("}", true);
70      }
71  
72      public SiteGraphNode findNode(String location, SiteGraphNode ref) {
73          if (location.startsWith("/")) {
74              location = location.substring(1);
75          } else {
76              // not absolute, so use the reference node
77              String prefix = null;
78              if (ref.getParent() != null) {
79                  prefix = ref.getParent().getPrefix();
80                  location = prefix + "_" + location;
81              }
82          }
83  
84          location = location.replaceAll("[//./////-//$//{//}]", "_");
85  
86          return (SiteGraphNode) nodeMap.get(location);
87      }
88  }