forked from dennwc/graphml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder.go
366 lines (358 loc) · 7.63 KB
/
decoder.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package graphml
import (
"bytes"
"encoding/xml"
"fmt"
"io"
)
// Decode reads a GraphML document from the stream.
func Decode(r io.Reader) (*Document, error) {
dec := xml.NewDecoder(r)
return DecodeFrom(dec)
}
// DecodeFrom is similar to Decode, but allows to specify a custom XML decoder.
func DecodeFrom(dec *xml.Decoder) (*Document, error) {
b := &docDecoder{
doc: new(Document),
keysAll: make(map[string]Key),
keys: make(map[docKey]Key),
ids: make(map[string]struct{}),
}
if err := b.DecodeFrom(dec); err != nil {
return nil, err
}
return b.doc, nil
}
func canSkip(t xml.Token) bool {
switch t := t.(type) {
case xml.Comment:
return true
case xml.CharData:
if len(bytes.TrimSpace([]byte(t))) == 0 {
return true
}
}
return false
}
type docKey struct {
name string
kind Kind
}
type docDecoder struct {
dec *xml.Decoder
keysAll map[string]Key
keys map[docKey]Key
ids map[string]struct{}
lastID int
doc *Document
}
func (d *docDecoder) token() (xml.Token, error) {
return d.dec.Token()
}
func (d *docDecoder) expectEnd(tok xml.Name) error {
for {
t, err := d.token()
if err == io.EOF {
return io.ErrUnexpectedEOF
} else if err != nil {
return err
} else if canSkip(t) {
continue
}
switch t := t.(type) {
case xml.EndElement:
if t.Name == tok {
return nil
}
}
return fmt.Errorf("unexpected token: %T: %#v", t, t)
}
}
func (d *docDecoder) startGraphML() (xml.StartElement, error) {
for {
t, err := d.token()
if err == io.EOF {
return xml.StartElement{}, io.ErrUnexpectedEOF
} else if err != nil {
return xml.StartElement{}, err
} else if canSkip(t) {
continue
}
switch t := t.(type) {
case xml.ProcInst:
d.doc.Instr = t.Copy()
continue
case xml.StartElement:
if t.Name.Local == "graphml" && t.Name.Space == Namespace {
d.doc.Attrs = t.Copy().Attr
return t, nil
}
}
return xml.StartElement{}, fmt.Errorf("unexpected token: %T: %#v", t, t)
}
}
func (d *docDecoder) DecodeFrom(dec *xml.Decoder) error {
d.dec = dec
start, err := d.startGraphML()
if err != nil {
return err
}
for {
t, err := d.token()
if err == io.EOF {
return io.ErrUnexpectedEOF
} else if err != nil {
return err
} else if canSkip(t) {
continue
}
switch t := t.(type) {
case xml.StartElement:
if t.Name.Space != Namespace {
return fmt.Errorf("unexpected element: %v", t.Name)
}
switch t.Name.Local {
case "key":
if err := d.decodeKey(t); err != nil {
return err
}
case "graph":
g, err := d.decodeGraph(t)
if err != nil {
return err
}
d.doc.Graphs = append(d.doc.Graphs, *g)
case "data":
data, err := d.decodeData(KindGraphML, t)
if err != nil {
return err
}
d.doc.Data = append(d.doc.Data, *data)
default:
return fmt.Errorf("unknown element: %v", t.Name)
}
continue
case xml.EndElement:
if t.Name == start.Name {
return nil
}
}
return fmt.Errorf("unexpected token: %T: %#v", t, t)
}
}
func (d *docDecoder) decodeKey(start xml.StartElement) error {
var k Key
for _, a := range start.Attr {
k.addAttr(a)
}
if k.For == "" {
k.For = KindAll
}
if k.For == KindAll {
if _, ok := d.keysAll[k.ID]; ok {
return fmt.Errorf("redefinition of key %q", k.ID)
}
d.keysAll[k.ID] = k
} else {
dk := docKey{name: k.ID, kind: k.For}
if _, ok := d.keys[dk]; ok {
return fmt.Errorf("redefinition of key %q for %v", k.ID, k.For)
}
d.keys[dk] = k
}
d.doc.Keys = append(d.doc.Keys, k)
if err := d.expectEnd(start.Name); err != nil {
return err
}
return nil
}
func (d *docDecoder) addID(id string) (string, error) {
if id == "" {
return "", nil
}
if _, ok := d.ids[id]; ok {
return "", fmt.Errorf("redefinition of id %q", id)
}
d.ids[id] = struct{}{}
return id, nil
}
func (d *docDecoder) decodeGraph(start xml.StartElement) (*Graph, error) {
var g Graph
for _, a := range start.Attr {
g.addAttr(a)
}
var err error
g.ID, err = d.addID(g.ID)
if err != nil {
return nil, err
}
if err := d.decodeGraphNodes(&g, start); err != nil {
return nil, err
}
return &g, nil
}
func (d *docDecoder) decodeGraphNodes(g *Graph, start xml.StartElement) error {
for {
t, err := d.token()
if err == io.EOF {
return io.ErrUnexpectedEOF
} else if err != nil {
return err
} else if canSkip(t) {
continue
}
switch t := t.(type) {
case xml.StartElement:
if t.Name.Space != Namespace {
return fmt.Errorf("unexpected element: %v", t.Name)
}
switch t.Name.Local {
case "data":
data, err := d.decodeData(KindGraph, t)
if err != nil {
return err
}
g.Data = append(g.Data, *data)
case "node":
n, err := d.decodeNode(t)
if err != nil {
return err
}
g.Nodes = append(g.Nodes, *n)
case "edge":
e, err := d.decodeEdge(t)
if err != nil {
return err
}
g.Edges = append(g.Edges, *e)
default:
return fmt.Errorf("unknown element: %v", t.Name)
}
continue
case xml.EndElement:
if t.Name == start.Name {
return nil
}
}
return fmt.Errorf("unexpected token: %T: %#v", t, t)
}
}
func (d *docDecoder) decodeData(kind Kind, start xml.StartElement) (*Data, error) {
var data Data
for _, a := range start.Attr {
data.addAttr(a)
}
if _, ok := d.keys[docKey{name: data.Key, kind: kind}]; !ok {
if _, ok = d.keysAll[data.Key]; !ok {
return nil, fmt.Errorf("unexpected attr for %v: %q", kind, data.Key)
}
}
for {
t, err := d.token()
if err == io.EOF {
return nil, io.ErrUnexpectedEOF
} else if err != nil {
return nil, err
}
switch e := t.(type) {
case xml.EndElement:
if e.Name == start.Name {
return &data, nil
}
}
t = xml.CopyToken(t)
data.Data = append(data.Data, t)
}
}
func (d *docDecoder) decodeNode(start xml.StartElement) (*Node, error) {
var n Node
for _, a := range start.Attr {
n.addAttr(a)
}
var err error
n.ID, err = d.addID(n.ID)
if err != nil {
return nil, err
}
for {
t, err := d.token()
if err == io.EOF {
return nil, io.ErrUnexpectedEOF
} else if err != nil {
return nil, err
} else if canSkip(t) {
continue
}
switch t := t.(type) {
case xml.StartElement:
if t.Name.Space != Namespace {
return nil, fmt.Errorf("unexpected element: %v", t.Name)
}
switch t.Name.Local {
case "data":
data, err := d.decodeData(KindNode, t)
if err != nil {
return nil, err
}
n.Data = append(n.Data, *data)
case "graph":
g, err := d.decodeGraph(t)
if err != nil {
return nil, err
}
n.Graphs = append(n.Graphs, *g)
default:
return nil, fmt.Errorf("unknown element: %v", t.Name)
}
continue
case xml.EndElement:
if t.Name == start.Name {
return &n, nil
}
}
return nil, fmt.Errorf("unexpected token: %T: %#v", t, t)
}
}
func (d *docDecoder) decodeEdge(start xml.StartElement) (*Edge, error) {
var e Edge
for _, a := range start.Attr {
e.addAttr(a)
}
var err error
e.ID, err = d.addID(e.ID)
if err != nil {
return nil, err
}
for {
t, err := d.token()
if err == io.EOF {
return nil, io.ErrUnexpectedEOF
} else if err != nil {
return nil, err
} else if canSkip(t) {
continue
}
switch t := t.(type) {
case xml.StartElement:
if t.Name.Space != Namespace {
return nil, fmt.Errorf("unexpected element: %v", t.Name)
}
switch t.Name.Local {
case "data":
data, err := d.decodeData(KindEdge, t)
if err != nil {
return nil, err
}
e.Data = append(e.Data, *data)
default:
return nil, fmt.Errorf("unknown element: %v", t.Name)
}
continue
case xml.EndElement:
if t.Name == start.Name {
return &e, nil
}
}
return nil, fmt.Errorf("unexpected token: %T: %#v", t, t)
}
}