File: DotGraph.java

package info (click to toggle)
graphviz 14.1.2-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 139,476 kB
  • sloc: ansic: 142,288; cpp: 11,975; python: 7,883; makefile: 4,044; yacc: 3,030; xml: 2,972; tcl: 2,495; sh: 1,391; objc: 1,159; java: 560; lex: 423; perl: 243; awk: 156; pascal: 139; php: 58; ruby: 49; cs: 31; sed: 1
file content (282 lines) | stat: -rw-r--r-- 6,470 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//
// Time-stamp: <30.10.2003 14:33:33h liekweg>
//
// This library is distributed under the BSD license.
// See license.txt for more information.
//
// $Id$
//

package dot;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;

/**
   <P>Represent a Dot(ty) graph.</P>

   @since   Sun Oct 26 16:27:23 2003
   @author  Florian Liekweg <TT>&lt;liekweg@ipd.info.uni-karlsruhe.de&gt;</TT>, Universitt Karlsruhe (TH), Germany
 */

public class DotGraph extends DotCluster
{
  /**
     <P>Create a new Graph from the given filename and the given defaults.</P>
  */
  public DotGraph (String graph_title,
                   String filename,
                   DotGraphAttr [] graph_defaults,
                   DotNodeAttr [] node_defaults,
                   DotEdgeAttr [] edge_defaults)
  {
    super (node_defaults);

    _title = graph_title;
    _filename = filename;
    _graph_defaults = graph_defaults;
    _edge_defaults = edge_defaults;

    _clusters = new java.util.Vector ();
    _edges    = new java.util.Vector ();
  }

  /**
     <P>Create a new Graph from the given filename and the given defaults.</P>
  */
  public DotGraph (String graph_title,
                   String filename,
                   DotNodeAttr [] node_defalts,
                   DotEdgeAttr [] edge_defalts)
  {
    this (graph_title, filename, null, node_defalts, edge_defalts);
  }

  /**
     <P>Create a new Graph from the given filename.</P>
  */
  public DotGraph (String graph_title,
                   String filename)
  {
    this (graph_title, filename, null, new DotNodeAttr [0], new DotEdgeAttr [0]);
  }

  // CLUSTER
  /**
     <P>Get a new cluster in this graph.</P>
     @param name        The identifier of the cluster.
     @param label       The text in the cluster.
  */
  public DotCluster get_cluster (String name, String label)
  {
    DotCluster cluster = new DotCluster (_node_defaults, name, label);

    add_cluster (cluster);

    return (cluster);
  }

  /**
     <P>Get a new cluster in this graph.</P>
     @param name        The identifier of the cluster.
  */
  public DotCluster get_cluster (String name)
  {
    DotCluster cluster = new DotCluster (_node_defaults, name);

    add_cluster (cluster);

    return (cluster);
  }

  /**
     <P>Get a new cluster in this graph.</P>
  */
  public DotCluster get_cluster ()
  {
    DotCluster cluster = new DotCluster (_node_defaults);

    add_cluster (cluster);

    return (cluster);
  }

  /**
      <P>Add a cluster to this graph.</P>
  */
  private void add_cluster (DotCluster cluster)
  {
    _clusters.addElement (cluster);
  }

  // EDGES
  /**
     <P>Add an edge from the node havng 'from' as its name to the node that has 'to' as its name.</P>
     @param from    The identifier of the start node.
     @param to      The identifier of the end node.
     @param label   The text in the node.
  */
  public void add_edge (String from, String to)
  {
    _edges.addElement (new DotEdge (from, to, _edge_defaults));
  }

  /**
     <P>Add an edge from the node havng 'from' as its name to the node that has 'to' as its name.</P>
     @param from    The identifier of the start node.
     @param to      The identifier of the end node.
     @param label   The text in the node.
  */
  public void add_edge (String from, String to, DotEdgeAttr [] attrs)
  {
    _edges.addElement (new DotEdge (from, to, attrs));
  }

  // WRITING
  /**
     <P>Write the dot file to disk.</P>
  */
  public void write ()
  {
    PrintWriter out = null;

    try {

      out = new PrintWriter
        (new FileOutputStream
         (new File (_filename)), false);

    } catch (java.io.IOException e) {
      System.out.println (e.toString ());
    }

    write_intro (out);

    write_clusters (out);
    write_nodes (out);
    write_edges (out);

    write_extro (out);
  }

  /**
     <P>Write out all clusters.</P>

     @param out The PrintWriter to write to.
  */
  private void write_clusters (PrintWriter out)
  {
    int n_clusters = _clusters.size ();

    for (int i = 0; i < n_clusters; i ++) {
      DotCluster cluster = (DotCluster) _clusters.elementAt (i);

      cluster.write (out);
    }
  }

  /**
     <P>Write out the graph intro.</P>

     @param out The PrintWriter to write to.
  */
  private void write_intro (PrintWriter out)
  {
    StringBuffer buf;

    out.print ("digraph \"");
    out.print (_title);
    out.print ("\"\n");
    out.print ("{\n");

    buf = new StringBuffer ();
    buf.append ("\t");
    new DotLabelAttr (_title).write (buf);
    buf.append (";\n");

    // graph defaults
    if (null != _graph_defaults) {
      for (int i = 0; i < _graph_defaults.length; i ++) {
        buf.append ("\t");
        _graph_defaults [i].write (buf);

        if (! (_graph_defaults [i] instanceof DotCommentedAttr)) {
          buf.append (";\n");
        } else {
          buf.append ("\n");
        }
      }
    }
    out.print (buf.toString ());

    out.print ("\n");

    // node defaults
    out.print ("\tnode [");

    buf = new StringBuffer ();
    for (int i = 0; i < _node_defaults.length; i ++) {
      _node_defaults [i].write (buf);

      if (i != _node_defaults.length-1)
        buf.append (", ");
    }
    out.print (buf.toString ());
    out.print ("];\n");
    out.print ("\n");

    // edge defaults
    out.print ("\tedge [");

    buf = new StringBuffer ();
    for (int i = 0; i < _edge_defaults.length; i ++) {
      _edge_defaults [i].write (buf);

      if (i != _edge_defaults.length-1)
        buf.append (", ");
    }
    out.print (buf.toString ());
    out.print ("];\n");
    out.print ("\n");
  }

  /**
     <P>Write out all edges.</P>

     @param out The PrintWriter to write to.
  */
  private void write_edges (PrintWriter out)
  {
    StringBuffer buf = new StringBuffer ();

    int n_edges = _edges.size ();

    for (int i = 0; i < n_edges; i ++) {
      DotEdge edge = (DotEdge) _edges.elementAt (i);
      edge.write (buf);
    }

    out.print (buf);
  }

  /**
     <P>Write out the graph extro.</P>

     @param out The PrintWriter to write to.
  */
  private static void write_extro (PrintWriter out)
  {
    out.print ("}\n");
    out.close ();
  }


  private final String _title;
  private final String _filename;
  private final DotEdgeAttr [] _edge_defaults;
  private DotGraphAttr [] _graph_defaults;

  private final java.util.Vector _clusters;
  private final java.util.Vector _edges;
}