|
| 1 | +package org.jabref.toolkit.cli; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.OutputStreamWriter; |
| 5 | +import java.nio.charset.StandardCharsets; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Optional; |
| 9 | +import java.util.concurrent.Callable; |
| 10 | + |
| 11 | +import org.jabref.logic.exporter.BibDatabaseWriter; |
| 12 | +import org.jabref.logic.importer.FetcherException; |
| 13 | +import org.jabref.logic.importer.fetcher.CrossRef; |
| 14 | +import org.jabref.logic.l10n.Localization; |
| 15 | +import org.jabref.model.database.BibDatabase; |
| 16 | +import org.jabref.model.database.BibDatabaseContext; |
| 17 | +import org.jabref.model.entry.BibEntry; |
| 18 | +import org.jabref.model.entry.identifier.DOI; |
| 19 | + |
| 20 | +import org.slf4j.Logger; |
| 21 | +import org.slf4j.LoggerFactory; |
| 22 | +import picocli.CommandLine; |
| 23 | +import picocli.CommandLine.Command; |
| 24 | +import picocli.CommandLine.Parameters; |
| 25 | + |
| 26 | +@Command(name = "doi-to-bibtex", description = "Converts a DOI to BibTeX") |
| 27 | +public class DoiToBibtex implements Callable<Integer> { |
| 28 | + |
| 29 | + private static final Logger LOGGER = LoggerFactory.getLogger(DoiToBibtex.class); |
| 30 | + |
| 31 | + @CommandLine.ParentCommand |
| 32 | + private ArgumentProcessor argumentProcessor; |
| 33 | + |
| 34 | + @CommandLine.Mixin |
| 35 | + private ArgumentProcessor.SharedOptions sharedOptions = new ArgumentProcessor.SharedOptions(); |
| 36 | + |
| 37 | + @Parameters(paramLabel = "DOI", description = "one or more DOIs to fetch", arity = "1..*") |
| 38 | + private String[] dois; |
| 39 | + |
| 40 | + @Override |
| 41 | + public Integer call() { |
| 42 | + CrossRef fetcher = new CrossRef(); |
| 43 | + List<BibEntry> entries = new ArrayList<>(dois.length); |
| 44 | + |
| 45 | + for (String doiString : dois) { |
| 46 | + Optional<DOI> doiParsed = DOI.parse(doiString); |
| 47 | + if (doiParsed.isEmpty()) { |
| 48 | + LOGGER.warn("Skipped DOI {}, because it is not a valid DOI string", doiString); |
| 49 | + System.out.println(Localization.lang("DOI %0 is invalid", doiString)); |
| 50 | + System.err.println(); |
| 51 | + continue; |
| 52 | + } |
| 53 | + Optional<BibEntry> entry; |
| 54 | + try { |
| 55 | + entry = fetcher.performSearchById(doiParsed.get().asString()); |
| 56 | + } catch (FetcherException e) { |
| 57 | + LOGGER.error("Could not fetch BibTeX based on DOI", e); |
| 58 | + System.err.print(Localization.lang("No data was found for the identifier")); |
| 59 | + System.err.println(" - " + doiString); |
| 60 | + System.err.println(e.getLocalizedMessage()); |
| 61 | + System.err.println(); |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + if (entry.isEmpty()) { |
| 66 | + LOGGER.error("Could not fetch BibTeX based on DOI - entry is empty"); |
| 67 | + System.err.print(Localization.lang("No data was found for the identifier")); |
| 68 | + System.err.println(" - " + doiString); |
| 69 | + System.err.println(); |
| 70 | + continue; |
| 71 | + } |
| 72 | + |
| 73 | + entries.add(entry.get()); |
| 74 | + } |
| 75 | + |
| 76 | + try (OutputStreamWriter writer = new OutputStreamWriter(System.out, StandardCharsets.UTF_8)) { |
| 77 | + BibDatabaseContext context = new BibDatabaseContext(new BibDatabase(entries)); |
| 78 | + BibDatabaseWriter bibWriter = new BibDatabaseWriter(writer, context, argumentProcessor.cliPreferences); |
| 79 | + bibWriter.writeDatabase(context); |
| 80 | + } catch (IOException e) { |
| 81 | + LOGGER.error("Could not write BibTeX", e); |
| 82 | + System.err.println(Localization.lang("Unable to write to %0.", "stdout")); |
| 83 | + return 1; |
| 84 | + } |
| 85 | + return 0; |
| 86 | + } |
| 87 | +} |
0 commit comments