-
Notifications
You must be signed in to change notification settings - Fork 13
/
render.go
87 lines (84 loc) · 1.77 KB
/
render.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 metha
import (
"compress/gzip"
"encoding/json"
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
// RenderOpts controls output by the metha-cat command.
type RenderOpts struct {
Writer io.Writer
Harvest Harvest
Root string
From string
Until string
UseJson bool
}
// RenderHarvest renders harvest to JSON or XML.
func Render(opts *RenderOpts) error {
files, err := ioutil.ReadDir(opts.Harvest.Dir())
if err != nil {
return err
}
if opts.Root != "" && !opts.UseJson {
if _, err := fmt.Fprintf(opts.Writer,
"<%s xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n", opts.Root); err != nil {
return err
}
defer func() {
fmt.Fprintf(opts.Writer, "</%s>\n", opts.Root)
}()
}
for _, file := range files {
if !strings.HasSuffix(file.Name(), ".xml.gz") {
continue
}
if opts.From != "" && file.Name() < opts.From {
continue
}
abspath := filepath.Join(opts.Harvest.Dir(), file.Name())
fi, err := os.Open(abspath)
if err != nil {
return err
}
r, err := gzip.NewReader(fi)
if err != nil {
return err
}
dec := xml.NewDecoder(r)
dec.Strict = false
var (
resp Response
b []byte
)
if err := dec.Decode(&resp); err != nil {
return err
}
for _, rec := range resp.ListRecords.Records {
if opts.From != "" && rec.Header.DateStamp < opts.From {
continue
}
if opts.Until != "" && rec.Header.DateStamp > opts.Until {
continue
}
if opts.UseJson {
b, err = json.Marshal(rec)
} else {
rec.XMLName = xml.Name{Local: "record", Space: "http://www.openarchives.org/OAI/2.0/"}
b, err = xml.Marshal(rec)
}
if err != nil {
return err
}
if _, err := io.WriteString(opts.Writer, string(b)+"\n"); err != nil {
return err
}
}
}
return nil
}