-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml.go
84 lines (71 loc) · 2.01 KB
/
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
package rknresolver
import (
"time"
"bytes"
"io/ioutil"
"encoding/xml"
"encoding/json"
"golang.org/x/net/html/charset"
)
// XML scheme: http://vigruzki.rkn.gov.ru/docs/description_for_operators_actual.pdf
type Register struct {
Content []Content `xml:"content" json:"content"`
}
type Content struct {
ID int `xml:"id,attr" json:"id"`
IP []string `xml:"ip" json:"ip"`
Subnet []string `xml:"ipSubnet" json:"ipSubnet"`
Domain []string `xml:"domain" json:"domain"`
URL []string `xml:"url" json:"url"`
IncludeTime RegTime `xml:"includeTime,attr" json:"includeTime"`
EntryType int `xml:"entryType,attr" json:"entryType"`
BlockType string `xml:"blockType,attr" json:"blockType"`
UrgencyType int `xml:"urgencyType,attr" json:"urgencyType"`
Hash string `xml:"hash,attr" json:"hash"`
Decision Decision `xml:"decision" json:"decision"`
}
type Decision struct {
Date RegTime `xml:"date,attr" json:"date"`
Number string `xml:"number,attr" json:"number"`
Org string `xml:"org,attr" json:"org"`
}
type RegTime struct {
time.Time
}
func (rt *RegTime) UnmarshalXMLAttr(attr xml.Attr) error {
var shortForm string
if attr.Name.Local == "date" {
shortForm = "2006-01-02" // yyyymmdd date format
} else {
shortForm = "2006-01-02T15:04:05"
}
parse, err := time.Parse(shortForm, attr.Value)
if err != nil {
return err
}
*rt = RegTime{parse}
return nil
}
func Parse(filepath string) (*Register, error) {
data, err := ioutil.ReadFile(filepath)
if err != nil {
return nil, err
}
var dump Register
reader := bytes.NewReader(data)
decoder := xml.NewDecoder(reader)
decoder.CharsetReader = charset.NewReaderLabel
err = decoder.Decode(&dump)
if err != nil {
return nil, err
}
return &dump, nil
}
func (r *Register) WriteJSONFile(filepath string) error {
jsonContent, err := json.MarshalIndent(r, "", " ")
err = ioutil.WriteFile(filepath, jsonContent, 0644)
if err != nil {
return err
}
return nil
}