Skip to content

Commit

Permalink
Modify canonical representation to normalize authors
Browse files Browse the repository at this point in the history
  • Loading branch information
koppor committed Mar 11, 2016
1 parent 2a80e9c commit 110bc19
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class CanonicalBibtexEntry {
* This returns a canonical BibTeX serialization. Special characters such as "{" or "&" are NOT escaped, but written
* as is
*
* authors and editors are normalized
*
* Serializes all fields, even the JabRef internal ones. Does NOT serialize "KEY_FIELD" as field, but as key
*/
public static String getCanonicalRepresentation(BibEntry e) {
Expand All @@ -34,7 +36,12 @@ public static String getCanonicalRepresentation(BibEntry e) {
if (!fieldName.equals(BibEntry.KEY_FIELD)) {
String lowerCaseFieldName = fieldName.toLowerCase(Locale.US);
sortedFields.add(lowerCaseFieldName);
mapFieldToValue.put(lowerCaseFieldName, e.getField(fieldName));
String value = e.getField(fieldName);
if ("author".equals(lowerCaseFieldName) || "editor".equals(lowerCaseFieldName)) {
// normalize field contents
value = AuthorList.getAuthorList(value).toString();
}
mapFieldToValue.put(lowerCaseFieldName, value);
}
}

Expand Down
62 changes: 21 additions & 41 deletions src/test/java/net/sf/jabref/logic/xmp/XMPUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.sf.jabref.model.database.BibDatabaseMode;
import net.sf.jabref.model.entry.AuthorList;
import net.sf.jabref.bibtex.BibEntryWriter;
import net.sf.jabref.bibtex.BibtexEntryAssert;
import net.sf.jabref.model.entry.IdGenerator;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.entry.BibtexEntryTypes;
Expand Down Expand Up @@ -456,7 +457,7 @@ public void testReadWriteXMP() throws Exception {
Assert.assertEquals(1, l.size());
BibEntry x = l.get(0);

assertEqualsBibtexEntry(e, x);
BibtexEntryAssert.assertEquals(e, x);
}

/**
Expand Down Expand Up @@ -500,7 +501,7 @@ public void testAttributeRead() throws Exception {
Assert.assertEquals(1, l.size());
BibEntry e = l.get(0);

assertEqualsBibtexEntry(t2BibtexEntry(), e);
BibtexEntryAssert.assertEquals(t2BibtexEntry(), e);
}

@Test
Expand Down Expand Up @@ -564,7 +565,7 @@ public void testSimpleUpdate() throws Exception {
Assert.assertEquals(1, l.size());
BibEntry e = l.get(0);

assertEqualsBibtexEntry(t1BibtexEntry(), e);
BibtexEntryAssert.assertEquals(t1BibtexEntry(), e);

// This is what we really want to test: Is the rest of the
// descriptions still there?
Expand Down Expand Up @@ -640,7 +641,7 @@ public void testSimpleUpdate() throws Exception {
Assert.assertEquals(1, l.size());
BibEntry e = l.get(0);

assertEqualsBibtexEntry(toSet, e);
BibtexEntryAssert.assertEquals(toSet, e);

// This is what we really want to test: Is the rest of the
// descriptions still there?
Expand Down Expand Up @@ -731,28 +732,7 @@ public void testXMLEscape() throws Exception {
Assert.assertEquals(1, l.size());
BibEntry x = l.get(0);

assertEqualsBibtexEntry(e, x);
}

public void assertEqualsBibtexEntry(BibEntry expected, BibEntry actual) {
Assert.assertNotNull(expected);
Assert.assertNotNull(actual);
Assert.assertEquals(expected.getCiteKey(), actual.getCiteKey());
Assert.assertEquals(expected.getType(), actual.getType());

for (String field : expected.getFieldNames()) {

if ("author".equalsIgnoreCase(field) || "editor".equalsIgnoreCase(field)) {

AuthorList expectedAuthors = AuthorList.getAuthorList(expected.getField(field));
AuthorList actualAuthors = AuthorList.getAuthorList(actual.getField(field));
Assert.assertEquals(expectedAuthors, actualAuthors);
} else {
Assert.assertEquals("comparing " + field, expected.getField(field), actual.getField(field));
}
}

Assert.assertEquals(expected.getFieldNames().size(), actual.getFieldNames().size());
BibtexEntryAssert.assertEquals(e, x);
}

/**
Expand Down Expand Up @@ -834,8 +814,8 @@ public void testReadMultiple() throws Exception {
b = tmp;
}

assertEqualsBibtexEntry(t2BibtexEntry(), a);
assertEqualsBibtexEntry(t3BibtexEntry(), b);
BibtexEntryAssert.assertEquals(t2BibtexEntry(), a);
BibtexEntryAssert.assertEquals(t3BibtexEntry(), b);
}

/**
Expand Down Expand Up @@ -865,8 +845,8 @@ public void testWriteMultiple() throws IOException, TransformerException {
b = tmp;
}

assertEqualsBibtexEntry(t2BibtexEntry(), a);
assertEqualsBibtexEntry(t3BibtexEntry(), b);
BibtexEntryAssert.assertEquals(t2BibtexEntry(), a);
BibtexEntryAssert.assertEquals(t3BibtexEntry(), b);
}

@Test
Expand All @@ -890,7 +870,7 @@ public void testReadWriteDC() throws IOException, TransformerException {
document.getDocumentInformation().getCustomMetadataValue("bibtex/bibtexkey"));
Assert.assertEquals("peanut, butter, jelly", document.getDocumentInformation().getKeywords());

assertEqualsBibtexEntry(t3BibtexEntry(),
BibtexEntryAssert.assertEquals(t3BibtexEntry(),
XMPUtil.getBibtexEntryFromDocumentInformation(document.getDocumentInformation()).get());

PDDocumentCatalog catalog = document.getDocumentCatalog();
Expand Down Expand Up @@ -929,7 +909,7 @@ public void testReadWriteDC() throws IOException, TransformerException {
*/
Assert.assertEquals(4, dcSchema.getRelationships().size());

assertEqualsBibtexEntry(t3BibtexEntry(), XMPUtil.getBibtexEntryFromDublinCore(dcSchema).get());
BibtexEntryAssert.assertEquals(t3BibtexEntry(), XMPUtil.getBibtexEntryFromDublinCore(dcSchema).get());

}

Expand All @@ -956,7 +936,7 @@ public void testWriteSingleUpdatesDCAndInfo() throws IOException, TransformerExc
document.getDocumentInformation().getCustomMetadataValue("bibtex/bibtexkey"));
Assert.assertEquals("peanut, butter, jelly", document.getDocumentInformation().getKeywords());

assertEqualsBibtexEntry(t3BibtexEntry(),
BibtexEntryAssert.assertEquals(t3BibtexEntry(),
XMPUtil.getBibtexEntryFromDocumentInformation(document.getDocumentInformation()).get());

PDDocumentCatalog catalog = document.getDocumentCatalog();
Expand Down Expand Up @@ -995,7 +975,7 @@ public void testWriteSingleUpdatesDCAndInfo() throws IOException, TransformerExc
*/
Assert.assertEquals(4, dcSchema.getRelationships().size());

assertEqualsBibtexEntry(t3BibtexEntry(), XMPUtil.getBibtexEntryFromDublinCore(dcSchema).get());
BibtexEntryAssert.assertEquals(t3BibtexEntry(), XMPUtil.getBibtexEntryFromDublinCore(dcSchema).get());

}
}
Expand Down Expand Up @@ -1070,7 +1050,7 @@ public void testCommandLineSingleBib() throws Exception {
}
List<BibEntry> l = XMPUtil.readXMP(pdfFile);
Assert.assertEquals(1, l.size());
assertEqualsBibtexEntry(t1BibtexEntry(), l.get(0));
BibtexEntryAssert.assertEquals(t1BibtexEntry(), l.get(0));

} finally {
if (!tempBib.delete()) {
Expand Down Expand Up @@ -1103,7 +1083,7 @@ public void testCommandLineSinglePdf() throws Exception {
Assert.assertEquals(1, c.size());
BibEntry x = c.iterator().next();

assertEqualsBibtexEntry(e, x);
BibtexEntryAssert.assertEquals(e, x);
}
}
// Write XMP to file
Expand Down Expand Up @@ -1137,7 +1117,7 @@ public void testCommandLineSinglePdf() throws Exception {
List<BibEntry> l = XMPUtil.readXMP(pdfFile);
Assert.assertEquals(1, l.size());

assertEqualsBibtexEntry(t1BibtexEntry(), l.get(0));
BibtexEntryAssert.assertEquals(t1BibtexEntry(), l.get(0));
}
}

Expand Down Expand Up @@ -1167,7 +1147,7 @@ public void testCommandLineByKey() throws Exception {
// PDF should be annotated:
List<BibEntry> l = XMPUtil.readXMP(pdfFile);
Assert.assertEquals(1, l.size());
assertEqualsBibtexEntry(t1BibtexEntry(), l.get(0));
BibtexEntryAssert.assertEquals(t1BibtexEntry(), l.get(0));
}
// Now try OezbekC06
try (ByteArrayOutputStream s = new ByteArrayOutputStream()) {
Expand All @@ -1183,7 +1163,7 @@ public void testCommandLineByKey() throws Exception {
// PDF should be annotated:
List<BibEntry> l = XMPUtil.readXMP(pdfFile);
Assert.assertEquals(1, l.size());
assertEqualsBibtexEntry(t2BibtexEntry(), l.get(0));
BibtexEntryAssert.assertEquals(t2BibtexEntry(), l.get(0));
} finally {
if (!tempBib.delete()) {
System.err.println("Cannot delete temporary file");
Expand Down Expand Up @@ -1231,8 +1211,8 @@ public void testCommandLineSeveral() throws Exception {
// Writing and reading will resolve strings!
t3.setField("month", "July");

assertEqualsBibtexEntry(t1, a);
assertEqualsBibtexEntry(t3, b);
BibtexEntryAssert.assertEquals(t1, a);
BibtexEntryAssert.assertEquals(t3, b);

} finally {
if (!tempBib.delete()) {
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/net/sf/jabref/model/entry/CanonicalBibEntryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,14 @@ public void canonicalRepresentationWithNewlines() {
Assert.assertEquals("@article{key,\n abstract = {line 1\nline 2}\n}", canonicalRepresentation);
}

@Test
public void canonicalRepresentationOfAnAuthor() {
BibEntry e = new BibEntry("id", BibtexEntryTypes.ARTICLE.getName());
e.setField(BibEntry.KEY_FIELD, "key");
e.setField("author", "K. Crowston and H. Annabi and J. Howison and C. Masango");
String canonicalRepresentation = CanonicalBibtexEntry.getCanonicalRepresentation(e);
Assert.assertEquals(
"@article{key,\n author = {Crowston, K. and Annabi, H. and Howison, J. and Masango, C.}\n}",
canonicalRepresentation);
}
}

0 comments on commit 110bc19

Please sign in to comment.