-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
146 lines (116 loc) · 2.89 KB
/
node.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
// Copyright 2018 Joe Beda
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package svgdata
import (
"encoding/xml"
"fmt"
)
const (
SvgNs = "http://www.w3.org/2000/svg"
)
type onMarshalAttrsFunc func(attr AttrMap)
type onUnmarshalAttrsFunc func(attr AttrMap) error
type Node interface {
xml.Unmarshaler
xml.Marshaler
Name() string
Attrs() AttrMap
Children() *[]Node
AddChild(n Node)
GetText() string
SetText(t string)
}
type nodeImpl struct {
name string
attrs AttrMap
children []Node
text string
// onMarshalAddrs is called during marshalling with a copy of the Attrs that the can be modified before marshalling.
onMarshalAttrs onMarshalAttrsFunc
// onUmarshalAttrs is called during unmarshalling. The calling function can modify the Attrs as necessary and
// changes will be stored with the node.
onUnmarshalAttrs onUnmarshalAttrsFunc
}
var _ Node = (*nodeImpl)(nil)
func (n *nodeImpl) Name() string {
return n.name
}
func (n *nodeImpl) Attrs() AttrMap {
if n.attrs == nil {
n.attrs = AttrMap{}
}
return n.attrs
}
func (n *nodeImpl) Children() *[]Node {
return &n.children
}
func (n *nodeImpl) AddChild(c Node) {
n.children = append(n.children, c)
}
func (n *nodeImpl) GetText() string {
return n.text
}
func (n *nodeImpl) SetText(t string) {
n.text = t
}
func (n *nodeImpl) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var err error
if start.Name.Space != SvgNs {
return fmt.Errorf("parsing non-SVG element: %v", start.Name)
}
n.name = start.Name.Local
n.attrs = makeAttrMap(start.Attr)
if n.onUnmarshalAttrs != nil {
err = n.onUnmarshalAttrs(n.attrs)
if err != nil {
return err
}
}
n.children, n.text, err = readChildren(d, &start)
return err
}
func (n *nodeImpl) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
am := copyAttrMap(n.attrs)
if n.onMarshalAttrs != nil {
n.onMarshalAttrs(am)
}
se := MakeStartElement(n.name, am)
// Do the namespace thing for the root. This is a total hack.
if n.name == "svg" {
se.Name.Space = SvgNs
}
err := e.EncodeToken(se)
if err != nil {
return err
}
if len(n.children) != 0 {
for _, c := range n.children {
err = e.Encode(c)
if err != nil {
return err
}
}
} else if n.text != "" {
tt := xml.CharData(n.text)
err = e.EncodeToken(tt)
if err != nil {
return err
}
}
err = e.EncodeToken(se.End())
if err != nil {
return err
}
return nil
}