-
Notifications
You must be signed in to change notification settings - Fork 2
/
graphml.go
231 lines (203 loc) · 4.79 KB
/
graphml.go
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
package graphml
import (
"encoding/xml"
"io"
)
const (
// Ext is a file extension for GraphML files.
Ext = ".graphml"
// Namespace is a canonical XML namespace for GraphML.
Namespace = "http://graphml.graphdrawing.org/xmlns"
)
type element interface {
addAttr(a xml.Attr)
attrs() []xml.Attr
}
func newAttr(ns, local, value string) xml.Attr {
return xml.Attr{Name: xml.Name{Local: local, Space: ns}, Value: value}
}
// Document is a self-contained GraphML document.
type Document struct {
Instr xml.ProcInst
Attrs []xml.Attr
Keys []Key
Graphs []Graph `xml:"graph"`
Data []Data `xml:"data"`
}
// Object is a set of common attributes for nodes edges and graphs.
type Object struct {
ID string `xml:"id,attr"`
Unrecognized []xml.Attr `xml:",any,attr"`
}
func (o *Object) addAttr(a xml.Attr) {
switch a.Name.Local {
case "id":
o.ID = a.Value
default:
o.Unrecognized = append(o.Unrecognized, a)
}
}
func (o *Object) attrs() []xml.Attr {
out := make([]xml.Attr, 0, len(o.Unrecognized)+1)
if o.ID != "" {
out = append(out, newAttr("", "id", o.ID))
}
out = append(out, o.Unrecognized...)
return out
}
// ExtObject is a common set of attributes for nodes that can be extended.
type ExtObject struct {
Object
Data []Data `xml:"data"`
}
// NewKey creates a new custom attribute definition.
func NewKey(kind Kind, id, name, typ string) Key {
return Key{
Object: Object{
ID: id,
},
For: kind,
Name: name, Type: typ,
}
}
// Key is a definition of a custom attribute.
type Key struct {
Object
For Kind `xml:"for,attr"`
Name string `xml:"attr.name,attr"`
Type string `xml:"attr.type,attr"`
}
func (k *Key) addAttr(a xml.Attr) {
switch a.Name.Local {
case "for":
k.For = Kind(a.Value)
case "attr.name":
k.Name = a.Value
case "attr.type":
k.Type = a.Value
default:
k.Object.addAttr(a)
}
}
func (k *Key) attrs() []xml.Attr {
attrs := k.Object.attrs()
attrs = append(attrs, newAttr("", "for", string(k.For)))
if k.Name != "" {
attrs = append(attrs, newAttr("", "attr.name", k.Name))
}
if k.Type != "" {
attrs = append(attrs, newAttr("", "attr.type", k.Type))
}
return attrs
}
// Graph is a set of nodes and edges.
type Graph struct {
ExtObject
// EdgeDefault is a default direction mode for edges (directed or undirected).
EdgeDefault EdgeDir `xml:"edgedefault,attr"`
Nodes []Node `xml:"node"`
Edges []Edge `xml:"edge"`
}
func (g *Graph) addAttr(a xml.Attr) {
switch a.Name.Local {
case "edgedefault":
g.EdgeDefault = EdgeDir(a.Value)
default:
g.Object.addAttr(a)
}
}
func (g *Graph) attrs() []xml.Attr {
attrs := g.Object.attrs()
if g.EdgeDefault != "" {
attrs = append(attrs, newAttr("", "edgedefault", string(g.EdgeDefault)))
}
return attrs
}
// Node is a node in a graph.
type Node struct {
ExtObject
Graphs []Graph `xml:"graph"`
}
func (n *Node) addAttr(a xml.Attr) {
n.Object.addAttr(a)
}
func (n *Node) attrs() []xml.Attr {
return n.Object.attrs()
}
// Edge is a connection between two nodes in a graph.
type Edge struct {
ExtObject
Source string `xml:"source,attr"`
Target string `xml:"target,attr"`
}
func (e *Edge) addAttr(a xml.Attr) {
switch a.Name.Local {
case "source":
e.Source = a.Value
case "target":
e.Target = a.Value
default:
e.Object.addAttr(a)
}
}
func (e *Edge) attrs() []xml.Attr {
attrs := e.Object.attrs()
attrs = append(attrs,
newAttr("", "source", e.Source),
newAttr("", "target", e.Target),
)
return attrs
}
// Data is a raw XML value for a custom attribute.
type Data struct {
Key string `xml:"key,attr"`
Unrecognized []xml.Attr `xml:",any,attr"`
Data []xml.Token
}
// Reader returns a XML token reader for this custom attribute. See xml.NewTokenDecoder().
func (d *Data) Reader() xml.TokenReader {
return &tokenReader{tokens: d.Data}
}
func (d *Data) addAttr(a xml.Attr) {
switch a.Name.Local {
case "key":
d.Key = a.Value
default:
d.Unrecognized = append(d.Unrecognized, a)
}
}
func (d *Data) attrs() []xml.Attr {
attrs := make([]xml.Attr, 0, len(d.Unrecognized)+1)
attrs = append(attrs, newAttr("", "key", d.Key))
attrs = append(attrs, d.Unrecognized...)
return attrs
}
type tokenReader struct {
tokens []xml.Token
}
func (r *tokenReader) Token() (xml.Token, error) {
if len(r.tokens) == 0 {
return nil, io.EOF
}
t := r.tokens[0]
r.tokens = r.tokens[1:]
return t, nil
}
// EdgeDir is a direction mode for edges (directed or undirected).
type EdgeDir string
const (
EdgeDirected = EdgeDir("directed")
EdgeUndirected = EdgeDir("undirected")
)
// Kind is an element kind used for extensions.
type Kind string
const (
KindAll = Kind("all")
KindGraphML = Kind("graphml")
KindGraph = Kind("graph")
KindNode = Kind("node")
KindEdge = Kind("edge")
KindHyperEdge = Kind("hyperedge")
KindPort = Kind("port")
KindEndpoint = Kind("endpoint")
)