Search in sources :

Example 6 with BibtexParser

use of org.jabref.logic.importer.fileformat.BibtexParser 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 7 with BibtexParser

use of org.jabref.logic.importer.fileformat.BibtexParser 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)

Example 8 with BibtexParser

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

the class BibEntryWriterTest method roundTripWithPrecedingCommentTest.

@Test
public void roundTripWithPrecedingCommentTest() throws IOException {
    // @formatter:off
    String bibtexEntry = "% Some random comment that should stay here" + 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();
    //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 9 with BibtexParser

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

the class BibEntryWriterTest method roundTripWithAppendedNewlines.

@Test
public void roundTripWithAppendedNewlines() throws IOException {
    // @formatter:off
    String bibtexEntry = "@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 + "}\n\n";
    // @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();
    // Only one appending newline is written by the writer, the rest by FileActions. So, these should be removed here.
    assertEquals(bibtexEntry.substring(0, bibtexEntry.length() - 1), 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 10 with BibtexParser

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

the class BibEntryWriterTest method monthFieldSpecialSyntax.

@Test
public void monthFieldSpecialSyntax() throws IOException {
    // @formatter:off
    String bibtexEntry = "@Article{test," + OS.NEWLINE + "  Author                   = {Foo Bar}," + OS.NEWLINE + "  Month                    = mar," + 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 month field
    Set<String> fields = entry.getFieldNames();
    assertTrue(fields.contains("month"));
    assertEquals("#mar#", entry.getField("month").get());
    //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)

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