-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
400 lines (318 loc) · 9.31 KB
/
parser.go
File metadata and controls
400 lines (318 loc) · 9.31 KB
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package smart
import (
"errors"
"fmt"
"github.com/clbanning/mxj"
"reflect"
)
// xml 节点信息
const (
RootElement = "process"
// map中节点类型
ElementType = "elementType"
// 变迁节点名称
NodeTransition = "transition"
// 节点属性名称
AttrName = "-name"
AttrDisplayName = "-displayName"
AttrInstanceUrl = "-instanceUrl"
AttrInstanceNoClazz = "-instanceNoClass"
AttrExpr = "-expr"
AttrHandleClazz = "-handleClass"
AttrForm = "-form"
AttrField = "-field"
AttrValue = "-value"
AttrAttr = "-attr"
attrType= "-type"
AttrAssignee = "-assignee"
AttrAssignmentHandler = "-assignmentHandler"
AttrPerormType = "-performType"
AttrTaskType = "-taskType"
AttrTo = "-to"
AttrProcessName = "-processName"
AttrVersion = "-version"
AttrExpireTime = "-expireTime"
AttrAutoExecute = "-autoExecute"
AttrCallback = "-callback"
AttrReminderTime = "-reminderTime"
AttrReminderRepeat = "-reminderRepeat"
AttrClazz = "-clazz"
AttrMethodName = "-methodName"
AttrArgs = "-args"
AttrVar = "-var"
AttrLayout = "-layout"
AttrG = "-g"
AttrOffset = "-offset"
AttrPreInterceptors = "-preInterceptors"
AttrPostInterceptors = "-postInterceptors"
)
//
type SmartrParserContainer interface {
// 添加解析
AddParserFactory(elementName string, f NodeParserFactory)
// 根据element名称获取对应的工厂
GetNodeParserFactory(elementName string) NodeParserFactory
}
// engine上挂载factory
type NodeParserFactory interface {
// 根据elementName查找使用哪个parser
NewParse() NodeParser
}
// 节点解析接口
type NodeParser interface {
// 节点dom元素解析方法,由实现类完成解析
Parse(element map[string]interface{}) (INodeModel, error)
}
type ModelGen interface {
newModel() INodeModel
}
type DefaultSnakerParserContainer struct {
container map[string]NodeParserFactory
}
func (d *DefaultSnakerParserContainer) AddParserFactory(elementName string, f NodeParserFactory) {
d.container[elementName] = f
}
func (d *DefaultSnakerParserContainer) GetNodeParserFactory(elementName string) NodeParserFactory {
if f, ok := d.container[elementName]; ok {
return f
} else {
panic(fmt.Sprintf("[%s]没有对应的解析工厂类", elementName))
}
}
func NewDefaultSmartParserContainer() *DefaultSnakerParserContainer {
container := make(map[string]NodeParserFactory)
// 注册解析工厂
container["start"] = &StartParserFactory{}
container["end"] = &EndParserFactory{}
container["task"] = &TaskParserFactory{}
container["decision"] = &DecisionParserFactory{}
container["custom"] = &CustomParserFactory{}
return &DefaultSnakerParserContainer{
container,
}
}
type AbstractNodeParser struct {
//model *model.NodeModel
Parent ModelGen
}
func (a *AbstractNodeParser) Parse(element map[string]interface{}) (INodeModel, error) {
m := a.Parent.newModel()
m.SetName(element[AttrName].(string))
m.SetDisplayName(element[AttrDisplayName].(string))
// interceptor
v := element[NodeTransition]
var tms []map[string]interface{}
if v != nil {
vv := reflect.ValueOf(v)
switch vv.Kind() {
case reflect.Map:
tms = append(tms, v.(map[string]interface{}))
case reflect.Slice:
for _, k := range v.([]interface{}) {
tms = append(tms, k.(map[string]interface{}))
}
}
}
for _, tte := range tms {
if _, ok := tte[AttrExpr]; !ok {
tte[AttrExpr] = ""
}
if _, ok := tte[AttrDisplayName]; !ok {
tte[AttrDisplayName] = ""
}
transition := &TransitionModel{
BaseModel: BaseModel{
Name: tte[AttrName].(string),
DisplayName: tte[AttrDisplayName].(string),
},
To: tte[AttrTo].(string),
Expr: tte[AttrExpr].(string),
Source: m,
}
m.SetOutputs(append(m.GetOutputs(), transition))
//m.Outputs = append(m.Outputs, transition)
}
//a.parseNode(m, element)
if p, ok := a.Parent.(ParseNode); ok {
if err := p.parseNode(m, element); err != nil {
return nil, err
}
}
return m, nil
}
type ParseNode interface {
parseNode(model INodeModel, element map[string]interface{}) error
}
// 子类可覆盖此方法,完成特定的解析
//func (a *AbstractNodeParser) parseNode(model *NodeModel, element map[string]interface{}) error {
// if p, ok := a.Parent.(ParseNode); ok {
// return p.parseNode(model, element)
// }
// return nil
//}
func (a *AbstractNodeParser) newModel() interface{} {
panic("未实现此方法")
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
type StartParser struct {
AbstractNodeParser
}
type StartParserFactory struct {
}
func (s *StartParserFactory) NewParse() NodeParser {
ss := new(StartParser)
ss.Parent = ss
return ss
}
func (s *StartParser) newModel() INodeModel {
newNode := NewNodeModel("", "")
startModel := &StartModel{ NodeModel: *newNode }
startModel.SetExec(startModel.exec)
return startModel
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
type EndParser struct {
AbstractNodeParser
}
type EndParserFactory struct {
}
func (e *EndParserFactory) NewParse() NodeParser {
end := new(EndParser)
end.Parent = end
return end
}
func (e *EndParser) newModel() INodeModel {
newNode := NewNodeModel("", "")
endModel := &EndModel{ NodeModel: *newNode }
endModel.SetExec(endModel.exec)
return endModel
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
type CustomParser struct {
AbstractNodeParser
}
type CustomParserFactory struct {
}
func (c *CustomParserFactory) NewParse() NodeParser {
custom := new(CustomParser)
custom.Parent = custom
return custom
}
func (c *CustomParser) newModel() INodeModel {
newNode := NewNodeModel("", "")
workModel := WorkModel{ *newNode, "" }
customModel := &CustomModel{ workModel, "" }
customModel.SetExec(customModel.exec)
return customModel
}
func (a *CustomParser) parseNode(model INodeModel, element map[string]interface{}) error {
customModel := model.(*CustomModel)
if element[AttrClazz] == nil {
return errors.New("自定义模型需要指定clazz")
}
customModel.Clazz = element[AttrClazz].(string)
return nil
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
type DecisionParser struct {
AbstractNodeParser
}
type DecisionParserFactory struct {
}
func (df *DecisionParserFactory) NewParse() NodeParser {
d := new(DecisionParser)
d.Parent = d
return d
}
func (df *DecisionParser) newModel() INodeModel {
newNode := NewNodeModel("", "")
decisionModel := &DecisionModel{ NodeModel: *newNode }
decisionModel.SetExec(decisionModel.exec)
return decisionModel
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
type TaskParser struct {
AbstractNodeParser
}
type TaskParserFactory struct {
}
func (tf *TaskParserFactory) NewParse() NodeParser {
t := new(TaskParser)
t.Parent = t
return t
}
func (tp *TaskParser) newModel() INodeModel {
newNode := NewNodeModel("", "")
workModel := WorkModel{ *newNode, "" }
taskModel := &TaskModel{ WorkModel: workModel }
taskModel.SetExec(taskModel.exec)
return taskModel
}
func (tp *TaskParser) parseNode(model INodeModel, element map[string]interface{}) error {
task := model.(*TaskModel)
task.AssignTo = element[AttrAssignee].(string)
return nil
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
type Parser interface {
ParseXml(content string) (*ProcessModel, error)
}
type XmlParser struct {
// xml 元素解析容器
ElementParserContainer SmartrParserContainer
}
// 解析流程定义文件,并将解析后的对象放入模型容器中
func (x *XmlParser) ParseXml(content string) (*ProcessModel, error) {
if c, err := mxj.NewMapXml([]byte(content)); err != nil {
return nil, errors.New(fmt.Sprintf("解析xml文件出错, content: %s", content))
} else {
// 根元素
root := c.Old()[RootElement].(map[string]interface{})
process := NewProcess(root[AttrName].(string), root[AttrDisplayName].(string))
for k, v := range root {
vv := reflect.ValueOf(v)
switch vv.Kind() {
case reflect.Map:
vvv := v.(map[string]interface{})
vvv[ElementType] = k
if m, err := x.parseModel(vvv); err != nil {
return nil, err
} else {
process.Nodes = append(process.Nodes, m)
}
case reflect.Slice:
// 节点类型多个时
// 是slice类型
for _, kk := range v.([]interface{}) {
vvv := kk.(map[string]interface{})
vvv[ElementType] = k
if m, err := x.parseModel(vvv); err != nil {
return nil, err
} else {
process.Nodes = append(process.Nodes, m)
}
}
}
}
for _, node := range process.Nodes {
for _, transition := range node.GetOutputs() {
to := transition.To
for _, node2 := range process.Nodes {
if to == node2.GetName() {
transition.Target = node2
node2.SetInputs(append(node2.GetInputs(), transition))
}
}
if transition.Target == nil {
panic("transition target is nil. ---> " + transition.Name + " ---> targetName: " + transition.To)
}
}
}
return process, nil
}
return nil, fmt.Errorf("解析xml失败")
}
// 对流程定义xml的节点,根据其节点对应的解析器解析节点内容
func (x *XmlParser) parseModel(node map[string]interface{}) (INodeModel, error) {
return x.ElementParserContainer.GetNodeParserFactory(node[ElementType].(string)).NewParse().Parse(node)
}