SegmentFixMTWhitespaceRemoval extends WSTaskAutomaticAction { public String fixWhitespace(String source, String target) { final Matcher source_matcher = regexPatternTokens.matcher(source); final Matcher target_matcher = regexPatternTokens.matcher(target); final int leftCaptureGroup = config.getLeftCaptureGroup(); final int leftIgnoreCaptureGroup = config.getLeftIgnoreCaptureGroup(); final int centerCaptureGroup = config.getCenterCaptureGroup(); final int compareCaptureGroup = config.getCompareCaptureGroup(); final int rightIgnoreCaptureGroup = config.getRightIgnoreCaptureGroup(); final int rightCaptureGroup = config.getRightCaptureGroup(); final Map<String, String[]> sourceMatches = new HashMap<>(); while (source_matcher.find()) { final int numberOfGroups = source_matcher.groupCount(); final String[] groups = new String[numberOfGroups + 1]; for (int i = 0; i <= numberOfGroups; i++) { groups[i] = source_matcher.group(i); } sourceMatches.put(groups[compareCaptureGroup], groups); } final StringBuffer final_target = new StringBuffer(); while (target_matcher.find()) { final String target_match = target_matcher.group(0); final String target_value = target_matcher.group(compareCaptureGroup); if (!sourceMatches.containsKey(target_value)) { LOG.warn(String.format("The target contained the placeholder {%s}, which was not found in the source: \"%s\". No fix will be applied.", target_match, source)); } else if (!sourceMatches.get(target_value)[0].equals(target_match)) { final StringBuilder replacement = new StringBuilder(); final String target_left = target_matcher.group(leftCaptureGroup); final String target_ignore_left = target_matcher.group(leftIgnoreCaptureGroup); final String target_center = target_matcher.group(centerCaptureGroup); final String target_ignore_right = target_matcher.group(rightIgnoreCaptureGroup); final String target_right = target_matcher.group(rightCaptureGroup); final String[] sourceGroups = sourceMatches.get(target_value); final String source_match = sourceGroups[0]; final String source_left = sourceGroups[leftCaptureGroup]; final String source_ignore_left = sourceGroups[leftIgnoreCaptureGroup]; final String source_value = sourceGroups[compareCaptureGroup]; final String source_ignore_right = sourceGroups[rightIgnoreCaptureGroup]; final String source_right = sourceGroups[rightCaptureGroup]; if (target_ignore_left.length() == 0) { replacement.append(source_left); } else { replacement.append(target_ignore_left).append(target_left); } replacement.append(target_center); if (target_ignore_right.length() == 0) { replacement.append(source_right); } else { replacement.append(target_ignore_right).append(target_right); } target_matcher.appendReplacement(final_target, Matcher.quoteReplacement(replacement.toString())); } } target_matcher.appendTail(final_target); return final_target.toString(); }  @Override String getDescription(); @Override String getName(); @Override String getVersion(); @Override String[] getReturns(); @Override WSActionResult execute(WSContext context, Map parameters, WSTask task); String fixWhitespace(String source, String target);  }
@Test public void fixWhitespaceTest() throws Exception { testWhitespace("Hello {1}my{2} darling", "Salut{1}disque{2}Darling", "Salut {1}disque{2} Darling"); testWhitespace("{0} Hello {1}my{2} darling {999}", "{0}Salut{1}disque{2}Darling{999}", "{0} Salut {1}disque{2} Darling {999}"); testWhitespace("{2} what {3} {4} is {5} that {6}", "{2}dangit{3}{4} bobby{5}hill {6}", "{2} dangit {3} {4} bobby {5} hill {6}"); testWhitespace("\t\n {1} Whoooaaa theree buddy\t\t\n{2}\t{3}\n\nThat ain't{4}\t\tcool!{5}", "{1}Test test test{2}{3}ahahhaha{4}awesome!{5}", "\t\n {1} Test test test\t\t\n{2}\t{3}\n\nahahhaha{4}\t\tawesome!{5}"); testWhitespace("\t \n {1} Whoooaaa theree buddy\t\t\n{2}\t {3} \n\nThat ain't{4}\t\tcool!{5} ", "{1}Test test test{2}{3}ahahhaha{4}awesome!{5}", "\t \n {1} Test test test\t\t\n{2}\t {3} \n\nahahhaha{4}\t\tawesome!{5} "); testWhitespace("Hello"+ " \t\n\u2006\u00A0\u2002\u2003\u2003\u202F \u200B \uDB40\uDC20 " + "{150002023023}" + " \t\n\u2006\u00A0\u2002\u2003\u2003\u202F \u200B \uDB40\uDC20 "+ "there", "Bonjour{150002023023}Là", "Bonjour"+ " \t\n\u2006\u00A0\u2002\u2003\u2003\u202F \u200B \uDB40\uDC20 " + "{150002023023}" + " \t\n\u2006\u00A0\u2002\u2003\u2003\u202F \u200B \uDB40\uDC20 "+ "Là"); testWhitespace("From the {1}Transfer Funds{2} {3}From{4} drop-down, choose the bank account the credit card was paid from.", "Dans la liste déroulante {1}Transférer des fonds{2} {3}depuis{4}, choisissez le compte bancaire d'où le paiement a été émis.", "Dans la liste déroulante {1}Transférer des fonds{2} {3}depuis{4}, choisissez le compte bancaire d'où le paiement a été émis."); testWhitespace("{1}This is one long sentence{2} in order to {3}test{4} punctuation {5}detection{6} which is {7}quite{8} rare.", "{1}This is now punctuated{2}, in order to {3}test!{4} Punctuation {5}detection,{6} while somewhat strange, is {7}important{8}!", "{1}This is now punctuated{2}, in order to {3}test!{4} Punctuation {5}detection,{6} while somewhat strange, is {7}important{8}!"); testWhitespace("{1}This is one long sentence{2} in order to {3}test{4} punctuation {5}detection{6} which is {7}quite{8} rare.", "{1}This is now punctuated{2}¿ In order to {3}test¡{4} Punctuation {5}detection¿{6} while somewhat strange, is {7}important{8}¡", "{1}This is now punctuated{2}¿ In order to {3}test¡{4} Punctuation {5}detection¿{6} while somewhat strange, is {7}important{8}¡"); testWhitespace("{1}reorder{2} {3}test{4}.", "{3}test{4} {1}reorder{2}.", "{3}test{4}{1}reorder{2}."); }