use of org.jabref.logic.exporter.BibtexDatabaseWriter in project jabref by JabRef.
the class Benchmarks method init.
@Setup
public void init() throws Exception {
Globals.prefs = JabRefPreferences.getInstance();
Random randomizer = new Random();
for (int i = 0; i < 1000; i++) {
BibEntry entry = new BibEntry();
entry.setCiteKey("id" + i);
entry.setField("title", "This is my title " + i);
entry.setField("author", "Firstname Lastname and FirstnameA LastnameA and FirstnameB LastnameB" + i);
entry.setField("journal", "Journal Title " + i);
entry.setField("keyword", "testkeyword");
entry.setField("year", "1" + i);
entry.setField("rnd", "2" + randomizer.nextInt());
database.insertEntry(entry);
}
BibtexDatabaseWriter<StringSaveSession> databaseWriter = new BibtexDatabaseWriter<>(StringSaveSession::new);
StringSaveSession saveSession = databaseWriter.savePartOfDatabase(new BibDatabaseContext(database, new MetaData(), new Defaults()), database.getEntries(), new SavePreferences());
bibtexString = saveSession.getStringValue();
latexConversionString = "{A} \\textbf{bold} approach {\\it to} ${{\\Sigma}}{\\Delta}$ modulator \\textsuperscript{2} \\$";
htmlConversionString = "<b>Österreich</b> – & characters ⪢ <i>italic</i>";
}
use of org.jabref.logic.exporter.BibtexDatabaseWriter in project jabref by JabRef.
the class BasePanel method saveDatabase.
private boolean saveDatabase(File file, boolean selectedOnly, Charset enc, SavePreferences.DatabaseSaveType saveType) throws SaveException {
SaveSession session;
frame.block();
final String SAVE_DATABASE = Localization.lang("Save library");
try {
SavePreferences prefs = SavePreferences.loadForSaveFromPreferences(Globals.prefs).withEncoding(enc).withSaveType(saveType);
BibtexDatabaseWriter<SaveSession> databaseWriter = new BibtexDatabaseWriter<>(FileSaveSession::new);
if (selectedOnly) {
session = databaseWriter.savePartOfDatabase(bibDatabaseContext, mainTable.getSelectedEntries(), prefs);
} else {
session = databaseWriter.saveDatabase(bibDatabaseContext, prefs);
}
registerUndoableChanges(session);
}// FIXME: not sure if this is really thrown anywhere
catch (UnsupportedCharsetException ex) {
JOptionPane.showMessageDialog(frame, Localization.lang("Could not save file.") + ' ' + Localization.lang("Character encoding '%0' is not supported.", enc.displayName()), SAVE_DATABASE, JOptionPane.ERROR_MESSAGE);
throw new SaveException("rt");
} catch (SaveException ex) {
if (ex.specificEntry()) {
// Error occurred during processing of the entry. Highlight it:
highlightEntry(ex.getEntry());
showEntry(ex.getEntry());
} else {
LOGGER.warn("Could not save", ex);
}
JOptionPane.showMessageDialog(frame, Localization.lang("Could not save file.") + "\n" + ex.getMessage(), SAVE_DATABASE, JOptionPane.ERROR_MESSAGE);
throw new SaveException("rt");
} finally {
frame.unblock();
}
boolean commit = true;
if (!session.getWriter().couldEncodeAll()) {
FormBuilder builder = FormBuilder.create().layout(new FormLayout("left:pref, 4dlu, fill:pref", "pref, 4dlu, pref"));
JTextArea ta = new JTextArea(session.getWriter().getProblemCharacters());
ta.setEditable(false);
builder.add(Localization.lang("The chosen encoding '%0' could not encode the following characters:", session.getEncoding().displayName())).xy(1, 1);
builder.add(ta).xy(3, 1);
builder.add(Localization.lang("What do you want to do?")).xy(1, 3);
String tryDiff = Localization.lang("Try different encoding");
int answer = JOptionPane.showOptionDialog(frame, builder.getPanel(), SAVE_DATABASE, JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, null, new String[] { Localization.lang("Save"), tryDiff, Localization.lang("Cancel") }, tryDiff);
if (answer == JOptionPane.NO_OPTION) {
// The user wants to use another encoding.
Object choice = JOptionPane.showInputDialog(frame, Localization.lang("Select encoding"), SAVE_DATABASE, JOptionPane.QUESTION_MESSAGE, null, Encodings.ENCODINGS_DISPLAYNAMES, enc);
if (choice == null) {
commit = false;
} else {
Charset newEncoding = Charset.forName((String) choice);
return saveDatabase(file, selectedOnly, newEncoding, saveType);
}
} else if (answer == JOptionPane.CANCEL_OPTION) {
commit = false;
}
}
if (commit) {
session.commit(file.toPath());
// Make sure to remember which encoding we used.
this.bibDatabaseContext.getMetaData().setEncoding(enc);
} else {
session.cancel();
}
return commit;
}
use of org.jabref.logic.exporter.BibtexDatabaseWriter in project jabref by JabRef.
the class BackupManager method performBackup.
private void performBackup(Path backupPath) {
try {
Charset charset = bibDatabaseContext.getMetaData().getEncoding().orElse(preferences.getDefaultEncoding());
SavePreferences savePreferences = SavePreferences.loadForSaveFromPreferences(preferences).withEncoding(charset).withMakeBackup(false);
new BibtexDatabaseWriter<>(FileSaveSession::new).saveDatabase(bibDatabaseContext, savePreferences).commit(backupPath);
} catch (SaveException e) {
LOGGER.error("Error while saving file.", e);
}
}
Aggregations