-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathsearch_xml.go
87 lines (78 loc) · 2.04 KB
/
search_xml.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
package rets
import (
"encoding/xml"
"io"
"github.com/jpfielding/gominidom/minidom"
"context"
)
// StandardXMLSearch if you set the wrong request Format you will get nothing back
func StandardXMLSearch(ctx context.Context, requester Requester, r SearchRequest) (*StandardXMLSearchResult, error) {
body, err := SearchStream(SearchResponse(ctx, requester, r))
if err != nil {
return nil, err
}
return NewStandardXMLSearchResult(body)
}
// StandardXMLSearchResult ...
type StandardXMLSearchResult struct {
Response Response
body io.ReadCloser
parser *xml.Decoder
}
// ForEach returns Count and MaxRows and any error that 'each' wont handle
func (c *StandardXMLSearchResult) ForEach(match minidom.Matcher, each minidom.EachDOM) (int, bool, error) {
defer c.body.Close()
count := 0
maxrows := false
md := minidom.MiniDom{
StartFunc: func(start xml.StartElement) {
switch start.Name.Local {
case "COUNT":
count, _ = countTag(start).Parse()
case "MAXROWS":
maxrows = true
}
},
// quit on the the xml tag
EndFunc: func(end xml.EndElement) bool {
switch end.Name.Local {
case XMLElemRETS, XMLElemRETSStatus:
return true
}
return false
},
}
err := md.Walk(c.parser, match, each)
return count, maxrows, err
}
// Close closesthe connection
func (c *StandardXMLSearchResult) Close() error {
if c == nil || c.body == nil {
return nil
}
return c.body.Close()
}
// NewStandardXMLSearchResult returns an XML search result handler to listen to elements
func NewStandardXMLSearchResult(body io.ReadCloser) (*StandardXMLSearchResult, error) {
parser := DefaultXMLDecoder(body, false)
result := &StandardXMLSearchResult{
body: body,
parser: parser,
}
// extract the basic content before delving into the data
for {
token, err := parser.Token()
if err != nil {
return result, err
}
switch t := token.(type) {
case xml.StartElement:
switch t.Name.Local {
case XMLElemRETS, XMLElemRETSStatus:
resp, err := ResponseTag(t).Parse()
result.Response = *resp
return result, err
}
}
}
}