-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5196 from JabRef/ottobib
- Loading branch information
Showing
7 changed files
with
193 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/main/java/org/jabref/logic/importer/fetcher/IsbnViaOttoBibFetcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package org.jabref.logic.importer.fetcher; | ||
|
||
import java.io.IOException; | ||
import java.net.MalformedURLException; | ||
import java.net.URISyntaxException; | ||
import java.net.URL; | ||
import java.util.Optional; | ||
|
||
import org.jabref.logic.importer.FetcherException; | ||
import org.jabref.logic.importer.ImportFormatPreferences; | ||
import org.jabref.logic.importer.ParseException; | ||
import org.jabref.logic.importer.fileformat.BibtexParser; | ||
import org.jabref.logic.net.URLDownload; | ||
import org.jabref.model.entry.BibEntry; | ||
import org.jabref.model.strings.StringUtil; | ||
import org.jabref.model.util.DummyFileUpdateMonitor; | ||
|
||
import org.jsoup.Jsoup; | ||
import org.jsoup.nodes.Document; | ||
import org.jsoup.nodes.Element; | ||
|
||
/** | ||
* Fetcher for ISBN using https://www.ottobib.com | ||
*/ | ||
public class IsbnViaOttoBibFetcher extends AbstractIsbnFetcher { | ||
|
||
private static final String BASE_URL = "https://www.ottobib.com/isbn/"; | ||
|
||
public IsbnViaOttoBibFetcher(ImportFormatPreferences importFormatPreferences) { | ||
super(importFormatPreferences); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "ISBN (OttoBib)"; | ||
} | ||
|
||
/** | ||
* @return null, because the identifier is passed using form data. This method is not used. | ||
*/ | ||
@Override | ||
public URL getURLForID(String identifier) throws URISyntaxException, MalformedURLException, FetcherException { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Optional<BibEntry> performSearchById(String identifier) throws FetcherException { | ||
if (StringUtil.isBlank(identifier)) { | ||
return Optional.empty(); | ||
} | ||
|
||
this.ensureThatIsbnIsValid(identifier); | ||
|
||
Document html; | ||
try { | ||
html = Jsoup.connect(BASE_URL + identifier + "/bibtex").userAgent(URLDownload.USER_AGENT).get(); | ||
} catch (IOException e) { | ||
throw new FetcherException("Could not ", e); | ||
} | ||
Element textArea = html.select("textarea").first(); | ||
Optional<BibEntry> entry = Optional.empty(); | ||
try { | ||
entry = BibtexParser.singleFromString(textArea.text(), importFormatPreferences, new DummyFileUpdateMonitor()); | ||
} catch (ParseException e) { | ||
throw new FetcherException("An internal parser error occurred", e); | ||
} | ||
return entry; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
src/test/java/org/jabref/logic/importer/fetcher/IsbnViaOttoBibFetcherTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package org.jabref.logic.importer.fetcher; | ||
|
||
import java.util.Optional; | ||
|
||
import org.jabref.logic.importer.FetcherException; | ||
import org.jabref.logic.importer.ImportFormatPreferences; | ||
import org.jabref.model.entry.BibEntry; | ||
import org.jabref.model.entry.StandardEntryType; | ||
import org.jabref.model.entry.field.StandardField; | ||
import org.jabref.testutils.category.FetcherTest; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Answers; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.mock; | ||
|
||
@FetcherTest | ||
public class IsbnViaOttoBibFetcherTest extends AbstractIsbnFetcherTest { | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
bibEntry = new BibEntry(); | ||
bibEntry.setType(StandardEntryType.Book); | ||
bibEntry.setCiteKey("bloch2008effective"); | ||
bibEntry.setField(StandardField.TITLE, "Effective Java"); | ||
bibEntry.setField(StandardField.PUBLISHER, "Addison-Wesley"); | ||
bibEntry.setField(StandardField.YEAR, "2008"); | ||
bibEntry.setField(StandardField.AUTHOR, "Bloch, Joshua"); | ||
bibEntry.setField(StandardField.ISBN, "9780321356680"); | ||
bibEntry.setField(StandardField.ADDRESS, "Upper Saddle River, NJ"); | ||
|
||
fetcher = new IsbnViaOttoBibFetcher(mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS)); | ||
} | ||
|
||
@Test | ||
@Override | ||
public void testName() { | ||
assertEquals("ISBN (OttoBib)", fetcher.getName()); | ||
} | ||
|
||
@Test | ||
@Override | ||
public void testHelpPage() { | ||
assertEquals("ISBNtoBibTeX", fetcher.getHelpPage().get().getPageName()); | ||
} | ||
|
||
@Test | ||
@Override | ||
public void searchByIdSuccessfulWithShortISBN() throws FetcherException { | ||
Optional<BibEntry> fetchedEntry = fetcher.performSearchById("0321356683"); | ||
bibEntry.setField(StandardField.ISBN, "0321356683"); | ||
assertEquals(Optional.of(bibEntry), fetchedEntry); | ||
} | ||
|
||
@Test | ||
@Override | ||
public void searchByIdSuccessfulWithLongISBN() throws FetcherException { | ||
Optional<BibEntry> fetchedEntry = fetcher.performSearchById("9780321356680"); | ||
bibEntry.setField(StandardField.ISBN, "9780321356680"); | ||
assertEquals(Optional.of(bibEntry), fetchedEntry); | ||
} | ||
|
||
@Test | ||
@Override | ||
public void authorsAreCorrectlyFormatted() throws Exception { | ||
BibEntry bibEntry = new BibEntry(); | ||
bibEntry.setType(StandardEntryType.Book); | ||
bibEntry.setCiteKey("dumas2018fundamentals"); | ||
bibEntry.setField(StandardField.TITLE, "Fundamentals of business process management"); | ||
bibEntry.setField(StandardField.PUBLISHER, "Springer"); | ||
bibEntry.setField(StandardField.AUTHOR, "Dumas, Marlon"); | ||
bibEntry.setField(StandardField.ADDRESS, "Berlin, Germany"); | ||
bibEntry.setField(StandardField.ISBN, "9783662565094"); | ||
bibEntry.setField(StandardField.YEAR, "2018"); | ||
|
||
Optional<BibEntry> fetchedEntry = fetcher.performSearchById("978-3-662-56509-4"); | ||
assertEquals(Optional.of(bibEntry), fetchedEntry); | ||
} | ||
|
||
@Test | ||
public void testISBNNotAvaiableOnEbookDeOrChimbori() throws Exception { | ||
bibEntry = new BibEntry(); | ||
bibEntry.setType(StandardEntryType.Book); | ||
bibEntry.setCiteKey("denis2012les"); | ||
bibEntry.setField(StandardField.TITLE, "Les mots du passé : roman"); | ||
bibEntry.setField(StandardField.PUBLISHER, "Éd. les Nouveaux auteurs"); | ||
bibEntry.setField(StandardField.ADDRESS, "Paris"); | ||
bibEntry.setField(StandardField.YEAR, "2012"); | ||
bibEntry.setField(StandardField.AUTHOR, "Denis, "); | ||
bibEntry.setField(StandardField.ISBN, "9782819502746"); | ||
|
||
Optional<BibEntry> fetchedEntry = fetcher.performSearchById("978-2-8195-02746"); | ||
assertEquals(Optional.of(bibEntry), fetchedEntry); | ||
|
||
} | ||
|
||
} |