-
Notifications
You must be signed in to change notification settings - Fork 18
/
sitemap_impl.go
70 lines (54 loc) · 1.3 KB
/
sitemap_impl.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
package sitemap
import (
"encoding/xml"
"io"
)
func entryParser(decoder *xml.Decoder, se *xml.StartElement, consume EntryConsumer) error {
if se.Name.Local == "url" {
entry := newSitemapEntry()
decodeError := decoder.DecodeElement(entry, se)
if decodeError != nil {
return decodeError
}
consumerError := consume(entry)
if consumerError != nil {
return consumerError
}
}
return nil
}
func indexEntryParser(decoder *xml.Decoder, se *xml.StartElement, consume IndexEntryConsumer) error {
if se.Name.Local == "sitemap" {
entry := new(sitemapIndexEntry)
decodeError := decoder.DecodeElement(entry, se)
if decodeError != nil {
return decodeError
}
consumerError := consume(entry)
if consumerError != nil {
return consumerError
}
}
return nil
}
type elementParser func(*xml.Decoder, *xml.StartElement) error
func parseLoop(reader io.Reader, parser elementParser) error {
decoder := xml.NewDecoder(reader)
for {
t, tokenError := decoder.Token()
if tokenError == io.EOF {
break
} else if tokenError != nil {
return tokenError
}
se, ok := t.(xml.StartElement)
if !ok {
continue
}
parserError := parser(decoder, &se)
if parserError != nil {
return parserError
}
}
return nil
}