From d9a80880080cc906a43ff666db92e438496f4f14 Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Fri, 6 Mar 2020 10:58:32 +0100 Subject: [PATCH] Add month normalization to AstroPhysicsFetcher (#6022) --- .../fetcher/AstrophysicsDataSystem.java | 3 ++- .../model/cleanup/FieldFormatterCleanup.java | 4 ++-- .../java/org/jabref/model/entry/BibEntry.java | 19 ++++++++++--------- .../NormalizeMonthFormatterTest.java | 5 +++++ 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/jabref/logic/importer/fetcher/AstrophysicsDataSystem.java b/src/main/java/org/jabref/logic/importer/fetcher/AstrophysicsDataSystem.java index 3415597a690..9dff54b8b74 100644 --- a/src/main/java/org/jabref/logic/importer/fetcher/AstrophysicsDataSystem.java +++ b/src/main/java/org/jabref/logic/importer/fetcher/AstrophysicsDataSystem.java @@ -14,6 +14,7 @@ import org.jabref.logic.cleanup.MoveFieldCleanup; import org.jabref.logic.formatter.bibtexfields.ClearFormatter; +import org.jabref.logic.formatter.bibtexfields.NormalizeMonthFormatter; import org.jabref.logic.formatter.bibtexfields.NormalizeNamesFormatter; import org.jabref.logic.formatter.bibtexfields.RemoveBracesFormatter; import org.jabref.logic.formatter.bibtexfields.RemoveNewlinesFormatter; @@ -143,6 +144,7 @@ public void doPostCleanup(BibEntry entry) { new FieldFormatterCleanup(StandardField.ABSTRACT, new RemoveNewlinesFormatter()).cleanup(entry); new FieldFormatterCleanup(StandardField.TITLE, new RemoveBracesFormatter()).cleanup(entry); new FieldFormatterCleanup(StandardField.AUTHOR, new NormalizeNamesFormatter()).cleanup(entry); + new FieldFormatterCleanup(StandardField.MONTH, new NormalizeMonthFormatter()).cleanup(entry); // Remove ADS note new FieldFormatterCleanup(new UnknownField("adsnote"), new ClearFormatter()).cleanup(entry); @@ -249,7 +251,6 @@ public Optional performSearchById(String identifier) throws FetcherExc * bibcodes */ private List performSearchByIds(Collection identifiers) throws FetcherException { - List ids = identifiers.stream().filter(identifier -> !StringUtil.isBlank(identifier)).collect(Collectors.toList()); if (ids.isEmpty()) { return Collections.emptyList(); diff --git a/src/main/java/org/jabref/model/cleanup/FieldFormatterCleanup.java b/src/main/java/org/jabref/model/cleanup/FieldFormatterCleanup.java index 5965ed3fdb2..546025aa679 100644 --- a/src/main/java/org/jabref/model/cleanup/FieldFormatterCleanup.java +++ b/src/main/java/org/jabref/model/cleanup/FieldFormatterCleanup.java @@ -49,7 +49,7 @@ public List cleanup(BibEntry entry) { private List cleanupSingleField(Field fieldKey, BibEntry entry) { if (!entry.hasField(fieldKey)) { // Not set -> nothing to do - return new ArrayList<>(); + return Collections.emptyList(); } String oldValue = entry.getField(fieldKey).orElse(null); @@ -57,7 +57,7 @@ private List cleanupSingleField(Field fieldKey, BibEntry entry) { String newValue = formatter.format(oldValue); if (oldValue.equals(newValue)) { - return new ArrayList<>(); + return Collections.emptyList(); } else { if (newValue.isEmpty()) { entry.clearField(fieldKey); diff --git a/src/main/java/org/jabref/model/entry/BibEntry.java b/src/main/java/org/jabref/model/entry/BibEntry.java index bfe7cadc8c8..d4c69cdfcdd 100644 --- a/src/main/java/org/jabref/model/entry/BibEntry.java +++ b/src/main/java/org/jabref/model/entry/BibEntry.java @@ -508,7 +508,7 @@ public void setField(Map fields) { /** * Set a field, and notify listeners about the change. * - * @param field The field to set + * @param field The field to set * @param value The value to set * @param eventSource Source the event is sent from */ @@ -543,7 +543,7 @@ public Optional setField(Field field, String value, EntriesEventSou /** * Set a field, and notify listeners about the change. * - * @param field The field to set. + * @param field The field to set. * @param value The value to set. */ public Optional setField(Field field, String value) { @@ -551,8 +551,7 @@ public Optional setField(Field field, String value) { } /** - * Remove the mapping for the field name, and notify listeners about - * the change. + * Remove the mapping for the field name, and notify listeners about the change. * * @param field The field to clear. */ @@ -588,9 +587,9 @@ public Optional clearField(Field field, EntriesEventSource eventSou * database argument is given, this method will try to look up missing fields in * entries linked by the "crossref" field, if any. * - * @param fields An array of field names to be checked. - * @param database The database in which to look up crossref'd entries, if any. This - * argument can be null, meaning that no attempt will be made to follow crossrefs. + * @param fields An array of field names to be checked. + * @param database The database in which to look up crossref'd entries, if any. This argument can be null, meaning + * that no attempt will be made to follow crossrefs. * @return true if all fields are set or could be resolved, false otherwise. */ public boolean allFieldsPresent(Collection fields, BibDatabase database) { @@ -610,9 +609,11 @@ public Object clone() { /** * This returns a canonical BibTeX serialization. Special characters such as "{" or "&" are NOT escaped, but written - * as is + * as is. In case the JabRef "hack" for distinguishing "field = value" and "field = {value}" (in .bib files) is + * used, it is output as "field = {#value#}", which may cause headaches in debugging. We nevertheless do it this way + * to a) enable debugging the internal representation and b) save time at this method. *

- * 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. */ @Override public String toString() { diff --git a/src/test/java/org/jabref/logic/formatter/bibtexfields/NormalizeMonthFormatterTest.java b/src/test/java/org/jabref/logic/formatter/bibtexfields/NormalizeMonthFormatterTest.java index 31c3f935989..32d196bcc2a 100644 --- a/src/test/java/org/jabref/logic/formatter/bibtexfields/NormalizeMonthFormatterTest.java +++ b/src/test/java/org/jabref/logic/formatter/bibtexfields/NormalizeMonthFormatterTest.java @@ -21,4 +21,9 @@ public void setUp() { public void formatExample() { assertEquals("#dec#", formatter.format(formatter.getExampleInput())); } + + @Test + public void plainAprilShouldBeApril() { + assertEquals("#apr#", formatter.format("#apr#")); + } }