Skip to content

Commit

Permalink
Refactor BibtexParser
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasdiez committed Sep 4, 2016
1 parent a90bc21 commit 4fc8551
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 94 deletions.
6 changes: 2 additions & 4 deletions src/jmh/java/net/sf/jabref/benchmarks/Benchmarks.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,8 @@ public void init() throws Exception {

@Benchmark
public ParserResult parse() throws IOException {
StringReader bibtexStringReader = new StringReader(bibtexString);
BibtexParser parser = new BibtexParser(bibtexStringReader,
Globals.prefs.getImportFormatPreferences());
return parser.parse();
BibtexParser parser = new BibtexParser(Globals.prefs.getImportFormatPreferences());
return parser.parse(new StringReader(bibtexString));
}

@Benchmark
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/sf/jabref/cli/CrossrefFetcherEvaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public class CrossrefFetcherEvaluator {
public static void main(String[] args) throws IOException, InterruptedException {
Globals.prefs = JabRefPreferences.getInstance();
try (FileReader reader = new FileReader(args[0])) {
BibtexParser parser = new BibtexParser(reader, Globals.prefs.getImportFormatPreferences());
ParserResult result = parser.parse();
BibtexParser parser = new BibtexParser(Globals.prefs.getImportFormatPreferences());
ParserResult result = parser.parse(reader);
BibDatabase db = result.getDatabase();

List<BibEntry> entries = db.getEntries();
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/net/sf/jabref/gui/ClipBoardManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,8 @@ public List<BibEntry> extractBibEntriesFromClipboard() {
entry.ifPresent(result::add);
} else {
// parse bibtex string
BibtexParser bp = new BibtexParser(new StringReader(data),
Globals.prefs.getImportFormatPreferences());
BibDatabase db = bp.parse().getDatabase();
BibtexParser bp = new BibtexParser(Globals.prefs.getImportFormatPreferences());
BibDatabase db = bp.parse(new StringReader(data)).getDatabase();
LOGGER.info("Parsed " + db.getEntryCount() + " entries from clipboard text");
if (db.hasEntries()) {
result = db.getEntries();
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/net/sf/jabref/gui/entryeditor/EntryEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -759,11 +759,9 @@ public synchronized void switchTo(BibEntry switchEntry) {
}

private boolean storeSource() {
BibtexParser bibtexParser = new BibtexParser(new StringReader(source.getText()),
Globals.prefs.getImportFormatPreferences());

BibtexParser bibtexParser = new BibtexParser(Globals.prefs.getImportFormatPreferences());
try {
ParserResult parserResult = bibtexParser.parse();
ParserResult parserResult = bibtexParser.parse(new StringReader(source.getText()));
BibDatabase database = parserResult.getDatabase();

if (database.getEntryCount() > 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,8 @@ private String getCitationsFromUrl(String urlQuery, Map<String, JLabel> ids) thr
private BibEntry downloadEntry(String link) throws IOException {
try {
String s = new URLDownload(link).downloadToString(StandardCharsets.UTF_8);
BibtexParser bp = new BibtexParser(new StringReader(s),
Globals.prefs.getImportFormatPreferences());
ParserResult pr = bp.parse();
BibtexParser bp = new BibtexParser(Globals.prefs.getImportFormatPreferences());
ParserResult pr = bp.parse(new StringReader(s));
if ((pr != null) && (pr.getDatabase() != null)) {
Collection<BibEntry> entries = pr.getDatabase().getEntries();
if (entries.size() == 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package net.sf.jabref.logic.importer;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
Expand Down Expand Up @@ -53,8 +54,8 @@ default List<BibEntry> performSearch(String query) throws FetcherException {
return Collections.emptyList();
}

try (InputStream is = getQueryURL(query).openStream()) {
return getParser().parseEntries(is);
try (InputStream stream = new BufferedInputStream(getQueryURL(query).openStream())) {
return getParser().parseEntries(stream);
} catch (URISyntaxException e) {
throw new FetcherException("Search URI is malformed", e);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package net.sf.jabref.logic.importer.fetcher;

import java.io.IOException;
import java.io.StringReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Optional;

import net.sf.jabref.logic.importer.ImportFormatPreferences;
import net.sf.jabref.logic.importer.ParserResult;
import net.sf.jabref.logic.importer.fileformat.BibtexParser;
import net.sf.jabref.logic.net.URLDownload;
import net.sf.jabref.model.entry.BibEntry;
Expand Down Expand Up @@ -39,14 +37,7 @@ public static Optional<BibEntry> getEntry(String entryUrl, ImportFormatPreferenc

URL url = new URL(BibsonomyScraper.BIBSONOMY_SCRAPER + cleanURL + BibsonomyScraper.BIBSONOMY_SCRAPER_POST);
String bibtex = new URLDownload(url).downloadToString(StandardCharsets.UTF_8);
BibtexParser bp = new BibtexParser(new StringReader(bibtex), importFormatPreferences);
ParserResult pr = bp.parse();
if ((pr != null) && pr.getDatabase().hasEntries()) {
return Optional.of(pr.getDatabase().getEntries().iterator().next());
} else {
return Optional.empty();
}

return BibtexParser.singleFromString(bibtex, importFormatPreferences);
} catch (IOException ex) {
LOGGER.warn("Could not download entry", ex);
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package net.sf.jabref.logic.importer.fileformat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PushbackReader;
import java.io.Reader;
import java.io.StringReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
Expand All @@ -18,6 +23,8 @@
import net.sf.jabref.logic.bibtex.FieldContentParser;
import net.sf.jabref.logic.exporter.SavePreferences;
import net.sf.jabref.logic.importer.ImportFormatPreferences;
import net.sf.jabref.logic.importer.Parser;
import net.sf.jabref.logic.importer.ParserException;
import net.sf.jabref.logic.importer.ParserResult;
import net.sf.jabref.logic.importer.util.ParseException;
import net.sf.jabref.logic.l10n.Localization;
Expand Down Expand Up @@ -50,11 +57,11 @@
* <p>
* Can be used stand-alone.
*/
public class BibtexParser {
public class BibtexParser implements Parser {

private static final Log LOGGER = LogFactory.getLog(BibtexParser.class);

private final PushbackReader pushbackReader;
private PushbackReader pushbackReader;
private BibDatabase database;
private Map<String, EntryType> entryTypes;
private boolean eof;
Expand All @@ -66,11 +73,9 @@ public class BibtexParser {
private final ImportFormatPreferences importFormatPreferences;


public BibtexParser(Reader in, ImportFormatPreferences importFormatPreferences) {
Objects.requireNonNull(in);
public BibtexParser(ImportFormatPreferences importFormatPreferences) {
this.importFormatPreferences = Objects.requireNonNull(importFormatPreferences);
fieldContentParser = new FieldContentParser(importFormatPreferences.getFieldContentParserPreferences());
pushbackReader = new PushbackReader(in, BibtexParser.LOOKAHEAD);
}

/**
Expand All @@ -79,9 +84,9 @@ public BibtexParser(Reader in, ImportFormatPreferences importFormatPreferences)
* @param in the Reader to read from
* @throws IOException
*/
@Deprecated // inline this method
public static ParserResult parse(Reader in, ImportFormatPreferences importFormatPreferences) throws IOException {
BibtexParser parser = new BibtexParser(in, importFormatPreferences);
return parser.parse();
return new BibtexParser(importFormatPreferences).parse(in);
}

/**
Expand All @@ -92,10 +97,10 @@ public static ParserResult parse(Reader in, ImportFormatPreferences importFormat
*/
public static List<BibEntry> fromString(String bibtexString, ImportFormatPreferences importFormatPreferences) {
StringReader reader = new StringReader(bibtexString);
BibtexParser parser = new BibtexParser(reader, importFormatPreferences);
BibtexParser parser = new BibtexParser(importFormatPreferences);

try {
return parser.parse().getDatabase().getEntries();
return parser.parse(reader).getDatabase().getEntries();
} catch (Exception e) {
LOGGER.warn("BibtexParser.fromString(String): " + e.getMessage(), e);
return Collections.emptyList();
Expand All @@ -119,6 +124,16 @@ public static Optional<BibEntry> singleFromString(String bibtexString,
return Optional.of(entries.iterator().next());
}

@Override
public List<BibEntry> parseEntries(InputStream inputStream) throws ParserException {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
try {
return parse(reader).getDatabase().getEntries();
} catch (IOException e) {
throw new ParserException(e);
}
}

/**
* Will parse the BibTex-Data found when reading from reader. Ignores any encoding supplied in the file by
* "Encoding: myEncoding".
Expand All @@ -130,11 +145,10 @@ public static Optional<BibEntry> singleFromString(String bibtexString,
* @return ParserResult
* @throws IOException
*/
public ParserResult parse() throws IOException {
// If we already parsed this, just return it.
if (parserResult != null) {
return parserResult;
}
public ParserResult parse(Reader in) throws IOException {
Objects.requireNonNull(in);
pushbackReader = new PushbackReader(in, BibtexParser.LOOKAHEAD);

// Bibtex related contents.
initializeParserResult();

Expand Down
9 changes: 4 additions & 5 deletions src/test/java/net/sf/jabref/BibtexTestData.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,16 @@ public static BibEntry getBibtexEntry(ImportFormatPreferences importFormatPrefer
}

public static BibDatabase getBibtexDatabase(ImportFormatPreferences importFormatPreferences) throws IOException {
StringReader reader = new StringReader(
"@ARTICLE{HipKro03,\n" + " author = {Eric von Hippel and Georg von Krogh},\n"
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" + "}");
+ "\n" + " publisher = {INFORMS}\n" + "}";

BibtexParser parser = new BibtexParser(reader, importFormatPreferences);
ParserResult result = parser.parse();
BibtexParser parser = new BibtexParser(importFormatPreferences);
ParserResult result = parser.parse(new StringReader(article));

return result.getDatabase();
}
Expand Down
5 changes: 2 additions & 3 deletions src/test/java/net/sf/jabref/logic/bibtex/BibEntryAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,8 @@ public static void assertEquals(Class<?> clazz, String resourceName, List<BibEnt
private static List<BibEntry> getListFromInputStream(InputStream is) throws IOException {
ParserResult result;
try (Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8)) {
BibtexParser parser = new BibtexParser(reader,
JabRefPreferences.getInstance().getImportFormatPreferences());
result = parser.parse();
BibtexParser parser = new BibtexParser(JabRefPreferences.getInstance().getImportFormatPreferences());
result = parser.parse(reader);
}
Assert.assertNotNull(result);
Assert.assertFalse(result.isNullResult());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public static void setUp() {

@SuppressWarnings("unused")
@Test(expected = NullPointerException.class)
public void initalizationWithNullThrowsNullPointerException() {
new BibtexParser(null, importFormatPreferences);
public void parseWithNullThrowsNullPointerException() throws Exception {
new BibtexParser(importFormatPreferences).parse(null);
}

@Test
Expand Down Expand Up @@ -126,15 +126,6 @@ public void singleFromStringReturnsNullIfNoEntryRecognized() {
assertEquals(Optional.empty(), parsed);
}

@Test
public void parseTwoTimesReturnsSameResult() throws IOException {
BibtexParser parser = new BibtexParser(new StringReader("@article{test,author={Ed von Test}}"),
importFormatPreferences);
ParserResult result = parser.parse();

assertEquals(result, parser.parse());
}

@Test
public void parseRecognizesEntry() throws IOException {
ParserResult result = BibtexParser.parse(new StringReader("@article{test,author={Ed von Test}}"),
Expand Down Expand Up @@ -1394,18 +1385,18 @@ public void parseTrimsWhitespaceInEpilogueAfterEntry() throws IOException {

@Test
public void parseRecognizesSaveActionsAfterEntry() throws IOException {
BibtexParser parser = new BibtexParser(
BibtexParser parser = new BibtexParser(importFormatPreferences);

ParserResult parserResult = parser.parse(
new StringReader("@InProceedings{6055279,\n" + " Title = {Educational session 1},\n"
+ " Booktitle = {Custom Integrated Circuits Conference (CICC), 2011 IEEE},\n"
+ " Year = {2011},\n" + " Month = {Sept},\n"
+ " Pages = {1-7},\n"
+ " Abstract = {Start of the above-titled section of the conference proceedings record.},\n"
+ " DOI = {10.1109/CICC.2011.6055279},\n"
+ " ISSN = {0886-5930}\n" + "}\n" + "\n"
+ "@comment{jabref-meta: saveActions:enabled;title[lower_case]}"),
importFormatPreferences);

ParserResult parserResult = parser.parse();
+ "@comment{jabref-meta: saveActions:enabled;title[lower_case]}")
);

FieldFormatterCleanups saveActions = parserResult.getMetaData().getSaveActions().get();

Expand All @@ -1416,11 +1407,10 @@ public void parseRecognizesSaveActionsAfterEntry() throws IOException {

@Test
public void integrationTestSaveActions() throws IOException {
BibtexParser parser = new BibtexParser(
new StringReader("@comment{jabref-meta: saveActions:enabled;title[lower_case]}"),
importFormatPreferences);
BibtexParser parser = new BibtexParser(importFormatPreferences);

ParserResult parserResult = parser.parse();
ParserResult parserResult = parser.parse(
new StringReader("@comment{jabref-meta: saveActions:enabled;title[lower_case]}"));
FieldFormatterCleanups saveActions = parserResult.getMetaData().getSaveActions().get();

assertTrue(saveActions.isEnabled());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@

import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import net.sf.jabref.logic.importer.ParserResult;
import net.sf.jabref.logic.importer.fileformat.BibtexParser;
import net.sf.jabref.model.database.BibDatabase;
import net.sf.jabref.model.entry.BibEntry;
import net.sf.jabref.model.entry.BibtexEntryTypes;
import net.sf.jabref.preferences.JabRefPreferences;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

Expand All @@ -30,25 +25,23 @@ public class RegExpFileSearchTests {
@Before
public void setUp() throws IOException {

StringReader reader = new StringReader(
"@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(reader, JabRefPreferences.getInstance().getImportFormatPreferences());
ParserResult result = null;

result = parser.parse();

database = result.getDatabase();
entry = database.getEntryByKey("HipKro03").get();

Assert.assertNotNull(database);
Assert.assertNotNull(entry);
entry = new BibEntry();
entry.setType(BibtexEntryTypes.ARTICLE);
entry.setCiteKey("HipKro03");
entry.setField("author", "Eric von Hippel and Georg von Krogh");
entry.setField("title", "Open Source Software and the \"Private-Collective\" Innovation Model: Issues for Organization Science");
entry.setField("journal", "Organization Science");
entry.setField("year", "2003");
entry.setField("volume", "14");
entry.setField("pages", "209--223");
entry.setField("number", "2");
entry.setField("address", "Institute for Operations Research and the Management Sciences (INFORMS), Linthicum, Maryland, USA");
entry.setField("doi", "http://dx.doi.org/10.1287/orsc.14.2.209.14992");
entry.setField("issn", "1526-5455");
entry.setField("publisher", "INFORMS");

database = new BibDatabase();
database.insertEntry(entry);
}

@Test
Expand Down

0 comments on commit 4fc8551

Please sign in to comment.