Search in sources :

Example 16 with BibDatabase

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

the class BibtexParser method initializeParserResult.

private void initializeParserResult() {
    database = new BibDatabase();
    // To store custom entry types parsed.
    entryTypes = new HashMap<>();
    parserResult = new ParserResult(database, new MetaData(), entryTypes);
}
Also used : ParserResult(org.jabref.logic.importer.ParserResult) MetaData(org.jabref.model.metadata.MetaData) BibDatabase(org.jabref.model.database.BibDatabase)

Example 17 with BibDatabase

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

the class MrDLibImporter method parse.

/**
     * Parses the input from the server to a ParserResult
     * @param input A BufferedReader with a reference to a string with the servers response
     * @throws IOException
     */
private void parse(BufferedReader input) throws IOException {
    // The Bibdatabase that gets returned in the ParserResult.
    BibDatabase bibDatabase = new BibDatabase();
    // The document to parse
    String recommendations = convertToString(input);
    // The sorted BibEntries gets stored here later
    List<BibEntry> bibEntries = new ArrayList<>();
    //Parsing the response with a SAX parser
    try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();
        MrDlibImporterHandler handler = new MrDlibImporterHandler();
        try (InputStream stream = new ByteArrayInputStream(recommendations.getBytes())) {
            saxParser.parse(stream, handler);
        } catch (SAXException e) {
            LOGGER.error(e.getMessage(), e);
        }
        List<RankedBibEntry> rankedBibEntries = handler.getRankedBibEntries();
        rankedBibEntries.sort((RankedBibEntry rankedBibEntry1, RankedBibEntry rankedBibEntry2) -> rankedBibEntry1.rank.compareTo(rankedBibEntry2.rank));
        bibEntries = rankedBibEntries.stream().map(e -> e.entry).collect(Collectors.toList());
    } catch (ParserConfigurationException | SAXException e) {
        LOGGER.error(e.getMessage(), e);
    }
    for (BibEntry bibentry : bibEntries) {
        bibDatabase.insertEntry(bibentry);
    }
    parserResult = new ParserResult(bibDatabase);
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) SAXException(org.xml.sax.SAXException) ParserResult(org.jabref.logic.importer.ParserResult) ByteArrayInputStream(java.io.ByteArrayInputStream) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) BibDatabase(org.jabref.model.database.BibDatabase) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 18 with BibDatabase

use of org.jabref.model.database.BibDatabase 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 19 with BibDatabase

use of org.jabref.model.database.BibDatabase 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 20 with BibDatabase

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

the class AuxParserTest method testNormal.

@Test
public void testNormal() throws URISyntaxException, IOException {
    InputStream originalStream = AuxParserTest.class.getResourceAsStream("origin.bib");
    File auxFile = Paths.get(AuxParserTest.class.getResource("paper.aux").toURI()).toFile();
    try (InputStreamReader originalReader = new InputStreamReader(originalStream, StandardCharsets.UTF_8)) {
        ParserResult result = new BibtexParser(importFormatPreferences).parse(originalReader);
        AuxParser auxParser = new AuxParser(auxFile.getAbsolutePath(), result.getDatabase());
        AuxParserResult auxResult = auxParser.parse();
        assertTrue(auxResult.getGeneratedBibDatabase().hasEntries());
        assertEquals(0, auxResult.getUnresolvedKeysCount());
        BibDatabase newDB = auxResult.getGeneratedBibDatabase();
        assertEquals(2, newDB.getEntries().size());
        assertEquals(2, auxResult.getResolvedKeysCount());
        assertEquals(2, auxResult.getFoundKeysInAux());
        assertEquals(auxResult.getFoundKeysInAux() + auxResult.getCrossRefEntriesCount(), auxResult.getResolvedKeysCount() + auxResult.getUnresolvedKeysCount());
        assertEquals(0, auxResult.getCrossRefEntriesCount());
    }
}
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)

Aggregations

BibDatabase (org.jabref.model.database.BibDatabase)88 BibEntry (org.jabref.model.entry.BibEntry)60 Test (org.junit.Test)44 ParserResult (org.jabref.logic.importer.ParserResult)20 ArrayList (java.util.ArrayList)19 HashMap (java.util.HashMap)15 BibDatabaseContext (org.jabref.model.database.BibDatabaseContext)15 BibtexParser (org.jabref.logic.importer.fileformat.BibtexParser)13 MetaData (org.jabref.model.metadata.MetaData)12 IOException (java.io.IOException)10 Defaults (org.jabref.model.Defaults)9 Before (org.junit.Before)9 File (java.io.File)8 InputStreamReader (java.io.InputStreamReader)8 InputStream (java.io.InputStream)7 PropertyVetoException (com.sun.star.beans.PropertyVetoException)6 UnknownPropertyException (com.sun.star.beans.UnknownPropertyException)6 WrappedTargetException (com.sun.star.lang.WrappedTargetException)6 LinkedHashMap (java.util.LinkedHashMap)5 BasePanel (org.jabref.gui.BasePanel)5