Search in sources :

Example 26 with ParserResult

use of org.jabref.logic.importer.ParserResult in project jabref by JabRef.

the class PdfImporter method doXMPImport.

private void doXMPImport(String fileName, List<BibEntry> res) {
    List<BibEntry> localRes = new ArrayList<>();
    PdfXmpImporter importer = new PdfXmpImporter(Globals.prefs.getXMPPreferences());
    Path filePath = Paths.get(fileName);
    ParserResult result = importer.importDatabase(filePath, Globals.prefs.getDefaultEncoding());
    if (result.hasWarnings()) {
        frame.showMessage(result.getErrorMessage());
    }
    localRes.addAll(result.getDatabase().getEntries());
    BibEntry entry;
    if (localRes.isEmpty()) {
        // import failed -> generate default entry
        LOGGER.info("Import failed");
        createNewBlankEntry(fileName).ifPresent(res::add);
        return;
    }
    // only one entry is imported
    entry = localRes.get(0);
    // insert entry to database and link file
    panel.getDatabase().insertEntry(entry);
    panel.markBaseChanged();
    FileListTableModel tm = new FileListTableModel();
    Path toLink = Paths.get(fileName);
    // Get a list of file directories:
    List<Path> dirsS = panel.getBibDatabaseContext().getFileDirectoriesAsPaths(Globals.prefs.getFileDirectoryPreferences());
    tm.addEntry(0, new FileListEntry(toLink.getFileName().toString(), FileUtil.shortenFileName(toLink, dirsS).toString(), ExternalFileTypes.getInstance().getExternalFileTypeByName("PDF")));
    entry.setField(FieldName.FILE, tm.getStringRepresentation());
    res.add(entry);
}
Also used : Path(java.nio.file.Path) BibEntry(org.jabref.model.entry.BibEntry) ParserResult(org.jabref.logic.importer.ParserResult) FileListTableModel(org.jabref.gui.filelist.FileListTableModel) ArrayList(java.util.ArrayList) FileListEntry(org.jabref.gui.filelist.FileListEntry) PdfXmpImporter(org.jabref.logic.importer.fileformat.PdfXmpImporter)

Example 27 with ParserResult

use of org.jabref.logic.importer.ParserResult in project jabref by JabRef.

the class AuxCommandLineTest method test.

@Test
public void test() throws URISyntaxException, IOException {
    InputStream originalStream = AuxCommandLineTest.class.getResourceAsStream("origin.bib");
    File auxFile = Paths.get(AuxCommandLineTest.class.getResource("paper.aux").toURI()).toFile();
    try (InputStreamReader originalReader = new InputStreamReader(originalStream, StandardCharsets.UTF_8)) {
        ParserResult result = new BibtexParser(importFormatPreferences).parse(originalReader);
        AuxCommandLine auxCommandLine = new AuxCommandLine(auxFile.getAbsolutePath(), result.getDatabase());
        BibDatabase newDB = auxCommandLine.perform();
        Assert.assertNotNull(newDB);
        Assert.assertEquals(2, newDB.getEntries().size());
    }
}
Also used : ParserResult(org.jabref.logic.importer.ParserResult) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) BibtexParser(org.jabref.logic.importer.fileformat.BibtexParser) File(java.io.File) BibDatabase(org.jabref.model.database.BibDatabase) Test(org.junit.Test)

Example 28 with ParserResult

use of org.jabref.logic.importer.ParserResult in project jabref by JabRef.

the class EntryFromFileCreatorManagerTest method testAddEntrysFromFiles.

@Test
@Ignore
public void testAddEntrysFromFiles() throws FileNotFoundException, IOException {
    try (FileInputStream stream = new FileInputStream(ImportDataTest.UNLINKED_FILES_TEST_BIB);
        InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)) {
        ParserResult result = new BibtexParser(mock(ImportFormatPreferences.class)).parse(reader);
        BibDatabase database = result.getDatabase();
        List<File> files = new ArrayList<>();
        files.add(ImportDataTest.FILE_NOT_IN_DATABASE);
        files.add(ImportDataTest.NOT_EXISTING_PDF);
        EntryFromFileCreatorManager manager = new EntryFromFileCreatorManager();
        List<String> errors = manager.addEntrysFromFiles(files, database, null, true);
        /**
             * One file doesn't exist, so adding it as an entry should lead to an error message.
             */
        Assert.assertEquals(1, errors.size());
        boolean file1Found = false;
        boolean file2Found = false;
        for (BibEntry entry : database.getEntries()) {
            String filesInfo = entry.getField("file").get();
            if (filesInfo.contains(files.get(0).getName())) {
                file1Found = true;
            }
            if (filesInfo.contains(files.get(1).getName())) {
                file2Found = true;
            }
        }
        Assert.assertTrue(file1Found);
        Assert.assertFalse(file2Found);
    }
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) InputStreamReader(java.io.InputStreamReader) BibtexParser(org.jabref.logic.importer.fileformat.BibtexParser) ArrayList(java.util.ArrayList) FileInputStream(java.io.FileInputStream) ParserResult(org.jabref.logic.importer.ParserResult) BibDatabase(org.jabref.model.database.BibDatabase) File(java.io.File) Ignore(org.junit.Ignore) ImportDataTest(org.jabref.logic.importer.ImportDataTest) Test(org.junit.Test)

Example 29 with ParserResult

use of org.jabref.logic.importer.ParserResult in project jabref by JabRef.

the class BibtexTestData method getBibtexDatabase.

public static BibDatabase getBibtexDatabase(ImportFormatPreferences importFormatPreferences) throws IOException {
    String article = "@ARTICLE{HipKro03,\n" + "  author = {Eric von Hippel and Georg von Krogh},\n" + "  title = {Open Source Software and the \"Private-Collective\" Innovation Model: Issues for Organization Science},\n" + "  journal = {Organization Science},\n" + "  year = {2003},\n" + "  volume = {14},\n" + "  pages = {209--223},\n" + "  number = {2},\n" + "  address = {Institute for Operations Research and the Management Sciences (INFORMS), Linthicum, Maryland, USA},\n" + "  doi = {http://dx.doi.org/10.1287/orsc.14.2.209.14992}," + "\n" + "  issn = {1526-5455}," + "\n" + "  publisher = {INFORMS}\n" + "}";
    BibtexParser parser = new BibtexParser(importFormatPreferences);
    ParserResult result = parser.parse(new StringReader(article));
    return result.getDatabase();
}
Also used : ParserResult(org.jabref.logic.importer.ParserResult) BibtexParser(org.jabref.logic.importer.fileformat.BibtexParser) StringReader(java.io.StringReader)

Example 30 with ParserResult

use of org.jabref.logic.importer.ParserResult in project jabref by JabRef.

the class BibEntryAssert method getListFromInputStream.

private static List<BibEntry> getListFromInputStream(InputStream is) throws IOException {
    ParserResult result;
    try (Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8)) {
        BibtexParser parser = new BibtexParser(mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS));
        result = parser.parse(reader);
    }
    Assert.assertNotNull(result);
    Assert.assertFalse(result.isEmpty());
    return result.getDatabase().getEntries();
}
Also used : ParserResult(org.jabref.logic.importer.ParserResult) InputStreamReader(java.io.InputStreamReader) BibtexParser(org.jabref.logic.importer.fileformat.BibtexParser) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) ImportFormatPreferences(org.jabref.logic.importer.ImportFormatPreferences)

Aggregations

ParserResult (org.jabref.logic.importer.ParserResult)196 Test (org.junit.Test)145 BibEntry (org.jabref.model.entry.BibEntry)131 StringReader (java.io.StringReader)130 BibtexParser (org.jabref.logic.importer.fileformat.BibtexParser)38 BibtexString (org.jabref.model.entry.BibtexString)30 ArrayList (java.util.ArrayList)23 BibDatabase (org.jabref.model.database.BibDatabase)20 Path (java.nio.file.Path)14 IOException (java.io.IOException)12 StringWriter (java.io.StringWriter)12 File (java.io.File)10 InputStreamReader (java.io.InputStreamReader)10 HashMap (java.util.HashMap)10 BibDatabaseContext (org.jabref.model.database.BibDatabaseContext)9 InputStream (java.io.InputStream)8 Defaults (org.jabref.model.Defaults)8 Charset (java.nio.charset.Charset)6 Scanner (java.util.Scanner)5 BufferedReader (java.io.BufferedReader)4