SoapMessageFormatter implements MessageFormatter { public String removeMultiRefs(String message) { HashMap<String, SubText> multiRefDefinition = extractMultiRef(message, PATTERN_MULTIREF_DEFINITION, PATTERN_MULTIREF_DEFINITION_GROUP_ID, 0); HashMap<String, SubText> multiRefTagNames = extractMultiRef(message, PATTERN_MULTIREF_DEFINITION, PATTERN_MULTIREF_DEFINITION_GROUP_ID, PATTERN_MULTIREF_DEFINITION_GROUP_TAG_NAME); HashMap<String, SubText> multiRefValues = extractMultiRef(message, PATTERN_MULTIREF_VALUES, PATTERN_MULTIREF_VALUES_GROUP_ID, PATTERN_MULTIREF_VALUES_GROUP_VALUE); HashMap<String, SubText> multiRefValuesToRemove = extractMultiRef(message, PATTERN_MULTIREF_VALUES, PATTERN_MULTIREF_VALUES_GROUP_ID, 0); HashMap<String, SubText> multiRefTagSchemas = extractMultiRef(message, PATTERN_MULTIREF_VALUES, PATTERN_MULTIREF_VALUES_GROUP_ID, PATTERN_MULTIREF_VALUES_GROUP_SCHEMA_DEF); StringBuilder sb = new StringBuilder(message); Set<String> stringSet = multiRefDefinition.keySet(); ArrayList<String> sortedKeys = new ArrayList<>(stringSet); Collections.sort(sortedKeys); for (String id : sortedKeys) { SubText subTextDef = multiRefDefinition.get(id); String toRemove = extractSubText(message, subTextDef); SubText subTextValue = multiRefValues.get(id); String replaceWith = extractSubText(message, subTextValue); int sbStart = sb.indexOf(toRemove); sb.replace(sbStart, sbStart + subTextDef.getLength(), replaceWith); String tagName = extractSubText(message, multiRefTagNames.get(id)); sb.insert(sbStart + replaceWith.length(), String.format("</%s>", tagName)); sb.insert(sbStart, String.format("<%s %s>", tagName, extractSubText(message, multiRefTagSchemas.get(id)))); SubText subTextValueToRemove = multiRefValuesToRemove.get(id); String sbToRemoveString = extractSubText(message, subTextValueToRemove); int sbIndexToRemove = sb.indexOf(sbToRemoveString); sb.delete(sbIndexToRemove, sbIndexToRemove + sbToRemoveString.length()); } return sb.toString(); }  @Override boolean formattingNeeded(String message); @Override String format(String message); String prettyFormat(String input, int indent, boolean omitXmlDeclaration); String removeXsiFromNulls(String xml); String removeMultiRefs(String message); String prettyFormat(String input); @Override String getName(); @Override String getDescription(); @Override String getPluginableId(); @Override int getApiVersion(); boolean isRemoveMultiRefs(); void setRemoveMultiRefs(boolean removeMultiRefs); boolean isRemoveXsiForNilElements(); void setRemoveXsiForNilElements(boolean removeXsiForNilElements); static final String REMOVE_NIL_PATTERN; static final String REMOVE_NIL_REPLACE_PATTERN; static final String XML_VERSION_1_0_ENCODING_UTF_8; }
@Test public void testRemoveMultiRefs() throws Exception { String soap = IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("soap/soapWithMultiref.xml")); String expected = IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("soap/soapWithMultiref.flatted.xml")); String removeMultiRefs = formatter.removeMultiRefs(soap); AssertJUnit.assertEquals(expected, removeMultiRefs); }