Search in sources :

Example 41 with ParserResult

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

the class TestVM method bibtexString2BibtexEntry.

private static BibEntry bibtexString2BibtexEntry(String s) throws IOException {
    ParserResult result = new BibtexParser(mock(ImportFormatPreferences.class, Answers.RETURNS_DEEP_STUBS)).parse(new StringReader(s));
    Collection<BibEntry> c = result.getDatabase().getEntries();
    Assert.assertEquals(1, c.size());
    return c.iterator().next();
}
Also used : ParserResult(org.jabref.logic.importer.ParserResult) BibEntry(org.jabref.model.entry.BibEntry) BibtexParser(org.jabref.logic.importer.fileformat.BibtexParser) StringReader(java.io.StringReader)

Example 42 with ParserResult

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

the class BibtexDatabaseWriterTest method roundtripWithUserComment.

@Test
public void roundtripWithUserComment() throws Exception {
    Path testBibtexFile = Paths.get("src/test/resources/testbib/bibWithUserComments.bib");
    Charset encoding = StandardCharsets.UTF_8;
    ParserResult result = new BibtexParser(importFormatPreferences).parse(Importer.getReader(testBibtexFile, encoding));
    SavePreferences preferences = new SavePreferences().withEncoding(encoding).withSaveInOriginalOrder(true);
    BibDatabaseContext context = new BibDatabaseContext(result.getDatabase(), result.getMetaData(), new Defaults(BibDatabaseMode.BIBTEX));
    StringSaveSession session = databaseWriter.savePartOfDatabase(context, result.getDatabase().getEntries(), preferences);
    try (Scanner scanner = new Scanner(testBibtexFile, encoding.name())) {
        assertEquals(scanner.useDelimiter("\\A").next(), session.getStringValue());
    }
}
Also used : Path(java.nio.file.Path) ParserResult(org.jabref.logic.importer.ParserResult) Scanner(java.util.Scanner) Defaults(org.jabref.model.Defaults) BibtexParser(org.jabref.logic.importer.fileformat.BibtexParser) Charset(java.nio.charset.Charset) BibDatabaseContext(org.jabref.model.database.BibDatabaseContext) Test(org.junit.Test)

Example 43 with ParserResult

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

the class BibtexDatabaseWriterTest method roundtrip.

@Test
public void roundtrip() throws Exception {
    Path testBibtexFile = Paths.get("src/test/resources/testbib/complex.bib");
    Charset encoding = StandardCharsets.UTF_8;
    ParserResult result = new BibtexParser(importFormatPreferences).parse(Importer.getReader(testBibtexFile, encoding));
    SavePreferences preferences = new SavePreferences().withEncoding(encoding).withSaveInOriginalOrder(true);
    BibDatabaseContext context = new BibDatabaseContext(result.getDatabase(), result.getMetaData(), new Defaults(BibDatabaseMode.BIBTEX));
    StringSaveSession session = databaseWriter.savePartOfDatabase(context, result.getDatabase().getEntries(), preferences);
    try (Scanner scanner = new Scanner(testBibtexFile, encoding.name())) {
        assertEquals(scanner.useDelimiter("\\A").next(), session.getStringValue());
    }
}
Also used : Path(java.nio.file.Path) ParserResult(org.jabref.logic.importer.ParserResult) Scanner(java.util.Scanner) Defaults(org.jabref.model.Defaults) BibtexParser(org.jabref.logic.importer.fileformat.BibtexParser) Charset(java.nio.charset.Charset) BibDatabaseContext(org.jabref.model.database.BibDatabaseContext) Test(org.junit.Test)

Example 44 with ParserResult

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

the class BibtexDatabaseWriterTest method roundtripWithUnknownMetaData.

@Test
public void roundtripWithUnknownMetaData() throws Exception {
    Path testBibtexFile = Paths.get("src/test/resources/testbib/unknownMetaData.bib");
    Charset encoding = StandardCharsets.UTF_8;
    ParserResult result = new BibtexParser(importFormatPreferences).parse(Importer.getReader(testBibtexFile, encoding));
    SavePreferences preferences = new SavePreferences().withEncoding(encoding).withSaveInOriginalOrder(true);
    BibDatabaseContext context = new BibDatabaseContext(result.getDatabase(), result.getMetaData(), new Defaults(BibDatabaseMode.BIBTEX));
    StringSaveSession session = databaseWriter.savePartOfDatabase(context, result.getDatabase().getEntries(), preferences);
    try (Scanner scanner = new Scanner(testBibtexFile, encoding.name())) {
        assertEquals(scanner.useDelimiter("\\A").next(), session.getStringValue());
    }
}
Also used : Path(java.nio.file.Path) ParserResult(org.jabref.logic.importer.ParserResult) Scanner(java.util.Scanner) Defaults(org.jabref.model.Defaults) BibtexParser(org.jabref.logic.importer.fileformat.BibtexParser) Charset(java.nio.charset.Charset) BibDatabaseContext(org.jabref.model.database.BibDatabaseContext) Test(org.junit.Test)

Example 45 with ParserResult

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

the class BibtexParserTest method integrationTestCustomEntryType.

@Test
public void integrationTestCustomEntryType() throws IOException {
    ParserResult result = parser.parse(new StringReader("@comment{jabref-entrytype: Lecturenotes: req[author;title] opt[language;url]}"));
    Map<String, EntryType> customEntryTypes = result.getEntryTypes();
    assertEquals(1, customEntryTypes.size());
    assertEquals("Lecturenotes", customEntryTypes.keySet().toArray()[0]);
    EntryType entryType = customEntryTypes.get("Lecturenotes");
    assertEquals("Lecturenotes", entryType.getName());
    assertEquals(Arrays.asList("author", "title"), entryType.getRequiredFields());
    assertEquals(Arrays.asList("language", "url"), entryType.getOptionalFields());
}
Also used : ParserResult(org.jabref.logic.importer.ParserResult) EntryType(org.jabref.model.entry.EntryType) StringReader(java.io.StringReader) BibtexString(org.jabref.model.entry.BibtexString) 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