View Javadoc

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