@Test public void shouldNotReplaceAnythingIfTextContainsNoVariables() { ConnectorManager connector = mock(ConnectorManager.class); String result = SearchHelper.insertVariableValues(connector, PROJECT_ID, "Hello Jon Doe, you are user no. 42!"); assertEquals("Hello Jon Doe, you are user no. " + COUNTER_VALUE + "!", result); }
public static String insertVariableValues(ConnectorManager connector, Long projectId, String text) throws IllegalStateException { final VariableStoreConnector variableStore = connector.getConnector(VariableStoreConnector.class); final CounterStoreConnector counterStore = connector.getConnector(CounterStoreConnector.class); final FileStoreConnector fileStore = connector.getConnector(FileStoreConnector.class); String result = "" + text; final Pattern pattern = Pattern.compile("\\{\\{(\\$|\\#|\\\\|:)(.*?)}}"); final Matcher matcher = pattern.matcher(result); while (matcher.find()) { final String type = matcher.group(1); final String name = matcher.group(2); switch (type) { case "$": result = result.replaceAll("\\{\\{\\$" + name + "}}", Matcher.quoteReplacement(variableStore.get(name))); break; case "#": result = result.replaceAll("\\{\\{\\#" + name + "}}", String.valueOf(counterStore.get(name))); break; case "\\": final String path = fileStore.getAbsoluteFileLocation(projectId, name); result = result.replaceAll("\\{\\{\\\\" + name + "}}", path); break; case ":": final String value = connector.getEnvironment().getVariablesAsMap().get(name); result = result.replaceAll("\\{\\{:" + name + "}}", value); break; default: break; } } return result; }
SearchHelper { public static String insertVariableValues(ConnectorManager connector, Long projectId, String text) throws IllegalStateException { final VariableStoreConnector variableStore = connector.getConnector(VariableStoreConnector.class); final CounterStoreConnector counterStore = connector.getConnector(CounterStoreConnector.class); final FileStoreConnector fileStore = connector.getConnector(FileStoreConnector.class); String result = "" + text; final Pattern pattern = Pattern.compile("\\{\\{(\\$|\\#|\\\\|:)(.*?)}}"); final Matcher matcher = pattern.matcher(result); while (matcher.find()) { final String type = matcher.group(1); final String name = matcher.group(2); switch (type) { case "$": result = result.replaceAll("\\{\\{\\$" + name + "}}", Matcher.quoteReplacement(variableStore.get(name))); break; case "#": result = result.replaceAll("\\{\\{\\#" + name + "}}", String.valueOf(counterStore.get(name))); break; case "\\": final String path = fileStore.getAbsoluteFileLocation(projectId, name); result = result.replaceAll("\\{\\{\\\\" + name + "}}", path); break; case ":": final String value = connector.getEnvironment().getVariablesAsMap().get(name); result = result.replaceAll("\\{\\{:" + name + "}}", value); break; default: break; } } return result; } }
SearchHelper { public static String insertVariableValues(ConnectorManager connector, Long projectId, String text) throws IllegalStateException { final VariableStoreConnector variableStore = connector.getConnector(VariableStoreConnector.class); final CounterStoreConnector counterStore = connector.getConnector(CounterStoreConnector.class); final FileStoreConnector fileStore = connector.getConnector(FileStoreConnector.class); String result = "" + text; final Pattern pattern = Pattern.compile("\\{\\{(\\$|\\#|\\\\|:)(.*?)}}"); final Matcher matcher = pattern.matcher(result); while (matcher.find()) { final String type = matcher.group(1); final String name = matcher.group(2); switch (type) { case "$": result = result.replaceAll("\\{\\{\\$" + name + "}}", Matcher.quoteReplacement(variableStore.get(name))); break; case "#": result = result.replaceAll("\\{\\{\\#" + name + "}}", String.valueOf(counterStore.get(name))); break; case "\\": final String path = fileStore.getAbsoluteFileLocation(projectId, name); result = result.replaceAll("\\{\\{\\\\" + name + "}}", path); break; case ":": final String value = connector.getEnvironment().getVariablesAsMap().get(name); result = result.replaceAll("\\{\\{:" + name + "}}", value); break; default: break; } } return result; } private  SearchHelper(); }
SearchHelper { public static String insertVariableValues(ConnectorManager connector, Long projectId, String text) throws IllegalStateException { final VariableStoreConnector variableStore = connector.getConnector(VariableStoreConnector.class); final CounterStoreConnector counterStore = connector.getConnector(CounterStoreConnector.class); final FileStoreConnector fileStore = connector.getConnector(FileStoreConnector.class); String result = "" + text; final Pattern pattern = Pattern.compile("\\{\\{(\\$|\\#|\\\\|:)(.*?)}}"); final Matcher matcher = pattern.matcher(result); while (matcher.find()) { final String type = matcher.group(1); final String name = matcher.group(2); switch (type) { case "$": result = result.replaceAll("\\{\\{\\$" + name + "}}", Matcher.quoteReplacement(variableStore.get(name))); break; case "#": result = result.replaceAll("\\{\\{\\#" + name + "}}", String.valueOf(counterStore.get(name))); break; case "\\": final String path = fileStore.getAbsoluteFileLocation(projectId, name); result = result.replaceAll("\\{\\{\\\\" + name + "}}", path); break; case ":": final String value = connector.getEnvironment().getVariablesAsMap().get(name); result = result.replaceAll("\\{\\{:" + name + "}}", value); break; default: break; } } return result; } private  SearchHelper(); static boolean search(String value, String text, boolean regex); static boolean searchWithRegex(String regex, String text); static String insertVariableValues(ConnectorManager connector, Long projectId, String text); }
SearchHelper { public static String insertVariableValues(ConnectorManager connector, Long projectId, String text) throws IllegalStateException { final VariableStoreConnector variableStore = connector.getConnector(VariableStoreConnector.class); final CounterStoreConnector counterStore = connector.getConnector(CounterStoreConnector.class); final FileStoreConnector fileStore = connector.getConnector(FileStoreConnector.class); String result = "" + text; final Pattern pattern = Pattern.compile("\\{\\{(\\$|\\#|\\\\|:)(.*?)}}"); final Matcher matcher = pattern.matcher(result); while (matcher.find()) { final String type = matcher.group(1); final String name = matcher.group(2); switch (type) { case "$": result = result.replaceAll("\\{\\{\\$" + name + "}}", Matcher.quoteReplacement(variableStore.get(name))); break; case "#": result = result.replaceAll("\\{\\{\\#" + name + "}}", String.valueOf(counterStore.get(name))); break; case "\\": final String path = fileStore.getAbsoluteFileLocation(projectId, name); result = result.replaceAll("\\{\\{\\\\" + name + "}}", path); break; case ":": final String value = connector.getEnvironment().getVariablesAsMap().get(name); result = result.replaceAll("\\{\\{:" + name + "}}", value); break; default: break; } } return result; } private  SearchHelper(); static boolean search(String value, String text, boolean regex); static boolean searchWithRegex(String regex, String text); static String insertVariableValues(ConnectorManager connector, Long projectId, String text);  }