Search in sources :

Example 11 with ImportFormatPreferences

use of org.jabref.logic.importer.ImportFormatPreferences 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)

Aggregations

ImportFormatPreferences (org.jabref.logic.importer.ImportFormatPreferences)11 Before (org.junit.Before)9 BibEntry (org.jabref.model.entry.BibEntry)7 FieldContentParserPreferences (org.jabref.logic.bibtex.FieldContentParserPreferences)5 ParserResult (org.jabref.logic.importer.ParserResult)2 BibDatabase (org.jabref.model.database.BibDatabase)2 MetaData (org.jabref.model.metadata.MetaData)2 File (java.io.File)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 Path (java.nio.file.Path)1 Collection (java.util.Collection)1 XMPMetadata (org.apache.jempbox.xmp.XMPMetadata)1 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)1 PDPage (org.apache.pdfbox.pdmodel.PDPage)1 BibEntryWriter (org.jabref.logic.bibtex.BibEntryWriter)1 LatexFieldFormatter (org.jabref.logic.bibtex.LatexFieldFormatter)1 EntryComparator (org.jabref.logic.bibtex.comparator.EntryComparator)1 BibtexParser (org.jabref.logic.importer.fileformat.BibtexParser)1