Search in sources :

Example 16 with Defaults

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);
}
Also used : Defaults(org.jabref.model.Defaults) MetaData(org.jabref.model.metadata.MetaData) ImportFormatPreferences(org.jabref.logic.importer.ImportFormatPreferences) BibDatabase(org.jabref.model.database.BibDatabase) BibDatabaseContext(org.jabref.model.database.BibDatabaseContext) Before(org.junit.Before)

Example 17 with Defaults

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());
    }
}
Also used : Path(java.nio.file.Path) ParserResult(org.jabref.logic.importer.ParserResult) BibEntry(org.jabref.model.entry.BibEntry) Scanner(java.util.Scanner) Defaults(org.jabref.model.Defaults) BibtexParser(org.jabref.logic.importer.fileformat.BibtexParser) Charset(java.nio.charset.Charset) BibDatabaseContext(org.jabref.model.database.BibDatabaseContext) Test(org.junit.Test)

Example 18 with Defaults

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());
}
Also used : ParserResult(org.jabref.logic.importer.ParserResult) Defaults(org.jabref.model.Defaults) BibtexParser(org.jabref.logic.importer.fileformat.BibtexParser) StringReader(java.io.StringReader) Charset(java.nio.charset.Charset) BibtexString(org.jabref.model.entry.BibtexString) BibDatabaseContext(org.jabref.model.database.BibDatabaseContext) Test(org.junit.Test)

Example 19 with Defaults

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());
    }
}
Also used : Path(java.nio.file.Path) ParserResult(org.jabref.logic.importer.ParserResult) Scanner(java.util.Scanner) Defaults(org.jabref.model.Defaults) BibtexParser(org.jabref.logic.importer.fileformat.BibtexParser) Charset(java.nio.charset.Charset) BibtexString(org.jabref.model.entry.BibtexString) BibDatabaseContext(org.jabref.model.database.BibDatabaseContext) Test(org.junit.Test)

Example 20 with Defaults

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));
            }
        }
    }
}
Also used : ParserResult(org.jabref.logic.importer.ParserResult) SaveException(org.jabref.logic.exporter.SaveException) Defaults(org.jabref.model.Defaults) BibtexDatabaseWriter(org.jabref.logic.exporter.BibtexDatabaseWriter) FileSaveSession(org.jabref.logic.exporter.FileSaveSession) SavePreferences(org.jabref.logic.exporter.SavePreferences) IExportFormat(org.jabref.logic.exporter.IExportFormat) SaveSession(org.jabref.logic.exporter.SaveSession) FileSaveSession(org.jabref.logic.exporter.FileSaveSession) BibDatabaseContext(org.jabref.model.database.BibDatabaseContext) File(java.io.File) JabRefException(org.jabref.JabRefException) BackingStoreException(java.util.prefs.BackingStoreException) SaveException(org.jabref.logic.exporter.SaveException) IOException(java.io.IOException) ImportException(org.jabref.logic.importer.ImportException)

Aggregations

Defaults (org.jabref.model.Defaults)24 BibDatabaseContext (org.jabref.model.database.BibDatabaseContext)24 BibDatabase (org.jabref.model.database.BibDatabase)9 ParserResult (org.jabref.logic.importer.ParserResult)8 BibEntry (org.jabref.model.entry.BibEntry)7 Test (org.junit.Test)7 Charset (java.nio.charset.Charset)6 BibtexParser (org.jabref.logic.importer.fileformat.BibtexParser)6 MetaData (org.jabref.model.metadata.MetaData)6 Path (java.nio.file.Path)5 Scanner (java.util.Scanner)5 BibtexDatabaseWriter (org.jabref.logic.exporter.BibtexDatabaseWriter)5 SavePreferences (org.jabref.logic.exporter.SavePreferences)5 Before (org.junit.Before)5 FileSaveSession (org.jabref.logic.exporter.FileSaveSession)3 SaveException (org.jabref.logic.exporter.SaveException)3 SaveSession (org.jabref.logic.exporter.SaveSession)3 FileDirectoryPreferences (org.jabref.model.metadata.FileDirectoryPreferences)3 IOException (java.io.IOException)2 JabRefFrame (org.jabref.gui.JabRefFrame)2