ImageSizesMerger extends AbstractElementMerger { @Override public void mergeNodes(List<Node> fromParents, Element toParent, Document toDocument) throws PDUStitchingException { ImageSize[] imageSizes = new ImageSize[fromParents.size()]; for (int i = 0; i < imageSizes.length; i++) { imageSizes[i] = ImageSizeHandler.extractImageSizeFromNode(fromParents.get(i), ""); } final ImageSize targetImageSize = ImageSizeHandler.createTargetImageSize(imageSizes); final String grid = fromParents.get(0).getAttributes().item(0).getNodeValue(); toParent.setAttribute("grid", grid); final Element startOffsetElement = toDocument.createElement("sentinel3:startOffset"); final Text startOffsetTextNode = toDocument.createTextNode("" + targetImageSize.getStartOffset()); startOffsetElement.appendChild(startOffsetTextNode); toParent.appendChild(startOffsetElement); final Element trackOffsetElement = toDocument.createElement("sentinel3:trackOffset"); final Text trackOffsetTextNode = toDocument.createTextNode("" + targetImageSize.getTrackOffset()); trackOffsetElement.appendChild(trackOffsetTextNode); toParent.appendChild(trackOffsetElement); final Element rowsElement = toDocument.createElement("sentinel3:rows"); final Text rowsTextNode = toDocument.createTextNode("" + targetImageSize.getRows()); rowsElement.appendChild(rowsTextNode); toParent.appendChild(rowsElement); final Element columnsElement = toDocument.createElement("sentinel3:columns"); final Text columnsTextNode = toDocument.createTextNode("" + targetImageSize.getColumns()); columnsElement.appendChild(columnsTextNode); toParent.appendChild(columnsElement); }  @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" + " <slstr:nadirImageSize grid=\"Tie Points\">\n" + " <sentinel3:startOffset>21687</sentinel3:startOffset>\n" + " <sentinel3:trackOffset>64</sentinel3:trackOffset>\n" + " <sentinel3:rows>2000</sentinel3:rows>\n" + " <sentinel3:columns>130</sentinel3:columns>\n" + " </slstr:nadirImageSize>").getFirstChild()); fromParents.add(ManifestTestUtils.createNode( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + " <slstr:nadirImageSize grid=\"Tie Points\">\n" + " <sentinel3:startOffset>23687</sentinel3:startOffset>\n" + " <sentinel3:trackOffset>64</sentinel3:trackOffset>\n" + " <sentinel3:rows>2000</sentinel3:rows>\n" + " <sentinel3:columns>130</sentinel3:columns>\n" + " </slstr:nadirImageSize>").getFirstChild()); fromParents.add(ManifestTestUtils.createNode( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + " <slstr:nadirImageSize grid=\"Tie Points\">\n" + " <sentinel3:startOffset>25687</sentinel3:startOffset>\n" + " <sentinel3:trackOffset>64</sentinel3:trackOffset>\n" + " <sentinel3:rows>2000</sentinel3:rows>\n" + " <sentinel3:columns>130</sentinel3:columns>\n" + " </slstr:nadirImageSize>").getFirstChild()); Document manifest = ManifestTestUtils.createDocument(); final Element manifestElement = manifest.createElement("slstr:nadirImageSize"); manifest.appendChild(manifestElement); new ImageSizesMerger().mergeNodes(fromParents, manifestElement, manifest); assertEquals(1, manifestElement.getAttributes().getLength()); assert(manifestElement.hasAttribute("grid")); assertEquals("Tie Points", manifestElement.getAttribute("grid")); final NodeList childNodes = manifestElement.getChildNodes(); assertEquals(4, childNodes.getLength()); assertEquals("sentinel3:startOffset", childNodes.item(0).getNodeName()); assertEquals("21687", childNodes.item(0).getFirstChild().getNodeValue()); assertEquals("sentinel3:trackOffset", childNodes.item(1).getNodeName()); assertEquals("64", childNodes.item(1).getFirstChild().getNodeValue()); assertEquals("sentinel3:rows", childNodes.item(2).getNodeName()); assertEquals("6000", childNodes.item(2).getFirstChild().getNodeValue()); assertEquals("sentinel3:columns", childNodes.item(3).getNodeName()); assertEquals("130", childNodes.item(3).getFirstChild().getNodeValue()); }