Search in sources :

Example 1 with ModsCollectionDefinition

use of org.jabref.logic.importer.fileformat.mods.ModsCollectionDefinition in project jabref by JabRef.

the class ModsExportFormat method createMarshallerAndWriteToFile.

private void createMarshallerAndWriteToFile(String file, JAXBElement<ModsCollectionDefinition> jaxbElement) throws JAXBException {
    if (context == null) {
        context = JAXBContext.newInstance(ModsCollectionDefinition.class);
    }
    Marshaller marshaller = context.createMarshaller();
    //format the output
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, MODS_SCHEMA_LOCATION);
    // Write to File
    marshaller.marshal(jaxbElement, new File(file));
}
Also used : Marshaller(javax.xml.bind.Marshaller) ModsCollectionDefinition(org.jabref.logic.importer.fileformat.mods.ModsCollectionDefinition) File(java.io.File)

Example 2 with ModsCollectionDefinition

use of org.jabref.logic.importer.fileformat.mods.ModsCollectionDefinition in project jabref by JabRef.

the class ModsExportFormat method performExport.

@Override
public void performExport(final BibDatabaseContext databaseContext, final String file, final Charset encoding, List<BibEntry> entries) throws SaveException, IOException {
    Objects.requireNonNull(databaseContext);
    Objects.requireNonNull(entries);
    if (entries.isEmpty()) {
        // Only export if entries exist
        return;
    }
    try {
        ModsCollectionDefinition modsCollection = new ModsCollectionDefinition();
        for (BibEntry bibEntry : entries) {
            ModsDefinition mods = new ModsDefinition();
            bibEntry.getCiteKeyOptional().ifPresent(citeKey -> addIdentifier("citekey", citeKey, mods));
            Map<String, String> fieldMap = bibEntry.getFieldMap();
            addGenre(bibEntry, mods);
            OriginInfoDefinition originInfo = new OriginInfoDefinition();
            PartDefinition partDefinition = new PartDefinition();
            RelatedItemDefinition relatedItem = new RelatedItemDefinition();
            for (Map.Entry<String, String> entry : fieldMap.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                switch(key) {
                    case FieldName.AUTHOR:
                        handleAuthors(mods, value);
                        break;
                    case "affiliation":
                        addAffiliation(mods, value);
                        break;
                    case FieldName.ABSTRACT:
                        addAbstract(mods, value);
                        break;
                    case FieldName.TITLE:
                        addTitle(mods, value);
                        break;
                    case FieldName.LANGUAGE:
                        addLanguage(mods, value);
                        break;
                    case FieldName.LOCATION:
                        addLocation(mods, value);
                        break;
                    case FieldName.URL:
                        addUrl(mods, value);
                        break;
                    case FieldName.NOTE:
                        addNote(mods, value);
                        break;
                    case FieldName.KEYWORDS:
                        addKeyWords(mods, value);
                        break;
                    case FieldName.VOLUME:
                        addDetail(FieldName.VOLUME, value, partDefinition);
                        break;
                    case FieldName.ISSUE:
                        addDetail(FieldName.ISSUE, value, partDefinition);
                        break;
                    case FieldName.PAGES:
                        addPages(partDefinition, value);
                        break;
                    case FieldName.URI:
                        addIdentifier(FieldName.URI, value, mods);
                        break;
                    case FieldName.ISBN:
                        addIdentifier(FieldName.ISBN, value, mods);
                        break;
                    case FieldName.ISSN:
                        addIdentifier(FieldName.ISSN, value, mods);
                        break;
                    case FieldName.DOI:
                        addIdentifier(FieldName.DOI, value, mods);
                        break;
                    case FieldName.PMID:
                        addIdentifier(FieldName.PMID, value, mods);
                        break;
                    case FieldName.JOURNAL:
                        addJournal(value, relatedItem);
                        break;
                    default:
                        break;
                }
                addOriginInformation(key, value, originInfo);
            }
            mods.getModsGroup().add(originInfo);
            addRelatedAndOriginInfoToModsGroup(relatedItem, partDefinition, mods);
            modsCollection.getMods().add(mods);
        }
        JAXBElement<ModsCollectionDefinition> jaxbElement = new JAXBElement<>(new QName(MODS_NAMESPACE_URI, "modsCollection"), ModsCollectionDefinition.class, modsCollection);
        createMarshallerAndWriteToFile(file, jaxbElement);
    } catch (JAXBException ex) {
        throw new SaveException(ex);
    }
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) ModsCollectionDefinition(org.jabref.logic.importer.fileformat.mods.ModsCollectionDefinition) ModsDefinition(org.jabref.logic.importer.fileformat.mods.ModsDefinition) QName(javax.xml.namespace.QName) JAXBException(javax.xml.bind.JAXBException) PartDefinition(org.jabref.logic.importer.fileformat.mods.PartDefinition) NamePartDefinition(org.jabref.logic.importer.fileformat.mods.NamePartDefinition) JAXBElement(javax.xml.bind.JAXBElement) OriginInfoDefinition(org.jabref.logic.importer.fileformat.mods.OriginInfoDefinition) RelatedItemDefinition(org.jabref.logic.importer.fileformat.mods.RelatedItemDefinition) Map(java.util.Map)

Example 3 with ModsCollectionDefinition

use of org.jabref.logic.importer.fileformat.mods.ModsCollectionDefinition in project jabref by JabRef.

the class ModsImporter method importDatabase.

@Override
public ParserResult importDatabase(BufferedReader input) throws IOException {
    Objects.requireNonNull(input);
    List<BibEntry> bibItems = new ArrayList<>();
    try {
        if (context == null) {
            context = JAXBContext.newInstance("org.jabref.logic.importer.fileformat.mods");
        }
        Unmarshaller unmarshaller = context.createUnmarshaller();
        //The unmarshalled object is a jaxbElement.
        JAXBElement<?> unmarshalledObject = (JAXBElement<?>) unmarshaller.unmarshal(input);
        Optional<ModsCollectionDefinition> collection = getElement(unmarshalledObject.getValue(), ModsCollectionDefinition.class);
        Optional<ModsDefinition> mods = getElement(unmarshalledObject.getValue(), ModsDefinition.class);
        if (collection.isPresent()) {
            List<ModsDefinition> modsDefinitions = collection.get().getMods();
            parseModsCollection(bibItems, modsDefinitions);
        } else if (mods.isPresent()) {
            ModsDefinition modsDefinition = mods.get();
            parseMods(bibItems, modsDefinition);
        } else {
            LOGGER.warn("Not expected root element found");
        }
    } catch (JAXBException e) {
        LOGGER.debug("could not parse document", e);
        return ParserResult.fromError(e);
    }
    return new ParserResult(bibItems);
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) ModsCollectionDefinition(org.jabref.logic.importer.fileformat.mods.ModsCollectionDefinition) ModsDefinition(org.jabref.logic.importer.fileformat.mods.ModsDefinition) JAXBException(javax.xml.bind.JAXBException) ArrayList(java.util.ArrayList) JAXBElement(javax.xml.bind.JAXBElement) ParserResult(org.jabref.logic.importer.ParserResult) Unmarshaller(javax.xml.bind.Unmarshaller)

Aggregations

ModsCollectionDefinition (org.jabref.logic.importer.fileformat.mods.ModsCollectionDefinition)3 JAXBElement (javax.xml.bind.JAXBElement)2 JAXBException (javax.xml.bind.JAXBException)2 ModsDefinition (org.jabref.logic.importer.fileformat.mods.ModsDefinition)2 BibEntry (org.jabref.model.entry.BibEntry)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Marshaller (javax.xml.bind.Marshaller)1 Unmarshaller (javax.xml.bind.Unmarshaller)1 QName (javax.xml.namespace.QName)1 ParserResult (org.jabref.logic.importer.ParserResult)1 NamePartDefinition (org.jabref.logic.importer.fileformat.mods.NamePartDefinition)1 OriginInfoDefinition (org.jabref.logic.importer.fileformat.mods.OriginInfoDefinition)1 PartDefinition (org.jabref.logic.importer.fileformat.mods.PartDefinition)1 RelatedItemDefinition (org.jabref.logic.importer.fileformat.mods.RelatedItemDefinition)1