DumpInformationMerger extends AbstractElementMerger { @Override public void mergeNodes(List<Node> fromParents, Element toParent, Document toDocument) throws PDUStitchingException { final Node actualParent = toParent.getParentNode(); actualParent.removeChild(toParent); for (Node fromParent : fromParents) { final Element newDumpElement = toDocument.createElement(fromParent.getNodeName()); final NodeList origChildNodes = fromParent.getChildNodes(); for (int j = 0; j < origChildNodes.getLength(); j++) { final Node origChild = origChildNodes.item(j); if (!(origChild.getNodeType() == Node.TEXT_NODE) && !origChild.getTextContent().contains("\n")) { final Element dumpChildElement = toDocument.createElement(origChild.getNodeName()); final String textContent = origChild.getTextContent(); final Text textNode = toDocument.createTextNode(textContent); dumpChildElement.appendChild(textNode); newDumpElement.appendChild(dumpChildElement); } } actualParent.appendChild(newDumpElement); } }  @Override void mergeNodes(List<Node> fromParents, Element toParent, Document toDocument);  }
@Test public void testMergeNodes() throws Exception { List<Node> fromParents = new ArrayList<>(); fromParents.add(ManifestTestUtils.createNode( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + " <sentinel3:dumpInformation>\n" + " <sentinel3:granuleNumber>4</sentinel3:granuleNumber>\n" + " <sentinel3:granulePosition>NONE</sentinel3:granulePosition>\n" + " <sentinel3:dumpStart>2013-07-07T15:12:52.000000Z</sentinel3:dumpStart>\n" + " <sentinel3:receivingGroundStation>CGS</sentinel3:receivingGroundStation>\n" + " <sentinel3:receivingStartTime>2015-02-17T18:26:57.418621Z</sentinel3:receivingStartTime>\n" + " <sentinel3:receivingStopTime>2015-02-17T18:27:10.880813Z</sentinel3:receivingStopTime>\n" + " </sentinel3:dumpInformation>").getFirstChild()); fromParents.add(ManifestTestUtils.createNode( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + " <sentinel3:dumpInformation>\n" + " <sentinel3:granuleNumber>5</sentinel3:granuleNumber>\n" + " <sentinel3:granulePosition>NONE</sentinel3:granulePosition>\n" + " <sentinel3:dumpStart>2013-07-07T15:12:52.000000Z</sentinel3:dumpStart>\n" + " <sentinel3:receivingGroundStation>CGS</sentinel3:receivingGroundStation>\n" + " <sentinel3:receivingStartTime>2015-02-17T18:27:10.909138Z</sentinel3:receivingStartTime>\n" + " <sentinel3:receivingStopTime>2015-02-17T18:27:24.371332Z</sentinel3:receivingStopTime>\n" + " </sentinel3:dumpInformation>").getFirstChild()); fromParents.add(ManifestTestUtils.createNode( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + " <sentinel3:dumpInformation>\n" + " <sentinel3:granuleNumber>6</sentinel3:granuleNumber>\n" + " <sentinel3:granulePosition>NONE</sentinel3:granulePosition>\n" + " <sentinel3:dumpStart>2013-07-07T15:12:52.000000Z</sentinel3:dumpStart>\n" + " <sentinel3:receivingGroundStation>CGS</sentinel3:receivingGroundStation>\n" + " <sentinel3:receivingStartTime>2015-02-17T18:27:24.399655Z</sentinel3:receivingStartTime>\n" + " <sentinel3:receivingStopTime>2015-02-17T18:27:37.861849Z</sentinel3:receivingStopTime>\n" + " </sentinel3:dumpInformation>").getFirstChild()); Document manifest = ManifestTestUtils.createDocument(); final Element parentElement = manifest.createElement("parent"); final Element manifestElement = manifest.createElement("sentinel3:dumpInformation"); parentElement.appendChild(manifestElement); manifest.appendChild(parentElement); new DumpInformationMerger().mergeNodes(fromParents, manifestElement, manifest); final NodeList manifestChildNodes = parentElement.getChildNodes(); assertEquals(3, manifestChildNodes.getLength()); for (int i = 0; i < fromParents.size(); i++) { assertEqualsElement(fromParents.get(i), manifestChildNodes.item(i)); } }