Skip to content
Open
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
28 changes: 28 additions & 0 deletions docs/decisions/0086-export-ordered-bibo-authors-and-creators.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
nav_order: 86
parent: Decision Records
---

# Export ordered BIBO authors and Dublin Core creators
`adr~export-ordered-bibo-authors-and-creators~1`

Needs: impl, utest

## Context and Problem Statement

The BIBO RDF exporter needs to retain author order while remaining compatible with consumers that read `dcterms:creator`.
The [BIBO author list](https://dcmi.github.io/bibo/#http://purl.org/ontology/bibo/authorList) links a document to an ordered collection; it does not itself assert a creator relation to each member.

## Considered Options

* Emit only `dcterms:creator` relations.
* Emit only an ordered `bibo:authorList`.
* Emit both relations and share the author nodes.

## Decision Outcome

Chosen option: Emit both relations and share the author nodes, because this preserves author order while supporting consumers that read direct creator relations. Each creator is a member of the `rdf:Seq` referenced by `bibo:authorList`, in source order. Shared blank-node identifiers connect the two representations without relying on name matching.

Consumers can use direct creator relations or the ordered list. Blank-node identifiers are generated for each export to avoid collisions between entries, so repeated exports can differ in their serialized identifiers even when their RDF graphs are equivalent.

<!-- markdownlint-disable-file MD022 MD033 -->
Original file line number Diff line number Diff line change
@@ -1,45 +1,39 @@
package org.jabref.logic.layout.format;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.jabref.logic.layout.LayoutFormatter;
import org.jabref.model.entry.Author;
import org.jabref.model.entry.AuthorList;

// [impl->adr~export-ordered-bibo-authors-and-creators~1]
public class CreateBibORDFAuthors implements LayoutFormatter {

@Override
public String format(String fieldText) {
// Yeah, the format is quite verbose... sorry about that :)

// <bibo:contribution>
// <bibo:Contribution>
// <bibo:role rdf:resource="http://purl.org/ontology/bibo/roles/author" />
// <bibo:contributor><foaf:Person foaf:name="Ola Spjuth"/></bibo:contributor>
// <bibo:position>1</bibo:position>
// </bibo:Contribution>
// </bibo:contribution>

StringBuilder sb = new StringBuilder(100);

if (!fieldText.contains(" and ")) {
singleAuthor(sb, fieldText, 1);
} else {
String[] names = fieldText.split(" and ");
for (int i = 0; i < names.length; i++) {
singleAuthor(sb, names[i], i + 1);
if (i < (names.length - 1)) {
sb.append('\n');
}
}
AuthorList authors = AuthorList.parse(fieldText);
if (authors.isEmpty()) {
return "";
}

return sb.toString();
}

private static void singleAuthor(StringBuilder sb, String author, int position) {
sb.append("<bibo:contribution>\n");
sb.append(" <bibo:Contribution>\n");
sb.append(" <bibo:role rdf:resource=\"http://purl.org/ontology/bibo/roles/author\" />\n");
sb.append(" <bibo:contributor><foaf:Person foaf:name=\"").append(author).append("\"/></bibo:contributor>\n");
sb.append(" <bibo:position>").append(position).append("</bibo:position>\n");
sb.append(" </bibo:Contribution>\n");
sb.append("</bibo:contribution>\n");
XMLChars xmlChars = new XMLChars();
StringBuilder result = new StringBuilder();
List<String> authorIds = new ArrayList<>();
for (Author author : authors) {
String id = "author" + UUID.randomUUID();
authorIds.add(id);
result.append("<dcterms:creator><foaf:Person rdf:nodeID=\"")
.append(id)
.append("\" foaf:name=\"")
.append(xmlChars.format(author.getGivenFamily(false)))
.append("\"/></dcterms:creator>\n");
}
result.append("<bibo:authorList><rdf:Seq>\n");
for (String id : authorIds) {
result.append("<rdf:li rdf:nodeID=\"").append(id).append("\"/>\n");
}
return result.append("</rdf:Seq></bibo:authorList>\n").toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


\begin{author}
\format[XMLChars,CreateBibORDFAuthors]{\author}
\format[CreateBibORDFAuthors]{\author}
\end{author}

</bibo:Article>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:bibo="http://purl.org/ontology/biblio/"
xmlns:bibo="http://purl.org/ontology/bibo/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:dcterms="http://purl.org/dc/terms/"
xmlns:foaf="http://xmlns.com/foaf/0.1/"
Expand Down
2 changes: 1 addition & 1 deletion jablib/src/main/resources/resource/layout/bibordf.layout
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
\begin{doi} <bibo:doi>\format[DOIStrip,XMLChars]{\doi}</bibo:doi>\end{doi}

\begin{author}
\format[XMLChars,CreateBibORDFAuthors]{\author}
\format[CreateBibORDFAuthors]{\author}
\end{author}

</bibo:Document>
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package org.jabref.logic.exporter;

import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.jabref.logic.layout.LayoutFormatterPreferences;
import org.jabref.logic.util.StandardFileType;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.types.StandardEntryType;
import org.jabref.model.metadata.SaveOrder;

import org.jspecify.annotations.NullMarked;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.api.parallel.ResourceLock;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

import static org.junit.jupiter.api.Assertions.assertEquals;

// [utest->adr~export-ordered-bibo-authors-and-creators~1]
@NullMarked
@ResourceLock("exporter")
class BibORdfExporterTest {
private static final String RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
private static final String BIBO = "http://purl.org/ontology/bibo/";
private static final String FOAF = "http://xmlns.com/foaf/0.1/";
private static final String DCTERMS = "http://purl.org/dc/terms/";

@ParameterizedTest
@MethodSource("authors")
void exportsOrderedAuthors(StandardEntryType type, String authors, List<String> expected, @TempDir Path directory)
throws IOException, SaveException, ParserConfigurationException, SAXException {
var exporter = new TemplateExporter("BibO RDF", "bibordf", "bibordf", "", StandardFileType.RDF,
LayoutFormatterPreferences.getDefault(), SaveOrder.getDefaultSaveOrder());
BibEntry entry = new BibEntry(type).withField(StandardField.AUTHOR, authors);
Path output = directory.resolve("export.rdf");
exporter.export(new BibDatabaseContext(), output, List.of(entry));

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
var document = factory.newDocumentBuilder().parse(output.toFile());
var creators = document.getElementsByTagNameNS(DCTERMS, "creator");
assertEquals(expected.size(), creators.getLength());
Map<String, String> namesById = IntStream.range(0, creators.getLength())
.mapToObj(index -> (Element) ((Element) creators.item(index)).getElementsByTagNameNS(FOAF, "Person").item(0))
.collect(Collectors.toMap(person -> person.getAttributeNS(RDF, "nodeID"), person -> person.getAttributeNS(FOAF, "name")));
var lists = document.getElementsByTagNameNS(BIBO, "authorList");
assertEquals(1, lists.getLength());
var sequences = ((Element) lists.item(0)).getElementsByTagNameNS(RDF, "Seq");
assertEquals(1, sequences.getLength());
var members = ((Element) sequences.item(0)).getElementsByTagNameNS(RDF, "li");
List<String> names = IntStream.range(0, members.getLength())
.mapToObj(index -> ((Element) members.item(index)).getAttributeNS(RDF, "nodeID"))
.map(namesById::get)
.toList();
assertEquals(expected, names);
}

private static Stream<Arguments> authors() {
return Stream.of(
Arguments.of(StandardEntryType.Article, "Doe, Jane", List.of("Jane Doe")),
Arguments.of(StandardEntryType.Article, "Doe, Jane and {Research and Development}", List.of("Jane Doe", "Research and Development")),
Arguments.of(StandardEntryType.Book, "Doe, Jane", List.of("Jane Doe")),
Arguments.of(StandardEntryType.Book, "Doe, Jane and {Smith & Jones}", List.of("Jane Doe", "Smith & Jones")));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.jabref.logic.layout.format;

import java.util.regex.Pattern;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

class CreateBibORDFAuthorsTest {
private static final Pattern AUTHOR_ID = Pattern.compile("author[0-9a-f-]{36}");

@Test
void formatsSingleAuthorAsSequence() {
assertEquals("""
<dcterms:creator><foaf:Person rdf:nodeID="author" foaf:name="Jane Doe"/></dcterms:creator>
<bibo:authorList><rdf:Seq>
<rdf:li rdf:nodeID="author"/>
</rdf:Seq></bibo:authorList>
""", normalizeIds(new CreateBibORDFAuthors().format("Doe, Jane")));
}

@Test
void preservesOrderAndProtectedNames() {
assertEquals("""
<dcterms:creator><foaf:Person rdf:nodeID="author" foaf:name="Jane Doe"/></dcterms:creator>
<dcterms:creator><foaf:Person rdf:nodeID="author" foaf:name="Research and Development"/></dcterms:creator>
<bibo:authorList><rdf:Seq>
<rdf:li rdf:nodeID="author"/>
<rdf:li rdf:nodeID="author"/>
</rdf:Seq></bibo:authorList>
""", normalizeIds(new CreateBibORDFAuthors().format("Doe, Jane and {Research and Development}")));
}

@Test
void escapesNamesForXmlAttributes() {
assertEquals("""
<dcterms:creator><foaf:Person rdf:nodeID="author" foaf:name="Smith &#x0026; Jones"/></dcterms:creator>
<bibo:authorList><rdf:Seq>
<rdf:li rdf:nodeID="author"/>
</rdf:Seq></bibo:authorList>
""", normalizeIds(new CreateBibORDFAuthors().format("{Smith & Jones}")));
}

private String normalizeIds(String result) {
return AUTHOR_ID.matcher(result).replaceAll("author");
}
}
Loading