From 4f2419fc381d389481efe1b13bc7316a32e4d3a0 Mon Sep 17 00:00:00 2001 From: Vadim-I Date: Wed, 23 Dec 2020 17:39:00 +0700 Subject: [PATCH] optimized getTemplateValue method --- .../web/ftl/component/EditUserTypeList.java | 20 ++++++- .../ftl/component/EditUserTypeListTest.java | 58 +++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 wfe-web/src/test/java/ru/runa/wf/web/ftl/component/EditUserTypeListTest.java diff --git a/wfe-web/src/main/java/ru/runa/wf/web/ftl/component/EditUserTypeList.java b/wfe-web/src/main/java/ru/runa/wf/web/ftl/component/EditUserTypeList.java index 93bac09685..06f0d65727 100644 --- a/wfe-web/src/main/java/ru/runa/wf/web/ftl/component/EditUserTypeList.java +++ b/wfe-web/src/main/java/ru/runa/wf/web/ftl/component/EditUserTypeList.java @@ -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 replacements = new HashMap(){{ + 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(); + } } } \ No newline at end of file diff --git a/wfe-web/src/test/java/ru/runa/wf/web/ftl/component/EditUserTypeListTest.java b/wfe-web/src/test/java/ru/runa/wf/web/ftl/component/EditUserTypeListTest.java new file mode 100644 index 0000000000..9944cdf4a9 --- /dev/null +++ b/wfe-web/src/test/java/ru/runa/wf/web/ftl/component/EditUserTypeListTest.java @@ -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 replacements = new HashMap() {{ + 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(); + } +}