/
standard.go
240 lines (227 loc) · 5.64 KB
/
standard.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
package parser
import (
"bytes"
"encoding/xml"
"io"
"os"
"path/filepath"
"sort"
"strings"
"github.com/ajankovic/xdiff/xtree"
)
// Standard is a standard parser using encoding/xml sax parser as engine.
type Standard struct {
// Handler for non-xml files encountered during directory traversal.
NonXMLHandler func(f *os.File, fi os.FileInfo) (*xtree.Node, error)
}
// NewStandard instantiates new standard parser.
func NewStandard() *Standard {
return &Standard{}
}
// ParseReader returns reference to the document node got by parsing bytes from the provided
// reader.
func (p *Standard) ParseReader(r io.Reader) (*xtree.Node, error) {
dec := xml.NewDecoder(r)
doc := xtree.NewNode(xtree.Document)
current := doc
for {
tok, err := dec.Token()
if err != nil && err != io.EOF {
return nil, err
}
if tok == nil {
break
}
switch el := tok.(type) {
case xml.StartElement:
child := &xtree.Node{
Type: xtree.Element,
Name: []byte(el.Name.Local),
}
for _, a := range el.Attr {
child.AppendChild(&xtree.Node{
Type: xtree.Attribute,
Name: []byte(a.Name.Local),
Value: []byte(a.Value),
})
}
current.AppendChild(child)
current = child
case xml.EndElement:
current = current.Parent
case xml.CharData:
content := make([]byte, len(el))
copy(content, el)
if strings.TrimSpace(string(content)) == "" {
continue
}
child := &xtree.Node{
Type: xtree.Data,
Value: content,
}
current.AppendChild(child)
case xml.Comment:
content := make([]byte, len(el))
copy(content, el)
child := &xtree.Node{
Type: xtree.Comment,
Value: content,
}
current.AppendChild(child)
case xml.Directive:
content := make([]byte, len(el))
copy(content, el)
child := &xtree.Node{
Type: xtree.Doctype,
Value: content,
}
current.AppendChild(child)
case xml.ProcInst:
var child *xtree.Node
if el.Target == "xml" {
content := make([]byte, len(el.Inst))
copy(content, el.Inst)
child = &xtree.Node{
Type: xtree.Declaration,
Name: []byte("xml"),
}
attrs, err := p.parseAttributes(content)
if err != nil {
return nil, err
}
for _, attr := range attrs {
child.AppendChild(attr)
}
} else {
child = &xtree.Node{
Type: xtree.ProcInstr,
Value: append([]byte(el.Target), el.Inst...),
}
}
current.AppendChild(child)
}
}
if err := xtree.Prepare(doc); err != nil {
return doc, err
}
return doc, nil
}
// parseAttributes is parsing declaration attributes since standard library
// parses it only as byte content.
//
// TODO(office@ajankovic.com) This is a happy path implementation so proper error handling
// is needed to cover issues with invalid attribute format.
func (p *Standard) parseAttributes(content []byte) ([]*xtree.Node, error) {
var attrs []*xtree.Node
buf := bytes.NewBuffer(bytes.TrimSpace(content))
for buf.Len() > 0 {
name, err := buf.ReadBytes('=')
if err != nil {
return nil, err
}
_, err = buf.ReadBytes('"')
if err != nil {
return nil, err
}
value, err := buf.ReadBytes('"')
if err != nil {
return nil, err
}
attrs = append(attrs, &xtree.Node{
Type: xtree.Attribute,
Name: bytes.TrimSpace(name[:len(name)-1]),
Value: bytes.TrimSpace(value[:len(value)-1]),
})
}
return attrs, nil
}
// ParseFile returns reference to the document node got by parsing provided filepath.
func (p *Standard) ParseFile(filepath string) (*xtree.Node, error) {
f, err := os.Open(filepath)
if err != nil {
return nil, err
}
return p.ParseReader(f)
}
// ParseDir returns reference to the directory node got by parsing provided dirpath.
// Each directory is parent to the child xml documents and documents have xtree structure
// got by parsing xml.
//
// RootDir
// |- Child1.xml
// | `- rootElement
// | |- childElement
// | `- childElement
// `- Child2.xml
// `- rootElement
//
// Use NonXMLHandler flag on the parser to configure behavior for handling
// non-xml files found in the directories.
func (p *Standard) ParseDir(dirpath string) (*xtree.Node, error) {
d, err := os.Open(dirpath)
if err != nil {
return nil, err
}
root := xtree.NewDirectory([]byte(filepath.Base(d.Name())))
list, err := d.Readdir(-1)
d.Close()
if err != nil {
return nil, err
}
sort.Slice(list, func(i, j int) bool { return list[i].Name() < list[j].Name() })
for _, fi := range list {
var ch *xtree.Node
if fi.IsDir() {
ch, err = p.ParseDir(filepath.Join(dirpath, fi.Name()))
} else {
ch, err = p.parseDirFile(filepath.Join(dirpath, fi.Name()), fi)
}
if err != nil {
return nil, err
}
if ch != nil {
root.AppendChild(ch)
}
}
return root, nil
}
// parseDirFile tires to detect if the file is xml or not. If it's not it then
// tries to efficiently load non-xml files as nodes making sure they are not
// too big.
func (p *Standard) parseDirFile(filepath string, fi os.FileInfo) (*xtree.Node, error) {
f, err := os.Open(filepath)
if err != nil {
return nil, err
}
defer f.Close()
b := make([]byte, 4)
_, err = f.Read(b)
if err == io.EOF {
return nil, nil
}
if err != nil {
return nil, err
}
b, i := ensureUTF8(b)
// Reset reader.
_, err = f.Seek(0, 0)
if err != nil {
return nil, err
}
size := fi.Size() + bytes.MinRead
var n *xtree.Node
if b[i] == '<' {
b, err = readAll(f, size)
n, err = p.ParseBytes(b)
} else if p.NonXMLHandler != nil {
n, err = p.NonXMLHandler(f, fi)
}
if n != nil && len(n.Name) == 0 {
n.Name = []byte(fi.Name())
}
return n, err
}
// ParseBytes returns reference to the document node parsed from provided bytes.
func (p *Standard) ParseBytes(b []byte) (*xtree.Node, error) {
return p.ParseReader(bytes.NewBuffer(b))
}