From 5d797b287fc1aea0ccd11a2ae2244b923ce63250 Mon Sep 17 00:00:00 2001 From: Christoph Date: Tue, 29 Nov 2016 21:04:31 +0100 Subject: [PATCH] Fix exporting via commandline in no gui mode (#2316) * Fix exporting via commandline in no gui mode Fix for #2273 Adjusted javadoc * Fixed changelog entry Remove string messages from requireNonNull --- CHANGELOG.md | 1 + .../net/sf/jabref/cli/ArgumentProcessor.java | 50 +++++++++---------- .../jabref/logic/exporter/ExportFormat.java | 13 +---- .../jabref/logic/exporter/IExportFormat.java | 8 +-- 4 files changed, 29 insertions(+), 43 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3461428a9b2..c05960f599d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `# - We fixed an issue when JabRef restores its session and a shared database was used: The error message "No suitable driver found" will not appear. - Update check now correctly notifies about new release if development version is used. [#2298](https://github.com/JabRef/jabref/issues/2298) - Fixed [#2311](https://github.com/JabRef/jabref/issues/2311): The DBLP fetcher has been rewritten and is working again. +- Fixed [#2273](https://github.com/JabRef/jabref/issues/2273): Export via commandline in no-gui mode is now working again. ### Removed diff --git a/src/main/java/net/sf/jabref/cli/ArgumentProcessor.java b/src/main/java/net/sf/jabref/cli/ArgumentProcessor.java index 4469ef5a6ff..76ab791dd07 100644 --- a/src/main/java/net/sf/jabref/cli/ArgumentProcessor.java +++ b/src/main/java/net/sf/jabref/cli/ArgumentProcessor.java @@ -52,17 +52,20 @@ import net.sf.jabref.preferences.SearchPreferences; import net.sf.jabref.shared.prefs.SharedDatabasePreferences; +import org.apache.commons.lang3.exception.ExceptionUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class ArgumentProcessor { + private static final Log LOGGER = LogFactory.getLog(ArgumentProcessor.class); + public enum Mode { - INITIAL_START, - REMOTE_START + INITIAL_START, REMOTE_START } + private final JabRefCLI cli; private final List parserResults; @@ -71,6 +74,7 @@ public enum Mode { private boolean noGUINeeded; + public ArgumentProcessor(String[] args, Mode startupMode) { cli = new JabRefCLI(args); this.startupMode = startupMode; @@ -170,8 +174,7 @@ private boolean exportMatches(List loaded) { BibDatabase dataBase = pr.getDatabase(); SearchPreferences searchPreferences = new SearchPreferences(Globals.prefs); - SearchQuery query = new SearchQuery(searchTerm, - searchPreferences.isCaseSensitive(), + SearchQuery query = new SearchQuery(searchTerm, searchPreferences.isCaseSensitive(), searchPreferences.isRegularExpression()); List matches = new DatabaseSearcher(query, dataBase).getMatches(); @@ -208,7 +211,7 @@ private boolean exportMatches(List loaded) { matches); } catch (Exception ex) { System.err.println(Localization.lang("Could not export file") + " '" + data[1] + "': " - + ex.getMessage()); + + ExceptionUtils.getStackTrace(ex)); } } } else { @@ -244,8 +247,7 @@ private List importAndOpenFiles() { boolean bibExtension = aLeftOver.toLowerCase(Locale.ENGLISH).endsWith("bib"); ParserResult pr = ParserResult.getNullResult(); if (bibExtension) { - pr = OpenDatabase.loadDatabase(aLeftOver, - Globals.prefs.getImportFormatPreferences()); + pr = OpenDatabase.loadDatabase(aLeftOver, Globals.prefs.getImportFormatPreferences()); } if (!bibExtension || (pr.isNullResult())) { @@ -296,12 +298,10 @@ private boolean generateAux(List loaded, String[] data) { try { System.out.println(Localization.lang("Saving") + ": " + subName); SavePreferences prefs = SavePreferences.loadForSaveFromPreferences(Globals.prefs); - BibDatabaseWriter databaseWriter = new BibtexDatabaseWriter<>( - FileSaveSession::new); + BibDatabaseWriter databaseWriter = new BibtexDatabaseWriter<>(FileSaveSession::new); Defaults defaults = new Defaults(BibDatabaseMode .fromPreference(Globals.prefs.getBoolean(JabRefPreferences.BIBLATEX_DEFAULT_MODE))); - SaveSession session = databaseWriter.saveDatabase(new BibDatabaseContext(newBase, defaults), - prefs); + SaveSession session = databaseWriter.saveDatabase(new BibDatabaseContext(newBase, defaults), prefs); // Show just a warning message if encoding did not work for all characters: if (!session.getWriter().couldEncodeAll()) { @@ -313,8 +313,7 @@ private boolean generateAux(List loaded, String[] data) { } session.commit(subName); } catch (SaveException ex) { - System.err.println( - Localization.lang("Could not save file.") + "\n" + ex.getLocalizedMessage()); + System.err.println(Localization.lang("Could not save file.") + "\n" + ex.getLocalizedMessage()); } notSavedMsg = true; @@ -339,8 +338,8 @@ private void exportFile(List loaded, String[] data) { try { System.out.println(Localization.lang("Saving") + ": " + data[0]); SavePreferences prefs = SavePreferences.loadForSaveFromPreferences(Globals.prefs); - Defaults defaults = new Defaults(BibDatabaseMode.fromPreference( - Globals.prefs.getBoolean(JabRefPreferences.BIBLATEX_DEFAULT_MODE))); + Defaults defaults = new Defaults(BibDatabaseMode + .fromPreference(Globals.prefs.getBoolean(JabRefPreferences.BIBLATEX_DEFAULT_MODE))); BibDatabaseWriter databaseWriter = new BibtexDatabaseWriter<>( FileSaveSession::new); SaveSession session = databaseWriter.saveDatabase( @@ -356,8 +355,7 @@ private void exportFile(List loaded, String[] data) { } session.commit(data[0]); } catch (SaveException ex) { - System.err.println( - Localization.lang("Could not save file.") + "\n" + ex.getLocalizedMessage()); + System.err.println(Localization.lang("Could not save file.") + "\n" + ex.getLocalizedMessage()); } } } else { @@ -389,10 +387,11 @@ private void exportFile(List loaded, String[] data) { format.performExport(pr.getDatabaseContext(), data[0], pr.getDatabaseContext().getMetaData().getEncoding() .orElse(Globals.prefs.getDefaultEncoding()), - null); + pr.getDatabaseContext().getDatabase().getEntries()); } catch (Exception ex) { + System.err.println(Localization.lang("Could not export file") + " '" + data[0] + "': " - + ex.getMessage()); + + ExceptionUtils.getStackTrace(ex)); } } @@ -456,8 +455,7 @@ private void regenerateBibtexKeys(List loaded) { // try to make a new label BibtexKeyPatternUtil.makeLabel( metaData.getCiteKeyPattern(Globals.prefs.getBibtexKeyPatternPreferences().getKeyPattern()), - database, entry, - Globals.prefs.getBibtexKeyPatternPreferences()); + database, entry, Globals.prefs.getBibtexKeyPatternPreferences()); } } else { LOGGER.info(Localization.lang("No meta data present in BIB_file. Cannot regenerate BibTeX keys")); @@ -546,9 +544,7 @@ private static Optional importFile(String argument) { try { file = new URLDownload(address).downloadToTemporaryFile(); } catch (IOException e) { - System.err.println( - Localization.lang("Problem downloading from %1", address) + - e.getLocalizedMessage()); + System.err.println(Localization.lang("Problem downloading from %1", address) + e.getLocalizedMessage()); return Optional.empty(); } } else { @@ -569,7 +565,7 @@ private static Optional importFile(String argument) { Optional importResult = importFile(file, importFormat); importResult.ifPresent(result -> { OutputPrinter printer = new SystemOutputPrinter(); - if(result.hasWarnings()) { + if (result.hasWarnings()) { printer.showMessage(result.getErrorMessage()); } }); @@ -593,8 +589,8 @@ private static Optional importFile(Path file, String importFormat) return Optional.of(importResult.parserResult); } } catch (ImportException ex) { - System.err.println( - Localization.lang("Error opening file") + " '" + file + "': " + ex.getLocalizedMessage()); + System.err + .println(Localization.lang("Error opening file") + " '" + file + "': " + ex.getLocalizedMessage()); return Optional.empty(); } } diff --git a/src/main/java/net/sf/jabref/logic/exporter/ExportFormat.java b/src/main/java/net/sf/jabref/logic/exporter/ExportFormat.java index e8e647a4c5d..2a0a069f39e 100644 --- a/src/main/java/net/sf/jabref/logic/exporter/ExportFormat.java +++ b/src/main/java/net/sf/jabref/logic/exporter/ExportFormat.java @@ -179,18 +179,7 @@ private Reader getReader(String filename) throws IOException { return reader; } - /** - * Perform the export of {@code database}. - * - * @param databaseContext the database to export from. - * @param file the file to write the resulting export to - * @param encoding The encoding of the database - * @param entries Contains all entries that should be exported. - * @throws IOException if a problem occurred while trying to write to {@code writer} - * or read from required resources. - * @throws Exception if any other error occurred during export. - * @see net.sf.jabref.logic.exporter.IExportFormat#performExport(BibDatabaseContext, String, Charset, List) - */ + @Override public void performExport(final BibDatabaseContext databaseContext, final String file, final Charset encoding, List entries) throws Exception { diff --git a/src/main/java/net/sf/jabref/logic/exporter/IExportFormat.java b/src/main/java/net/sf/jabref/logic/exporter/IExportFormat.java index d9dad6e73ad..5d8536cab7c 100644 --- a/src/main/java/net/sf/jabref/logic/exporter/IExportFormat.java +++ b/src/main/java/net/sf/jabref/logic/exporter/IExportFormat.java @@ -31,8 +31,8 @@ public interface IExportFormat { * @param encoding * The encoding to use. * @param entries - * (may be null) A list containing all entries that - * should be exported. If null, all entries will be exported. + * A list containing all entries that + * should be exported. The list of entries must be non null * @throws Exception */ void performExport(BibDatabaseContext databaseContext, String file, Charset encoding, List entries) @@ -45,8 +45,8 @@ void performExport(BibDatabaseContext databaseContext, String file, Charset enco * @param databaseContext the database to export from. * @param file the Path to the file to write to.The path should be an java.nio.Path * @param encoding The encoding to use. - * @param entries (may be null) A list containing all entries that - * should be exported. If null, all entries will be exported. + * @param entries A list containing all entries that + * should be exported. The list of entries must be non null * @throws Exception */ void performExport(BibDatabaseContext databaseContext, Path file, Charset encoding, List entries)