Skip to content

Commit

Permalink
Fix #410 Doi constructor throws IllegalArgumentException when passing…
Browse files Browse the repository at this point in the history
… arbitraty text. Replace by builder method that returns an Optional
  • Loading branch information
stefan-kolb committed Mar 10, 2016
1 parent 53ad703 commit 74bca22
Showing 1 changed file with 13 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ of the License, or (at your option) any later version.
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -209,12 +210,11 @@ private static String streamlineTitle(String title) {

@Override
public List<BibEntry> importEntries(InputStream in, OutputPrinter status) throws IOException {
final ArrayList<BibEntry> res = new ArrayList<>(1);
final ArrayList<BibEntry> result = new ArrayList<>(1);

try (PDDocument document = PDDocument.load(in)) {
if (document.isEncrypted()) {
LOGGER.error("Encrypted documents are not supported");
//return res;
}

PDFTextStripper stripper = new PDFTextStripper();
Expand All @@ -226,12 +226,9 @@ public List<BibEntry> importEntries(InputStream in, OutputPrinter status) throws
stripper.writeText(document, writer);
String textResult = writer.toString();

String doi = new DOI(textResult).getDOI();
if (doi.length() < textResult.length()) {
// A Doi was found in the text
// We do NO parsing of the text, but use the Doi fetcher

ImportInspector iI = new ImportInspector() {
Optional<DOI> doi = DOI.build(textResult);
if (doi.isPresent()) {
ImportInspector inspector = new ImportInspector() {

@Override
public void toFront() {
Expand All @@ -246,15 +243,13 @@ public void setProgress(int current, int max) {
@Override
public void addEntry(BibEntry entry) {
// add the entry to the result object
res.add(entry);
result.add(entry);
}
};
PdfContentImporter.doiToBibTeXFetcher.processQuery(doi, iI, status);
if (!res.isEmpty()) {
// if something has been found, return the result
return res;
} else {
// otherwise, we just parse the PDF

doiToBibTeXFetcher.processQuery(doi.get().getDOI(), inspector, status);
if (!result.isEmpty()) {
return result;
}
}

Expand All @@ -275,7 +270,7 @@ public void addEntry(BibEntry entry) {
if (i >= split.length) {
// PDF could not be parsed or is empty
// return empty list
return res;
return result;
}

curString = split[i];
Expand Down Expand Up @@ -530,7 +525,7 @@ public void addEntry(BibEntry entry) {

entry.setField("review", textResult);

res.add(entry);
result.add(entry);
} catch (NoClassDefFoundError e) {
if ("org/bouncycastle/jce/provider/BouncyCastleProvider".equals(e.getMessage())) {
status.showMessage(Localization.lang("Java Bouncy Castle library not found. Please download and install it. For more information see http://www.bouncycastle.org/."));
Expand All @@ -540,7 +535,7 @@ public void addEntry(BibEntry entry) {
} catch (IOException e) {
LOGGER.error("Could not load document", e);
}
return res;
return result;
}

/**
Expand Down

0 comments on commit 74bca22

Please sign in to comment.