-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathDot.cs
More file actions
255 lines (227 loc) · 6.78 KB
/
Copy pathDot.cs
File metadata and controls
255 lines (227 loc) · 6.78 KB
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
namespace KismetAnalyzer.Dot;
interface IStatement
{
void Write(TextWriter writer);
}
public class Graph : AbstractGraph, IStatement
{
public string Type { get; set; }
public Graph(string type)
{
Type = type;
}
public void Write(TextWriter writer)
{
writer.WriteLine(Type);
writer.WriteLine("{");
WriteStatements(writer);
writer.WriteLine("}");
}
}
public class Subgraph : AbstractGraph, IStatement
{
public string? Id { get; set; }
public Subgraph(string? id = null)
{
Id = id;
}
public void Write(TextWriter writer)
{
if (Id != null) writer.WriteLine(AbstractGraph.EscapeId(Id));
writer.WriteLine("{");
WriteStatements(writer);
writer.WriteLine("}");
}
}
public abstract class AbstractGraph
{
public Attributes Attributes = new Attributes();
public List<Node> Nodes { get; } = new List<Node>();
public List<Edge> Edges { get; } = new List<Edge>();
public List<Subgraph> Subgraphs { get; } = new List<Subgraph>();
public Attributes GraphAttributes { get; } = new Attributes();
public Attributes NodeAttributes { get; } = new Attributes();
public Attributes EdgeAttributes { get; } = new Attributes();
public virtual void WriteStatements(TextWriter writer)
{
Attributes.WriteStatements(writer);
WriteAttributes(writer, "graph", GraphAttributes);
WriteAttributes(writer, "node", NodeAttributes);
WriteAttributes(writer, "edge", EdgeAttributes);
foreach (var node in Nodes)
{
node.Write(writer);
}
foreach (var edge in Edges)
{
edge.Write(writer);
}
foreach (var subgraph in Subgraphs)
{
subgraph.Write(writer);
}
}
static void WriteAttributes(TextWriter writer, string label, Attributes attributes)
{
if (attributes.Count <= 0) return;
writer.Write($"{label} ");
attributes.Write(writer);
}
public static string EscapeId(string id)
{
return $"\"{id.Replace("\"", "\\\"")}\"";
}
}
public class Attributes : Dictionary<string, string>
{
public void Write(TextWriter writer)
{
writer.Write("[");
writer.Write(String.Join("; ", this.Select(attr => $"{attr.Key} = \"{attr.Value}\"").ToList()));
writer.WriteLine("]");
}
public void WriteStatements(TextWriter writer)
{
foreach (var attr in this)
{
writer.WriteLine($"{attr.Key} = \"{attr.Value}\";");
}
}
}
public class Node : IStatement
{
public string Id { get; set; }
public Attributes Attributes { get; } = new Attributes();
public IHtmlElement? HtmlLabel { get; set; }
public Node(string id)
{
Id = id;
}
public void Write(TextWriter writer)
{
writer.Write(AbstractGraph.EscapeId(Id));
if (HtmlLabel != null || Attributes.Count > 0)
{
writer.Write(" [");
var parts = new List<string>();
if (HtmlLabel != null)
{
var sw = new StringWriter();
sw.Write("<");
HtmlLabel.Write(sw);
sw.Write(">");
parts.Add($"label = {sw}");
}
parts.AddRange(Attributes.Select(attr => $"{attr.Key} = \"{attr.Value}\""));
writer.Write(string.Join("; ", parts));
writer.WriteLine("]");
}
else
{
writer.WriteLine();
}
}
}
public class Edge : IStatement
{
public string A { get; set; }
public string? ACompass { get; set; }
public string B { get; set; }
public string? BCompass { get; set; }
public Attributes Attributes { get; } = new Attributes();
public Edge(string a, string b)
{
A = a;
ACompass = null;
B = b;
BCompass = null;
}
public Edge(string a, string aCompass, string b, string bCompass)
{
A = a;
ACompass = aCompass;
B = b;
BCompass = bCompass;
}
public void Write(TextWriter writer)
{
writer.Write($"{AbstractGraph.EscapeId(A)}{(ACompass == null ? "" : ":" + ACompass)} -> {AbstractGraph.EscapeId(B)}{(BCompass == null ? "" : ":" + BCompass)}");
if (Attributes.Count > 0)
{
writer.Write(" ");
Attributes.Write(writer);
}
else
{
writer.WriteLine();
}
}
}
// HTML-like label support for GraphViz
public interface IHtmlElement
{
void Write(TextWriter writer);
}
public class HtmlText : IHtmlElement
{
public string Value { get; }
public HtmlText(string value) => Value = value;
public void Write(TextWriter writer)
{
writer.Write(Value
.Replace("&", "&")
.Replace("<", "<")
.Replace(">", ">")
.Replace("\"", """));
}
}
public class HtmlRaw : IHtmlElement
{
public string Value { get; }
public HtmlRaw(string value) => Value = value;
public void Write(TextWriter writer) => writer.Write(Value);
}
public class HtmlElement : IHtmlElement
{
public string Tag { get; }
public Dictionary<string, string> Attributes { get; } = new();
public List<IHtmlElement> Children { get; } = new();
public bool SelfClosing { get; set; }
public HtmlElement(string tag) => Tag = tag;
public HtmlElement Attr(string key, string value) { Attributes[key] = value; return this; }
public HtmlElement Add(IHtmlElement child) { Children.Add(child); return this; }
public HtmlElement Add(string text) { Children.Add(new HtmlText(text)); return this; }
public void Write(TextWriter writer)
{
writer.Write($"<{Tag}");
foreach (var attr in Attributes)
writer.Write($" {attr.Key}=\"{attr.Value}\"");
if (SelfClosing)
{
writer.Write("/>");
}
else
{
writer.Write(">");
foreach (var child in Children)
child.Write(writer);
writer.Write($"</{Tag}>");
}
}
}
public static class Html
{
public static HtmlElement Table(string? bgcolor = null)
{
var table = new HtmlElement("TABLE")
.Attr("BORDER", "0").Attr("CELLBORDER", "1").Attr("CELLSPACING", "0");
if (bgcolor != null) table.Attr("BGCOLOR", bgcolor);
return table;
}
public static HtmlElement Tr() => new("TR");
public static HtmlElement Td() => new("TD");
public static HtmlElement Font(string color) => new HtmlElement("FONT").Attr("COLOR", color);
public static HtmlElement Br() => new HtmlElement("BR") { SelfClosing = true };
public static HtmlText Text(string value) => new(value);
public static HtmlRaw Raw(string value) => new(value);
}