Search in sources :

Example 1 with NamePartDefinition

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

the class ModsExportFormat method handleAuthors.

private void handleAuthors(ModsDefinition mods, String value) {
    String[] authors = value.split("and");
    for (String author : authors) {
        NameDefinition name = new NameDefinition();
        name.setAtType("personal");
        NamePartDefinition namePart = new NamePartDefinition();
        if (author.contains(",")) {
            //if author contains ","  then this indicates that the author has a forename and family name
            int commaIndex = author.indexOf(',');
            String familyName = author.substring(0, commaIndex);
            namePart.setAtType("family");
            namePart.setValue(familyName);
            JAXBElement<NamePartDefinition> element = new JAXBElement<>(new QName(MODS_NAMESPACE_URI, "namePart"), NamePartDefinition.class, namePart);
            name.getNamePartOrDisplayFormOrAffiliation().add(element);
            //now take care of the forenames
            String forename = author.substring(commaIndex + 1, author.length());
            String[] forenames = forename.split(" ");
            for (String given : forenames) {
                if (!given.isEmpty()) {
                    NamePartDefinition namePartDefinition = new NamePartDefinition();
                    namePartDefinition.setAtType("given");
                    namePartDefinition.setValue(given);
                    element = new JAXBElement<>(new QName(MODS_NAMESPACE_URI, "namePart"), NamePartDefinition.class, namePartDefinition);
                    name.getNamePartOrDisplayFormOrAffiliation().add(element);
                }
            }
            mods.getModsGroup().add(name);
        } else {
            //no "," indicates that there should only be a family name
            namePart.setAtType("family");
            namePart.setValue(author);
            JAXBElement<NamePartDefinition> element = new JAXBElement<>(new QName(MODS_NAMESPACE_URI, "namePart"), NamePartDefinition.class, namePart);
            name.getNamePartOrDisplayFormOrAffiliation().add(element);
            mods.getModsGroup().add(name);
        }
    }
}
Also used : QName(javax.xml.namespace.QName) NamePartDefinition(org.jabref.logic.importer.fileformat.mods.NamePartDefinition) JAXBElement(javax.xml.bind.JAXBElement) NameDefinition(org.jabref.logic.importer.fileformat.mods.NameDefinition)

Example 2 with NamePartDefinition

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

the class ModsImporter method handleAuthorsInNamePart.

private void handleAuthorsInNamePart(NameDefinition name, List<String> authors, Map<String, String> fields) {
    List<JAXBElement<?>> namePartOrDisplayFormOrAffiliation = name.getNamePartOrDisplayFormOrAffiliation();
    List<String> foreName = new ArrayList<>();
    String familyName = "";
    String author = "";
    for (JAXBElement<?> element : namePartOrDisplayFormOrAffiliation) {
        Object value = element.getValue();
        String elementName = element.getName().getLocalPart();
        if (value instanceof NamePartDefinition) {
            NamePartDefinition namePart = (NamePartDefinition) value;
            String type = namePart.getAtType();
            if ((type == null) && (namePart.getValue() != null)) {
                authors.add(namePart.getValue());
            } else if ("family".equals(type) && (namePart.getValue() != null)) {
                //we have to check if forename and family name are not empty in case it's the first author
                if (!foreName.isEmpty() && !familyName.isEmpty()) {
                    //now set and add the old author
                    author = familyName + ", " + Joiner.on(" ").join(foreName);
                    authors.add(author);
                    //remove old forenames
                    foreName.clear();
                } else if (foreName.isEmpty() && !familyName.isEmpty()) {
                    authors.add(familyName);
                }
                familyName = namePart.getValue();
            } else if ("given".equals(type) && (namePart.getValue() != null)) {
                foreName.add(namePart.getValue());
            }
        } else if ((value instanceof StringPlusLanguage) && "affiliation".equals(elementName)) {
            StringPlusLanguage affiliation = (StringPlusLanguage) value;
            putIfValueNotNull(fields, "affiliation", affiliation.getValue());
        }
    }
    //last author is not added, so do it here
    if (!foreName.isEmpty() && !familyName.isEmpty()) {
        author = familyName + ", " + Joiner.on(" ").join(foreName);
        authors.add(author.trim());
        foreName.clear();
    } else if (foreName.isEmpty() && !familyName.isEmpty()) {
        authors.add(familyName.trim());
    }
}
Also used : StringPlusLanguage(org.jabref.logic.importer.fileformat.mods.StringPlusLanguage) ArrayList(java.util.ArrayList) NamePartDefinition(org.jabref.logic.importer.fileformat.mods.NamePartDefinition) JAXBElement(javax.xml.bind.JAXBElement)

Aggregations

JAXBElement (javax.xml.bind.JAXBElement)2 NamePartDefinition (org.jabref.logic.importer.fileformat.mods.NamePartDefinition)2 ArrayList (java.util.ArrayList)1 QName (javax.xml.namespace.QName)1 NameDefinition (org.jabref.logic.importer.fileformat.mods.NameDefinition)1 StringPlusLanguage (org.jabref.logic.importer.fileformat.mods.StringPlusLanguage)1