From dae1c81e7ef32f3131c3fd3aba88d41054c62f7e Mon Sep 17 00:00:00 2001 From: Christoph Date: Fri, 11 May 2018 19:00:51 +0200 Subject: [PATCH] Do not trim or remove whitespace from the title based id fetcher (#4016) * Do not trim text for TitleBasedFetcher Fixes #4014 * add changelog * add trimming epxlicit to fetchers add some tests * move doi trim to parse add test --- CHANGELOG.md | 7 ++++--- src/main/java/org/jabref/gui/EntryTypeDialog.java | 13 +++++++------ .../org/jabref/logic/importer/fetcher/ArXiv.java | 6 ++++-- .../jabref/logic/importer/fetcher/DoiFetcher.java | 1 - .../logic/importer/fetcher/IacrEprintFetcher.java | 6 ++++-- .../java/org/jabref/model/entry/identifier/DOI.java | 4 +++- .../jabref/logic/importer/fetcher/ArXivTest.java | 6 ++++++ .../logic/importer/fetcher/DoiFetcherTest.java | 7 +++++++ .../org/jabref/model/entry/identifier/DOITest.java | 8 ++++++++ 9 files changed, 43 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fbf47a6bbaa..041054e8bdc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,9 +15,10 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `# - We added a fetcher based on RFC-IDs. [#3971](https://github.com/JabRef/jabref/issues/3971) ### Fixed -We fixed an issue where the export to clipboard functionality could not be invoked [#3994](https://github.com/JabRef/jabref/issues/3994) -We fixed an issue with the migration of invalid Look and Feels [#3995, comment](https://github.com/JabRef/jabref/issues/3995#issuecomment-385649448) -We fixed an issue where JabRef would no longer start, when the option "Override default font settings" was activated [#3986](https://github.com/JabRef/jabref/issues/3986) +We fixed an issue where the export to clipboard functionality could not be invoked. [#3994](https://github.com/JabRef/jabref/issues/3994) +We fixed an issue with the migration of invalid Look and Feels. [#3995, comment](https://github.com/JabRef/jabref/issues/3995#issuecomment-385649448) +We fixed an issue where JabRef would no longer start, when the option "Override default font settings" was activated. [#3986](https://github.com/JabRef/jabref/issues/3986) +We fixed an issue where JabRef removed whitespace from the Title-fetcher which resulting in no entries being found. [#4014](https://github.com/JabRef/jabref/issues/4014) We fixed an issue where fetched entries from the ACM fetcher could not be imported. [#4018](https://github.com/JabRef/jabref/issues/4018) ### Removed diff --git a/src/main/java/org/jabref/gui/EntryTypeDialog.java b/src/main/java/org/jabref/gui/EntryTypeDialog.java index 84e2a78d4d1..40b19ab5c94 100644 --- a/src/main/java/org/jabref/gui/EntryTypeDialog.java +++ b/src/main/java/org/jabref/gui/EntryTypeDialog.java @@ -73,6 +73,7 @@ public EntryTypeDialog(JabRefFrame frame) { setTitle(Localization.lang("Select entry type")); addWindowListener(new WindowAdapter() { + @Override public void windowClosing(WindowEvent e) { cancelAction.actionPerformed(null); @@ -246,7 +247,6 @@ static class TypeButton extends JButton implements Comparable { private final EntryType type; - TypeButton(String label, EntryType type) { super(label); this.type = type; @@ -263,6 +263,7 @@ public EntryType getType() { } class CancelAction extends AbstractAction { + public CancelAction() { super("Cancel"); } @@ -275,6 +276,7 @@ public void actionPerformed(ActionEvent e) { } private class FetcherWorker extends SwingWorker, Void> { + private boolean fetcherException = false; private String fetcherExceptionMessage = ""; private IdBasedFetcher fetcher = null; @@ -288,10 +290,9 @@ protected Optional doInBackground() throws Exception { generateButton.setText(Localization.lang("Searching...")); }); - Globals.prefs.put(JabRefPreferences.ID_ENTRY_GENERATOR,String.valueOf(comboBox.getSelectedItem())); - searchID = idTextField.getText().trim(); - searchID = searchID.replaceAll(" ", ""); + Globals.prefs.put(JabRefPreferences.ID_ENTRY_GENERATOR, String.valueOf(comboBox.getSelectedItem())); fetcher = WebFetchers.getIdBasedFetchers(Globals.prefs.getImportFormatPreferences()).get(comboBox.getSelectedIndex()); + searchID = idTextField.getText(); if (!searchID.isEmpty()) { try { bibEntry = fetcher.performSearchById(searchID); @@ -311,7 +312,7 @@ protected void done() { if (result.isPresent()) { final BibEntry bibEntry = result.get(); if ((DuplicateCheck.containsDuplicate(frame.getCurrentBasePanel().getDatabase(), bibEntry, frame.getCurrentBasePanel().getBibDatabaseContext().getMode()).isPresent())) { - //If there are duplicates starts ImportInspectionDialog + //If there are duplicates starts ImportInspectionDialog final BasePanel panel = (BasePanel) frame.getTabbedPane().getSelectedComponent(); ImportInspectionDialog diag = new ImportInspectionDialog(frame, panel, Localization.lang("Import"), false); @@ -321,7 +322,7 @@ protected void done() { diag.setVisible(true); diag.toFront(); } else { - // Regenerate CiteKey of imported BibEntry + // Regenerate CiteKey of imported BibEntry new BibtexKeyGenerator(frame.getCurrentBasePanel().getBibDatabaseContext(), Globals.prefs.getBibtexKeyPatternPreferences()).generateAndSetKey(bibEntry); // Update Timestamps if (Globals.prefs.getTimestampPreferences().includeCreatedTimestamp()) { diff --git a/src/main/java/org/jabref/logic/importer/fetcher/ArXiv.java b/src/main/java/org/jabref/logic/importer/fetcher/ArXiv.java index a0829e42406..24b0f338101 100644 --- a/src/main/java/org/jabref/logic/importer/fetcher/ArXiv.java +++ b/src/main/java/org/jabref/logic/importer/fetcher/ArXiv.java @@ -258,8 +258,10 @@ public List performSearch(String query) throws FetcherException { @Override public Optional performSearchById(String identifier) throws FetcherException { - return searchForEntryById(identifier).map( - (arXivEntry) -> arXivEntry.toBibEntry(importFormatPreferences.getKeywordSeparator())); + String cleanedIdentifier = identifier.trim(); + cleanedIdentifier = identifier.replaceAll(" ", ""); + + return searchForEntryById(cleanedIdentifier).map((arXivEntry) -> arXivEntry.toBibEntry(importFormatPreferences.getKeywordSeparator())); } @Override diff --git a/src/main/java/org/jabref/logic/importer/fetcher/DoiFetcher.java b/src/main/java/org/jabref/logic/importer/fetcher/DoiFetcher.java index 697df25f5fc..766f2570164 100644 --- a/src/main/java/org/jabref/logic/importer/fetcher/DoiFetcher.java +++ b/src/main/java/org/jabref/logic/importer/fetcher/DoiFetcher.java @@ -45,7 +45,6 @@ public HelpFile getHelpPage() { @Override public Optional performSearchById(String identifier) throws FetcherException { Optional doi = DOI.parse(identifier); - try { if (doi.isPresent()) { URL doiURL = new URL(doi.get().getURIAsASCIIString()); diff --git a/src/main/java/org/jabref/logic/importer/fetcher/IacrEprintFetcher.java b/src/main/java/org/jabref/logic/importer/fetcher/IacrEprintFetcher.java index a87989b9481..af03b30b49f 100644 --- a/src/main/java/org/jabref/logic/importer/fetcher/IacrEprintFetcher.java +++ b/src/main/java/org/jabref/logic/importer/fetcher/IacrEprintFetcher.java @@ -37,8 +37,10 @@ public class IacrEprintFetcher implements IdBasedFetcher { private static final Logger LOGGER = LoggerFactory.getLogger(IacrEprintFetcher.class); private static final Pattern DATE_FROM_WEBSITE_AFTER_2000_PATTERN = Pattern.compile("[a-z ]+(\\d{1,2} [A-Za-z][a-z]{2} \\d{4})"); - private static final DateTimeFormatter DATE_FORMAT_WEBSITE_AFTER_2000 = DateTimeFormatter.ofPattern("d MMM yyyy", Locale.US); private static final Pattern DATE_FROM_WEBSITE_BEFORE_2000_PATTERN = Pattern.compile("[A-Za-z ]+? ([A-Za-z][a-z]{2,10} \\d{1,2}(th|st|nd|rd)?, \\d{4})\\.?"); + private static final Pattern WITHOUT_LETTERS_SPACE = Pattern.compile("[^0-9/]"); + + private static final DateTimeFormatter DATE_FORMAT_WEBSITE_AFTER_2000 = DateTimeFormatter.ofPattern("d MMM yyyy", Locale.US); private static final DateTimeFormatter DATE_FORMAT_WEBSITE_BEFORE_2000_LONG_MONTHS = DateTimeFormatter.ofPattern("MMMM d['th']['st']['nd']['rd'] yyyy", Locale.US); private static final DateTimeFormatter DATE_FORMAT_WEBSITE_BEFORE_2000_SHORT_MONTHS = DateTimeFormatter.ofPattern("MMM d['th']['st']['nd']['rd'] yyyy", Locale.US); private static final DateTimeFormatter DATE_FORMAT_BIBTEX = DateTimeFormatter.ISO_LOCAL_DATE; @@ -55,7 +57,7 @@ public IacrEprintFetcher(ImportFormatPreferences prefs) { @Override public Optional performSearchById(String identifier) throws FetcherException { - String identifierWithoutLettersAndSpaces = identifier.replaceAll("[^0-9/]", " ").trim(); + String identifierWithoutLettersAndSpaces = WITHOUT_LETTERS_SPACE.matcher(identifier).replaceAll(" ").trim(); if (!IDENTIFIER_PREDICATE.test(identifierWithoutLettersAndSpaces)) { throw new FetcherException(Localization.lang("Invalid identifier: '%0'.", identifier)); diff --git a/src/main/java/org/jabref/model/entry/identifier/DOI.java b/src/main/java/org/jabref/model/entry/identifier/DOI.java index 38c66733758..49c06bd8730 100644 --- a/src/main/java/org/jabref/model/entry/identifier/DOI.java +++ b/src/main/java/org/jabref/model/entry/identifier/DOI.java @@ -95,7 +95,9 @@ public DOI(String doi) { */ public static Optional parse(String doi) { try { - return Optional.of(new DOI(doi)); + String cleanedDOI = doi.trim(); + cleanedDOI = doi.replaceAll(" ", ""); + return Optional.of(new DOI(cleanedDOI)); } catch (IllegalArgumentException | NullPointerException e) { return Optional.empty(); } diff --git a/src/test/java/org/jabref/logic/importer/fetcher/ArXivTest.java b/src/test/java/org/jabref/logic/importer/fetcher/ArXivTest.java index a79142ffc34..3a438659b31 100644 --- a/src/test/java/org/jabref/logic/importer/fetcher/ArXivTest.java +++ b/src/test/java/org/jabref/logic/importer/fetcher/ArXivTest.java @@ -168,6 +168,11 @@ public void searchEntryByIdWith4DigitsAndPrefix() throws Exception { assertEquals(Optional.of(sliceTheoremPaper), finder.performSearchById("arXiv:1405.2249")); } + @Test + public void searchEntryByIdWith4DigitsAndPrefixAndNotTrimmed() throws Exception { + assertEquals(Optional.of(sliceTheoremPaper), finder.performSearchById("arXiv : 1405. 2249")); + } + @Test public void searchEntryByIdWith5Digits() throws Exception { assertEquals(Optional.of( @@ -186,4 +191,5 @@ public void searchIdentifierForSlicePaper() throws Exception { assertEquals(ArXivIdentifier.parse("1405.2249v1"), finder.findIdentifier(sliceTheoremPaper)); } + } diff --git a/src/test/java/org/jabref/logic/importer/fetcher/DoiFetcherTest.java b/src/test/java/org/jabref/logic/importer/fetcher/DoiFetcherTest.java index d268730212b..177d48b7123 100644 --- a/src/test/java/org/jabref/logic/importer/fetcher/DoiFetcherTest.java +++ b/src/test/java/org/jabref/logic/importer/fetcher/DoiFetcherTest.java @@ -48,6 +48,7 @@ public void setUp() { bibEntryDecker2007.setField("doi", "10.1109/icws.2007.59"); } + @Test public void testGetName() { assertEquals("DOI", fetcher.getName()); @@ -80,4 +81,10 @@ public void testPerformSearchInvalidDOI() { assertThrows(FetcherException.class, () -> fetcher.performSearchById("10.1002/9781118257517F")); } + + @Test + public void testPerformSearchNonTrimmedDOI() throws FetcherException { + Optional fetchedEntry = fetcher.performSearchById("http s://doi.org/ 10.1109 /ICWS .2007.59 "); + assertEquals(Optional.of(bibEntryDecker2007), fetchedEntry); + } } diff --git a/src/test/java/org/jabref/model/entry/identifier/DOITest.java b/src/test/java/org/jabref/model/entry/identifier/DOITest.java index 457b779ff75..e1e8636c1f1 100644 --- a/src/test/java/org/jabref/model/entry/identifier/DOITest.java +++ b/src/test/java/org/jabref/model/entry/identifier/DOITest.java @@ -143,4 +143,12 @@ public void findDoiInsideArbitraryText() { public void noDOIFoundInsideArbitraryText() { assertEquals(Optional.empty(), DOI.findInText("text without 28282 a doi")); } + + @Test + public void parseDOIWithWhiteSpace() { + String doiWithSpace = "https : / / doi.org / 10 .1109 /V LHCC.20 04.20"; + assertEquals("https://doi.org/10.1109/VLHCC.2004.20", DOI.parse(doiWithSpace).get().getURIAsASCIIString()); + + } + }