View Javadoc

    1   /*
    2    * $Id: SubGraph.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.ArrayList;
    25  import java.util.Iterator;
    26  import java.util.List;
    27
    28  /***
    29   */
    30  public class SubGraph implements Render {
    31      protected String name;
    32      protected SubGraph parent;
    33      protected List subGraphs;
    34      protected List nodes;
    35
    36      public SubGraph(String name) {
    37          this.name = name;
    38          this.subGraphs = new ArrayList();
    39          this.nodes = new ArrayList();
    40      }
    41
    42      public String getName() {
    43          return name;
    44      }
    45
    46      public void addSubGraph(SubGraph subGraph) {
    47          subGraph.setParent(this);
    48          subGraphs.add(subGraph);
    49      }
    50
    51      public void setParent(SubGraph parent) {
    52          this.parent = parent;
    53      }
    54
    55      public void addNode(SiteGraphNode node) {
    56          node.setParent(this);
    57          Graph.nodeMap.put(node.getFullName(), node);
    58          nodes.add(node);
    59      }
    60
    61      public void render(IndentWriter writer) throws IOException {
    62          // write the header
    63          writer.write("subgraph cluster_" + getPrefix() + " {", true);
    64          writer.write("color=grey;");
    65          writer.write("fontcolor=grey;");
    66          writer.write("label=\"" + name + "\";");
    67
    68          // write out the subgraphs
    69          for (Iterator iterator = subGraphs.iterator(); iterator.hasNext();) {
    70              SubGraph subGraph = (SubGraph) iterator.next();
    71              subGraph.render(new IndentWriter(writer));
    72          }
    73
    74          // write out the actions
    75          for (Iterator iterator = nodes.iterator(); iterator.hasNext();) {
    76              SiteGraphNode siteGraphNode = (SiteGraphNode) iterator.next();
    77              siteGraphNode.render(writer);
    78          }
    79
    80          // .. footer
    81          writer.write("}", true);
    82      }
    83
    84      public String getPrefix() {
    85          if (parent == null) {
    86              return name;
    87          } else {
    88              String prefix = parent.getPrefix();
    89              if (prefix.equals("")) {
    90                  return name;
    91              } else {
    92                  return prefix + "_" + name;
    93              }
    94          }
    95      }
    96
    97      public SubGraph create(String namespace) {
    98          if (namespace.equals("")) {
    99              return this;
    100         }
    101
    102         String[] parts = namespace.split("///");
    103         SubGraph last = this;
    104         for (int i = 0; i < parts.length; i++) {
    105             String part = parts[i];
    106             if (part.equals("")) {
    107                 continue;
    108             }
    109
    110             SubGraph subGraph = findSubGraph(part);
    111             if (subGraph == null) {
    112                 subGraph = new SubGraph(part);
    113                 last.addSubGraph(subGraph);
    114             }
    115
    116             last = subGraph;
    117         }
    118
    119         return last;
    120     }
    121
    122     private SubGraph findSubGraph(String name) {
    123         for (Iterator iterator = subGraphs.iterator(); iterator.hasNext();) {
    124             SubGraph subGraph = (SubGraph) iterator.next();
    125             if (subGraph.getName().equals(name)) {
    126                 return subGraph;
    127             }
    128         }
    129
    130         return null;
    131     }
    132 }