-
Notifications
You must be signed in to change notification settings - Fork 2
/
encoder.go
130 lines (124 loc) · 2.82 KB
/
encoder.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
package graphml
import (
"encoding/xml"
"io"
)
// Encode writes a GraphML document to the stream.
func Encode(w io.Writer, doc *Document) error {
enc := xml.NewEncoder(w)
return EncodeTo(enc, doc)
}
// EncodeTo is similar to Encode, but allows to provide a custom XML encoder.
func EncodeTo(enc *xml.Encoder, doc *Document) error {
d := &docEncoder{enc: enc}
if err := d.Encode(doc); err != nil {
return err
}
return enc.Flush()
}
func mlName(name string) xml.Name {
return xml.Name{Local: name}
}
type docEncoder struct {
enc *xml.Encoder
err error
}
func (d *docEncoder) token(t xml.Token) error {
if d.err == nil {
d.err = d.enc.EncodeToken(t)
}
return d.err
}
func (d *docEncoder) start(name xml.Name, attrs []xml.Attr) error {
return d.token(xml.StartElement{Name: name, Attr: attrs})
}
func (d *docEncoder) end(name xml.Name) error {
return d.token(xml.EndElement{Name: name})
}
func (d *docEncoder) startEnd(name xml.Name, attrs []xml.Attr) error {
t := xml.StartElement{Name: name, Attr: attrs}
if err := d.token(t); err != nil {
return err
}
return d.token(t.End())
}
func (d *docEncoder) Encode(doc *Document) error {
if err := d.token(doc.Instr); err != nil {
return err
}
if err := d.start(mlName("graphml"), doc.Attrs); err != nil {
return err
}
for _, k := range doc.Keys {
if err := d.startEnd(mlName("key"), k.attrs()); err != nil {
return err
}
}
for _, g := range doc.Graphs {
if err := d.encodeGraph(&g); err != nil {
return err
}
}
if err := d.encodeData(doc.Data); err != nil {
return err
}
return d.end(mlName("graphml"))
}
func (d *docEncoder) encodeData(data []Data) error {
for _, dt := range data {
if err := d.start(mlName("data"), dt.attrs()); err != nil {
return err
}
for _, t := range dt.Data {
if err := d.token(t); err != nil {
return err
}
}
if err := d.end(mlName("data")); err != nil {
return err
}
}
return nil
}
func (d *docEncoder) encodeGraph(g *Graph) error {
if err := d.start(mlName("graph"), g.attrs()); err != nil {
return err
}
if err := d.encodeData(g.Data); err != nil {
return err
}
for _, n := range g.Nodes {
if err := d.encodeNode(&n); err != nil {
return err
}
}
for _, e := range g.Edges {
if err := d.encodeEdge(&e); err != nil {
return err
}
}
return d.end(mlName("graph"))
}
func (d *docEncoder) encodeNode(n *Node) error {
if err := d.start(mlName("node"), n.attrs()); err != nil {
return err
}
if err := d.encodeData(n.Data); err != nil {
return err
}
for _, g := range n.Graphs {
if err := d.encodeGraph(&g); err != nil {
return err
}
}
return d.end(mlName("node"))
}
func (d *docEncoder) encodeEdge(e *Edge) error {
if err := d.start(mlName("edge"), e.attrs()); err != nil {
return err
}
if err := d.encodeData(e.Data); err != nil {
return err
}
return d.end(mlName("edge"))
}