Search in sources :

Example 56 with ParserResult

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

the class BibtexParserTest method parseSavesNewlinesBeforeEntryInParsedSerialization.

@Test
public void parseSavesNewlinesBeforeEntryInParsedSerialization() throws IOException {
    String testEntry = "@article{test,author={Ed von Test}}";
    ParserResult result = parser.parse(new StringReader(OS.NEWLINE + OS.NEWLINE + OS.NEWLINE + testEntry));
    Collection<BibEntry> c = result.getDatabase().getEntries();
    assertEquals(1, c.size());
    BibEntry e = c.iterator().next();
    assertEquals(OS.NEWLINE + OS.NEWLINE + OS.NEWLINE + testEntry, e.getParsedSerialization());
}
Also used : ParserResult(org.jabref.logic.importer.ParserResult) BibEntry(org.jabref.model.entry.BibEntry) StringReader(java.io.StringReader) BibtexString(org.jabref.model.entry.BibtexString) Test(org.junit.Test)

Example 57 with ParserResult

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

the class BibtexParserTest method parseRecognizesMultipleEntries.

@Test
public void parseRecognizesMultipleEntries() throws IOException {
    ParserResult result = BibtexParser.parse(new StringReader("@article{canh05," + "  author = {Crowston, K. and Annabi, H.},\n" + "  title = {Title A}}\n" + "@inProceedings{foo," + "  author={Norton Bar}}"), importFormatPreferences);
    List<BibEntry> parsed = result.getDatabase().getEntries();
    List<BibEntry> expected = new ArrayList<>();
    BibEntry firstEntry = new BibEntry();
    firstEntry.setType("article");
    firstEntry.setCiteKey("canh05");
    firstEntry.setField("author", "Crowston, K. and Annabi, H.");
    firstEntry.setField("title", "Title A");
    expected.add(firstEntry);
    BibEntry secondEntry = new BibEntry();
    secondEntry.setType("inproceedings");
    secondEntry.setCiteKey("foo");
    secondEntry.setField("author", "Norton Bar");
    expected.add(secondEntry);
    assertEquals(expected, parsed);
}
Also used : ParserResult(org.jabref.logic.importer.ParserResult) BibEntry(org.jabref.model.entry.BibEntry) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 58 with ParserResult

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

the class BibtexParserTest method parseSavesOneNewlineAfterStringInParsedSerialization.

@Test
public void parseSavesOneNewlineAfterStringInParsedSerialization() throws IOException {
    String string = "@string{bourdieu = {Bourdieu, Pierre}}" + OS.NEWLINE;
    ParserResult result = parser.parse(new StringReader(string + OS.NEWLINE + OS.NEWLINE));
    assertEquals(1, result.getDatabase().getStringCount());
    BibtexString s = result.getDatabase().getStringValues().iterator().next();
    assertEquals(string, s.getParsedSerialization());
}
Also used : ParserResult(org.jabref.logic.importer.ParserResult) StringReader(java.io.StringReader) BibtexString(org.jabref.model.entry.BibtexString) BibtexString(org.jabref.model.entry.BibtexString) Test(org.junit.Test)

Example 59 with ParserResult

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

the class BibtexParserTest method parsePrecedingComment.

@Test
public void parsePrecedingComment() 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 = parser.parse(new StringReader(bibtexEntry));
    Collection<BibEntry> entries = result.getDatabase().getEntries();
    assertEquals(1, entries.size());
    BibEntry entry = entries.iterator().next();
    assertEquals(Optional.of("test"), entry.getCiteKeyOptional());
    assertEquals(5, entry.getFieldNames().size());
    Set<String> fields = entry.getFieldNames();
    assertTrue(fields.contains("author"));
    assertEquals(Optional.of("Foo Bar"), entry.getField("author"));
    assertEquals(bibtexEntry, entry.getParsedSerialization());
}
Also used : ParserResult(org.jabref.logic.importer.ParserResult) BibEntry(org.jabref.model.entry.BibEntry) StringReader(java.io.StringReader) BibtexString(org.jabref.model.entry.BibtexString) Test(org.junit.Test)

Example 60 with ParserResult

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

the class BibtexParserTest method parseCombinesMultipleAuthorFields.

@Test
public void parseCombinesMultipleAuthorFields() throws IOException {
    ParserResult result = parser.parse(new StringReader("@article{test,author={Ed von Test},author={Second Author},author={Third Author}}"));
    Collection<BibEntry> c = result.getDatabase().getEntries();
    assertEquals(1, c.size());
    BibEntry e = c.iterator().next();
    assertEquals("article", e.getType());
    assertEquals(Optional.of("test"), e.getCiteKeyOptional());
    assertEquals(2, e.getFieldNames().size());
    assertEquals(Optional.of("Ed von Test and Second Author and Third Author"), e.getField("author"));
}
Also used : ParserResult(org.jabref.logic.importer.ParserResult) BibEntry(org.jabref.model.entry.BibEntry) 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