Skip to content

Commit

Permalink
Fix DOI parsing error (#10405)
Browse files Browse the repository at this point in the history
* Fix DOI parsing error

Reported by Thilo

* fix import
  • Loading branch information
Siedlerchr authored Sep 19, 2023
1 parent c66f863 commit e53c390
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
import javafx.scene.control.TextInputControl;

import org.jabref.gui.DialogService;
import org.jabref.gui.Globals;
import org.jabref.gui.actions.ActionFactory;
import org.jabref.gui.actions.StandardActions;
import org.jabref.gui.edit.CopyDoiUrlAction;
import org.jabref.logic.formatter.bibtexfields.CleanupUrlFormatter;
import org.jabref.logic.formatter.bibtexfields.NormalizeNamesFormatter;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.strings.StringUtil;
import org.jabref.preferences.PreferencesService;

import com.tobiasdiez.easybind.EasyBind;

Expand Down Expand Up @@ -53,9 +53,9 @@ public static Supplier<List<MenuItem>> getNameMenu(final TextInputControl textIn
* @param textArea text-area that this menu will be connected to
* @return menu containing items of the default menu and an item for copying a DOI/DOI URL
*/
public static Supplier<List<MenuItem>> getDOIMenu(TextArea textArea, DialogService dialogService) {
public static Supplier<List<MenuItem>> getDOIMenu(TextArea textArea, DialogService dialogService, PreferencesService preferencesService) {
return () -> {
ActionFactory factory = new ActionFactory(Globals.getKeyPrefs());
ActionFactory factory = new ActionFactory(preferencesService.getKeyBindingRepository());
MenuItem copyDoiMenuItem = factory.createMenuItem(StandardActions.COPY_DOI, new CopyDoiUrlAction(textArea, StandardActions.COPY_DOI, dialogService));
MenuItem copyDoiUrlMenuItem = factory.createMenuItem(StandardActions.COPY_DOI_URL, new CopyDoiUrlAction(textArea, StandardActions.COPY_DOI_URL, dialogService));
List<MenuItem> menuItems = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public IdentifierEditor(Field field,
new Tooltip(Localization.lang("Look up %0", field.getDisplayName())));

if (field.equals(StandardField.DOI)) {
textArea.initContextMenu(EditorMenus.getDOIMenu(textArea, dialogService));
textArea.initContextMenu(EditorMenus.getDOIMenu(textArea, dialogService, preferencesService));
} else {
textArea.initContextMenu(new DefaultMenu(textArea));
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/jabref/model/entry/identifier/DOI.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ public static Optional<DOI> parse(String doi) {
cleanedDOI = cleanedDOI.replaceAll(CHARS_TO_REMOVE, "");

if (cleanedDOI.startsWith("_") && cleanedDOI.endsWith("_")) {
if (cleanedDOI.length() == 1) {
return Optional.empty();
}
cleanedDOI = cleanedDOI.substring(1, cleanedDOI.length() - 1);
}

Expand Down
7 changes: 7 additions & 0 deletions src/test/java/org/jabref/model/entry/identifier/DOITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,13 @@ public void rejectInvalidDirectoryIndicatorInShortDoi() {
assertThrows(IllegalArgumentException.class, () -> new DOI("20/abcd"));
}

@Test
public void emptyOrUndescoreOnlyReturnsEmpty() {
assertEquals(Optional.empty(), DOI.parse("_"));
assertEquals(Optional.empty(), DOI.parse("\t_"));
assertEquals(Optional.empty(), DOI.parse("___"));
}

@Test
public void rejectInvalidDoiUri() {
assertThrows(IllegalArgumentException.class, () -> new DOI("https://thisisnouri"));
Expand Down

0 comments on commit e53c390

Please sign in to comment.