-
Notifications
You must be signed in to change notification settings - Fork 21
/
xml.go
144 lines (134 loc) · 4.92 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package xj2s
import (
"regexp"
"strings"
)
func XmlPath2SrtructLinesNoNesting(paths []string) (string, map[string]StructNode, map[string]map[string]StructNode) {
var RootName string
RootStruct := make(map[string]StructNode)
RestStructs := make(map[string]map[string]StructNode)
RootName = strings.Split(paths[0], ".")[0]
deDuplicateMap := make(map[string]string)
removeNum := regexp.MustCompile(`\[(\d+)\]`)
for _, path := range paths {
path = removeNum.ReplaceAllString(path, "[]")
Flods := strings.Count(path, "[")
path = strings.Replace(path, "[]", "", -1)
splitedPath := strings.Split(path, ".")
last := splitedPath[len(splitedPath)-1]
if strings.Index(last, "-") == 0 { //Attr
if RootName == splitedPath[len(splitedPath)-2] { //RootAttr
NodeName := strings.Title(last[1:])
xmlRoute := "`xml:" + `"` + last[1:] + `,attr"` + "`"
if _, exist := deDuplicateMap[NodeName]; exist {
if deDuplicateMap[NodeName] != xmlRoute {
NodeName = "Rss" + NodeName
deDuplicateMap[NodeName] = xmlRoute
}
} else {
deDuplicateMap[NodeName] = xmlRoute
}
StructLineAppend := StructNode{Name: NodeName, Type: "string", Path: xmlRoute}
RootStruct[xmlRoute] = StructLineAppend
} else { //NoneRootAttr
NodeName := strings.Title(splitedPath[len(splitedPath)-2])
xmlRoute := strings.Join(splitedPath[1:len(splitedPath)-1], ">")
xmlPath := "`xml:" + `"` + xmlRoute + `"` + "`"
Stype := NodeName
for i := 0; i < Flods; i++ {
Stype = "[]" + Stype
}
if _, exist := deDuplicateMap[NodeName]; exist {
if deDuplicateMap[NodeName] != xmlRoute {
NodeName = ""
for _, v := range strings.Split(xmlRoute, ">") {
NodeName = strings.Title(v) + NodeName
}
deDuplicateMap[NodeName] = xmlRoute
}
} else {
deDuplicateMap[NodeName] = xmlRoute
}
StructLineAppend := StructNode{Name: NodeName, Type: Stype, Path: xmlPath}
RootStruct[xmlRoute] = StructLineAppend
LeafName := strings.Title(last[1:])
RsetStructLineAppend := StructNode{Name: LeafName, Type: "string", Path: "`xml:" + `"` + last[1:] + `,attr"` + "`"}
// log.Println(NodeName, LeafName)
if _, exist := RestStructs[NodeName]; exist {
RestStructs[NodeName][LeafName] = RsetStructLineAppend
} else {
NewLeafStruct := make(map[string]StructNode)
NewLeafStruct[LeafName] = RsetStructLineAppend
RestStructs[NodeName] = NewLeafStruct
}
}
} else if strings.Index(last, "#") == 0 { //chardata
if RootName == splitedPath[len(splitedPath)-2] { //RootChartata
NodeName := strings.Title(last[1:])
xmlRoute := "`xml:" + `",chardata"` + "`"
if _, exist := deDuplicateMap[NodeName]; exist {
if deDuplicateMap[NodeName] != xmlRoute {
NodeName = "Rss" + NodeName
deDuplicateMap[NodeName] = xmlRoute
}
} else {
deDuplicateMap[NodeName] = xmlRoute
}
StructLineAppend := StructNode{Name: NodeName, Type: "string", Path: xmlRoute}
RootStruct[xmlRoute] = StructLineAppend
} else { //NonRootChardata
NodeName := strings.Title(splitedPath[len(splitedPath)-2])
xmlRoute := strings.Join(splitedPath[1:len(splitedPath)-1], ">")
xmlPath := "`xml:" + `"` + xmlRoute + `"` + "`"
Stype := NodeName
for i := 0; i < Flods; i++ {
Stype = "[]" + Stype
}
if _, exist := deDuplicateMap[NodeName]; exist {
if deDuplicateMap[NodeName] != xmlRoute {
NodeName = ""
for _, v := range strings.Split(xmlRoute, ">") {
NodeName = strings.Title(v) + NodeName
}
deDuplicateMap[NodeName] = xmlRoute
}
} else {
deDuplicateMap[NodeName] = xmlRoute
}
StructLineAppend := StructNode{Name: NodeName, Type: Stype, Path: xmlPath}
RootStruct[xmlRoute] = StructLineAppend
LeafName := strings.Title(last[1:])
RsetStructLineAppend := StructNode{Name: LeafName, Type: "string", Path: "`xml:" + `",chardata"` + "`"}
if _, exist := RestStructs[NodeName]; exist {
RestStructs[NodeName][LeafName] = RsetStructLineAppend
} else {
NewLeafStruct := make(map[string]StructNode)
NewLeafStruct[LeafName] = RsetStructLineAppend
RestStructs[NodeName] = NewLeafStruct
}
}
} else { //NormalString
NodeName := strings.Title(splitedPath[len(splitedPath)-1])
xmlRoute := strings.Join(splitedPath[1:], ">")
xmlPath := "`xml:" + `"` + xmlRoute + `"` + "`"
Stype := "string"
for i := 0; i < Flods; i++ {
Stype = "[]" + Stype
}
if _, exist := deDuplicateMap[NodeName]; exist {
if deDuplicateMap[NodeName] != xmlRoute {
NodeName = ""
for _, v := range strings.Split(xmlRoute, ">") {
NodeName = strings.Title(v) + NodeName
}
deDuplicateMap[NodeName] = xmlRoute
}
} else {
deDuplicateMap[NodeName] = xmlRoute
}
StructLineAppend := StructNode{Name: NodeName, Type: Stype, Path: xmlPath}
RootStruct[xmlRoute] = StructLineAppend
}
}
return strings.Title(RootName), RootStruct, RestStructs
}