forked from antlinker/flow
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse_xml.go
294 lines (267 loc) · 7.98 KB
/
parse_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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package flow
import (
"context"
"strconv"
"github.com/antlinker/flow/util"
"github.com/beevik/etree"
)
// NewXMLParser xml解析器
func NewXMLParser() Parser {
return &xmlParser{}
}
type xmlParser struct {
}
func (p *xmlParser) Parse(ctx context.Context, content []byte) (*ParseResult, error) {
result := &ParseResult{
FlowStatus: 2,
}
var err error
doc := etree.NewDocument()
if err = doc.ReadFromBytes(content); err != nil {
panic(err)
}
root := doc.SelectElement("definitions")
//root.Tag
process := root.SelectElement("process")
if id := process.SelectAttr("id"); id != nil {
result.FlowID = id.Value
}
if name := process.SelectAttr("name"); name != nil {
result.FlowName = name.Value
}
if v := process.SelectAttr("isExecutable"); v != nil {
b, _ := strconv.ParseBool(v.Value)
if b {
result.FlowStatus = 1
}
}
if version := process.SelectAttr("versionTag"); version != nil {
result.FlowVersion, err = util.StringToInt(version.Value)
if err != nil {
return nil, err
}
}
// 定义一个用于辅助的map,由节点id映射到noderesult
nodeMap := make(map[string]*NodeResult)
// 遍历找到所有的节点,因为是解析一个树,所以先解析节点,再解析sequenceFlow部分
// 解析sequenceFlow部分时,nodeMap里面应该已经有对应的nodeId了
for _, element := range process.ChildElements() {
if element.Tag == "documentation" ||
element.Tag == "extensionElements" ||
element.Tag == "sequenceFlow" {
continue
}
node, _ := p.ParseNode(element)
var nodeResult NodeResult
nodeResult.NodeID = node.Code
nodeResult.NodeName = node.Name
nodeResult.NodeType, err = GetNodeTypeByName(node.Type)
if err != nil {
return nil, err
}
nodeResult.CandidateExpressions = node.CandidateUsers
// yupengfei 2018-01-17 增加了form的解析
nodeResult.FormResult = node.FormResult
nodeResult.Properties = node.Properties
nodeMap[nodeResult.NodeID] = &nodeResult
// 如果节点是一个路由的话,需要特殊处理
}
for _, element := range process.ChildElements() {
if element.Tag == "sequenceFlow" {
sequenceFlow, _ := p.ParsesequenceFlow(element)
var routerResult RouterResult
routerResult.Expression = sequenceFlow.Expression
routerResult.Explain = sequenceFlow.Explain
routerResult.TargetNodeID = sequenceFlow.TargetRef
if nodeResult, exist := nodeMap[sequenceFlow.SourceRef]; exist {
nodeResult.Routers = append(nodeResult.Routers, &routerResult)
}
}
}
for _, nodeResult := range nodeMap {
result.Nodes = append(result.Nodes, nodeResult)
}
return result, nil
}
func (p *xmlParser) ParseNode(element *etree.Element) (*nodeInfo, error) {
var node nodeInfo
node.Type = element.Tag
if node.Type == "endEvent" {
for _, e := range element.ChildElements() {
if e.Tag == "terminateEventDefinition" {
node.Type = "terminateEvent"
}
}
}
if name := element.SelectAttr("name"); name != nil {
node.Name = name.Value
}
if id := element.SelectAttr("id"); id != nil {
node.Code = id.Value
}
if candidateUsers := element.SelectAttr("candidateUsers"); candidateUsers != nil {
node.CandidateUsers = []string{candidateUsers.Value}
}
nodeFormResult := new(NodeFormResult)
if formKey := element.SelectAttr("formKey"); formKey != nil {
nodeFormResult.ID = formKey.Value
}
if extensionElements := element.SelectElement("extensionElements"); extensionElements != nil {
if formData := extensionElements.SelectElement("formData"); formData != nil {
form, err := p.ParseFormData(formData)
if err != nil {
return nil, err
}
if form != nil {
nodeFormResult.Fields = form.Fields
}
}
if propertyData := extensionElements.SelectElement("properties"); propertyData != nil {
// 解析节点属性
for _, p := range propertyData.SelectElements("property") {
var item PropertyResult
if name := p.SelectAttr("name"); name != nil {
item.Name = name.Value
}
if value := p.SelectAttr("value"); value != nil {
item.Value = value.Value
}
if item.Name != "" {
node.Properties = append(node.Properties, &item)
}
}
}
}
node.FormResult = nodeFormResult
return &node, nil
}
func (p *xmlParser) ParsesequenceFlow(element *etree.Element) (*sequenceFlow, error) {
hasExpression := false
var seq sequenceFlow
seq.XMLName = element.Tag
seq.Code = element.SelectAttr("id").Value
seq.SourceRef = element.SelectAttr("sourceRef").Value
seq.TargetRef = element.SelectAttr("targetRef").Value
for _, element := range element.ChildElements() {
if element.Tag == "documentation" {
seq.Explain = element.Text()
} else if element.Tag == "conditionExpression" {
seq.Expression = element.Text()
hasExpression = true
}
}
if !hasExpression {
seq.Expression = ""
}
return &seq, nil
}
func (p *xmlParser) ParseFormData(element *etree.Element) (*NodeFormResult, error) {
var formResult = &NodeFormResult{}
if id := element.SelectAttr("id"); id != nil {
formResult.ID = id.Value
}
if fieldList := element.SelectElements("formField"); fieldList != nil {
for _, item := range fieldList {
var field = &FormFieldResult{}
var err error
if properties := item.SelectElement("properties"); properties != nil {
field.Properties, err = p.ParseProperties(properties)
if err != nil {
return nil, err
}
}
if validations := item.SelectElement("validation"); validations != nil {
field.Validations, err = p.ParseValidations(validations)
if err != nil {
return nil, err
}
}
if nodeType := item.SelectAttr("type"); nodeType != nil {
field.Type = nodeType.Value
if field.Type == "enum" {
field.Values, err = p.ParseEnumValues(item)
if err != nil {
return nil, err
}
}
}
if id := item.SelectAttr("id"); id != nil {
field.ID = id.Value
}
if label := item.SelectAttr("label"); label != nil {
field.Label = label.Value
}
if defaultValue := item.SelectAttr("defaultValue"); defaultValue != nil {
field.DefaultValue = defaultValue.Value
}
formResult.Fields = append(formResult.Fields, field)
}
}
return formResult, nil
}
func (p *xmlParser) ParseProperties(element *etree.Element) ([]*FieldProperty, error) {
var properties = make([]*FieldProperty, 0)
if propertyList := element.SelectElements("property"); propertyList != nil {
for _, item := range propertyList {
var property = &FieldProperty{}
if id := item.SelectAttr("id"); id != nil {
property.ID = id.Value
}
if value := item.SelectAttr("value"); value != nil {
property.Value = value.Value
}
properties = append(properties, property)
}
}
return properties, nil
}
func (p *xmlParser) ParseValidations(element *etree.Element) ([]*FieldValidation, error) {
var validations = make([]*FieldValidation, 0)
if validationList := element.SelectElements("constraint"); validationList != nil {
for _, item := range validationList {
var validation = &FieldValidation{}
if name := item.SelectAttr("name"); name != nil {
validation.Name = name.Value
}
if config := item.SelectAttr("config"); config != nil {
validation.Config = config.Value
}
validations = append(validations, validation)
}
}
return validations, nil
}
func (p *xmlParser) ParseEnumValues(element *etree.Element) ([]*FieldOption, error) {
var options = make([]*FieldOption, 0)
if optionList := element.SelectElements("value"); optionList != nil {
for _, item := range optionList {
var option = &FieldOption{}
if id := item.SelectAttr("id"); id != nil {
option.ID = id.Value
}
if name := item.SelectAttr("name"); name != nil {
option.Name = name.Value
}
options = append(options, option)
}
}
return options, nil
}
type nodeInfo struct {
ProcessCode string
Type string
Code string
Name string
CandidateUsers []string
Properties []*PropertyResult
FormResult *NodeFormResult
}
type sequenceFlow struct {
ProcessCode string
XMLName string
Code string
SourceRef string
TargetRef string
Explain string
Expression string
}