@Test(expected = FileNotFoundException.class) public void testPronomFileNotFound() throws FileNotFoundException, FileFormatException { PronomParser.getPronom(PropertiesUtils.findFile(FILE_TO_TEST_IO_EXCEPTION)); }
@SuppressWarnings("unchecked") public static List<FileFormatModel> getPronom(File xmlPronomFile) throws FileFormatException { boolean bExtension = false; boolean bFileFormat = false; boolean bPriorityOverId = false; validateSchema(xmlPronomFile); List<FileFormatModel> fileFormatModels = new ArrayList<>(); final Map<String, String> idToPUID = new HashMap<>(); String creationDate = null; String pronomVersion = null; FileFormatModel fileFormatModel = new FileFormatModel(); MultiValuedMap<String, String> hasPriorityOverFileFormatIDByPuid = new ArrayListValuedHashMap<>(); String updateDate = LocalDateUtil.getFormattedDateForMongo(LocalDateUtil.now()); try (InputStream xmlPronom = new FileInputStream(xmlPronomFile)) { final XMLInputFactory xmlInputFactory = XMLInputFactoryUtils.newInstance(); final XMLEventReader eventReader = xmlInputFactory.createXMLEventReader(xmlPronom); while (eventReader.hasNext()) { final XMLEvent event = eventReader.nextEvent(); switch (event.getEventType()) { case XMLStreamConstants.START_ELEMENT: final StartElement startElement = event.asStartElement(); String qName = startElement.getName().getLocalPart(); if (qName.equalsIgnoreCase(TAG_FFSIGNATUREFILE)) { final Iterator<Attribute> attributes = startElement.getAttributes(); while (attributes.hasNext()) { final Attribute attribute = attributes.next(); switch (attribute.getName().toString()) { case ATTR_CREATEDDATE: creationDate = LocalDateUtil.getFormattedDateForMongo(attribute.getValue()); break; case ATTR_VERSION: pronomVersion = attribute.getValue(); break; } } } else if (qName.equalsIgnoreCase(TAG_FILEFORMAT)) { fileFormatModel = new FileFormatModel() .setVersionPronom(pronomVersion) .setCreatedDate(creationDate) .setUpdateDate(updateDate); bFileFormat = true; final Iterator<Attribute> attributes = startElement.getAttributes(); String id = null; while (attributes.hasNext()) { final Attribute attribute = attributes.next(); String value = attribute.getValue(); switch (attribute.getName().getLocalPart()) { case EXTERNAL_MIME_TYPE: fileFormatModel.setMimeType(value.replace(",", ";")); break; case ATTR_VERSION: fileFormatModel.setVersion(value); break; case ATTR_NAME: fileFormatModel.setName(value); break; case ATTR_PUID: fileFormatModel.setPuid(value); break; case ATTR_ID: id = value; break; } } idToPUID.put(id, fileFormatModel.getPuid()); } else if (qName.equalsIgnoreCase(TAG_EXTENSION)) { bExtension = true; } else if (qName.equalsIgnoreCase(TAG_HASPRIORITYOVERFILEFORMATID)) { bPriorityOverId = true; } break; case XMLStreamConstants.CHARACTERS: final Characters characters = event.asCharacters(); if (bExtension && bFileFormat) { fileFormatModel.getExtension().add(characters.getData()); bExtension = false; } if (bPriorityOverId && bFileFormat) { hasPriorityOverFileFormatIDByPuid.put(fileFormatModel.getPuid(), characters.getData()); bPriorityOverId = false; } break; case XMLStreamConstants.END_ELEMENT: final EndElement endElement = event.asEndElement(); qName = endElement.getName().getLocalPart(); if (qName.equalsIgnoreCase(TAG_FILEFORMAT)) { fileFormatModels.add(fileFormatModel); fileFormatModel = null; } break; } } } catch (final XMLStreamException e) { throw new InvalidFileFormatParseException("Invalid xml file format", e); } catch (IOException e) { throw new InvalidFileFormatParseException("Could not load xml file format", e); } for (FileFormatModel formatModel : fileFormatModels) { for (String id : hasPriorityOverFileFormatIDByPuid.get(formatModel.getPuid())) { formatModel.getHasPriorityOverFileFormatID().add(idToPUID.get(id)); } } return fileFormatModels; }
PronomParser { @SuppressWarnings("unchecked") public static List<FileFormatModel> getPronom(File xmlPronomFile) throws FileFormatException { boolean bExtension = false; boolean bFileFormat = false; boolean bPriorityOverId = false; validateSchema(xmlPronomFile); List<FileFormatModel> fileFormatModels = new ArrayList<>(); final Map<String, String> idToPUID = new HashMap<>(); String creationDate = null; String pronomVersion = null; FileFormatModel fileFormatModel = new FileFormatModel(); MultiValuedMap<String, String> hasPriorityOverFileFormatIDByPuid = new ArrayListValuedHashMap<>(); String updateDate = LocalDateUtil.getFormattedDateForMongo(LocalDateUtil.now()); try (InputStream xmlPronom = new FileInputStream(xmlPronomFile)) { final XMLInputFactory xmlInputFactory = XMLInputFactoryUtils.newInstance(); final XMLEventReader eventReader = xmlInputFactory.createXMLEventReader(xmlPronom); while (eventReader.hasNext()) { final XMLEvent event = eventReader.nextEvent(); switch (event.getEventType()) { case XMLStreamConstants.START_ELEMENT: final StartElement startElement = event.asStartElement(); String qName = startElement.getName().getLocalPart(); if (qName.equalsIgnoreCase(TAG_FFSIGNATUREFILE)) { final Iterator<Attribute> attributes = startElement.getAttributes(); while (attributes.hasNext()) { final Attribute attribute = attributes.next(); switch (attribute.getName().toString()) { case ATTR_CREATEDDATE: creationDate = LocalDateUtil.getFormattedDateForMongo(attribute.getValue()); break; case ATTR_VERSION: pronomVersion = attribute.getValue(); break; } } } else if (qName.equalsIgnoreCase(TAG_FILEFORMAT)) { fileFormatModel = new FileFormatModel() .setVersionPronom(pronomVersion) .setCreatedDate(creationDate) .setUpdateDate(updateDate); bFileFormat = true; final Iterator<Attribute> attributes = startElement.getAttributes(); String id = null; while (attributes.hasNext()) { final Attribute attribute = attributes.next(); String value = attribute.getValue(); switch (attribute.getName().getLocalPart()) { case EXTERNAL_MIME_TYPE: fileFormatModel.setMimeType(value.replace(",", ";")); break; case ATTR_VERSION: fileFormatModel.setVersion(value); break; case ATTR_NAME: fileFormatModel.setName(value); break; case ATTR_PUID: fileFormatModel.setPuid(value); break; case ATTR_ID: id = value; break; } } idToPUID.put(id, fileFormatModel.getPuid()); } else if (qName.equalsIgnoreCase(TAG_EXTENSION)) { bExtension = true; } else if (qName.equalsIgnoreCase(TAG_HASPRIORITYOVERFILEFORMATID)) { bPriorityOverId = true; } break; case XMLStreamConstants.CHARACTERS: final Characters characters = event.asCharacters(); if (bExtension && bFileFormat) { fileFormatModel.getExtension().add(characters.getData()); bExtension = false; } if (bPriorityOverId && bFileFormat) { hasPriorityOverFileFormatIDByPuid.put(fileFormatModel.getPuid(), characters.getData()); bPriorityOverId = false; } break; case XMLStreamConstants.END_ELEMENT: final EndElement endElement = event.asEndElement(); qName = endElement.getName().getLocalPart(); if (qName.equalsIgnoreCase(TAG_FILEFORMAT)) { fileFormatModels.add(fileFormatModel); fileFormatModel = null; } break; } } } catch (final XMLStreamException e) { throw new InvalidFileFormatParseException("Invalid xml file format", e); } catch (IOException e) { throw new InvalidFileFormatParseException("Could not load xml file format", e); } for (FileFormatModel formatModel : fileFormatModels) { for (String id : hasPriorityOverFileFormatIDByPuid.get(formatModel.getPuid())) { formatModel.getHasPriorityOverFileFormatID().add(idToPUID.get(id)); } } return fileFormatModels; } }
PronomParser { @SuppressWarnings("unchecked") public static List<FileFormatModel> getPronom(File xmlPronomFile) throws FileFormatException { boolean bExtension = false; boolean bFileFormat = false; boolean bPriorityOverId = false; validateSchema(xmlPronomFile); List<FileFormatModel> fileFormatModels = new ArrayList<>(); final Map<String, String> idToPUID = new HashMap<>(); String creationDate = null; String pronomVersion = null; FileFormatModel fileFormatModel = new FileFormatModel(); MultiValuedMap<String, String> hasPriorityOverFileFormatIDByPuid = new ArrayListValuedHashMap<>(); String updateDate = LocalDateUtil.getFormattedDateForMongo(LocalDateUtil.now()); try (InputStream xmlPronom = new FileInputStream(xmlPronomFile)) { final XMLInputFactory xmlInputFactory = XMLInputFactoryUtils.newInstance(); final XMLEventReader eventReader = xmlInputFactory.createXMLEventReader(xmlPronom); while (eventReader.hasNext()) { final XMLEvent event = eventReader.nextEvent(); switch (event.getEventType()) { case XMLStreamConstants.START_ELEMENT: final StartElement startElement = event.asStartElement(); String qName = startElement.getName().getLocalPart(); if (qName.equalsIgnoreCase(TAG_FFSIGNATUREFILE)) { final Iterator<Attribute> attributes = startElement.getAttributes(); while (attributes.hasNext()) { final Attribute attribute = attributes.next(); switch (attribute.getName().toString()) { case ATTR_CREATEDDATE: creationDate = LocalDateUtil.getFormattedDateForMongo(attribute.getValue()); break; case ATTR_VERSION: pronomVersion = attribute.getValue(); break; } } } else if (qName.equalsIgnoreCase(TAG_FILEFORMAT)) { fileFormatModel = new FileFormatModel() .setVersionPronom(pronomVersion) .setCreatedDate(creationDate) .setUpdateDate(updateDate); bFileFormat = true; final Iterator<Attribute> attributes = startElement.getAttributes(); String id = null; while (attributes.hasNext()) { final Attribute attribute = attributes.next(); String value = attribute.getValue(); switch (attribute.getName().getLocalPart()) { case EXTERNAL_MIME_TYPE: fileFormatModel.setMimeType(value.replace(",", ";")); break; case ATTR_VERSION: fileFormatModel.setVersion(value); break; case ATTR_NAME: fileFormatModel.setName(value); break; case ATTR_PUID: fileFormatModel.setPuid(value); break; case ATTR_ID: id = value; break; } } idToPUID.put(id, fileFormatModel.getPuid()); } else if (qName.equalsIgnoreCase(TAG_EXTENSION)) { bExtension = true; } else if (qName.equalsIgnoreCase(TAG_HASPRIORITYOVERFILEFORMATID)) { bPriorityOverId = true; } break; case XMLStreamConstants.CHARACTERS: final Characters characters = event.asCharacters(); if (bExtension && bFileFormat) { fileFormatModel.getExtension().add(characters.getData()); bExtension = false; } if (bPriorityOverId && bFileFormat) { hasPriorityOverFileFormatIDByPuid.put(fileFormatModel.getPuid(), characters.getData()); bPriorityOverId = false; } break; case XMLStreamConstants.END_ELEMENT: final EndElement endElement = event.asEndElement(); qName = endElement.getName().getLocalPart(); if (qName.equalsIgnoreCase(TAG_FILEFORMAT)) { fileFormatModels.add(fileFormatModel); fileFormatModel = null; } break; } } } catch (final XMLStreamException e) { throw new InvalidFileFormatParseException("Invalid xml file format", e); } catch (IOException e) { throw new InvalidFileFormatParseException("Could not load xml file format", e); } for (FileFormatModel formatModel : fileFormatModels) { for (String id : hasPriorityOverFileFormatIDByPuid.get(formatModel.getPuid())) { formatModel.getHasPriorityOverFileFormatID().add(idToPUID.get(id)); } } return fileFormatModels; } private  PronomParser(); }
PronomParser { @SuppressWarnings("unchecked") public static List<FileFormatModel> getPronom(File xmlPronomFile) throws FileFormatException { boolean bExtension = false; boolean bFileFormat = false; boolean bPriorityOverId = false; validateSchema(xmlPronomFile); List<FileFormatModel> fileFormatModels = new ArrayList<>(); final Map<String, String> idToPUID = new HashMap<>(); String creationDate = null; String pronomVersion = null; FileFormatModel fileFormatModel = new FileFormatModel(); MultiValuedMap<String, String> hasPriorityOverFileFormatIDByPuid = new ArrayListValuedHashMap<>(); String updateDate = LocalDateUtil.getFormattedDateForMongo(LocalDateUtil.now()); try (InputStream xmlPronom = new FileInputStream(xmlPronomFile)) { final XMLInputFactory xmlInputFactory = XMLInputFactoryUtils.newInstance(); final XMLEventReader eventReader = xmlInputFactory.createXMLEventReader(xmlPronom); while (eventReader.hasNext()) { final XMLEvent event = eventReader.nextEvent(); switch (event.getEventType()) { case XMLStreamConstants.START_ELEMENT: final StartElement startElement = event.asStartElement(); String qName = startElement.getName().getLocalPart(); if (qName.equalsIgnoreCase(TAG_FFSIGNATUREFILE)) { final Iterator<Attribute> attributes = startElement.getAttributes(); while (attributes.hasNext()) { final Attribute attribute = attributes.next(); switch (attribute.getName().toString()) { case ATTR_CREATEDDATE: creationDate = LocalDateUtil.getFormattedDateForMongo(attribute.getValue()); break; case ATTR_VERSION: pronomVersion = attribute.getValue(); break; } } } else if (qName.equalsIgnoreCase(TAG_FILEFORMAT)) { fileFormatModel = new FileFormatModel() .setVersionPronom(pronomVersion) .setCreatedDate(creationDate) .setUpdateDate(updateDate); bFileFormat = true; final Iterator<Attribute> attributes = startElement.getAttributes(); String id = null; while (attributes.hasNext()) { final Attribute attribute = attributes.next(); String value = attribute.getValue(); switch (attribute.getName().getLocalPart()) { case EXTERNAL_MIME_TYPE: fileFormatModel.setMimeType(value.replace(",", ";")); break; case ATTR_VERSION: fileFormatModel.setVersion(value); break; case ATTR_NAME: fileFormatModel.setName(value); break; case ATTR_PUID: fileFormatModel.setPuid(value); break; case ATTR_ID: id = value; break; } } idToPUID.put(id, fileFormatModel.getPuid()); } else if (qName.equalsIgnoreCase(TAG_EXTENSION)) { bExtension = true; } else if (qName.equalsIgnoreCase(TAG_HASPRIORITYOVERFILEFORMATID)) { bPriorityOverId = true; } break; case XMLStreamConstants.CHARACTERS: final Characters characters = event.asCharacters(); if (bExtension && bFileFormat) { fileFormatModel.getExtension().add(characters.getData()); bExtension = false; } if (bPriorityOverId && bFileFormat) { hasPriorityOverFileFormatIDByPuid.put(fileFormatModel.getPuid(), characters.getData()); bPriorityOverId = false; } break; case XMLStreamConstants.END_ELEMENT: final EndElement endElement = event.asEndElement(); qName = endElement.getName().getLocalPart(); if (qName.equalsIgnoreCase(TAG_FILEFORMAT)) { fileFormatModels.add(fileFormatModel); fileFormatModel = null; } break; } } } catch (final XMLStreamException e) { throw new InvalidFileFormatParseException("Invalid xml file format", e); } catch (IOException e) { throw new InvalidFileFormatParseException("Could not load xml file format", e); } for (FileFormatModel formatModel : fileFormatModels) { for (String id : hasPriorityOverFileFormatIDByPuid.get(formatModel.getPuid())) { formatModel.getHasPriorityOverFileFormatID().add(idToPUID.get(id)); } } return fileFormatModels; } private  PronomParser(); @SuppressWarnings("unchecked") static List<FileFormatModel> getPronom(File xmlPronomFile); }
PronomParser { @SuppressWarnings("unchecked") public static List<FileFormatModel> getPronom(File xmlPronomFile) throws FileFormatException { boolean bExtension = false; boolean bFileFormat = false; boolean bPriorityOverId = false; validateSchema(xmlPronomFile); List<FileFormatModel> fileFormatModels = new ArrayList<>(); final Map<String, String> idToPUID = new HashMap<>(); String creationDate = null; String pronomVersion = null; FileFormatModel fileFormatModel = new FileFormatModel(); MultiValuedMap<String, String> hasPriorityOverFileFormatIDByPuid = new ArrayListValuedHashMap<>(); String updateDate = LocalDateUtil.getFormattedDateForMongo(LocalDateUtil.now()); try (InputStream xmlPronom = new FileInputStream(xmlPronomFile)) { final XMLInputFactory xmlInputFactory = XMLInputFactoryUtils.newInstance(); final XMLEventReader eventReader = xmlInputFactory.createXMLEventReader(xmlPronom); while (eventReader.hasNext()) { final XMLEvent event = eventReader.nextEvent(); switch (event.getEventType()) { case XMLStreamConstants.START_ELEMENT: final StartElement startElement = event.asStartElement(); String qName = startElement.getName().getLocalPart(); if (qName.equalsIgnoreCase(TAG_FFSIGNATUREFILE)) { final Iterator<Attribute> attributes = startElement.getAttributes(); while (attributes.hasNext()) { final Attribute attribute = attributes.next(); switch (attribute.getName().toString()) { case ATTR_CREATEDDATE: creationDate = LocalDateUtil.getFormattedDateForMongo(attribute.getValue()); break; case ATTR_VERSION: pronomVersion = attribute.getValue(); break; } } } else if (qName.equalsIgnoreCase(TAG_FILEFORMAT)) { fileFormatModel = new FileFormatModel() .setVersionPronom(pronomVersion) .setCreatedDate(creationDate) .setUpdateDate(updateDate); bFileFormat = true; final Iterator<Attribute> attributes = startElement.getAttributes(); String id = null; while (attributes.hasNext()) { final Attribute attribute = attributes.next(); String value = attribute.getValue(); switch (attribute.getName().getLocalPart()) { case EXTERNAL_MIME_TYPE: fileFormatModel.setMimeType(value.replace(",", ";")); break; case ATTR_VERSION: fileFormatModel.setVersion(value); break; case ATTR_NAME: fileFormatModel.setName(value); break; case ATTR_PUID: fileFormatModel.setPuid(value); break; case ATTR_ID: id = value; break; } } idToPUID.put(id, fileFormatModel.getPuid()); } else if (qName.equalsIgnoreCase(TAG_EXTENSION)) { bExtension = true; } else if (qName.equalsIgnoreCase(TAG_HASPRIORITYOVERFILEFORMATID)) { bPriorityOverId = true; } break; case XMLStreamConstants.CHARACTERS: final Characters characters = event.asCharacters(); if (bExtension && bFileFormat) { fileFormatModel.getExtension().add(characters.getData()); bExtension = false; } if (bPriorityOverId && bFileFormat) { hasPriorityOverFileFormatIDByPuid.put(fileFormatModel.getPuid(), characters.getData()); bPriorityOverId = false; } break; case XMLStreamConstants.END_ELEMENT: final EndElement endElement = event.asEndElement(); qName = endElement.getName().getLocalPart(); if (qName.equalsIgnoreCase(TAG_FILEFORMAT)) { fileFormatModels.add(fileFormatModel); fileFormatModel = null; } break; } } } catch (final XMLStreamException e) { throw new InvalidFileFormatParseException("Invalid xml file format", e); } catch (IOException e) { throw new InvalidFileFormatParseException("Could not load xml file format", e); } for (FileFormatModel formatModel : fileFormatModels) { for (String id : hasPriorityOverFileFormatIDByPuid.get(formatModel.getPuid())) { formatModel.getHasPriorityOverFileFormatID().add(idToPUID.get(id)); } } return fileFormatModels; } private  PronomParser(); @SuppressWarnings("unchecked") static List<FileFormatModel> getPronom(File xmlPronomFile);  }