Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
- We fixed nested LaTeX formatting, spacing, and ampersand escaping in HTML output and JStyle bibliographies. [#17921](https://github.com/JabRef/jabref/pull/17921)
- We fixed an issue where RIS import stored a book's ISBN in the ISSN field. [#17889](https://github.com/JabRef/jabref/issues/17889)
- We fixed an issue with wrong text shown for preferences menu item and the preferences dialog shortcut opening directly LibreOffice panel in preferences. [#17966](https://github.com/JabRef/jabref/issues/17966)
- We fixed MODS contributor name and role handling, including names containing “and”, organizations, suffixes, and patent assignees. [#17898](https://github.com/JabRef/jabref/issues/17898)

### Removed

Expand Down
1 change: 1 addition & 0 deletions _typos.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ bigrading = "bigrading"
nd = "nd"
edn = "edn"
thes = "thes"
ths = "ths"
rin = "rin"
ein = "ein"
ot = "ot"
Expand Down
17 changes: 17 additions & 0 deletions docs/requirements/export.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,22 @@ Local attachments that cannot be resolved or represented relatively are omitted.

Needs: impl, utest

## MODS exports must preserve supported contributor names
`req~export.mods.names~1`

MODS exports preserve supported contributor names without incorrectly splitting names containing "and".
Brace-protected organizations are exported as corporate names, personal suffixes as `termsOfAddress`, and the BibTeX `and others` placeholder is omitted.
Exports preserve initial punctuation and attached prefixes. Explicit initials supply the given name when it is unavailable, and an extended name with no standard parts is exported as literal name text.
See the [MODS name guidelines](https://www.loc.gov/standards/mods/userguide/name.html) and the [MODS 3.6 to MARC 21 mapping](https://www.loc.gov/standards/mods/v3/mods2marc-mapping-3-6.html).

Each supported contributor field is exported with its corresponding [MARC relator code](https://www.loc.gov/marc/relators/relaterm.html).
Re-importing the export restores each supported contributor role to its corresponding field.
Composer (`cmp`), illustrator (`ill`), and thesis advisor (`ths`) use custom fields. Generic contributors (`ctb`) and names with only unmapped roles use the `contributor` field.
Patent assignees and patent holders share the `pth` relator and re-import as patent holders.

See [issue 17898](https://github.com/JabRef/jabref/issues/17898).

Needs: impl, utest

<!-- markdownlint-disable-file MD013 MD033 MD041 -->
<!-- markdownlint-disable-file MD022 -->
117 changes: 83 additions & 34 deletions jablib/src/main/java/org/jabref/logic/exporter/ModsExporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Pattern;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
Expand All @@ -24,12 +25,16 @@
import javax.xml.transform.stream.StreamSource;

import org.jabref.logic.util.StandardFileType;
import org.jabref.logic.util.strings.StringUtil;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.Author;
import org.jabref.model.entry.AuthorList;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.field.UnknownField;
import org.jabref.model.entry.types.EntryType;
import org.jabref.model.entry.types.IEEETranEntryType;

import org.jspecify.annotations.NonNull;
import org.slf4j.Logger;
Expand All @@ -42,6 +47,25 @@ class ModsExporter extends Exporter {
private static final String MINUS = "-";
private static final String DOUBLE_MINUS = "--";
private static final String MODS_SCHEMA_LOCATION = "http://www.loc.gov/standards/mods/v3/mods-3-6.xsd";
private static final Pattern REPEATED_HORIZONTAL_WHITESPACE = Pattern.compile("\\h{2,}");
private static final Pattern WHITESPACE_PATTERN = Pattern.compile("\\s+");
// https://www.loc.gov/marc/relators/relaterm.html
private static final Map<Field, String> PERSON_NAME_ROLES = Map.ofEntries(
Map.entry(StandardField.AFTERWORD, "waw"),
Map.entry(StandardField.ANNOTATOR, "ann"),
Map.entry(StandardField.ASSIGNEE, "asg"),
Map.entry(StandardField.AUTHOR, "aut"),
Map.entry(StandardField.COMMENTATOR, "cwt"),
Map.entry(new UnknownField("composer"), "cmp"),
Map.entry(new UnknownField("contributor"), "ctb"),
Map.entry(StandardField.EDITOR, "edt"),
Map.entry(StandardField.FOREWORD, "wfw"),
Map.entry(StandardField.HOLDER, "pth"),
Map.entry(new UnknownField("illustrator"), "ill"),
Map.entry(StandardField.INTRODUCTION, "win"),
Map.entry(new UnknownField("thesisadvisor"), "ths"),
Map.entry(StandardField.TRANSLATOR, "trl")
);

private static final Logger LOGGER = LoggerFactory.getLogger(ModsExporter.class);

Expand Down Expand Up @@ -82,8 +106,12 @@ public void export(@NonNull final BibDatabaseContext databaseContext,
Field field = entry.getKey();
String value = entry.getValue();

if (StandardField.AUTHOR == field) {
handleAuthors(writer, value);
if (PERSON_NAME_ROLES.containsKey(field)) {
// MARC asg denotes a printing or publishing license assignee.
String role = field == StandardField.ASSIGNEE && bibEntry.getType() == IEEETranEntryType.Patent
? "pth"
: PERSON_NAME_ROLES.get(field);
handleNames(writer, value, role);
} else if (new UnknownField("affiliation").equals(field)) {
addAffiliation(writer, value);
} else if (StandardField.ABSTRACT == field) {
Expand Down Expand Up @@ -359,44 +387,65 @@ private void addKeyWords(XMLStreamWriter writer, String value) throws XMLStreamE
}
}

private void handleAuthors(XMLStreamWriter writer, String value) throws XMLStreamException {
String[] authors = value.split("and");
for (String author : authors) {
// [impl->req~export.mods.names~1]
private void handleNames(XMLStreamWriter writer, String value, String role) throws XMLStreamException {
// BibTeX names use "and" as a separator. The shared parser also accepts repeated spaces in pasted lists.
String names = REPEATED_HORIZONTAL_WHITESPACE.matcher(value).replaceAll(" ");
for (Author author : AuthorList.parse(names).getNamedAuthors()) {
writer.writeStartElement("mods", "name", MODS_NAMESPACE_URI);
writer.writeAttribute("type", "personal");

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);
writer.writeStartElement("mods", "namePart", MODS_NAMESPACE_URI);
writer.writeAttribute("type", "family");
writer.writeCharacters(familyName);
writer.writeEndElement();

// now take care of the forenames
String forename = author.substring(commaIndex + 1);
String[] forenames = forename.split(" ");
for (String given : forenames) {
if (!given.isEmpty()) {
writer.writeStartElement("mods", "namePart", MODS_NAMESPACE_URI);
writer.writeAttribute("type", "given");
writer.writeCharacters(given);
writer.writeEndElement();
}
}
writer.writeEndElement();
String familyName = author.getNamePrefixAndFamilyNameForDisplay();
String givenName = author.getGivenNameWithOriginalPunctuation()
.or(() -> author.getExplicitGivenNameInitials().filter(initials -> !initials.isEmpty()))
.orElse("");
String suffix = author.getNameSuffix().orElse("");
// https://www.loc.gov/standards/mods/userguide/name.html
if (StringUtil.isInCurlyBrackets(familyName) && givenName.isEmpty() && suffix.isEmpty()) {
writer.writeAttribute("type", "corporate");
writeNamePart(writer, "", Author.FORMATTER.format(familyName));
} else if (familyName.isEmpty() && givenName.isEmpty() && suffix.isEmpty()) {
// A custom extended name may have no standard BibLaTeX name parts.
writeNamePart(writer, "", author.getAsBibtex());
} else {
// no "," indicates that there should only be a family name
writer.writeStartElement("mods", "namePart", MODS_NAMESPACE_URI);
writer.writeAttribute("type", "family");
writer.writeCharacters(author);
writer.writeEndElement();
writer.writeEndElement();
writer.writeAttribute("type", "personal");
writePersonalNameParts(writer, familyName, givenName, suffix);
}

writer.writeStartElement("mods", "role", MODS_NAMESPACE_URI);
writer.writeStartElement("mods", "roleTerm", MODS_NAMESPACE_URI);
writer.writeAttribute("type", "code");
writer.writeAttribute("authority", "marcrelator");
writer.writeCharacters(role);
writer.writeEndElement();
writer.writeEndElement();
writer.writeEndElement();
}
}

private void writePersonalNameParts(XMLStreamWriter writer, String familyName, String givenName, String suffix) throws XMLStreamException {
if (!familyName.isEmpty()) {
writeNamePart(writer, "family", familyName);
}
if (!givenName.isEmpty()) {
for (String given : WHITESPACE_PATTERN.split(givenName)) {
writeNamePart(writer, "given", given);
}
}
if (!suffix.isEmpty()) {
// termsOfAddress maps to MARC 100/700 $c.
// https://www.loc.gov/standards/mods/v3/mods2marc-mapping-3-6.html
writeNamePart(writer, "termsOfAddress", suffix);
}
}

private void writeNamePart(XMLStreamWriter writer, String type, String name) throws XMLStreamException {
writer.writeStartElement("mods", "namePart", MODS_NAMESPACE_URI);
if (!type.isEmpty()) {
writer.writeAttribute("type", type);
}
writer.writeCharacters(name);
writer.writeEndElement();
}

private void addIdentifier(XMLStreamWriter writer, Field field, String value) throws XMLStreamException {

if (new UnknownField("citekey").equals(field)) {
Expand Down
Loading
Loading