1
ļ»æ// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
3
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4
// software and associated documentation files (the "Software"), to deal in the Software
5
// without restriction, including without limitation the rights to use, copy, modify, merge,
6
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7
// to whom the Software is furnished to do so, subject to the following conditions:
9
// The above copyright notice and this permission notice shall be included in all copies or
10
// substantial portions of the Software.
12
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17
// DEALINGS IN THE SOFTWARE.
20
using System.Collections.Generic;
21
using System.Diagnostics;
22
using System.Globalization;
24
using System.Text.RegularExpressions;
26
namespace ICSharpCode.NRefactory.Utils
31
public sealed class GraphVizGraph
33
List<GraphVizNode> nodes = new List<GraphVizNode>();
34
List<GraphVizEdge> edges = new List<GraphVizEdge>();
36
public string rankdir;
39
public void AddEdge(GraphVizEdge edge)
44
public void AddNode(GraphVizNode node)
49
public void Save(string fileName)
51
using (StreamWriter writer = new StreamWriter(fileName))
60
public void Show(string name)
65
foreach (char c in Path.GetInvalidFileNameChars())
66
name = name.Replace(c, '-');
67
string fileName = name != null ? Path.Combine(Path.GetTempPath(), name) : Path.GetTempFileName();
68
Save(fileName + ".gv");
69
Process.Start("dot", "\"" + fileName + ".gv\" -Tpng -o \"" + fileName + ".png\"").WaitForExit();
70
Process.Start(fileName + ".png");
73
static string Escape(string text)
75
if (Regex.IsMatch(text, @"^[\w\d]+$")) {
78
return "\"" + text.Replace("\\", "\\\\").Replace("\r", "").Replace("\n", "\\n").Replace("\"", "\\\"") + "\"";
82
static void WriteGraphAttribute(TextWriter writer, string name, string value)
85
writer.WriteLine("{0}={1};", name, Escape(value));
88
internal static void WriteAttribute(TextWriter writer, string name, double? value, ref bool isFirst)
91
WriteAttribute(writer, name, value.Value.ToString(CultureInfo.InvariantCulture), ref isFirst);
95
internal static void WriteAttribute(TextWriter writer, string name, bool? value, ref bool isFirst)
98
WriteAttribute(writer, name, value.Value ? "true" : "false", ref isFirst);
102
internal static void WriteAttribute(TextWriter writer, string name, string value, ref bool isFirst)
109
writer.Write("{0}={1}", name, Escape(value));
113
public void Save(TextWriter writer)
116
throw new ArgumentNullException("writer");
117
writer.WriteLine("digraph G {");
118
writer.WriteLine("node [fontsize = 16];");
119
WriteGraphAttribute(writer, "rankdir", rankdir);
120
foreach (GraphVizNode node in nodes) {
123
foreach (GraphVizEdge edge in edges) {
126
writer.WriteLine("}");
130
public sealed class GraphVizEdge
132
public readonly string Source, Target;
134
/// <summary>edge stroke color</summary>
136
/// <summary>use edge to affect node ranking</summary>
137
public bool? constraint;
143
/// <summary>point size of label</summary>
144
public int? fontsize;
146
public GraphVizEdge(string source, string target)
149
throw new ArgumentNullException("source");
151
throw new ArgumentNullException("target");
152
this.Source = source;
153
this.Target = target;
156
public GraphVizEdge(int source, int target)
158
this.Source = source.ToString(CultureInfo.InvariantCulture);
159
this.Target = target.ToString(CultureInfo.InvariantCulture);
162
public void Save(TextWriter writer)
164
writer.Write("{0} -> {1} [", Source, Target);
166
GraphVizGraph.WriteAttribute(writer, "label", label, ref isFirst);
167
GraphVizGraph.WriteAttribute(writer, "style", style, ref isFirst);
168
GraphVizGraph.WriteAttribute(writer, "fontsize", fontsize, ref isFirst);
169
GraphVizGraph.WriteAttribute(writer, "color", color, ref isFirst);
170
GraphVizGraph.WriteAttribute(writer, "constraint", constraint, ref isFirst);
171
writer.WriteLine("];");
175
public sealed class GraphVizNode
177
public readonly string ID;
180
public string labelloc;
182
/// <summary>point size of label</summary>
183
public int? fontsize;
185
/// <summary>minimum height in inches</summary>
186
public double? height;
188
/// <summary>space around label</summary>
189
public string margin;
191
/// <summary>node shape</summary>
194
public GraphVizNode(string id)
197
throw new ArgumentNullException("id");
201
public GraphVizNode(int id)
203
this.ID = id.ToString(CultureInfo.InvariantCulture);
206
public void Save(TextWriter writer)
211
GraphVizGraph.WriteAttribute(writer, "label", label, ref isFirst);
212
GraphVizGraph.WriteAttribute(writer, "labelloc", labelloc, ref isFirst);
213
GraphVizGraph.WriteAttribute(writer, "fontsize", fontsize, ref isFirst);
214
GraphVizGraph.WriteAttribute(writer, "margin", margin, ref isFirst);
215
GraphVizGraph.WriteAttribute(writer, "shape", shape, ref isFirst);
216
writer.WriteLine("];");