-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmlparserbuild.go
172 lines (145 loc) · 3.87 KB
/
xmlparserbuild.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package gobatis
import (
"reflect"
"regexp"
"strings"
"github.com/antonmedv/expr"
"github.com/beevik/etree"
)
var (
ptnOutputVar = regexp.MustCompile(`#{(.*?)}`)
ptnOutputReplace = regexp.MustCompile(`\${(.*?)}`)
)
type XmlParserBuild struct {
inputValue map[string]interface{}
outputValue map[string]interface{}
}
func NewXmlParserBuild(inputValue, outputValue map[string]interface{}) *XmlParserBuild {
return &XmlParserBuild{
inputValue: inputValue,
outputValue: outputValue,
}
}
func (xml *XmlParserBuild) Build(element *etree.Element) (string, error) {
var err error
var builder strings.Builder
for _, child := range element.Child {
tmp := ""
switch reflect.TypeOf(child).String() {
case "*etree.CharData":
tmp = xml.filter(strings.TrimSpace(child.(*etree.CharData).Data))
case "*etree.Element":
el := child.(*etree.Element)
switch el.Tag {
case "trim":
if tmp, err = xml.buildTrim(el); err != nil {
break
}
case "where":
tmp = xml.buildWhere(el)
case "set":
tmp = xml.buildSet(el)
case "if":
tmp = xml.buildIf(el)
case "foreach":
b := XmlParseForeach{}
tmp, err = b.Build(el, xml.inputValue, xml.outputValue)
if err != nil {
return "", err
}
}
}
if tmp != "" {
builder.WriteString(tmp)
builder.WriteString(" ")
}
}
return strings.TrimSpace(builder.String()), err
}
func (xml *XmlParserBuild) buildTrim(el *etree.Element) (string, error) {
tmp, err := xml.Build(el)
if err != nil {
return "", err
}
if tmp == "" {
return tmp, err
}
if str := el.SelectAttrValue("prefixOverrides", ""); str != "" {
for _, tag := range strings.Split(str, "|") {
tmp = strings.TrimLeft(tmp, strings.TrimSpace(tag))
}
}
if str := el.SelectAttrValue("suffixOverrides", ""); str != "" {
for _, tag := range strings.Split(str, "|") {
tmp = strings.TrimRight(tmp, strings.TrimSpace(tag))
}
}
if tmp != "" {
tmp = el.SelectAttrValue("prefix", "") + tmp + el.SelectAttrValue("suffix", "")
}
return tmp, nil
}
func (xml *XmlParserBuild) buildWhere(el *etree.Element) string {
wheres := xml.buildWhereOrSet(el)
if len(wheres) > 0 {
return "WHERE " + strings.Join(wheres, " AND ")
}
return ""
}
func (xml *XmlParserBuild) buildSet(el *etree.Element) string {
sets := xml.buildWhereOrSet(el)
if len(sets) > 0 {
return "SET " + strings.Join(sets, ", ")
}
return ""
}
func (xml *XmlParserBuild) buildWhereOrSet(el *etree.Element) []string {
ifElements := el.FindElements("if")
wheres := make([]string, len(ifElements))
index := 0
for _, el := range ifElements {
str := strings.TrimSpace(el.SelectAttrValue("test", ""))
if xml.parseTest(str, xml.inputValue) {
wheres[index] = xml.filter(el.Text())
index++
}
}
return wheres[0:index]
}
func (xml *XmlParserBuild) buildIf(el *etree.Element) string {
str := strings.TrimSpace(el.SelectAttrValue("test", ""))
if xml.parseTest(str, xml.inputValue) {
return xml.filter(el.Text())
}
return ""
}
func (xml *XmlParserBuild) filter(str string) string {
str = strings.Replace(str, "<", "<", -1)
str = strings.Replace(str, ">", ">", -1)
str = ptnOutputVar.ReplaceAllStringFunc(str, func(s string) string {
key := strings.TrimSpace(s[2 : len(s)-1])
xml.outputValue[key] = xml.inputValue[key]
return ":" + key
})
str = ptnOutputReplace.ReplaceAllStringFunc(str, func(s string) string {
key := strings.TrimSpace(s[2 : len(s)-1])
return toStr(xml.inputValue[key])
})
return strings.TrimSpace(str)
}
func (xml *XmlParserBuild) parseTest(str string, args map[string]interface{}) bool {
if str == "" {
return true
}
p, err := expr.Eval(str, args)
if err != nil {
logger.Error("eval ", str, " error: ", err)
return false
}
valueType := reflect.ValueOf(p).Kind().String()
if valueType == "bool" {
return p.(bool)
}
logger.Warn("Bool is required by \"", str+"\", but it is ", reflect.TypeOf(p))
return false
}