Search in sources :

Example 36 with ParserResult

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

the class AuxParserTest method testNestedAux.

@Test
public void testNestedAux() throws URISyntaxException, IOException {
    InputStream originalStream = AuxParserTest.class.getResourceAsStream("origin.bib");
    File auxFile = Paths.get(AuxParserTest.class.getResource("nested.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)

Example 37 with ParserResult

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

the class AuxParserTest method testCrossRef.

@Test
public void testCrossRef() throws URISyntaxException, IOException {
    InputStream originalStream = AuxParserTest.class.getResourceAsStream("origin.bib");
    File auxFile = Paths.get(AuxParserTest.class.getResource("crossref.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(2, auxResult.getUnresolvedKeysCount());
        BibDatabase newDB = auxResult.getGeneratedBibDatabase();
        assertEquals(4, newDB.getEntries().size());
        assertEquals(3, auxResult.getResolvedKeysCount());
        assertEquals(4, auxResult.getFoundKeysInAux());
        assertEquals(auxResult.getFoundKeysInAux() + auxResult.getCrossRefEntriesCount(), auxResult.getResolvedKeysCount() + auxResult.getUnresolvedKeysCount());
        assertEquals(1, 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)

Example 38 with ParserResult

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

the class BibEntryWriterTest method roundTripWithPrependingNewlines.

@Test
public void roundTripWithPrependingNewlines() throws IOException {
    // @formatter:off
    String bibtexEntry = "\r\n@Article{test," + OS.NEWLINE + "  Author                   = {Foo Bar}," + OS.NEWLINE + "  Journal                  = {International Journal of Something}," + OS.NEWLINE + "  Note                     = {some note}," + OS.NEWLINE + "  Number                   = {1}" + OS.NEWLINE + "}";
    // @formatter:on
    // read in bibtex string
    ParserResult result = new BibtexParser(importFormatPreferences).parse(new StringReader(bibtexEntry));
    Collection<BibEntry> entries = result.getDatabase().getEntries();
    BibEntry entry = entries.iterator().next();
    //write out bibtex string
    StringWriter stringWriter = new StringWriter();
    writer.write(entry, stringWriter, BibDatabaseMode.BIBTEX);
    String actual = stringWriter.toString();
    assertEquals(bibtexEntry, actual);
}
Also used : ParserResult(org.jabref.logic.importer.ParserResult) BibEntry(org.jabref.model.entry.BibEntry) StringWriter(java.io.StringWriter) BibtexParser(org.jabref.logic.importer.fileformat.BibtexParser) StringReader(java.io.StringReader) Test(org.junit.Test)

Example 39 with ParserResult

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

the class BibEntryWriterTest method testSingleWrite.

private String testSingleWrite(String bibtexEntry) throws IOException {
    // read in bibtex string
    ParserResult result = new BibtexParser(importFormatPreferences).parse(new StringReader(bibtexEntry));
    Collection<BibEntry> entries = result.getDatabase().getEntries();
    BibEntry entry = entries.iterator().next();
    //write out bibtex string
    StringWriter stringWriter = new StringWriter();
    writer.write(entry, stringWriter, BibDatabaseMode.BIBTEX);
    String actual = stringWriter.toString();
    assertEquals(bibtexEntry, actual);
    return actual;
}
Also used : ParserResult(org.jabref.logic.importer.ParserResult) BibEntry(org.jabref.model.entry.BibEntry) StringWriter(java.io.StringWriter) BibtexParser(org.jabref.logic.importer.fileformat.BibtexParser) StringReader(java.io.StringReader)

Example 40 with ParserResult

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

the class BibEntryWriterTest method roundTripWithModification.

@Test
public void roundTripWithModification() throws IOException {
    // @formatter:off
    String bibtexEntry = OS.NEWLINE + "@Article{test," + OS.NEWLINE + "  Author                   = {Foo Bar}," + OS.NEWLINE + "  Journal                  = {International Journal of Something}," + OS.NEWLINE + "  Note                     = {some note}," + OS.NEWLINE + "  Number                   = {1}," + OS.NEWLINE + "}";
    // @formatter:on
    // read in bibtex string
    ParserResult result = new BibtexParser(importFormatPreferences).parse(new StringReader(bibtexEntry));
    Collection<BibEntry> entries = result.getDatabase().getEntries();
    BibEntry entry = entries.iterator().next();
    // Modify entry
    entry.setField("author", "BlaBla");
    // write out bibtex string
    StringWriter stringWriter = new StringWriter();
    writer.write(entry, stringWriter, BibDatabaseMode.BIBTEX);
    String actual = stringWriter.toString();
    // @formatter:off
    String expected = OS.NEWLINE + "@Article{test," + OS.NEWLINE + "  author  = {BlaBla}," + OS.NEWLINE + "  journal = {International Journal of Something}," + OS.NEWLINE + "  number  = {1}," + OS.NEWLINE + "  note    = {some note}," + OS.NEWLINE + "}" + OS.NEWLINE;
    // @formatter:on
    assertEquals(expected, actual);
}
Also used : ParserResult(org.jabref.logic.importer.ParserResult) BibEntry(org.jabref.model.entry.BibEntry) StringWriter(java.io.StringWriter) BibtexParser(org.jabref.logic.importer.fileformat.BibtexParser) StringReader(java.io.StringReader) Test(org.junit.Test)

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