-
Notifications
You must be signed in to change notification settings - Fork 18
/
parser.go
136 lines (118 loc) · 2.7 KB
/
parser.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
package svgparser
import (
"bytes"
"encoding/xml"
"io"
"io/ioutil"
"strings"
"golang.org/x/net/html/charset"
)
// ValidationError contains errors which have occured when parsing svg input.
type ValidationError struct {
msg string
}
func (err ValidationError) Error() string {
return err.msg
}
// Element is a representation of an SVG element.
type Element struct {
Name string
Attributes map[string]string
Children []*Element
Content string
}
// NewElement creates element from decoder token.
func NewElement(token xml.StartElement) *Element {
element := &Element{}
attributes := make(map[string]string)
for _, attr := range token.Attr {
attributes[attr.Name.Local] = attr.Value
}
element.Name = token.Name.Local
element.Attributes = attributes
return element
}
// Compare compares two elements.
func (e *Element) Compare(o *Element) bool {
if e.Name != o.Name || e.Content != o.Content ||
len(e.Attributes) != len(o.Attributes) ||
len(e.Children) != len(o.Children) {
return false
}
for k, v := range e.Attributes {
if v != o.Attributes[k] {
return false
}
}
for i, child := range e.Children {
if !child.Compare(o.Children[i]) {
return false
}
}
return true
}
// DecodeFirst creates the first element from the decoder.
func DecodeFirst(decoder *xml.Decoder) (*Element, error) {
for {
token, err := decoder.Token()
if token == nil && err == io.EOF {
break
}
if err != nil {
return nil, err
}
switch element := token.(type) {
case xml.StartElement:
return NewElement(element), nil
}
}
return &Element{}, nil
}
// Decode decodes the child elements of element.
func (e *Element) Decode(decoder *xml.Decoder) error {
for {
token, err := decoder.Token()
if token == nil && err == io.EOF {
break
}
if err != nil {
return err
}
switch element := token.(type) {
case xml.StartElement:
nextElement := NewElement(element)
err := nextElement.Decode(decoder)
if err != nil {
return err
}
e.Children = append(e.Children, nextElement)
case xml.CharData:
data := strings.TrimSpace(string(element))
if data != "" {
e.Content = string(element)
}
case xml.EndElement:
if element.Name.Local == e.Name {
return nil
}
}
}
return nil
}
// Parse creates an Element instance from an SVG input.
func Parse(source io.Reader, validate bool) (*Element, error) {
raw, err := ioutil.ReadAll(source)
if err != nil {
return nil, err
}
decoder := xml.NewDecoder(bytes.NewReader(raw))
decoder.CharsetReader = charset.NewReaderLabel
element, err := DecodeFirst(decoder)
if err != nil {
return nil, err
}
if err := element.Decode(decoder); err != nil && err != io.EOF {
return nil, err
}
return element, nil
}