From 568ccda980db38856ec57fbe47106461dbf12598 Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Sat, 9 Nov 2019 16:28:22 +0100 Subject: [PATCH] Fix DublinCoreExtractor (and add more tests) --- .../jabref/logic/xmp/DublinCoreExtractor.java | 24 ++++++++++--------- .../model/entry/CanonicalBibtexEntry.java | 12 ++++++---- .../importer/fileformat/BibtexParserTest.java | 9 +++++++ .../model/entry/CanonicalBibtexEntryTest.java | 17 +++++++++++++ 4 files changed, 46 insertions(+), 16 deletions(-) create mode 100644 src/test/java/org/jabref/model/entry/CanonicalBibtexEntryTest.java diff --git a/src/main/java/org/jabref/logic/xmp/DublinCoreExtractor.java b/src/main/java/org/jabref/logic/xmp/DublinCoreExtractor.java index 190b8477ada5..9c54524bc0ac 100644 --- a/src/main/java/org/jabref/logic/xmp/DublinCoreExtractor.java +++ b/src/main/java/org/jabref/logic/xmp/DublinCoreExtractor.java @@ -85,9 +85,10 @@ private void extractYearAndMonth() { } if (calender != null) { bibEntry.setField(StandardField.YEAR, String.valueOf(calender.get(Calendar.YEAR))); + int monthNumber = calender.get(Calendar.MONTH) + 1; // not the 1st of January - if (!((calender.get(Calendar.MONTH) == 0) && (calender.get(Calendar.DAY_OF_MONTH) == 1))) { - Month.getMonthByNumber(calender.get(Calendar.MONTH) + 1) + if (!((monthNumber == 1) && (calender.get(Calendar.DAY_OF_MONTH) == 1))) { + Month.getMonthByNumber(monthNumber) .ifPresent(month -> bibEntry.setMonth(month)); } } @@ -125,16 +126,13 @@ private void extractPublisher() { } /** - * This method sets all fields, which are custom in bibtext and therefore supported by jabref, but which are not + * This method sets all fields, which are custom in BibTeX and therefore supported by JabRef, but which are not * included in the DublinCore format. - *

+ *

* The relation attribute of DublinCore is abused to insert these custom fields. */ private void extractBibTexFields() { - List relationships = dcSchema.getRelations(); - Predicate isBibTeXElement = s -> s.startsWith("bibtex/"); - Consumer splitBibTeXElement = s -> { // the default pattern is bibtex/key/value, but some fields contains url etc. // so the value property contains additional slashes, which makes the usage of @@ -151,10 +149,11 @@ private void extractBibTexFields() { // see also DublinCoreExtractor#extractYearAndMonth if (StandardField.MONTH.equals(key)) { Optional parsedMonth = Month.parse(value); - parsedMonth.ifPresent(month -> bibEntry.setField(key, month.getShortName())); + parsedMonth.ifPresent(bibEntry::setMonth); } } }; + List relationships = dcSchema.getRelations(); if (relationships != null) { relationships.stream() .filter(isBibTeXElement) @@ -221,19 +220,22 @@ private void extractType() { * To understand how to get hold of a DublinCore have a look in the test cases for XMPUtil. *

* The BibEntry is build by mapping individual fields in the dublin core (like creator, title, subject) to fields in - * a bibtex bibEntry. + * a bibtex bibEntry. In case special "bibtex/" entries are contained, the normal dublin core fields take + * precedence. For instance, the dublin core date takes precedence over bibtex/month. * - * @return The bibtex bibEntry found in the document information. + * @return The bibEntry extracted from the document information. */ public Optional extractBibtexEntry() { + // first extract "bibtex/" entries + this.extractBibTexFields(); + // then extract all "standard" dublin core entries this.extractEditor(); this.extractAuthor(); this.extractYearAndMonth(); this.extractAbstract(); this.extractDOI(); this.extractPublisher(); - this.extractBibTexFields(); this.extractRights(); this.extractSource(); this.extractSubject(); diff --git a/src/main/java/org/jabref/model/entry/CanonicalBibtexEntry.java b/src/main/java/org/jabref/model/entry/CanonicalBibtexEntry.java index 0f9ba8e30223..4e98ae9908c8 100644 --- a/src/main/java/org/jabref/model/entry/CanonicalBibtexEntry.java +++ b/src/main/java/org/jabref/model/entry/CanonicalBibtexEntry.java @@ -17,10 +17,13 @@ private CanonicalBibtexEntry() { } /** - * This returns a canonical BibTeX serialization. Special characters such as "{" or "&" are NOT escaped, but written - * as is + * This returns a canonical BibTeX serialization. Serializes all fields, even the JabRef internal ones. Does NOT + * serialize "KEY_FIELD" as field, but as key * - * Serializes all fields, even the JabRef internal ones. Does NOT serialize "KEY_FIELD" as field, but as key + *

*/ public static String getCanonicalRepresentation(BibEntry entry) { StringBuilder sb = new StringBuilder(); @@ -50,7 +53,7 @@ public static String getCanonicalRepresentation(BibEntry entry) { // generate field entries StringJoiner sj = new StringJoiner(",\n", "", "\n"); for (String fieldName : sortedFields) { - String line = String.format(" %s = {%s}", fieldName, String.valueOf(mapFieldToValue.get(fieldName)).replaceAll("\\r\\n","\n")); + String line = String.format(" %s = {%s}", fieldName, String.valueOf(mapFieldToValue.get(fieldName)).replaceAll("\\r\\n", "\n")); sj.add(line); } sb.append(sj); @@ -59,5 +62,4 @@ public static String getCanonicalRepresentation(BibEntry entry) { sb.append('}'); return sb.toString(); } - } diff --git a/src/test/java/org/jabref/logic/importer/fileformat/BibtexParserTest.java b/src/test/java/org/jabref/logic/importer/fileformat/BibtexParserTest.java index 1dcd5ba75c85..75affd8e6324 100644 --- a/src/test/java/org/jabref/logic/importer/fileformat/BibtexParserTest.java +++ b/src/test/java/org/jabref/logic/importer/fileformat/BibtexParserTest.java @@ -1784,4 +1784,13 @@ void bibTeXConstantAprilIsDisplayedAsConstant() throws ParseException { assertEquals("#apr#", result.get().getField(StandardField.MONTH).get()); } + + @Test + void bibTeXConstantAprilIsParsedAsStringMonthAprilWhenReadingTheField() throws ParseException { + Optional result = parser.parseSingleEntry("@Misc{m, month = apr }" ); + + assertEquals(Optional.of("#apr#"), result.get().getField(StandardField.MONTH)); + } + + } diff --git a/src/test/java/org/jabref/model/entry/CanonicalBibtexEntryTest.java b/src/test/java/org/jabref/model/entry/CanonicalBibtexEntryTest.java new file mode 100644 index 000000000000..1944885891df --- /dev/null +++ b/src/test/java/org/jabref/model/entry/CanonicalBibtexEntryTest.java @@ -0,0 +1,17 @@ +package org.jabref.model.entry; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class CanonicalBibtexEntryTest { + + @Test + void canonicalRepresentationIsCorrectForStringMonth() { + BibEntry entry = new BibEntry(); + entry.setMonth(Month.MAY); + assertEquals("@misc{,\n" + + " month = {#may#}\n" + + "}", CanonicalBibtexEntry.getCanonicalRepresentation(entry)); + } +}