ContainerSignatureSaxParser { public ContainerSignatureDefinitions parse(InputStream in) throws SignatureParseException { try { Unmarshaller unmarshaller = context.createUnmarshaller(); ContainerSignatureDefinitions definitions = (ContainerSignatureDefinitions) unmarshaller.unmarshal(in); compileAllBinarySignatures(definitions); return definitions; } catch (JAXBException e) { throw new SignatureParseException(e.getMessage(), e); } } ContainerSignatureSaxParser(); ContainerSignatureDefinitions parse(InputStream in);  }
@Test public void testParseSignatures() throws Exception { String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<ContainerSignatureMapping>" + " <ContainerSignatures>" + " <ContainerSignature Id=\"9\">" + " <Description>Microsoft Word 97</Description>" + " <Files>" + " <File Required=\"true\">" + " <Path>WordDocument</Path>" + " </File>" + " </Files>" + " </ContainerSignature>" + " <ContainerSignature Id=\"10\">" + " <Description>Microsoft Excel 97</Description>" + " <Files>" + " <File Required=\"false\">" + " <Path>Workbook</Path>" + " </File>" + " </Files>" + " </ContainerSignature>" + " </ContainerSignatures>" + "</ContainerSignatureMapping>"; ContainerSignatureSaxParser parser = new ContainerSignatureSaxParser(); InputStream in = new ByteArrayInputStream(xml.getBytes("UTF-8")); List<ContainerSignature> signatures = parser.parse(in).getContainerSignatures(); assertEquals(2, signatures.size()); ContainerSignature wordSignature = signatures.get(0); assertEquals("Microsoft Word 97", wordSignature.getDescription()); assertEquals(9, wordSignature.getId()); assertEquals(1, wordSignature.getFiles().size()); assertEquals("WordDocument", wordSignature.listFiles().get(0).getPath()); ContainerSignature excelSignature = signatures.get(1); assertEquals("Microsoft Excel 97", excelSignature.getDescription()); assertEquals(10, excelSignature.getId()); assertEquals(1, excelSignature.getFiles().size()); assertEquals("Workbook", excelSignature.listFiles().get(0).getPath()); }
@Test public void folderBasedContainerSignaturesShouldKeepThePathsAsPresentedInSignatureFileForContainerFileMap() throws JAXBException, UnsupportedEncodingException, SignatureParseException { String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<ContainerSignatureMapping>" + " <ContainerSignatures>" + " <ContainerSignature ContainerType=\"ZIP\" Id=\"31020\">" + " <Description>SIARD 2.1</Description>" + " <Files>" + " <File>" + " <Path>header/siardversion/2.1/</Path>" + " </File>" + " </Files>" + " </ContainerSignature>" + " <ContainerSignature ContainerType=\"ZIP\" Id=\"31010\">" + " <Description>SIARD 2.0</Description>" + " <Files>" + " <File>" + " <Path>header/metadata.xml</Path>" + " <BinarySignatures>" + " <InternalSignatureCollection>" + " <InternalSignature ID=\"31010\">" + " <ByteSequence Reference=\"BOFoffset\">" + " <SubSequence Position=\"1\" SubSeqMaxOffset=\"256\"\n SubSeqMinOffset=\"50\">" + " <Sequence>'xmlns=\"http: + " </SubSequence>" + " </ByteSequence>" + " </InternalSignature>" + " </InternalSignatureCollection>" + " </BinarySignatures>" + " </File>" + " </Files>" + " </ContainerSignature>" + " </ContainerSignatures>" + "</ContainerSignatureMapping>"; ContainerSignatureSaxParser parser = new ContainerSignatureSaxParser(); InputStream in = new ByteArrayInputStream(xml.getBytes("UTF-8")); List<ContainerSignature> signatures = parser.parse(in).getContainerSignatures(); Optional<ContainerSignature> siard2_0 = signatures.stream().filter(sig -> sig.getId() == 31010).findFirst(); assertTrue("siard20 signature not found", siard2_0.isPresent()); Map<String, ContainerFile> fileMap = siard2_0.get().getFiles(); assertTrue("expected an entry for 2_0 in file map but found none", fileMap.containsKey("header/metadata.xml")); Optional<ContainerSignature> siard2_1 = signatures.stream().filter(sig -> sig.getId() == 31020).findFirst(); assertTrue("siard20 signature not found", siard2_1.isPresent()); fileMap = siard2_1.get().getFiles(); assertTrue("expected an entry for 2_1 in file map but found none", fileMap.containsKey("header/siardversion/2.1/")); }