Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimized getTemplateValue method #191

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,26 @@ public String getTemplateValue(TemplateModel arg0) throws TemplateModelException
+ definition.getName();
WfVariable templateComponentVariable = ViewUtil.createComponentVariable(variable, suffix, definition.getFormatNotNull(), null);
String inputComponentHtml = ViewUtil.getComponentInput(user, webHelper, templateComponentVariable);
return inputComponentHtml.replaceAll("\"", "'").replaceAll("\n", "").replace("[]", "{}");
return convertComponentHtml(inputComponentHtml);
}

private String convertComponentHtml(String inputComponentHtml) {
HashMap<String, String> replacements = new HashMap<String, String>(){{
put("\"", "'");
put("\n", "");
put("[]", "{}");
}};
String regexp = "\"|\n|\\[]";
StringBuffer sb = new StringBuffer();
Pattern pattern = Pattern.compile(regexp);
Matcher matcher = pattern.matcher(inputString);

while (matcher.find()) {
matcher.appendReplacement(sb, replacements.get(matcher.group()));
}
matcher.appendTail(sb);

return sb.toString();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EditUserTypeListTest {

@Test
public void testEmptyString() {
String string = "";
Assertions.assertEquals(convertComponentHtml(string), string.replaceAll("\"", "'").replaceAll("\n", "").replace("[]", "{}"));
}

@Test
public void testStringOne() {
String string = "Glass at 866 \" 284 \" Not";
Assertions.assertEquals(convertComponentHtml(string), string.replaceAll("\"", "'").replaceAll("\n", "").replace("[]", "{}"));
}

@Test
public void testStringTwo() {
String string = "\n Max tax _21 i0 tree";
Assertions.assertEquals(convertComponentHtml(string), string.replaceAll("\"", "'").replaceAll("\n", "").replace("[]", "{}"));
}

@Test
public void testStringThree() {
String string = "Nothing any ][]{}goes [] ant";
Assertions.assertEquals(convertComponentHtml(string), string.replaceAll("\"", "'").replaceAll("\n", "").replace("[]", "{}"));
}

@Test
public void testStringFour() {
String string = "North \n behind [] mens \" 44";
Assertions.assertEquals(convertComponentHtml(string), string.replaceAll("\"", "'").replaceAll("\n", "").replace("[]", "{}"));
}

private String convertComponentHtml(String inputString) {
HashMap<String, String> replacements = new HashMap<String, String>() {{
put("\"", "'");
put("\n", "");
put("[]", "{}");
}};
String regexp = "\"|\n|\\[]";
StringBuffer sb = new StringBuffer();
Pattern pattern = Pattern.compile(regexp);
Matcher matcher = pattern.matcher(inputString);

while (matcher.find()) {
matcher.appendReplacement(sb, replacements.get(matcher.group()));
}
matcher.appendTail(sb);

return sb.toString();
}
}