-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcometxml.go
86 lines (76 loc) · 2.05 KB
/
cometxml.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
package main
import (
"io/ioutil"
"strings"
"time"
log "github.com/Sirupsen/logrus"
"github.com/clbanning/mxj"
)
func readCometXML(xmlfile string, waitMode bool) ([]map[string]interface{}, error) {
xmlData, err := readXMLFile(xmlfile, waitMode)
if err != nil {
log.Error("Failed in reading XML file ", err)
return nil, err
}
mxj.PrependAttrWithHyphen(false)
mapVal, err := mxj.NewMapXml(xmlData)
if err != nil {
log.Error("Failed in parsing xml contents", err)
return nil, err
}
date, err := mapVal.ValuesForKey("date")
if err != nil {
log.Error("Failed in getting date", err)
return nil, err
}
file, err := mapVal.ValuesForKey("base_name")
if err != nil {
log.Error("Failed in getting file name", err)
return nil, err
}
fileType, err := mapVal.ValuesForKey("raw_data")
if err != nil {
log.Error("Failed in getting fileType name", err)
return nil, err
}
xmlVal, err := mapVal.ValuesForKey("spectrum_query")
if err != nil {
log.Error("Failed in getting spectrum_query", err)
return nil, err
}
dbVal, err := mapVal.ValuesForKey("local_path")
if err != nil {
log.Error("Failed in getting database_name ", err)
return nil, err
}
searchEngVal, err := mapVal.ValuesForKey("search_engine")
if err != nil {
log.Error("Failed in getting search_engine ", err)
return nil, err
}
var xmlMap []map[string]interface{}
for _, val := range xmlVal {
valMap := val.(map[string]interface{})
valMap["dbname"] = dbVal[0]
valMap["searcheng"] = searchEngVal[0]
valMap["date"] = date[0]
valMap["file"] = file[0].(string) + fileType[0].(string)
valMap["fileType"] = strings.Replace(fileType[0].(string), ".", "", 1)
xmlMap = append(xmlMap, valMap)
}
return xmlMap, nil
}
func readXMLFile(xmlfile string, waitMode bool) ([]byte, error) {
for {
xmldata, err := ioutil.ReadFile(xmlfile)
if err != nil && waitMode && strings.Contains(err.Error(), "no such file") {
log.Debug(xmlfile, " not created/found, sleeping..")
time.Sleep(10 * time.Second)
continue
}
if err != nil {
return nil, err
}
return xmldata, nil
}
}