Search in sources :

Example 41 with BibDatabaseContext

use of org.jabref.model.database.BibDatabaseContext in project jabref by JabRef.

the class MsBibExportFormatTest method setUp.

@Before
public void setUp() throws Exception {
    databaseContext = new BibDatabaseContext();
    charset = Charsets.UTF_8;
    msBibExportFormat = new MSBibExportFormat();
    tempFile = testFolder.newFile();
}
Also used : BibDatabaseContext(org.jabref.model.database.BibDatabaseContext) Before(org.junit.Before)

Example 42 with BibDatabaseContext

use of org.jabref.model.database.BibDatabaseContext in project jabref by JabRef.

the class HtmlExportFormatTest method setUp.

@Before
public void setUp() {
    Map<String, ExportFormat> customFormats = new HashMap<>();
    LayoutFormatterPreferences layoutPreferences = mock(LayoutFormatterPreferences.class, Answers.RETURNS_DEEP_STUBS);
    SavePreferences savePreferences = mock(SavePreferences.class);
    ExportFormats.initAllExports(customFormats, layoutPreferences, savePreferences);
    exportFormat = ExportFormats.getExportFormat("html");
    databaseContext = new BibDatabaseContext();
    charset = Charsets.UTF_8;
    BibEntry entry = new BibEntry();
    entry.setField("title", "my paper title");
    entry.setField("author", "Stefan Kolb");
    entry.setCiteKey("mykey");
    entries = Arrays.asList(entry);
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) LayoutFormatterPreferences(org.jabref.logic.layout.LayoutFormatterPreferences) HashMap(java.util.HashMap) BibDatabaseContext(org.jabref.model.database.BibDatabaseContext) Before(org.junit.Before)

Example 43 with BibDatabaseContext

use of org.jabref.model.database.BibDatabaseContext 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)

Example 44 with BibDatabaseContext

use of org.jabref.model.database.BibDatabaseContext in project jabref by JabRef.

the class ArgumentProcessor method exportMatches.

private boolean exportMatches(List<ParserResult> loaded) {
    String[] data = cli.getExportMatches().split(",");
    //enables blanks within the search term:
    String searchTerm = data[0].replace("\\$", " ");
    //$ stands for a blank
    ParserResult pr = loaded.get(loaded.size() - 1);
    BibDatabaseContext databaseContext = pr.getDatabaseContext();
    BibDatabase dataBase = pr.getDatabase();
    SearchPreferences searchPreferences = new SearchPreferences(Globals.prefs);
    SearchQuery query = new SearchQuery(searchTerm, searchPreferences.isCaseSensitive(), searchPreferences.isRegularExpression());
    List<BibEntry> matches = new DatabaseSearcher(query, dataBase).getMatches();
    //export matches
    if (!matches.isEmpty()) {
        String formatName;
        //read in the export format, take default format if no format entered
        switch(data.length) {
            case 3:
                formatName = data[2];
                break;
            case 2:
                //default ExportFormat: HTML table (with Abstract & BibTeX)
                formatName = "tablerefsabsbib";
                break;
            default:
                System.err.println(Localization.lang("Output file missing").concat(". \n \t ").concat(Localization.lang("Usage")).concat(": ") + JabRefCLI.getExportMatchesSyntax());
                noGUINeeded = true;
                return false;
        }
        //export new database
        IExportFormat format = ExportFormats.getExportFormat(formatName);
        if (format == null) {
            System.err.println(Localization.lang("Unknown export format") + ": " + formatName);
        } else {
            // We have an ExportFormat instance:
            try {
                System.out.println(Localization.lang("Exporting") + ": " + data[1]);
                format.performExport(databaseContext, data[1], databaseContext.getMetaData().getEncoding().orElse(Globals.prefs.getDefaultEncoding()), matches);
            } catch (Exception ex) {
                System.err.println(Localization.lang("Could not export file") + " '" + data[1] + "': " + ExceptionUtils.getStackTrace(ex));
            }
        }
    } else {
        System.err.println(Localization.lang("No search matches."));
    }
    return true;
}
Also used : SearchQuery(org.jabref.logic.search.SearchQuery) ParserResult(org.jabref.logic.importer.ParserResult) SearchPreferences(org.jabref.preferences.SearchPreferences) BibEntry(org.jabref.model.entry.BibEntry) IExportFormat(org.jabref.logic.exporter.IExportFormat) BibDatabaseContext(org.jabref.model.database.BibDatabaseContext) BibDatabase(org.jabref.model.database.BibDatabase) 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) DatabaseSearcher(org.jabref.logic.search.DatabaseSearcher)

Example 45 with BibDatabaseContext

use of org.jabref.model.database.BibDatabaseContext 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>&Ouml;sterreich</b> &#8211; &amp; characters &#x2aa2; <i>italic</i>";
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) Defaults(org.jabref.model.Defaults) Random(java.util.Random) BibtexDatabaseWriter(org.jabref.logic.exporter.BibtexDatabaseWriter) MetaData(org.jabref.model.metadata.MetaData) SavePreferences(org.jabref.logic.exporter.SavePreferences) StringSaveSession(org.jabref.logic.exporter.StringSaveSession) BibDatabaseContext(org.jabref.model.database.BibDatabaseContext) Setup(org.openjdk.jmh.annotations.Setup)

Aggregations

BibDatabaseContext (org.jabref.model.database.BibDatabaseContext)48 Defaults (org.jabref.model.Defaults)24 Before (org.junit.Before)16 BibDatabase (org.jabref.model.database.BibDatabase)15 Test (org.junit.Test)13 BibEntry (org.jabref.model.entry.BibEntry)11 ParserResult (org.jabref.logic.importer.ParserResult)9 MetaData (org.jabref.model.metadata.MetaData)8 Charset (java.nio.charset.Charset)6 BibtexParser (org.jabref.logic.importer.fileformat.BibtexParser)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 IOException (java.io.IOException)4 SaveException (org.jabref.logic.exporter.SaveException)4 BibtexImporter (org.jabref.logic.importer.fileformat.BibtexImporter)4 LayoutFormatterPreferences (org.jabref.logic.layout.LayoutFormatterPreferences)4 FileSaveSession (org.jabref.logic.exporter.FileSaveSession)3 SaveSession (org.jabref.logic.exporter.SaveSession)3