From ce2bf6e4d1e97fb69eb00470aad15136c4748a75 Mon Sep 17 00:00:00 2001 From: I569349 Date: Wed, 1 Nov 2023 20:32:40 +0100 Subject: [PATCH 1/2] ShortDoi - Introduced predicate to check if the value is already a short DOI - UTest --- .../logic/formatter/bibtexfields/ShortenDOIFormatter.java | 5 ++++- .../formatter/bibtexfields/ShortenDOIFormatterTest.java | 5 +++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/jabref/logic/formatter/bibtexfields/ShortenDOIFormatter.java b/src/main/java/org/jabref/logic/formatter/bibtexfields/ShortenDOIFormatter.java index 5393210ec7d..0ca6f79bcf8 100644 --- a/src/main/java/org/jabref/logic/formatter/bibtexfields/ShortenDOIFormatter.java +++ b/src/main/java/org/jabref/logic/formatter/bibtexfields/ShortenDOIFormatter.java @@ -1,6 +1,8 @@ package org.jabref.logic.formatter.bibtexfields; import java.util.Objects; +import java.util.function.Predicate; +import java.util.regex.Pattern; import org.jabref.logic.cleanup.Formatter; import org.jabref.logic.importer.util.ShortDOIService; @@ -14,6 +16,7 @@ public class ShortenDOIFormatter extends Formatter { private static final Logger LOGGER = LoggerFactory.getLogger(ShortenDOIFormatter.class); + private static final Predicate SHORT_DOI_FORMAT = Pattern.compile("10/[a-zA-Z0-9]+").asPredicate(); @Override public String getName() { @@ -28,7 +31,7 @@ public String getKey() { @Override public String format(String value) { Objects.requireNonNull(value); - return DOI.parse(value) + return SHORT_DOI_FORMAT.test(value) ? value : DOI.parse(value) .map(doi -> { try { return new ShortDOIService().getShortDOI(doi).getDOI(); diff --git a/src/test/java/org/jabref/logic/formatter/bibtexfields/ShortenDOIFormatterTest.java b/src/test/java/org/jabref/logic/formatter/bibtexfields/ShortenDOIFormatterTest.java index 1da025cdb4b..15ea8081908 100644 --- a/src/test/java/org/jabref/logic/formatter/bibtexfields/ShortenDOIFormatterTest.java +++ b/src/test/java/org/jabref/logic/formatter/bibtexfields/ShortenDOIFormatterTest.java @@ -26,4 +26,9 @@ public void formatDoi() { public void invalidDoiIsKept() { assertEquals("invalid-doi", formatter.format("invalid-doi")); } + + @Test + public void shortDoi() { + assertEquals("10/adc", formatter.format("10/adc")); + } } From fbf536937fbd188095d1b58d84d8a240fa3723f2 Mon Sep 17 00:00:00 2001 From: I569349 Date: Thu, 2 Nov 2023 08:23:21 +0100 Subject: [PATCH 2/2] Changelog - Added entry to CHANGELOG - Modified the regex pattern --- CHANGELOG.md | 1 + .../logic/formatter/bibtexfields/ShortenDOIFormatter.java | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6060ff558b8..2aeb53cd903 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv - We moved the location of the 'Open only one instance of JabRef' preference option from "Network" to "General". [#9306](https://github.com/JabRef/jabref/issues/9306) - The two previews in the change resolver dialog now have their scrollbars synchronized. [#9576](https://github.com/JabRef/jabref/issues/9576). - We changed the setting of the keyword separator to accept a single character only. [#177](https://github.com/koppor/jabref/issues/177) +- Short DOI formatter now checks, if the value is already formatted. If so, it returns the value instead of calling the ShortDOIService again. [#10589](https://github.com/JabRef/jabref/issues/10589) ### Fixed diff --git a/src/main/java/org/jabref/logic/formatter/bibtexfields/ShortenDOIFormatter.java b/src/main/java/org/jabref/logic/formatter/bibtexfields/ShortenDOIFormatter.java index 0ca6f79bcf8..56ee1af1c0f 100644 --- a/src/main/java/org/jabref/logic/formatter/bibtexfields/ShortenDOIFormatter.java +++ b/src/main/java/org/jabref/logic/formatter/bibtexfields/ShortenDOIFormatter.java @@ -16,7 +16,7 @@ public class ShortenDOIFormatter extends Formatter { private static final Logger LOGGER = LoggerFactory.getLogger(ShortenDOIFormatter.class); - private static final Predicate SHORT_DOI_FORMAT = Pattern.compile("10/[a-zA-Z0-9]+").asPredicate(); + private static final Predicate SHORT_DOI_FORMAT = Pattern.compile("^10/[a-zA-Z0-9]+$").asPredicate(); @Override public String getName() {