use of org.jabref.model.Defaults in project jabref by JabRef.
the class BibtexDatabaseWriterTest method setUp.
@Before
public void setUp() {
// Write to a string instead of to a file
databaseWriter = new BibtexDatabaseWriter<>(StringSaveSession::new);
database = new BibDatabase();
metaData = new MetaData();
bibtexContext = new BibDatabaseContext(database, metaData, new Defaults(BibDatabaseMode.BIBTEX));
importFormatPreferences = mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS);
}
use of org.jabref.model.Defaults in project jabref by JabRef.
the class BibtexDatabaseWriterTest method roundtripWithUserCommentAndEntryChange.
@Test
public void roundtripWithUserCommentAndEntryChange() throws Exception {
Path testBibtexFile = Paths.get("src/test/resources/testbib/bibWithUserComments.bib");
Charset encoding = StandardCharsets.UTF_8;
ParserResult result = new BibtexParser(importFormatPreferences).parse(Importer.getReader(testBibtexFile, encoding));
BibEntry entry = result.getDatabase().getEntryByKey("1137631").get();
entry.setField("author", "Mr. Author");
SavePreferences preferences = new SavePreferences().withEncoding(encoding).withSaveInOriginalOrder(true);
BibDatabaseContext context = new BibDatabaseContext(result.getDatabase(), result.getMetaData(), new Defaults(BibDatabaseMode.BIBTEX));
StringSaveSession session = databaseWriter.savePartOfDatabase(context, result.getDatabase().getEntries(), preferences);
try (Scanner scanner = new Scanner(Paths.get("src/test/resources/testbib/bibWithUserCommentAndEntryChange.bib"), encoding.name())) {
assertEquals(scanner.useDelimiter("\\A").next(), session.getStringValue());
}
}
use of org.jabref.model.Defaults in project jabref by JabRef.
the class BibtexDatabaseWriterTest method roundtripWithContentSelectorsAndUmlauts.
@Test
public void roundtripWithContentSelectorsAndUmlauts() throws IOException, SaveException {
String fileContent = "% Encoding: UTF-8" + OS.NEWLINE + OS.NEWLINE + "@Comment{jabref-meta: selector_journal:Test {\\\\\"U}mlaut;}" + OS.NEWLINE;
Charset encoding = StandardCharsets.UTF_8;
ParserResult firstParse = new BibtexParser(importFormatPreferences).parse(new StringReader(fileContent));
SavePreferences preferences = new SavePreferences().withEncoding(encoding).withSaveInOriginalOrder(true);
BibDatabaseContext context = new BibDatabaseContext(firstParse.getDatabase(), firstParse.getMetaData(), new Defaults(BibDatabaseMode.BIBTEX));
StringSaveSession session = databaseWriter.savePartOfDatabase(context, firstParse.getDatabase().getEntries(), preferences);
assertEquals(fileContent, session.getStringValue());
}
use of org.jabref.model.Defaults in project jabref by JabRef.
the class BibtexDatabaseWriterTest method roundtripWithUserCommentBeforeStringAndChange.
@Test
public void roundtripWithUserCommentBeforeStringAndChange() throws Exception {
Path testBibtexFile = Paths.get("src/test/resources/testbib/complex.bib");
Charset encoding = StandardCharsets.UTF_8;
ParserResult result = new BibtexParser(importFormatPreferences).parse(Importer.getReader(testBibtexFile, encoding));
for (BibtexString string : result.getDatabase().getStringValues()) {
// Mark them as changed
string.setContent(string.getContent());
}
SavePreferences preferences = new SavePreferences().withEncoding(encoding).withSaveInOriginalOrder(true);
BibDatabaseContext context = new BibDatabaseContext(result.getDatabase(), result.getMetaData(), new Defaults(BibDatabaseMode.BIBTEX));
StringSaveSession session = databaseWriter.savePartOfDatabase(context, result.getDatabase().getEntries(), preferences);
try (Scanner scanner = new Scanner(testBibtexFile, encoding.name())) {
assertEquals(scanner.useDelimiter("\\A").next(), session.getStringValue());
}
}
use of org.jabref.model.Defaults in project jabref by JabRef.
the class ArgumentProcessor method exportFile.
private void exportFile(List<ParserResult> loaded, String[] data) {
if (data.length == 1) {
// format to the given file.
if (!loaded.isEmpty()) {
ParserResult pr = loaded.get(loaded.size() - 1);
if (!pr.isInvalid()) {
try {
System.out.println(Localization.lang("Saving") + ": " + data[0]);
SavePreferences prefs = SavePreferences.loadForSaveFromPreferences(Globals.prefs);
Defaults defaults = new Defaults(Globals.prefs.getDefaultBibDatabaseMode());
BibDatabaseWriter<SaveSession> databaseWriter = new BibtexDatabaseWriter<>(FileSaveSession::new);
SaveSession session = databaseWriter.saveDatabase(new BibDatabaseContext(pr.getDatabase(), pr.getMetaData(), defaults), prefs);
// Show just a warning message if encoding did not work for all characters:
if (!session.getWriter().couldEncodeAll()) {
System.err.println(Localization.lang("Warning") + ": " + Localization.lang("The chosen encoding '%0' could not encode the following characters:", session.getEncoding().displayName()) + " " + session.getWriter().getProblemCharacters());
}
session.commit(data[0]);
} catch (SaveException ex) {
System.err.println(Localization.lang("Could not save file.") + "\n" + ex.getLocalizedMessage());
}
}
} else {
System.err.println(Localization.lang("The output option depends on a valid import option."));
}
} else if (data.length == 2) {
// This signals that the latest import should be stored in the given
// format to the given file.
ParserResult pr = loaded.get(loaded.size() - 1);
// Set the global variable for this database's file directory before exporting,
// so formatters can resolve linked files correctly.
// (This is an ugly hack!)
File theFile = pr.getFile().get();
if (!theFile.isAbsolute()) {
theFile = theFile.getAbsoluteFile();
}
BibDatabaseContext databaseContext = pr.getDatabaseContext();
databaseContext.setDatabaseFile(theFile);
Globals.prefs.fileDirForDatabase = databaseContext.getFileDirectories(Globals.prefs.getFileDirectoryPreferences());
System.out.println(Localization.lang("Exporting") + ": " + data[0]);
IExportFormat format = ExportFormats.getExportFormat(data[1]);
if (format == null) {
System.err.println(Localization.lang("Unknown export format") + ": " + data[1]);
} else {
// We have an ExportFormat instance:
try {
format.performExport(pr.getDatabaseContext(), data[0], pr.getDatabaseContext().getMetaData().getEncoding().orElse(Globals.prefs.getDefaultEncoding()), pr.getDatabaseContext().getDatabase().getEntries());
} catch (Exception ex) {
System.err.println(Localization.lang("Could not export file") + " '" + data[0] + "': " + ExceptionUtils.getStackTrace(ex));
}
}
}
}
Aggregations