Skip to content

Commit

Permalink
Fix exporting via commandline in no gui mode
Browse files Browse the repository at this point in the history
Fix for #2273
Adjusted javadoc
  • Loading branch information
Siedlerchr committed Nov 28, 2016
1 parent a3389cf commit 629a0fd
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 45 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,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 modus is now working again.

### Removed

Expand Down
50 changes: 23 additions & 27 deletions src/main/java/net/sf/jabref/cli/ArgumentProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<ParserResult> parserResults;
Expand All @@ -71,6 +74,7 @@ public enum Mode {

private boolean noGUINeeded;


public ArgumentProcessor(String[] args, Mode startupMode) {
cli = new JabRefCLI(args);
this.startupMode = startupMode;
Expand Down Expand Up @@ -170,8 +174,7 @@ private boolean exportMatches(List<ParserResult> 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<BibEntry> matches = new DatabaseSearcher(query, dataBase).getMatches();

Expand Down Expand Up @@ -208,7 +211,7 @@ private boolean exportMatches(List<ParserResult> loaded) {
matches);
} catch (Exception ex) {
System.err.println(Localization.lang("Could not export file") + " '" + data[1] + "': "
+ ex.getMessage());
+ ExceptionUtils.getStackTrace(ex));
}
}
} else {
Expand Down Expand Up @@ -244,8 +247,7 @@ private List<ParserResult> 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())) {
Expand Down Expand Up @@ -296,12 +298,10 @@ private boolean generateAux(List<ParserResult> loaded, String[] data) {
try {
System.out.println(Localization.lang("Saving") + ": " + subName);
SavePreferences prefs = SavePreferences.loadForSaveFromPreferences(Globals.prefs);
BibDatabaseWriter<SaveSession> databaseWriter = new BibtexDatabaseWriter<>(
FileSaveSession::new);
BibDatabaseWriter<SaveSession> 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()) {
Expand All @@ -313,8 +313,7 @@ private boolean generateAux(List<ParserResult> 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;
Expand All @@ -339,8 +338,8 @@ private void exportFile(List<ParserResult> 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<SaveSession> databaseWriter = new BibtexDatabaseWriter<>(
FileSaveSession::new);
SaveSession session = databaseWriter.saveDatabase(
Expand All @@ -356,8 +355,7 @@ private void exportFile(List<ParserResult> 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 {
Expand Down Expand Up @@ -389,10 +387,11 @@ private void exportFile(List<ParserResult> 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));
}
}

Expand Down Expand Up @@ -456,8 +455,7 @@ private void regenerateBibtexKeys(List<ParserResult> 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"));
Expand Down Expand Up @@ -546,9 +544,7 @@ private static Optional<ParserResult> 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 {
Expand All @@ -569,7 +565,7 @@ private static Optional<ParserResult> importFile(String argument) {
Optional<ParserResult> importResult = importFile(file, importFormat);
importResult.ifPresent(result -> {
OutputPrinter printer = new SystemOutputPrinter();
if(result.hasWarnings()) {
if (result.hasWarnings()) {
printer.showMessage(result.getErrorMessage());
}
});
Expand All @@ -593,8 +589,8 @@ private static Optional<ParserResult> 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();
}
}
Expand Down
17 changes: 3 additions & 14 deletions src/main/java/net/sf/jabref/logic/exporter/ExportFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,23 +179,12 @@ 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<BibEntry> entries) throws Exception {
Objects.requireNonNull(databaseContext);
Objects.requireNonNull(entries);
Objects.requireNonNull(databaseContext, "Database Context must not be null!");
Objects.requireNonNull(entries, "Entries must not be null!");
if (entries.isEmpty()) { // Do not export if no entries to export -- avoids exports with only template text
return;
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/net/sf/jabref/logic/exporter/IExportFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<BibEntry> entries)
Expand All @@ -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<BibEntry> entries)
Expand Down

0 comments on commit 629a0fd

Please sign in to comment.