Search in sources :

Example 1 with BibEntryWriter

use of org.jabref.logic.bibtex.BibEntryWriter in project jabref by JabRef.

the class EntryEditor method getSourceString.

private static String getSourceString(BibEntry entry, BibDatabaseMode type) throws IOException {
    StringWriter stringWriter = new StringWriter(200);
    LatexFieldFormatter formatter = LatexFieldFormatter.buildIgnoreHashes(Globals.prefs.getLatexFieldFormatterPreferences());
    new BibEntryWriter(formatter, false).writeWithoutPrependedNewlines(entry, stringWriter, type);
    return stringWriter.getBuffer().toString();
}
Also used : StringWriter(java.io.StringWriter) BibEntryWriter(org.jabref.logic.bibtex.BibEntryWriter) LatexFieldFormatter(org.jabref.logic.bibtex.LatexFieldFormatter)

Example 2 with BibEntryWriter

use of org.jabref.logic.bibtex.BibEntryWriter in project jabref by JabRef.

the class SendAsEMailAction method run.

@Override
public void run() {
    if (!Desktop.isDesktopSupported()) {
        message = Localization.lang("Error creating email");
        return;
    }
    BasePanel panel = frame.getCurrentBasePanel();
    if (panel == null) {
        return;
    }
    if (panel.getSelectedEntries().isEmpty()) {
        message = Localization.lang("This operation requires one or more entries to be selected.");
        return;
    }
    StringWriter sw = new StringWriter();
    List<BibEntry> bes = panel.getSelectedEntries();
    // write the entries using sw, which is used later to form the email content
    BibEntryWriter bibtexEntryWriter = new BibEntryWriter(new LatexFieldFormatter(Globals.prefs.getLatexFieldFormatterPreferences()), true);
    for (BibEntry entry : bes) {
        try {
            bibtexEntryWriter.write(entry, sw, panel.getBibDatabaseContext().getMode());
        } catch (IOException e) {
            LOGGER.warn("Problem creating BibTeX file for mailing.", e);
        }
    }
    List<String> attachments = new ArrayList<>();
    // open folders is needed to indirectly support email programs, which cannot handle
    //   the unofficial "mailto:attachment" property
    boolean openFolders = JabRefPreferences.getInstance().getBoolean(JabRefPreferences.OPEN_FOLDERS_OF_ATTACHED_FILES);
    List<Path> fileList = FileUtil.getListOfLinkedFiles(bes, frame.getCurrentBasePanel().getBibDatabaseContext().getFileDirectoriesAsPaths(Globals.prefs.getFileDirectoryPreferences()));
    for (Path f : fileList) {
        attachments.add(f.toAbsolutePath().toString());
        if (openFolders) {
            try {
                JabRefDesktop.openFolderAndSelectFile(f.toAbsolutePath());
            } catch (IOException e) {
                LOGGER.debug("Cannot open file", e);
            }
        }
    }
    String mailTo = "?Body=".concat(sw.getBuffer().toString());
    mailTo = mailTo.concat("&Subject=");
    mailTo = mailTo.concat(JabRefPreferences.getInstance().get(JabRefPreferences.EMAIL_SUBJECT));
    for (String path : attachments) {
        mailTo = mailTo.concat("&Attachment=\"").concat(path);
        mailTo = mailTo.concat("\"");
    }
    URI uriMailTo;
    try {
        uriMailTo = new URI("mailto", mailTo, null);
    } catch (URISyntaxException e1) {
        message = Localization.lang("Error creating email");
        LOGGER.warn(message, e1);
        return;
    }
    Desktop desktop = Desktop.getDesktop();
    try {
        desktop.mail(uriMailTo);
    } catch (IOException e) {
        message = Localization.lang("Error creating email");
        LOGGER.warn(message, e);
        return;
    }
    message = String.format("%s: %d", Localization.lang("Entries added to an email"), bes.size());
}
Also used : Path(java.nio.file.Path) BibEntry(org.jabref.model.entry.BibEntry) BasePanel(org.jabref.gui.BasePanel) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) BibEntryWriter(org.jabref.logic.bibtex.BibEntryWriter) URI(java.net.URI) LatexFieldFormatter(org.jabref.logic.bibtex.LatexFieldFormatter) StringWriter(java.io.StringWriter) Desktop(java.awt.Desktop) JabRefDesktop(org.jabref.gui.desktop.JabRefDesktop)

Example 3 with BibEntryWriter

use of org.jabref.logic.bibtex.BibEntryWriter in project jabref by JabRef.

the class XMPUtilTest method bibtexEntry2BibtexString.

private static String bibtexEntry2BibtexString(BibEntry e) throws IOException {
    StringWriter sw = new StringWriter();
    new BibEntryWriter(new LatexFieldFormatter(mock(LatexFieldFormatterPreferences.class, Answers.RETURNS_DEEP_STUBS)), false).write(e, sw, BibDatabaseMode.BIBTEX);
    return sw.getBuffer().toString();
}
Also used : StringWriter(java.io.StringWriter) LatexFieldFormatterPreferences(org.jabref.logic.bibtex.LatexFieldFormatterPreferences) BibEntryWriter(org.jabref.logic.bibtex.BibEntryWriter) LatexFieldFormatter(org.jabref.logic.bibtex.LatexFieldFormatter)

Example 4 with BibEntryWriter

use of org.jabref.logic.bibtex.BibEntryWriter in project jabref by JabRef.

the class XMPUtilMain method main.

/**
     * Command-line tool for working with XMP-data.
     *
     * Read or write XMP-metadata from or to pdf file.
     *
     * Usage:
     * <dl>
     * <dd>Read from PDF and print as bibtex:</dd>
     * <dt>xmpUtil PDF</dt>
     * <dd>Read from PDF and print raw XMP:</dd>
     * <dt>xmpUtil -x PDF</dt>
     * <dd>Write the entry in BIB given by KEY to the PDF:</dd>
     * <dt>xmpUtil KEY BIB PDF</dt>
     * <dd>Write all entries in BIB to the PDF:</dd>
     * <dt>xmpUtil BIB PDF</dt>
     * </dl>
     *
     * @param args
     *            Command line strings passed to utility.
     * @throws IOException
     *             If any of the given files could not be read or written.
     * @throws TransformerException
     *             If the given BibEntry is malformed.
     */
public static void main(String[] args) throws IOException, TransformerException {
    // Don't forget to initialize the preferences
    if (Globals.prefs == null) {
        Globals.prefs = JabRefPreferences.getInstance();
    }
    XMPPreferences xmpPreferences = Globals.prefs.getXMPPreferences();
    ImportFormatPreferences importFormatPreferences = Globals.prefs.getImportFormatPreferences();
    switch(args.length) {
        case 0:
            usage();
            break;
        case 1:
            if (args[0].endsWith(".pdf")) {
                // Read from pdf and write as BibTex
                List<BibEntry> l = XMPUtil.readXMP(new File(args[0]), xmpPreferences);
                BibEntryWriter bibtexEntryWriter = new BibEntryWriter(new LatexFieldFormatter(Globals.prefs.getLatexFieldFormatterPreferences()), false);
                for (BibEntry entry : l) {
                    StringWriter sw = new StringWriter();
                    bibtexEntryWriter.write(entry, sw, BibDatabaseMode.BIBTEX);
                    System.out.println(sw.getBuffer());
                }
            } else if (args[0].endsWith(".bib")) {
                // Read from BIB and write as XMP
                try (FileReader fr = new FileReader(args[0])) {
                    ParserResult result = new BibtexParser(importFormatPreferences).parse(fr);
                    Collection<BibEntry> entries = result.getDatabase().getEntries();
                    if (entries.isEmpty()) {
                        System.err.println("Could not find BibEntry in " + args[0]);
                    } else {
                        System.out.println(XMPUtil.toXMP(entries, result.getDatabase(), xmpPreferences));
                    }
                }
            } else {
                usage();
            }
            break;
        case 2:
            if ("-x".equals(args[0]) && args[1].endsWith(".pdf")) {
                // Read from pdf and write as BibTex
                Optional<XMPMetadata> meta = XMPUtil.readRawXMP(new File(args[1]));
                if (meta.isPresent()) {
                    XMLUtil.save(meta.get().getXMPDocument(), System.out, StandardCharsets.UTF_8.name());
                } else {
                    System.err.println("The given pdf does not contain any XMP-metadata.");
                }
                break;
            }
            if (args[0].endsWith(".bib") && args[1].endsWith(".pdf")) {
                ParserResult result = new BibtexParser(importFormatPreferences).parse(new FileReader(args[0]));
                Collection<BibEntry> entries = result.getDatabase().getEntries();
                if (entries.isEmpty()) {
                    System.err.println("Could not find BibEntry in " + args[0]);
                } else {
                    XMPUtil.writeXMP(new File(args[1]), entries, result.getDatabase(), false, xmpPreferences);
                    System.out.println("XMP written.");
                }
                break;
            }
            usage();
            break;
        case 3:
            if (!args[1].endsWith(".bib") && !args[2].endsWith(".pdf")) {
                usage();
                break;
            }
            ParserResult result = new BibtexParser(importFormatPreferences).parse(new FileReader(args[1]));
            Optional<BibEntry> bibEntry = result.getDatabase().getEntryByKey(args[0]);
            if (bibEntry.isPresent()) {
                XMPUtil.writeXMP(new File(args[2]), bibEntry.get(), result.getDatabase(), xmpPreferences);
                System.out.println("XMP written.");
            } else {
                System.err.println("Could not find BibEntry " + args[0] + " in " + args[0]);
            }
            break;
        default:
            usage();
    }
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) BibtexParser(org.jabref.logic.importer.fileformat.BibtexParser) BibEntryWriter(org.jabref.logic.bibtex.BibEntryWriter) LatexFieldFormatter(org.jabref.logic.bibtex.LatexFieldFormatter) ParserResult(org.jabref.logic.importer.ParserResult) XMPPreferences(org.jabref.logic.xmp.XMPPreferences) StringWriter(java.io.StringWriter) XMPMetadata(org.apache.jempbox.xmp.XMPMetadata) ImportFormatPreferences(org.jabref.logic.importer.ImportFormatPreferences) Collection(java.util.Collection) FileReader(java.io.FileReader) File(java.io.File)

Example 5 with BibEntryWriter

use of org.jabref.logic.bibtex.BibEntryWriter in project jabref by JabRef.

the class MergeEntries method updateAll.

/**
     * Update the merged BibEntry with source and preview panel every time something is changed
     */
private void updateAll() {
    if (!doneBuilding) {
        // If we are not done adding everything, do not do anything...
        return;
    }
    // Check if the type has changed
    if (!identicalTypes && typeRadioButtons.get(0).isSelected()) {
        mergedEntry.setType(leftEntry.getType());
    } else {
        mergedEntry.setType(rightEntry.getType());
    }
    // Check the potentially different fields
    for (String field : differentFields) {
        if (radioButtons.get(field).get(0).isSelected()) {
            // Will only happen if field exists
            mergedEntry.setField(field, leftEntry.getField(field).get());
        } else if (radioButtons.get(field).get(2).isSelected()) {
            // Will only happen if field exists
            mergedEntry.setField(field, rightEntry.getField(field).get());
        } else {
            mergedEntry.clearField(field);
        }
    }
    // Update the PreviewPanel
    entryPreview.setEntry(mergedEntry);
    // Update the BibTeX source view
    StringWriter writer = new StringWriter();
    try {
        new BibEntryWriter(new LatexFieldFormatter(Globals.prefs.getLatexFieldFormatterPreferences()), false).write(mergedEntry, writer, databaseType);
    } catch (IOException ex) {
        LOGGER.error("Error in entry", ex);
    }
    sourceView.setText(writer.getBuffer().toString());
    sourceView.setCaretPosition(0);
}
Also used : StringWriter(java.io.StringWriter) IOException(java.io.IOException) BibEntryWriter(org.jabref.logic.bibtex.BibEntryWriter) LatexFieldFormatter(org.jabref.logic.bibtex.LatexFieldFormatter)

Aggregations

StringWriter (java.io.StringWriter)6 BibEntryWriter (org.jabref.logic.bibtex.BibEntryWriter)6 LatexFieldFormatter (org.jabref.logic.bibtex.LatexFieldFormatter)6 IOException (java.io.IOException)3 BibEntry (org.jabref.model.entry.BibEntry)2 Desktop (java.awt.Desktop)1 File (java.io.File)1 FileReader (java.io.FileReader)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 XMPMetadata (org.apache.jempbox.xmp.XMPMetadata)1 BasePanel (org.jabref.gui.BasePanel)1 JabRefDesktop (org.jabref.gui.desktop.JabRefDesktop)1 LatexFieldFormatterPreferences (org.jabref.logic.bibtex.LatexFieldFormatterPreferences)1 ImportFormatPreferences (org.jabref.logic.importer.ImportFormatPreferences)1 ParserResult (org.jabref.logic.importer.ParserResult)1 BibtexParser (org.jabref.logic.importer.fileformat.BibtexParser)1