forked from OpenIntegrationEngine/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeTemplateUtil.java
More file actions
264 lines (212 loc) · 10.9 KB
/
CodeTemplateUtil.java
File metadata and controls
264 lines (212 loc) · 10.9 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
/*
* Copyright (c) Mirth Corporation. All rights reserved.
*
* http://www.mirthcorp.com
*
* The software in this package is published under the terms of the MPL license a copy of which has
* been included with this distribution in the LICENSE.txt file.
*/
package com.mirth.connect.util;
import java.io.IOException;
import java.io.StringReader;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.WordUtils;
import org.mozilla.javascript.CompilerEnvirons;
import org.mozilla.javascript.Parser;
import org.mozilla.javascript.ast.AstNode;
import org.mozilla.javascript.ast.AstRoot;
import org.mozilla.javascript.ast.Comment;
import org.mozilla.javascript.ast.FunctionNode;
import org.mozilla.javascript.ast.Name;
import org.mozilla.javascript.ast.NodeVisitor;
import com.mirth.connect.model.Parameter;
import com.mirth.connect.model.Parameters;
import com.mirth.connect.model.codetemplates.CodeTemplateFunctionDefinition;
public class CodeTemplateUtil {
private static Pattern COMMENT_PATTERN = Pattern.compile("^/\\*{2}([\\s\\S](?!\\*/))*[\\s\\S]?\\*/(\r\n|\r|\n)*");
public static CodeTemplateDocumentation getDocumentation(String code) {
String description = null;
CodeTemplateFunctionDefinition functionDefinition = null;
if (StringUtils.isNotBlank(code)) {
try {
try {
// First try the function AST
FunctionVisitor visitor = parseFunction(code);
description = visitor.getDescription();
functionDefinition = visitor.getFunctionDefinition();
} catch (Throwable t) {
// If that didn't work, find the description comment and chop it off
Matcher matcher = COMMENT_PATTERN.matcher(code);
if (matcher.find()) {
description = matcher.group().replaceAll("^\\s*/\\*+\\s*|\\s*\\*+/\\s*$|(\r\n|\r|\n)\\s*@[\\s\\S]*", "").trim();
code = StringUtils.substring(code, matcher.end());
// Then parse into AST and get function definition
FunctionVisitor visitor = parseFunction(code);
functionDefinition = visitor.getFunctionDefinition();
}
}
} catch (Throwable t) {
// Ignore
}
}
return new CodeTemplateDocumentation(description, functionDefinition);
}
private static FunctionVisitor parseFunction(String code) throws IOException {
FunctionVisitor visitor = new FunctionVisitor();
CompilerEnvirons env = new CompilerEnvirons();
env.setRecordingLocalJsDocComments(true);
env.setAllowSharpComments(true);
env.setRecordingComments(true);
new Parser(env).parse(new StringReader(code), null, 1).visitAll(visitor);
return visitor;
}
public static String updateCode(String code) {
if (StringUtils.isNotBlank(code)) {
code = StringUtils.trim(code);
int endIndex = 0;
Matcher matcher = COMMENT_PATTERN.matcher(code);
if (matcher.find()) {
endIndex = matcher.end();
}
CodeTemplateDocumentation documentation = getDocumentation(code);
String description = documentation.getDescription();
CodeTemplateFunctionDefinition functionDefinition = documentation.getFunctionDefinition();
if (StringUtils.isBlank(description)) {
description = "Modify the description here. Modify the function name and parameters as needed. One function per template is recommended; create a new code template for each new function.";
}
StringBuilder builder = new StringBuilder("/**");
for (String descriptionLine : description.split("\r\n|\r|\n")) {
builder.append("\n\t").append(WordUtils.wrap(descriptionLine, 100, "\n\t", false));
}
if (functionDefinition != null) {
builder.append('\n');
if (CollectionUtils.isNotEmpty(functionDefinition.getParameters())) {
for (Parameter parameter : functionDefinition.getParameters()) {
StringBuilder parameterBuilder = new StringBuilder("\n\t@param {");
parameterBuilder.append(StringUtils.defaultString(parameter.getType(), "Any"));
parameterBuilder.append("} ").append(parameter.getName()).append(" - ").append(StringUtils.trimToEmpty(parameter.getDescription()));
builder.append(WordUtils.wrap(parameterBuilder.toString(), 100, "\n\t\t", false));
}
}
StringBuilder returnBuilder = new StringBuilder("\n\t@return {").append(StringUtils.defaultString(functionDefinition.getReturnType(), "Any")).append("} ").append(StringUtils.trimToEmpty(functionDefinition.getReturnDescription()));
builder.append(WordUtils.wrap(returnBuilder.toString(), 100, "\n\t\t", false));
}
builder.append("\n*/\n");
return builder.toString() + code.substring(endIndex);
}
return code;
}
public static String stripDocumentation(String code) {
if (StringUtils.isNotBlank(code)) {
code = StringUtils.trim(code);
Matcher matcher = COMMENT_PATTERN.matcher(code);
if (matcher.find()) {
return StringUtils.trim(code.substring(matcher.end()));
}
}
return code;
}
public static class CodeTemplateDocumentation {
private String description;
private CodeTemplateFunctionDefinition functionDefinition;
public CodeTemplateDocumentation(String description, CodeTemplateFunctionDefinition functionDefinition) {
this.description = description;
this.functionDefinition = functionDefinition;
}
public String getDescription() {
return description;
}
public CodeTemplateFunctionDefinition getFunctionDefinition() {
return functionDefinition;
}
}
private static class FunctionVisitor implements NodeVisitor {
private static Pattern DOCUMENTATION_PATTERN = Pattern.compile("/\\*{2,}(?<desc>([^\r\n]|(\r\n|\r|\n)(?!\\*/|\\s*@))+)|(?<anno>(\r\n|\r|\n)\\s*@([^\r\n]|(\r\n|\r|\n)(?!\\*/|\\s*@))+)");
private static Pattern ANNOTATION_PARAM_PATTERN = Pattern.compile("@param(\\s*\\{(?<type>[^\\}]*)\\})?\\s*(?<name>\\w+)\\s*(?:-\\s*)?(?<desc>[\\s\\S]*)");
private static Pattern ANNOTATION_RETURN_PATTERN = Pattern.compile("@returns?(\\s*\\{(?<type>[^\\}]*)\\})?\\s*(?<desc>[\\s\\S]*)");
private Comment commentNode;
private boolean found;
private String description;
private CodeTemplateFunctionDefinition functionDefinition;
public String getDescription() {
return description;
}
public CodeTemplateFunctionDefinition getFunctionDefinition() {
return functionDefinition;
}
@Override
public boolean visit(AstNode node) {
if (node instanceof AstRoot) {
return true;
}
if (commentNode == null) {
Comment comment = null;
if (node instanceof Comment) {
comment = (Comment) node;
} else {
comment = node.getJsDocNode();
}
if (comment != null) {
Matcher matcher = DOCUMENTATION_PATTERN.matcher(comment.getValue());
while (matcher.find()) {
String desc = matcher.group("desc");
if (StringUtils.isNotBlank(desc)) {
description = convertDesc(desc);
}
}
commentNode = comment;
}
}
if (!found && node instanceof FunctionNode) {
FunctionNode functionNode = (FunctionNode) node;
functionDefinition = new CodeTemplateFunctionDefinition(functionNode.getFunctionName().getIdentifier());
Map<String, Parameter> parameterMap = new LinkedHashMap<String, Parameter>();
for (AstNode param : functionNode.getParams()) {
if (param instanceof Name) {
String paramName = ((Name) param).getIdentifier();
parameterMap.put(paramName, new Parameter(paramName));
}
}
Comment comment = null;
if (commentNode != null) {
comment = commentNode;
} else {
comment = functionNode.getJsDocNode();
}
if (comment != null) {
Matcher matcher = DOCUMENTATION_PATTERN.matcher(comment.getValue());
while (matcher.find()) {
String annotation = convertDesc(matcher.group("anno"));
if (StringUtils.startsWithIgnoreCase(annotation, "@param")) {
Matcher annotationMatcher = ANNOTATION_PARAM_PATTERN.matcher(annotation);
if (annotationMatcher.find()) {
Parameter parameter = parameterMap.get(annotationMatcher.group("name"));
if (parameter != null) {
parameter.setType(StringUtils.trimToEmpty(annotationMatcher.group("type")));
parameter.setDescription(convertDesc(annotationMatcher.group("desc")));
}
}
} else if (StringUtils.startsWithIgnoreCase(annotation, "@return")) {
Matcher annotationMatcher = ANNOTATION_RETURN_PATTERN.matcher(annotation);
if (annotationMatcher.find()) {
functionDefinition.setReturnType(StringUtils.trimToEmpty(annotationMatcher.group("type")));
functionDefinition.setReturnDescription(convertDesc(annotationMatcher.group("desc")));
}
}
}
}
functionDefinition.setParameters(new Parameters(parameterMap.values()));
found = true;
}
return false;
}
private String convertDesc(String desc) {
return StringUtils.trimToEmpty(desc).replaceAll("\\s*\\*+/\\s*$", "").replaceAll("[ \t\\x0B\f]*(\r\n|\r|\n)[ \t\\x0B\f]*", "\n").replaceAll("(?<=\\S)\n(?=\\S)", " ").replaceAll("\n{2,}", "\n\n");
}
}
}