-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemplateEngine(Updated).java
More file actions
324 lines (287 loc) · 9.67 KB
/
TemplateEngine(Updated).java
File metadata and controls
324 lines (287 loc) · 9.67 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
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Stack;
public class TemplateEngine {
private static final Character TEMPLATE_START_PREFIX = '$';
private static final Character TEMPLATE_START = '{';
private static final Character TEMPLATE_END = '}';
private static final String MM_KEEP = "keep-unmatched";
private static final String MM_DELETE = "delete-unmatched";
private static final String MM_OPTIMIZE = "optimization";
public TemplateEngine() {
}
public String evaluate(String templateString, EntryMap entryMap, String matchingMode) {
Result result = getResult(templateString, entryMap, matchingMode);
if (result == null)
return templateString;
else
return result.getInstancedString();
}
public int templatesReplaced(String templateString, EntryMap entryMap, String matchingMode) {
Result result = getResult(templateString, entryMap, matchingMode);
if (result == null)
return 0;
else
return result.getTemplatesReplaced();
}
private Result getResult(String templateString, EntryMap entryMap, String matchingMode) {
if (!isEvaluationPossible(templateString, entryMap)) {
return null;
}
if (!isMatchingModeValid(matchingMode)) {
matchingMode = MM_DELETE;
}
HashSet<Template> templates = identifyTemplates(templateString);
HashSet<Template> copytemplates = identifyTemplates(templateString);
ArrayList<Template> sortedTemplates = sortTemplates(templates);
ArrayList<Template> sortedcopyTemplates = sortTemplates(copytemplates);
if (matchingMode.equals(MM_OPTIMIZE)) {
Result delete_res = instantiate(templateString, sortedcopyTemplates, entryMap.getEntries(), MM_DELETE);
Result keep_res = instantiate(templateString, sortedTemplates, entryMap.getEntries(), MM_KEEP);
if (keep_res.getTemplatesReplaced() >= delete_res.getTemplatesReplaced())
return keep_res;
else
return delete_res;
} else {
Result result = instantiate(templateString, sortedTemplates, entryMap.getEntries(), matchingMode);
return result;
}
}
private Boolean isEvaluationPossible(String templateString, EntryMap entryMap) {
if (templateString == null) {
return Boolean.FALSE;
}
if (templateString.isEmpty()) {
return Boolean.FALSE;
}
if (entryMap == null) {
return Boolean.FALSE;
}
return Boolean.TRUE;
}
private Boolean isMatchingModeValid(String matchingMode) {
if (matchingMode == null) {
return Boolean.FALSE;
}
if (matchingMode.equals(MM_KEEP)) {
return Boolean.TRUE;
}
if (matchingMode.equals(MM_DELETE)) {
return Boolean.TRUE;
}
if (matchingMode.equals(MM_OPTIMIZE)) {
return Boolean.TRUE;
}
return Boolean.FALSE;
}
private HashSet<Template> identifyTemplates(String templateString) {
HashSet<Template> templates = new HashSet<>();
Stack<Integer> templateCandidates = new Stack<>();
Integer charIndex = 0;
Boolean underSequence = Boolean.FALSE;
while (charIndex < templateString.length()) {
if (Character.compare(templateString.charAt(charIndex), TEMPLATE_START_PREFIX) == 0) {
underSequence = Boolean.TRUE;
charIndex++;
continue;
}
if (Character.compare(templateString.charAt(charIndex), TEMPLATE_START) == 0) {
if (underSequence) {
templateCandidates.add(charIndex);
}
underSequence = Boolean.FALSE;
charIndex++;
continue;
}
if (Character.compare(templateString.charAt(charIndex), TEMPLATE_END) == 0) {
if (!templateCandidates.isEmpty()) {
Template template;
Integer startIndex = templateCandidates.pop();
if ((startIndex + 1) == charIndex) {
template = new Template(startIndex, charIndex, "");
} else {
template = new Template(startIndex, charIndex,
templateString.substring(startIndex + 1, charIndex));
}
templates.add(template);
}
underSequence = Boolean.FALSE;
charIndex++;
continue;
}
underSequence = Boolean.FALSE;
charIndex++;
}
return templates;
}
private ArrayList<Template> sortTemplates(HashSet<Template> templates) {
ArrayList<Template> sortedTemplates = new ArrayList<>();
Template currentTemplate;
Integer minLength;
Integer startIndex;
while (!templates.isEmpty()) {
currentTemplate = null;
minLength = Integer.MAX_VALUE;
startIndex = Integer.MAX_VALUE;
for (Template current : templates) {
if (current.getContent().length() < minLength) {
currentTemplate = current;
minLength = current.getContent().length();
startIndex = current.getStartIndex();
} else {
if (current.getContent().length() == minLength) {
if (current.getStartIndex() < startIndex) {
currentTemplate = current;
minLength = current.getContent().length();
startIndex = current.getStartIndex();
}
}
}
}
if (currentTemplate != null) {
templates.remove(currentTemplate);
sortedTemplates.add(currentTemplate);
} else {
throw new RuntimeException();
}
}
return sortedTemplates;
}
private Result instantiate(String instancedString, ArrayList<Template> sortedTemplates,
ArrayList<EntryMap.Entry> sortedEntries, String matchingMode) {
Integer templatesReplaced = 0;
Boolean replaceHappened;
Template currentTemplate;
EntryMap.Entry currentEntry;
for (Integer i = 0; i < sortedTemplates.size(); i++) {
currentTemplate = sortedTemplates.get(i);
replaceHappened = Boolean.FALSE;
for (Integer j = 0; j < sortedEntries.size(); j++) {
currentEntry = sortedEntries.get(j);
if (isAMatch(currentTemplate, currentEntry)) {
instancedString = doReplace(instancedString, currentTemplate, i, currentEntry.getValue(),
sortedTemplates);
replaceHappened = Boolean.TRUE;
break;
}
}
if (replaceHappened) {
templatesReplaced++;
} else {
if (matchingMode.equals(MM_DELETE)) {
instancedString = doReplace(instancedString, currentTemplate, i, "", sortedTemplates);
}
}
}
return new Result(instancedString, templatesReplaced);
}
private Boolean isAMatch(Template template, EntryMap.Entry entry) {
String leftHandSide = template.getContent().replaceAll("\\s", "");
String rightHandSide = entry.getPattern().replaceAll("\\s", "");
if (entry.caseSensitive) {
return leftHandSide.equals(rightHandSide);
} else {
return leftHandSide.toLowerCase().equals(rightHandSide.toLowerCase());
}
}
private String doReplace(String instancedString, Template currentTemplate, Integer currentTemplateIndex,
String replaceValue, ArrayList<Template> sortedTemplates) {
Integer diff = 3 + currentTemplate.getContent().length() - replaceValue.length();
String firstHalf;
String secondHalf;
if (currentTemplate.getStartIndex() == 1) {
firstHalf = "";
} else {
firstHalf = instancedString.substring(0, currentTemplate.getStartIndex() - 1);
}
if (currentTemplate.getEndIndex() == instancedString.length()) {
secondHalf = "";
} else {
secondHalf = instancedString.substring(currentTemplate.getEndIndex() + 1);
}
StringBuilder builder = new StringBuilder();
builder.append(firstHalf);
builder.append(replaceValue);
builder.append(secondHalf);
String updatedInstancedString = builder.toString();
Template temp = null;
for (int i = currentTemplateIndex + 1; i < sortedTemplates.size(); i++) {
temp = sortedTemplates.get(i);
if ((temp.getStartIndex() < currentTemplate.getStartIndex())
&& (temp.getEndIndex() > currentTemplate.getEndIndex())) {
sortedTemplates.get(i).setEndIndex(temp.getEndIndex() - diff);
sortedTemplates.get(i).setContent(updatedInstancedString
.substring(sortedTemplates.get(i).getStartIndex() + 1, sortedTemplates.get(i).getEndIndex()));
} else {
if (temp.getStartIndex() > currentTemplate.getEndIndex()) {
sortedTemplates.get(i).setStartIndex(temp.getStartIndex() - diff);
sortedTemplates.get(i).setEndIndex(temp.getEndIndex() - diff);
}
}
}
return updatedInstancedString;
}
public class Template {
Integer startIndex;
Integer endIndex;
String content;
public Template(Integer startIndex, Integer endIndex, String content) {
this.startIndex = startIndex;
this.endIndex = endIndex;
this.content = content;
}
public Integer getStartIndex() {
return startIndex;
}
public Integer getEndIndex() {
return endIndex;
}
public String getContent() {
return content;
}
public void setStartIndex(Integer startIndex) {
this.startIndex = startIndex;
}
public void setEndIndex(Integer endIndex) {
this.endIndex = endIndex;
}
public void setContent(String content) {
this.content = content;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Template template = (Template) o;
if (getStartIndex() != null ? !getStartIndex().equals(template.getStartIndex())
: template.getStartIndex() != null)
return false;
if (getEndIndex() != null ? !getEndIndex().equals(template.getEndIndex()) : template.getEndIndex() != null)
return false;
return getContent() != null ? getContent().equals(template.getContent()) : template.getContent() == null;
}
@Override
public int hashCode() {
int result = getStartIndex() != null ? getStartIndex().hashCode() : 0;
result = 31 * result + (getEndIndex() != null ? getEndIndex().hashCode() : 0);
result = 31 * result + (getContent() != null ? getContent().hashCode() : 0);
return result;
}
}
public class Result {
String instancedString;
Integer templatesReplaced;
public Result(String instancedString, Integer templatesReplaced) {
this.instancedString = instancedString;
this.templatesReplaced = templatesReplaced;
}
public String getInstancedString() {
return instancedString;
}
public Integer getTemplatesReplaced() {
return templatesReplaced;
}
}
}