Search in sources :

Example 41 with BibtexParser

use of org.jabref.logic.importer.fileformat.BibtexParser in project jabref by JabRef.

the class BibtexDatabaseWriterTest method roundtripWithUserCommentAndEntryChange.

@Test
public void roundtripWithUserCommentAndEntryChange() 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));
    BibEntry entry = result.getDatabase().getEntryByKey("1137631").get();
    entry.setField("author", "Mr. Author");
    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(Paths.get("src/test/resources/testbib/bibWithUserCommentAndEntryChange.bib"), encoding.name())) {
        assertEquals(scanner.useDelimiter("\\A").next(), session.getStringValue());
    }
}
Also used : Path(java.nio.file.Path) ParserResult(org.jabref.logic.importer.ParserResult) BibEntry(org.jabref.model.entry.BibEntry) 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 42 with BibtexParser

use of org.jabref.logic.importer.fileformat.BibtexParser in project jabref by JabRef.

the class BibtexDatabaseWriterTest method roundtripWithContentSelectorsAndUmlauts.

@Test
public void roundtripWithContentSelectorsAndUmlauts() throws IOException, SaveException {
    String fileContent = "% Encoding: UTF-8" + OS.NEWLINE + OS.NEWLINE + "@Comment{jabref-meta: selector_journal:Test {\\\\\"U}mlaut;}" + OS.NEWLINE;
    Charset encoding = StandardCharsets.UTF_8;
    ParserResult firstParse = new BibtexParser(importFormatPreferences).parse(new StringReader(fileContent));
    SavePreferences preferences = new SavePreferences().withEncoding(encoding).withSaveInOriginalOrder(true);
    BibDatabaseContext context = new BibDatabaseContext(firstParse.getDatabase(), firstParse.getMetaData(), new Defaults(BibDatabaseMode.BIBTEX));
    StringSaveSession session = databaseWriter.savePartOfDatabase(context, firstParse.getDatabase().getEntries(), preferences);
    assertEquals(fileContent, session.getStringValue());
}
Also used : ParserResult(org.jabref.logic.importer.ParserResult) Defaults(org.jabref.model.Defaults) BibtexParser(org.jabref.logic.importer.fileformat.BibtexParser) StringReader(java.io.StringReader) Charset(java.nio.charset.Charset) BibtexString(org.jabref.model.entry.BibtexString) BibDatabaseContext(org.jabref.model.database.BibDatabaseContext) Test(org.junit.Test)

Example 43 with BibtexParser

use of org.jabref.logic.importer.fileformat.BibtexParser in project jabref by JabRef.

the class BibtexDatabaseWriterTest method roundtripWithUserCommentBeforeStringAndChange.

@Test
public void roundtripWithUserCommentBeforeStringAndChange() 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));
    for (BibtexString string : result.getDatabase().getStringValues()) {
        // Mark them as changed
        string.setContent(string.getContent());
    }
    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) BibtexString(org.jabref.model.entry.BibtexString) BibDatabaseContext(org.jabref.model.database.BibDatabaseContext) Test(org.junit.Test)

Aggregations

BibtexParser (org.jabref.logic.importer.fileformat.BibtexParser)43 ParserResult (org.jabref.logic.importer.ParserResult)38 Test (org.junit.Test)28 BibEntry (org.jabref.model.entry.BibEntry)27 StringReader (java.io.StringReader)20 BibDatabase (org.jabref.model.database.BibDatabase)13 InputStreamReader (java.io.InputStreamReader)12 StringWriter (java.io.StringWriter)12 File (java.io.File)8 InputStream (java.io.InputStream)7 Path (java.nio.file.Path)7 Charset (java.nio.charset.Charset)6 Defaults (org.jabref.model.Defaults)6 BibDatabaseContext (org.jabref.model.database.BibDatabaseContext)6 Scanner (java.util.Scanner)5 IOException (java.io.IOException)4 FileInputStream (java.io.FileInputStream)3 Collection (java.util.Collection)3 Optional (java.util.Optional)3 FetcherException (org.jabref.logic.importer.FetcherException)3