/
activity.go
executable file
·226 lines (192 loc) · 5.31 KB
/
activity.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
/*
* Copyright © 2020. TIBCO Software Inc.
* This file is subject to the license terms contained
* in the license file that is distributed with this file.
*/
package textreplacer
import (
"bytes"
"errors"
"fmt"
"sync"
"github.com/TIBCOSoftware/flogo-lib/core/activity"
"github.com/TIBCOSoftware/flogo-lib/core/data"
"github.com/TIBCOSoftware/flogo-lib/logger"
"github.com/TIBCOSoftware/labs-graphbuilder-lib/util"
)
// activityLogger is the default logger for the Filter Activity
var log = logger.GetLogger("activity-textreplacer")
const (
sLeftToken = "leftToken"
sRightToken = "rightToken"
iInputDocument = "inputDocument"
iReplacements = "replacements"
oOutputDocument = "outputDocument"
)
// Mapping is an Activity that is used to Filter a message to the console
type TextReplacer struct {
metadata *activity.Metadata
initialized bool
mux sync.Mutex
tokenMap map[string][]string
}
// NewActivity creates a new AppActivity
func NewActivity(metadata *activity.Metadata) activity.Activity {
aCSVParserActivity := &TextReplacer{
metadata: metadata,
tokenMap: make(map[string][]string),
}
return aCSVParserActivity
}
// Metadata returns the activity's metadata
func (a *TextReplacer) Metadata() *activity.Metadata {
return a.metadata
}
// Eval implements api.Activity.Eval - Filters the Message
func (a *TextReplacer) Eval(ctx activity.Context) (done bool, err error) {
tokens, err := a.getTokens(ctx)
if nil != err {
return false, err
}
inputDocument, ok := ctx.GetInput(iInputDocument).(string)
if !ok {
return false, errors.New("Invalid document ... ")
}
replacements := ctx.GetInput(iReplacements).(*data.ComplexObject)
replacementMap := replacements.Value.(map[string]interface{})
mapper := NewKeywordMapper(inputDocument, tokens[0], tokens[1])
document := mapper.replace("", replacementMap)
log.Info("document = ", document)
ctx.SetOutput(oOutputDocument, document)
return true, nil
}
func (a *TextReplacer) getTokens(context activity.Context) ([]string, error) {
myId := util.ActivityId(context)
tokens := a.tokenMap[myId]
log.Info("tokenMap : ", a.tokenMap, ", myId : ", myId)
if nil == tokens {
a.mux.Lock()
defer a.mux.Unlock()
tokens = a.tokenMap[myId]
if nil == tokens {
tokens = make([]string, 2)
leftToken, _ := context.GetSetting(sLeftToken)
if nil != leftToken {
tokens[0] = leftToken.(string)
}
rightToken, _ := context.GetSetting(sRightToken)
if nil != rightToken {
tokens[1] = rightToken.(string)
}
a.tokenMap[myId] = tokens
}
}
return tokens, nil
}
type KeywordReplaceHandler struct {
result string
keywordMap map[string]interface{}
}
func (this *KeywordReplaceHandler) setMap(keywordMap map[string]interface{}) {
this.keywordMap = keywordMap
}
func (this *KeywordReplaceHandler) startToMap() {
this.result = ""
}
func (this *KeywordReplaceHandler) replace(keyword string) string {
if nil != this.keywordMap[keyword] {
return this.keywordMap[keyword].(string)
}
return ""
}
func (this *KeywordReplaceHandler) endOfMapping(document string) {
this.result = document
}
func (this *KeywordReplaceHandler) getResult() string {
return this.result
}
func NewKeywordMapper(
template string,
lefttag string,
righttag string) *KeywordMapper {
mapper := KeywordMapper{
template: template,
keywordOnly: false,
slefttag: lefttag,
srighttag: righttag,
slefttaglen: len(lefttag),
srighttaglen: len(righttag),
}
return &mapper
}
type KeywordMapper struct {
template string
keywordOnly bool
slefttag string
srighttag string
slefttaglen int
srighttaglen int
document bytes.Buffer
keyword bytes.Buffer
mh KeywordReplaceHandler
}
func (this *KeywordMapper) replace(template string, keywordMap map[string]interface{}) string {
if "" == template {
template = this.template
if "" == template {
return ""
}
}
this.mh.setMap(keywordMap)
this.document.Reset()
this.keyword.Reset()
scope := false
boundary := false
skeyword := ""
svalue := ""
this.mh.startToMap()
for i := 0; i < len(template); i++ {
//log.Infof("template[%d] = ", i, template[i])
// maybe find a keyword beginning Tag - now isn't in a keyword
if !scope && template[i] == this.slefttag[0] {
if this.isATag(i, this.slefttag, template) {
this.keyword.Reset()
scope = true
}
} else if scope && template[i] == this.srighttag[0] {
// maybe find a keyword ending Tag - now in a keyword
if this.isATag(i, this.srighttag, template) {
i = i + this.srighttaglen - 1
skeyword = this.keyword.String()[this.slefttaglen:this.keyword.Len()]
svalue = this.mh.replace(skeyword)
if "" == svalue {
svalue = fmt.Sprintf("%s%s%s", this.slefttag, skeyword, this.srighttag)
}
//log.Info("value ->", svalue);
this.document.WriteString(svalue)
boundary = true
scope = false
}
}
if !boundary {
if !scope && !this.keywordOnly {
this.document.WriteByte(template[i])
} else {
this.keyword.WriteByte(template[i])
}
} else {
boundary = false
}
//log.Info("document = ", this.document)
}
this.mh.endOfMapping(this.document.String())
return this.mh.getResult()
}
func (this *KeywordMapper) isATag(i int, tag string, template string) bool {
for j := 0; j < len(tag); j++ {
if tag[j] != template[i+j] {
return false
}
}
return true
}