diff --git a/CHANGELOG.md b/CHANGELOG.md index cff84bdce1c..3259d3b40aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `# ## [Unreleased] ### Changed +- Added fetcher for [MathSciNet](http://www.ams.org/mathscinet), [zbMATH](https://www.zbmath.org/) and [Astrophysics Data System](http://www.adsabs.harvard.edu/) - Implemented [#825](https://github.com/JabRef/jabref/issues/825): Search Bar across all bib files instead each having its own - Implemented [#573](https://github.com/JabRef/jabref/issues/573): Add key shortcut for global search (`ctrl+shift+f`, if the searchfield is empty it will be focused instead) - The search result Window will now show which entry belongs to which bib file diff --git a/src/jmh/java/net/sf/jabref/benchmarks/Benchmarks.java b/src/jmh/java/net/sf/jabref/benchmarks/Benchmarks.java index 0a844d41a17..fb093f2baf9 100644 --- a/src/jmh/java/net/sf/jabref/benchmarks/Benchmarks.java +++ b/src/jmh/java/net/sf/jabref/benchmarks/Benchmarks.java @@ -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 diff --git a/src/main/java/net/sf/jabref/cli/CrossrefFetcherEvaluator.java b/src/main/java/net/sf/jabref/cli/CrossrefFetcherEvaluator.java index 37de1b72ec7..b88eedfdda7 100644 --- a/src/main/java/net/sf/jabref/cli/CrossrefFetcherEvaluator.java +++ b/src/main/java/net/sf/jabref/cli/CrossrefFetcherEvaluator.java @@ -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 entries = db.getEntries(); diff --git a/src/main/java/net/sf/jabref/gui/ClipBoardManager.java b/src/main/java/net/sf/jabref/gui/ClipBoardManager.java index c50648e4b02..d7b3915d8ca 100644 --- a/src/main/java/net/sf/jabref/gui/ClipBoardManager.java +++ b/src/main/java/net/sf/jabref/gui/ClipBoardManager.java @@ -95,9 +95,8 @@ public List 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(); diff --git a/src/main/java/net/sf/jabref/gui/actions/CleanupAction.java b/src/main/java/net/sf/jabref/gui/actions/CleanupAction.java index a914f9749f7..50ba23cae03 100644 --- a/src/main/java/net/sf/jabref/gui/actions/CleanupAction.java +++ b/src/main/java/net/sf/jabref/gui/actions/CleanupAction.java @@ -5,7 +5,6 @@ import javax.swing.JOptionPane; -import net.sf.jabref.BibDatabaseContext; import net.sf.jabref.Globals; import net.sf.jabref.gui.BasePanel; import net.sf.jabref.gui.JabRefFrame; @@ -145,11 +144,8 @@ private int showDialog(CleanupPresetPanel presetPanel) { */ private void doCleanup(CleanupPreset preset, BibEntry entry, NamedCompound ce) { // Create and run cleaner - BibDatabaseContext bibDatabaseContext = panel.getBibDatabaseContext(); - CleanupWorker cleaner = new CleanupWorker(bibDatabaseContext, - Globals.prefs.get(JabRefPreferences.IMPORT_FILENAMEPATTERN), - Globals.prefs.getLayoutFormatterPreferences(Globals.journalAbbreviationLoader), - Globals.prefs.getFileDirectoryPreferences()); + CleanupWorker cleaner = new CleanupWorker(panel.getBibDatabaseContext(), preferences.getCleanupPreferences( + Globals.journalAbbreviationLoader)); List changes = cleaner.cleanup(preset, entry); unsuccessfulRenames = cleaner.getUnsuccessfulRenames(); @@ -163,4 +159,5 @@ private void doCleanup(CleanupPreset preset, BibEntry entry, NamedCompound ce) { ce.addEdit(new UndoableFieldChange(change)); } } + } diff --git a/src/main/java/net/sf/jabref/gui/entryeditor/EntryEditor.java b/src/main/java/net/sf/jabref/gui/entryeditor/EntryEditor.java index c767fc8301b..e502afb8a5b 100644 --- a/src/main/java/net/sf/jabref/gui/entryeditor/EntryEditor.java +++ b/src/main/java/net/sf/jabref/gui/entryeditor/EntryEditor.java @@ -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) { diff --git a/src/main/java/net/sf/jabref/gui/importer/fetcher/EntryFetchers.java b/src/main/java/net/sf/jabref/gui/importer/fetcher/EntryFetchers.java index f3df013e6cf..a8c074cdac6 100644 --- a/src/main/java/net/sf/jabref/gui/importer/fetcher/EntryFetchers.java +++ b/src/main/java/net/sf/jabref/gui/importer/fetcher/EntryFetchers.java @@ -8,9 +8,12 @@ import net.sf.jabref.Globals; import net.sf.jabref.logic.importer.IdBasedFetcher; import net.sf.jabref.logic.importer.fetcher.ArXiv; +import net.sf.jabref.logic.importer.fetcher.AstrophysicsDataSystem; import net.sf.jabref.logic.importer.fetcher.DiVA; import net.sf.jabref.logic.importer.fetcher.GvkFetcher; import net.sf.jabref.logic.importer.fetcher.IsbnFetcher; +import net.sf.jabref.logic.importer.fetcher.MathSciNet; +import net.sf.jabref.logic.importer.fetcher.zbMATH; import net.sf.jabref.logic.journals.JournalAbbreviationLoader; public class EntryFetchers { @@ -34,6 +37,10 @@ public EntryFetchers(JournalAbbreviationLoader abbreviationLoader) { entryFetchers.add(new SearchBasedEntryFetcher(new ArXiv())); entryFetchers.add(new SearchBasedEntryFetcher(new GvkFetcher())); + entryFetchers.add( + new SearchBasedEntryFetcher(new AstrophysicsDataSystem(Globals.prefs.getImportFormatPreferences()))); + entryFetchers.add(new SearchBasedEntryFetcher(new MathSciNet(Globals.prefs.getImportFormatPreferences()))); + entryFetchers.add(new SearchBasedEntryFetcher(new zbMATH(Globals.prefs.getImportFormatPreferences()))); } public List getEntryFetchers() { diff --git a/src/main/java/net/sf/jabref/gui/importer/fetcher/GoogleScholarFetcher.java b/src/main/java/net/sf/jabref/gui/importer/fetcher/GoogleScholarFetcher.java index 8b290eb0372..ed07f0082a9 100644 --- a/src/main/java/net/sf/jabref/gui/importer/fetcher/GoogleScholarFetcher.java +++ b/src/main/java/net/sf/jabref/gui/importer/fetcher/GoogleScholarFetcher.java @@ -242,9 +242,8 @@ private String getCitationsFromUrl(String urlQuery, Map 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 entries = pr.getDatabase().getEntries(); if (entries.size() == 1) { diff --git a/src/main/java/net/sf/jabref/logic/bibtex/FieldContentParser.java b/src/main/java/net/sf/jabref/logic/bibtex/FieldContentParser.java index f8b1e92e289..fafd9437791 100644 --- a/src/main/java/net/sf/jabref/logic/bibtex/FieldContentParser.java +++ b/src/main/java/net/sf/jabref/logic/bibtex/FieldContentParser.java @@ -1,6 +1,7 @@ package net.sf.jabref.logic.bibtex; import java.util.HashSet; +import java.util.Objects; import java.util.Set; import java.util.regex.Pattern; @@ -21,6 +22,8 @@ public class FieldContentParser { public FieldContentParser(FieldContentParserPreferences prefs) { + Objects.requireNonNull(prefs); + multiLineFields = new HashSet<>(); // the following two are also coded in net.sf.jabref.logic.bibtex.LatexFieldFormatter.format(String, String) multiLineFields.add(FieldName.ABSTRACT); diff --git a/src/main/java/net/sf/jabref/logic/cleanup/CleanupPreferences.java b/src/main/java/net/sf/jabref/logic/cleanup/CleanupPreferences.java new file mode 100644 index 00000000000..ffb56f41e28 --- /dev/null +++ b/src/main/java/net/sf/jabref/logic/cleanup/CleanupPreferences.java @@ -0,0 +1,30 @@ +package net.sf.jabref.logic.cleanup; + +import net.sf.jabref.FileDirectoryPreferences; +import net.sf.jabref.logic.layout.LayoutFormatterPreferences; + +public class CleanupPreferences { + + private final String fileNamePattern; + private final LayoutFormatterPreferences layoutFormatterPreferences; + private final FileDirectoryPreferences fileDirectoryPreferences; + + public CleanupPreferences(String fileNamePattern, LayoutFormatterPreferences layoutFormatterPreferences, + FileDirectoryPreferences fileDirectoryPreferences) { + this.fileNamePattern = fileNamePattern; + this.layoutFormatterPreferences = layoutFormatterPreferences; + this.fileDirectoryPreferences = fileDirectoryPreferences; + } + + public String getFileNamePattern() { + return fileNamePattern; + } + + public LayoutFormatterPreferences getLayoutFormatterPreferences() { + return layoutFormatterPreferences; + } + + public FileDirectoryPreferences getFileDirectoryPreferences() { + return fileDirectoryPreferences; + } +} diff --git a/src/main/java/net/sf/jabref/logic/cleanup/CleanupWorker.java b/src/main/java/net/sf/jabref/logic/cleanup/CleanupWorker.java index 56c448dd112..0e74d56d41c 100644 --- a/src/main/java/net/sf/jabref/logic/cleanup/CleanupWorker.java +++ b/src/main/java/net/sf/jabref/logic/cleanup/CleanupWorker.java @@ -19,12 +19,11 @@ public class CleanupWorker { private int unsuccessfulRenames; - public CleanupWorker(BibDatabaseContext databaseContext, String fileNamePattern, LayoutFormatterPreferences prefs, - FileDirectoryPreferences fileDirectoryPreferences) { + public CleanupWorker(BibDatabaseContext databaseContext, CleanupPreferences cleanupPreferences) { this.databaseContext = databaseContext; - this.fileNamePattern = fileNamePattern; - this.prefs = prefs; - this.fileDirectoryPreferences = fileDirectoryPreferences; + this.fileNamePattern = cleanupPreferences.getFileNamePattern(); + this.prefs = cleanupPreferences.getLayoutFormatterPreferences(); + this.fileDirectoryPreferences = cleanupPreferences.getFileDirectoryPreferences(); } public int getUnsuccessfulRenames() { diff --git a/src/main/java/net/sf/jabref/logic/cleanup/MoveFieldCleanup.java b/src/main/java/net/sf/jabref/logic/cleanup/MoveFieldCleanup.java new file mode 100644 index 00000000000..84eb20c769b --- /dev/null +++ b/src/main/java/net/sf/jabref/logic/cleanup/MoveFieldCleanup.java @@ -0,0 +1,31 @@ +package net.sf.jabref.logic.cleanup; + +import java.util.List; +import java.util.Optional; + +import net.sf.jabref.logic.util.OptionalUtil; +import net.sf.jabref.model.FieldChange; +import net.sf.jabref.model.entry.BibEntry; + +/** + * Moves the content of one field to another field. + */ +public class MoveFieldCleanup implements CleanupJob { + + private String sourceField; + private String targetField; + + public MoveFieldCleanup(String sourceField, String targetField) { + this.sourceField = sourceField; + this.targetField = targetField; + } + + @Override + public List cleanup(BibEntry entry) { + + Optional setFieldChange = entry.getField(sourceField).flatMap( + value -> entry.setField(targetField, value)); + Optional clearFieldChange = entry.clearField(sourceField); + return OptionalUtil.toList(setFieldChange, clearFieldChange); + } +} diff --git a/src/main/java/net/sf/jabref/logic/importer/EntryBasedFetcher.java b/src/main/java/net/sf/jabref/logic/importer/EntryBasedFetcher.java new file mode 100644 index 00000000000..408be47bd1d --- /dev/null +++ b/src/main/java/net/sf/jabref/logic/importer/EntryBasedFetcher.java @@ -0,0 +1,21 @@ +package net.sf.jabref.logic.importer; + +import java.util.List; + +import net.sf.jabref.model.entry.BibEntry; + +/** + * Searches web resources for bibliographic information based on a {@link BibEntry}. + * Useful to complete an existing entry with fetched information. + * May return multiple search hits. + */ +public interface EntryBasedFetcher extends WebFetcher { + + /** + * Looks for hits which are matched by the given {@link BibEntry}. + * + * @param entry entry to search bibliographic information for + * @return a list of {@link BibEntry}, which are matched by the query (may be empty) + */ + List performSearch(BibEntry entry) throws FetcherException; +} diff --git a/src/main/java/net/sf/jabref/logic/importer/EntryBasedParserFetcher.java b/src/main/java/net/sf/jabref/logic/importer/EntryBasedParserFetcher.java new file mode 100644 index 00000000000..780ae827d9a --- /dev/null +++ b/src/main/java/net/sf/jabref/logic/importer/EntryBasedParserFetcher.java @@ -0,0 +1,71 @@ +package net.sf.jabref.logic.importer; + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.List; +import java.util.Objects; + +import net.sf.jabref.logic.formatter.Formatter; +import net.sf.jabref.model.entry.BibEntry; + +/** + * Provides a convenient interface for entry-based fetcher, which follow the usual three-step procedure: + * 1. Open a URL based on the entry + * 2. Parse the response to get a list of {@link BibEntry} + * 3. Post-process fetched entries + */ +public interface EntryBasedParserFetcher extends EntryBasedFetcher { + + /** + * Constructs a URL based on the {@link BibEntry}. + * @param entry the entry to look information for + */ + URL getURLForEntry(BibEntry entry) throws URISyntaxException, MalformedURLException, FetcherException; + + /** + * Returns the parser used to convert the response to a list of {@link BibEntry}. + */ + Parser getParser(); + + /** + * Performs a cleanup of the fetched entry. + * + * Only systematic errors of the fetcher should be corrected here + * (i.e. if information is consistently contained in the wrong field or the wrong format) + * but not cosmetic issues which may depend on the user's taste (for example, LateX code vs HTML in the abstract). + * + * Try to reuse existing {@link Formatter} for the cleanup. For example, + * {@code new FieldFormatterCleanup(FieldName.TITLE, new RemoveBracesFormatter()).cleanup(entry);} + * + * By default, no cleanup is done. + * @param entry the entry to be cleaned-up + */ + default void doPostCleanup(BibEntry entry) { + // Do nothing by default + } + + @Override + default List performSearch(BibEntry entry) throws FetcherException { + Objects.requireNonNull(entry); + + try (InputStream stream = new BufferedInputStream(getURLForEntry(entry).openStream())) { + List fetchedEntries = getParser().parseEntries(stream); + + // Post-cleanup + fetchedEntries.forEach(this::doPostCleanup); + + return fetchedEntries; + } catch (URISyntaxException e) { + throw new FetcherException("Search URI is malformed", e); + } catch (IOException e) { + // TODO: Catch HTTP Response 401 errors and report that user has no rights to access resource + throw new FetcherException("An I/O exception occurred", e); + } catch (ParserException e) { + throw new FetcherException("An internal parser error occurred", e); + } + } +} diff --git a/src/main/java/net/sf/jabref/logic/importer/Parser.java b/src/main/java/net/sf/jabref/logic/importer/Parser.java new file mode 100644 index 00000000000..a92b2fb8819 --- /dev/null +++ b/src/main/java/net/sf/jabref/logic/importer/Parser.java @@ -0,0 +1,14 @@ +package net.sf.jabref.logic.importer; + +import java.io.InputStream; +import java.util.List; + +import net.sf.jabref.model.entry.BibEntry; + +/** + * A parser converts an {@link InputStream} into a list of {@link BibEntry}. + */ +public interface Parser { + + List parseEntries(InputStream inputStream) throws ParserException; +} diff --git a/src/main/java/net/sf/jabref/logic/importer/ParserException.java b/src/main/java/net/sf/jabref/logic/importer/ParserException.java new file mode 100644 index 00000000000..91bda29b26d --- /dev/null +++ b/src/main/java/net/sf/jabref/logic/importer/ParserException.java @@ -0,0 +1,17 @@ +package net.sf.jabref.logic.importer; + +public class ParserException extends Exception { + + + public ParserException(String errorMessage, Exception cause) { + super(errorMessage, cause); + } + + public ParserException(String errorMessage) { + super(errorMessage); + } + + public ParserException(Exception cause) { + super(cause); + } +} diff --git a/src/main/java/net/sf/jabref/logic/importer/SearchBasedParserFetcher.java b/src/main/java/net/sf/jabref/logic/importer/SearchBasedParserFetcher.java new file mode 100644 index 00000000000..f63332689c0 --- /dev/null +++ b/src/main/java/net/sf/jabref/logic/importer/SearchBasedParserFetcher.java @@ -0,0 +1,75 @@ +package net.sf.jabref.logic.importer; + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.Collections; +import java.util.List; + +import net.sf.jabref.logic.formatter.Formatter; +import net.sf.jabref.model.entry.BibEntry; + +import org.jsoup.helper.StringUtil; + +/** + * Provides a convenient interface for search-based fetcher, which follow the usual three-step procedure: + * 1. Open a URL based on the search query + * 2. Parse the response to get a list of {@link BibEntry} + * 3. Post-process fetched entries + */ +public interface SearchBasedParserFetcher extends SearchBasedFetcher { + + /** + * Constructs a URL based on the query. + * @param query the search query + */ + URL getURLForQuery(String query) throws URISyntaxException, MalformedURLException, FetcherException; + + /** + * Returns the parser used to convert the response to a list of {@link BibEntry}. + */ + Parser getParser(); + + /** + * Performs a cleanup of the fetched entry. + * + * Only systematic errors of the fetcher should be corrected here + * (i.e. if information is consistently contained in the wrong field or the wrong format) + * but not cosmetic issues which may depend on the user's taste (for example, LateX code vs HTML in the abstract). + * + * Try to reuse existing {@link Formatter} for the cleanup. For example, + * {@code new FieldFormatterCleanup(FieldName.TITLE, new RemoveBracesFormatter()).cleanup(entry);} + * + * By default, no cleanup is done. + * @param entry the entry to be cleaned-up + */ + default void doPostCleanup(BibEntry entry) { + // Do nothing by default + } + + @Override + default List performSearch(String query) throws FetcherException { + if (StringUtil.isBlank(query)) { + return Collections.emptyList(); + } + + try (InputStream stream = new BufferedInputStream(getURLForQuery(query).openStream())) { + List fetchedEntries = getParser().parseEntries(stream); + + // Post-cleanup + fetchedEntries.forEach(this::doPostCleanup); + + return fetchedEntries; + } catch (URISyntaxException e) { + throw new FetcherException("Search URI is malformed", e); + } catch (IOException e) { + // TODO: Catch HTTP Response 401 errors and report that user has no rights to access resource + throw new FetcherException("An I/O exception occurred", e); + } catch (ParserException e) { + throw new FetcherException("An internal parser error occurred", e); + } + } +} diff --git a/src/main/java/net/sf/jabref/logic/importer/WebFetcher.java b/src/main/java/net/sf/jabref/logic/importer/WebFetcher.java index 19389189dde..0122373f5e4 100644 --- a/src/main/java/net/sf/jabref/logic/importer/WebFetcher.java +++ b/src/main/java/net/sf/jabref/logic/importer/WebFetcher.java @@ -20,5 +20,7 @@ public interface WebFetcher { * * @return the {@link HelpFile} enum constant for the help page */ - HelpFile getHelpPage(); + default HelpFile getHelpPage() { + return null; // no help page by default + } } diff --git a/src/main/java/net/sf/jabref/logic/importer/fetcher/AstrophysicsDataSystem.java b/src/main/java/net/sf/jabref/logic/importer/fetcher/AstrophysicsDataSystem.java new file mode 100644 index 00000000000..a4b3efb0cb8 --- /dev/null +++ b/src/main/java/net/sf/jabref/logic/importer/fetcher/AstrophysicsDataSystem.java @@ -0,0 +1,107 @@ +package net.sf.jabref.logic.importer.fetcher; + +import java.net.MalformedURLException; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.Objects; + +import net.sf.jabref.logic.cleanup.FieldFormatterCleanup; +import net.sf.jabref.logic.formatter.bibtexfields.ClearFormatter; +import net.sf.jabref.logic.formatter.bibtexfields.NormalizeNamesFormatter; +import net.sf.jabref.logic.formatter.bibtexfields.RemoveBracesFormatter; +import net.sf.jabref.logic.importer.EntryBasedParserFetcher; +import net.sf.jabref.logic.importer.FetcherException; +import net.sf.jabref.logic.importer.ImportFormatPreferences; +import net.sf.jabref.logic.importer.Parser; +import net.sf.jabref.logic.importer.SearchBasedParserFetcher; +import net.sf.jabref.logic.importer.fileformat.BibtexParser; +import net.sf.jabref.model.entry.BibEntry; +import net.sf.jabref.model.entry.FieldName; + +import org.apache.http.client.utils.URIBuilder; + +/** + * Fetches data from the SAO/NASA Astrophysics Data System (http://www.adsabs.harvard.edu/) + * + * Search query-based: http://adsabs.harvard.edu/basic_search.html + * Entry -based: http://adsabs.harvard.edu/abstract_service.html + * + * There is also a new API (https://github.com/adsabs/adsabs-dev-api) but it returns JSON + * (or at least needs multiple calls to get BibTeX, status: September 2016) + */ +public class AstrophysicsDataSystem implements SearchBasedParserFetcher, EntryBasedParserFetcher { + + private static String API_QUERY_URL = "http://adsabs.harvard.edu/cgi-bin/nph-basic_connect"; + private static String API_ENTRY_URL = "http://adsabs.harvard.edu/cgi-bin/nph-abs_connect"; + private final ImportFormatPreferences preferences; + + public AstrophysicsDataSystem(ImportFormatPreferences preferences) { + this.preferences = Objects.requireNonNull(preferences); + } + + @Override + public String getName() { + return "SAO/NASA Astrophysics Data System"; + } + + private URIBuilder getBaseUrl(String apiUrl) throws URISyntaxException { + URIBuilder uriBuilder = new URIBuilder(apiUrl); + uriBuilder.addParameter("data_type", "BIBTEXPLUS"); + uriBuilder.addParameter("start_nr", String.valueOf(1)); + uriBuilder.addParameter("nr_to_return", String.valueOf(200)); + return uriBuilder; + } + + @Override + public URL getURLForQuery(String query) throws URISyntaxException, MalformedURLException, FetcherException { + URIBuilder uriBuilder = getBaseUrl(API_QUERY_URL); + uriBuilder.addParameter("qsearch", query); + return uriBuilder.build().toURL(); + } + + @Override + public URL getURLForEntry(BibEntry entry) throws URISyntaxException, MalformedURLException, FetcherException { + URIBuilder uriBuilder = getBaseUrl(API_ENTRY_URL); + + // Search astronomy + physics + arXiv db + uriBuilder.addParameter("db_key", "AST"); + uriBuilder.addParameter("db_key", "PHY"); + uriBuilder.addParameter("db_key", "PRE"); + + // Add title search + entry.getFieldOrAlias(FieldName.TITLE).ifPresent(title -> { + uriBuilder.addParameter("ttl_logic", "OR"); + uriBuilder.addParameter("title", title); + uriBuilder.addParameter("ttl_syn", "YES"); // Synonym replacement + uriBuilder.addParameter("ttl_wt", "0.3"); // Weight + uriBuilder.addParameter("ttl_wgt", "YES"); // Consider Weight + }); + + // Add author search + entry.getFieldOrAlias(FieldName.AUTHOR).ifPresent(author -> { + uriBuilder.addParameter("aut_logic", "OR"); + uriBuilder.addParameter("author", author); + uriBuilder.addParameter("aut_syn", "YES"); // Synonym replacement + uriBuilder.addParameter("aut_wt", "1.0"); // Weight + uriBuilder.addParameter("aut_wgt", "YES"); // Consider weight + }); + + return uriBuilder.build().toURL(); + } + + @Override + public Parser getParser() { + return new BibtexParser(preferences); + } + + @Override + public void doPostCleanup(BibEntry entry) { + new FieldFormatterCleanup(FieldName.ABSTRACT, new RemoveBracesFormatter()).cleanup(entry); + new FieldFormatterCleanup(FieldName.TITLE, new RemoveBracesFormatter()).cleanup(entry); + new FieldFormatterCleanup(FieldName.AUTHOR, new NormalizeNamesFormatter()).cleanup(entry); + + // Remove url to ADS page + new FieldFormatterCleanup("adsnote", new ClearFormatter()).cleanup(entry); + new FieldFormatterCleanup("adsurl", new ClearFormatter()).cleanup(entry); + } +} diff --git a/src/main/java/net/sf/jabref/logic/importer/fetcher/BibsonomyScraper.java b/src/main/java/net/sf/jabref/logic/importer/fetcher/BibsonomyScraper.java index 8672c8d8a96..f2a2ca52976 100644 --- a/src/main/java/net/sf/jabref/logic/importer/fetcher/BibsonomyScraper.java +++ b/src/main/java/net/sf/jabref/logic/importer/fetcher/BibsonomyScraper.java @@ -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; @@ -39,14 +37,7 @@ public static Optional 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(); diff --git a/src/main/java/net/sf/jabref/logic/importer/fetcher/GvkFetcher.java b/src/main/java/net/sf/jabref/logic/importer/fetcher/GvkFetcher.java index c7167ce9390..615cdd62890 100644 --- a/src/main/java/net/sf/jabref/logic/importer/fetcher/GvkFetcher.java +++ b/src/main/java/net/sf/jabref/logic/importer/fetcher/GvkFetcher.java @@ -1,30 +1,24 @@ package net.sf.jabref.logic.importer.fetcher; -import java.io.IOException; -import java.io.InputStream; import java.net.MalformedURLException; import java.net.URISyntaxException; import java.net.URL; import java.util.Arrays; import java.util.Collection; -import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; -import javax.xml.parsers.ParserConfigurationException; - import net.sf.jabref.logic.help.HelpFile; import net.sf.jabref.logic.importer.FetcherException; -import net.sf.jabref.logic.importer.SearchBasedFetcher; -import net.sf.jabref.model.entry.BibEntry; +import net.sf.jabref.logic.importer.Parser; +import net.sf.jabref.logic.importer.SearchBasedParserFetcher; +import net.sf.jabref.logic.importer.fileformat.GvkParser; import org.apache.http.client.utils.URIBuilder; -import org.jsoup.helper.StringUtil; -import org.xml.sax.SAXException; -public class GvkFetcher implements SearchBasedFetcher { +public class GvkFetcher implements SearchBasedParserFetcher { private static final String URL_PATTERN = "http://sru.gbv.de/gvk?"; @@ -74,7 +68,8 @@ protected String getSearchQueryString(String query) throws FetcherException { } } - protected URL getQueryURL(String query) throws URISyntaxException, MalformedURLException, FetcherException { + @Override + public URL getURLForQuery(String query) throws URISyntaxException, MalformedURLException, FetcherException { String gvkQuery = getSearchQueryString(query); URIBuilder uriBuilder = new URIBuilder(URL_PATTERN); uriBuilder.addParameter("version", "1.1"); @@ -86,21 +81,9 @@ protected URL getQueryURL(String query) throws URISyntaxException, MalformedURLE return uriBuilder.build().toURL(); } - @Override - public List performSearch(String query) throws FetcherException { - if (StringUtil.isBlank(query)) { - return Collections.emptyList(); - } - - try (InputStream is = getQueryURL(query).openStream()) { - return (new GVKParser()).parseEntries(is); - } catch (URISyntaxException e) { - throw new FetcherException("URI malformed error", e); - } catch (IOException e) { - throw new FetcherException("An I/O exception occurred", e); - } catch (SAXException | ParserConfigurationException e) { - throw new FetcherException("An internal parser error occurred", e); - } + public Parser getParser() { + return new GvkParser(); } + } diff --git a/src/main/java/net/sf/jabref/logic/importer/fetcher/MathSciNet.java b/src/main/java/net/sf/jabref/logic/importer/fetcher/MathSciNet.java new file mode 100644 index 00000000000..6caf5a72d8d --- /dev/null +++ b/src/main/java/net/sf/jabref/logic/importer/fetcher/MathSciNet.java @@ -0,0 +1,101 @@ +package net.sf.jabref.logic.importer.fetcher; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.net.MalformedURLException; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +import net.sf.jabref.logic.cleanup.FieldFormatterCleanup; +import net.sf.jabref.logic.cleanup.MoveFieldCleanup; +import net.sf.jabref.logic.formatter.bibtexfields.ClearFormatter; +import net.sf.jabref.logic.importer.EntryBasedParserFetcher; +import net.sf.jabref.logic.importer.FetcherException; +import net.sf.jabref.logic.importer.ImportFormatPreferences; +import net.sf.jabref.logic.importer.Parser; +import net.sf.jabref.logic.importer.SearchBasedParserFetcher; +import net.sf.jabref.logic.importer.fileformat.BibtexParser; +import net.sf.jabref.logic.util.OS; +import net.sf.jabref.model.entry.BibEntry; +import net.sf.jabref.model.entry.FieldName; + +import org.apache.http.client.utils.URIBuilder; + +/** + * Fetches data from the MathSciNet (http://www.ams.org/mathscinet) + */ +public class MathSciNet implements SearchBasedParserFetcher, EntryBasedParserFetcher { + + private final ImportFormatPreferences preferences; + + public MathSciNet(ImportFormatPreferences preferences) { + this.preferences = Objects.requireNonNull(preferences); + } + + @Override + public String getName() { + return "MathSciNet"; + } + + /** + * We use MR Lookup (http://www.ams.org/mrlookup) instead of the usual search since this tool is also available + * without subscription and, moreover, is optimized for finding a publication based on partial information. + */ + @Override + public URL getURLForEntry(BibEntry entry) throws URISyntaxException, MalformedURLException, FetcherException { + URIBuilder uriBuilder = new URIBuilder("http://www.ams.org/mrlookup"); + uriBuilder.addParameter("format", "bibtex"); + + entry.getFieldOrAlias(FieldName.TITLE).ifPresent(title -> uriBuilder.addParameter("ti", title)); + entry.getFieldOrAlias(FieldName.AUTHOR).ifPresent(author -> uriBuilder.addParameter("au", author)); + entry.getFieldOrAlias(FieldName.JOURNAL).ifPresent(journal -> uriBuilder.addParameter("jrnl", journal)); + entry.getFieldOrAlias(FieldName.YEAR).ifPresent(year -> uriBuilder.addParameter("year", year)); + + return uriBuilder.build().toURL(); + } + + @Override + public URL getURLForQuery(String query) throws URISyntaxException, MalformedURLException, FetcherException { + URIBuilder uriBuilder = new URIBuilder("http://www.ams.org/mathscinet/search/publications.html"); + uriBuilder.addParameter("pg7", "ALLF"); // search all fields + uriBuilder.addParameter("s7", query); // query + uriBuilder.addParameter("r", "1"); // start index + uriBuilder.addParameter("extend", "1"); // should return up to 100 items (instead of default 10) + uriBuilder.addParameter("fmt", "bibtex"); // BibTeX format + return uriBuilder.build().toURL(); + } + + @Override + public Parser getParser() { + + // MathSciNet returns the BibTeX result embedded in HTML + // So we extract the BibTeX string from the
bibtex
tags and pass the content to the BibTeX parser + return inputStream -> { + String response = new BufferedReader(new InputStreamReader(inputStream)).lines().collect( + Collectors.joining(OS.NEWLINE)); + + List entries = new ArrayList<>(); + BibtexParser bibtexParser = new BibtexParser(preferences); + Pattern pattern = Pattern.compile("
(?s)(.*)
"); + Matcher matcher = pattern.matcher(response); + while (matcher.find()) { + String bibtexEntryString = matcher.group(); + entries.addAll(bibtexParser.parseEntries(bibtexEntryString)); + } + return entries; + }; + } + + @Override + public void doPostCleanup(BibEntry entry) { + new MoveFieldCleanup("fjournal", FieldName.JOURNAL).cleanup(entry); + new MoveFieldCleanup("mrclass", FieldName.KEYWORDS).cleanup(entry); + new FieldFormatterCleanup(FieldName.URL, new ClearFormatter()).cleanup(entry); + } +} diff --git a/src/main/java/net/sf/jabref/logic/importer/fetcher/zbMATH.java b/src/main/java/net/sf/jabref/logic/importer/fetcher/zbMATH.java new file mode 100644 index 00000000000..26ba101591a --- /dev/null +++ b/src/main/java/net/sf/jabref/logic/importer/fetcher/zbMATH.java @@ -0,0 +1,131 @@ +package net.sf.jabref.logic.importer.fetcher; + +import java.net.MalformedURLException; +import java.net.URISyntaxException; +import java.net.URL; +import java.security.SecureRandom; +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; +import java.util.Objects; + +import javax.net.ssl.HttpsURLConnection; +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; + +import net.sf.jabref.logic.cleanup.FieldFormatterCleanup; +import net.sf.jabref.logic.cleanup.MoveFieldCleanup; +import net.sf.jabref.logic.formatter.bibtexfields.RemoveBracesFormatter; +import net.sf.jabref.logic.importer.FetcherException; +import net.sf.jabref.logic.importer.ImportFormatPreferences; +import net.sf.jabref.logic.importer.Parser; +import net.sf.jabref.logic.importer.SearchBasedParserFetcher; +import net.sf.jabref.logic.importer.fileformat.BibtexParser; +import net.sf.jabref.model.entry.BibEntry; +import net.sf.jabref.model.entry.FieldName; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.http.client.utils.URIBuilder; + +/** + * Fetches data from the Zentralblatt Math (https://www.zbmath.org/) + */ +public class zbMATH implements SearchBasedParserFetcher { + + private static final Log LOGGER = LogFactory.getLog(zbMATH.class); + + private final ImportFormatPreferences preferences; + + public zbMATH(ImportFormatPreferences preferences) { + this.preferences = Objects.requireNonNull(preferences); + } + + @Override + public String getName() { + return "zbMATH"; + } + + /** + * TODO: Implement EntryBasedParserFetcher + * We use the zbMATH Citation matcher (https://www.zbmath.org/citationmatching/) + * instead of the usual search since this tool is optimized for finding a publication based on partial information. + */ + /* + @Override + public URL getURLForEntry(BibEntry entry) throws URISyntaxException, MalformedURLException, FetcherException { + // Example: https://zbmath.org/citationmatching/match?q=Ratiu + } + */ + + @Override + public URL getURLForQuery(String query) throws URISyntaxException, MalformedURLException, FetcherException { + URIBuilder uriBuilder = new URIBuilder("https://zbmath.org/bibtexoutput/"); + uriBuilder.addParameter("q", query); // search all fields + uriBuilder.addParameter("start", "0"); // start index + uriBuilder.addParameter("count", "200"); // should return up to 200 items (instead of default 100) + + fixSSLVerification(); + + return uriBuilder.build().toURL(); + } + + /** + * Older java VMs does not automatically trust the zbMATH certificate. In this case the following exception is thrown: + * sun.security.validator.ValidatorException: PKIX path building failed: + * sun.security.provider.certpath.SunCertPathBuilderException: unable to find + * valid certification path to requested target + * JM > 8u101 may trust the certificate by default according to http://stackoverflow.com/a/34111150/873661 + * + * We will fix this issue by accepting all (!) certificates. This is ugly; but as JabRef does not rely on + * security-relevant information this is kind of OK (no, actually it is not...). + * + * Taken from http://stackoverflow.com/a/6055903/873661 + */ + private void fixSSLVerification() { + + LOGGER.warn("Fix SSL exception by accepting ALL certificates"); + + // Create a trust manager that does not validate certificate chains + TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { + + @Override + public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { + + } + + @Override + public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { + + } + + @Override + public X509Certificate[] getAcceptedIssuers() { + return new X509Certificate[0]; + } + } }; + + // Install the all-trusting trust manager + try { + SSLContext sc = SSLContext.getInstance("TLS"); + sc.init(null, trustAllCerts, new SecureRandom()); + HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); + } catch (Exception e) { + LOGGER.error("SSL problem", e); + } + } + + @Override + public Parser getParser() { + return new BibtexParser(preferences); + } + + @Override + public void doPostCleanup(BibEntry entry) { + new MoveFieldCleanup("msc2010", FieldName.KEYWORDS).cleanup(entry); + new MoveFieldCleanup("fjournal", FieldName.JOURNAL).cleanup(entry); + new FieldFormatterCleanup(FieldName.JOURNAL, new RemoveBracesFormatter()).cleanup(entry); + new FieldFormatterCleanup(FieldName.TITLE, new RemoveBracesFormatter()).cleanup(entry); + } + +} diff --git a/src/main/java/net/sf/jabref/logic/importer/fileformat/BibtexParser.java b/src/main/java/net/sf/jabref/logic/importer/fileformat/BibtexParser.java index 4e3a39eecdb..383c57fd2b1 100644 --- a/src/main/java/net/sf/jabref/logic/importer/fileformat/BibtexParser.java +++ b/src/main/java/net/sf/jabref/logic/importer/fileformat/BibtexParser.java @@ -1,9 +1,13 @@ 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.StandardCharsets; import java.util.Collection; import java.util.Collections; import java.util.Deque; @@ -18,6 +22,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; @@ -50,11 +56,11 @@ *

* 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 entryTypes; private boolean eof; @@ -66,11 +72,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); } /** @@ -78,10 +82,10 @@ 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); } /** @@ -89,13 +93,14 @@ public static ParserResult parse(Reader in, ImportFormatPreferences importFormat * * @param bibtexString * @return Returns returns an empty collection if no entries where found or if an error occurred. + * @deprecated use parseEntries */ + @Deprecated public static List 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.parseEntries(bibtexString); } catch (Exception e) { LOGGER.warn("BibtexParser.fromString(String): " + e.getMessage(), e); return Collections.emptyList(); @@ -119,6 +124,24 @@ public static Optional singleFromString(String bibtexString, return Optional.of(entries.iterator().next()); } + @Override + public List parseEntries(InputStream inputStream) throws ParserException { + BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)); + return parseEntries(reader); + } + + public List parseEntries(Reader reader) throws ParserException { + try { + return parse(reader).getDatabase().getEntries(); + } catch (IOException e) { + throw new ParserException(e); + } + } + + public List parseEntries(String bibtexString) throws ParserException { + return parseEntries(new StringReader(bibtexString)); + } + /** * Will parse the BibTex-Data found when reading from reader. Ignores any encoding supplied in the file by * "Encoding: myEncoding". @@ -130,11 +153,10 @@ public static Optional 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(); diff --git a/src/main/java/net/sf/jabref/logic/importer/fetcher/GVKParser.java b/src/main/java/net/sf/jabref/logic/importer/fileformat/GvkParser.java similarity index 95% rename from src/main/java/net/sf/jabref/logic/importer/fetcher/GVKParser.java rename to src/main/java/net/sf/jabref/logic/importer/fileformat/GvkParser.java index 095c3105e58..9f7b93aeb3e 100644 --- a/src/main/java/net/sf/jabref/logic/importer/fetcher/GVKParser.java +++ b/src/main/java/net/sf/jabref/logic/importer/fileformat/GvkParser.java @@ -1,4 +1,4 @@ -package net.sf.jabref.logic.importer.fetcher; +package net.sf.jabref.logic.importer.fileformat; import java.io.IOException; import java.io.InputStream; @@ -9,6 +9,8 @@ import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; +import net.sf.jabref.logic.importer.Parser; +import net.sf.jabref.logic.importer.ParserException; import net.sf.jabref.model.entry.BibEntry; import net.sf.jabref.model.entry.FieldName; import net.sf.jabref.model.entry.IdGenerator; @@ -22,17 +24,21 @@ import org.w3c.dom.NodeList; import org.xml.sax.SAXException; -class GVKParser { - private static final Log LOGGER = LogFactory.getLog(GVKParser.class); +public class GvkParser implements Parser { + private static final Log LOGGER = LogFactory.getLog(GvkParser.class); - List parseEntries(InputStream is) - throws ParserConfigurationException, SAXException, IOException { - DocumentBuilder dbuild = DocumentBuilderFactory.newInstance().newDocumentBuilder(); - Document content = dbuild.parse(is); - return this.parseEntries(content); + @Override + public List parseEntries(InputStream inputStream) throws ParserException { + try { + DocumentBuilder dbuild = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + Document content = dbuild.parse(inputStream); + return this.parseEntries(content); + } catch (ParserConfigurationException|SAXException|IOException exception) { + throw new ParserException(exception); + } } - List parseEntries(Document content) { + private List parseEntries(Document content) { List result = new LinkedList<>(); // used for creating test cases diff --git a/src/main/java/net/sf/jabref/logic/util/OptionalUtil.java b/src/main/java/net/sf/jabref/logic/util/OptionalUtil.java new file mode 100644 index 00000000000..d9187a4e47c --- /dev/null +++ b/src/main/java/net/sf/jabref/logic/util/OptionalUtil.java @@ -0,0 +1,23 @@ +package net.sf.jabref.logic.util; + +import java.util.Collections; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class OptionalUtil { + + public static List toList(Optional value) { + if (value.isPresent()) { + return Collections.singletonList(value.get()); + } else { + return Collections.emptyList(); + } + } + + @SuppressWarnings("varargs") + public static List toList(Optional... values) { + return Stream.of(values).flatMap(optional -> toList(optional).stream()).collect(Collectors.toList()); + } +} diff --git a/src/main/java/net/sf/jabref/preferences/JabRefPreferences.java b/src/main/java/net/sf/jabref/preferences/JabRefPreferences.java index d72769646f2..e1f83867aad 100644 --- a/src/main/java/net/sf/jabref/preferences/JabRefPreferences.java +++ b/src/main/java/net/sf/jabref/preferences/JabRefPreferences.java @@ -43,6 +43,7 @@ import net.sf.jabref.logic.bibtex.FieldContentParserPreferences; import net.sf.jabref.logic.bibtex.LatexFieldFormatterPreferences; import net.sf.jabref.logic.bibtexkeypattern.BibtexKeyPatternPreferences; +import net.sf.jabref.logic.cleanup.CleanupPreferences; import net.sf.jabref.logic.cleanup.CleanupPreset; import net.sf.jabref.logic.cleanup.FieldFormatterCleanup; import net.sf.jabref.logic.exporter.CustomExportList; @@ -1511,6 +1512,11 @@ public void setProtectedTermsPreferences(ProtectedTermsLoader loader) { } + public CleanupPreferences getCleanupPreferences(JournalAbbreviationLoader journalAbbreviationLoader) { + return new CleanupPreferences(get(IMPORT_FILENAMEPATTERN), + getLayoutFormatterPreferences(journalAbbreviationLoader), getFileDirectoryPreferences()); + } + public RemotePreferences getRemotePreferences() { return new RemotePreferences(getInt(REMOTE_SERVER_PORT), getBoolean(USE_REMOTE_SERVER)); } diff --git a/src/test/java/net/sf/jabref/BibtexTestData.java b/src/test/java/net/sf/jabref/BibtexTestData.java index bda74c94a22..f9e88fc00ee 100644 --- a/src/test/java/net/sf/jabref/BibtexTestData.java +++ b/src/test/java/net/sf/jabref/BibtexTestData.java @@ -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(); } diff --git a/src/test/java/net/sf/jabref/logic/bibtex/BibEntryAssert.java b/src/test/java/net/sf/jabref/logic/bibtex/BibEntryAssert.java index e042e70a19b..7564ec4603e 100644 --- a/src/test/java/net/sf/jabref/logic/bibtex/BibEntryAssert.java +++ b/src/test/java/net/sf/jabref/logic/bibtex/BibEntryAssert.java @@ -62,9 +62,8 @@ public static void assertEquals(Class clazz, String resourceName, List 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()); diff --git a/src/test/java/net/sf/jabref/logic/cleanup/CleanupWorkerTest.java b/src/test/java/net/sf/jabref/logic/cleanup/CleanupWorkerTest.java index cf77ec80f80..75ff6cbf3f5 100644 --- a/src/test/java/net/sf/jabref/logic/cleanup/CleanupWorkerTest.java +++ b/src/test/java/net/sf/jabref/logic/cleanup/CleanupWorkerTest.java @@ -54,8 +54,9 @@ public void setUp() throws IOException { metaData.setDefaultFileDirectory(pdfFolder.getAbsolutePath()); BibDatabaseContext context = new BibDatabaseContext(new BibDatabase(), metaData, bibFolder.newFile("test.bib")); worker = new CleanupWorker(context, - JabRefPreferences.getInstance().get(JabRefPreferences.IMPORT_FILENAMEPATTERN), - mock(LayoutFormatterPreferences.class), JabRefPreferences.getInstance().getFileDirectoryPreferences()); + new CleanupPreferences(JabRefPreferences.getInstance().get(JabRefPreferences.IMPORT_FILENAMEPATTERN), + mock(LayoutFormatterPreferences.class), + JabRefPreferences.getInstance().getFileDirectoryPreferences())); } @Test(expected = NullPointerException.class) diff --git a/src/test/java/net/sf/jabref/logic/cleanup/ISSNCleanupTest.java b/src/test/java/net/sf/jabref/logic/cleanup/ISSNCleanupTest.java index eb82eb644a1..86a0b17758c 100644 --- a/src/test/java/net/sf/jabref/logic/cleanup/ISSNCleanupTest.java +++ b/src/test/java/net/sf/jabref/logic/cleanup/ISSNCleanupTest.java @@ -20,8 +20,9 @@ public class ISSNCleanupTest { @Before public void setUp() { - worker = new CleanupWorker(mock(BibDatabaseContext.class), "", mock(LayoutFormatterPreferences.class), - mock(FileDirectoryPreferences.class)); + worker = new CleanupWorker(mock(BibDatabaseContext.class), + new CleanupPreferences("", mock(LayoutFormatterPreferences.class), + mock(FileDirectoryPreferences.class))); } @Test diff --git a/src/test/java/net/sf/jabref/logic/importer/fetcher/AstrophysicsDataSystemTest.java b/src/test/java/net/sf/jabref/logic/importer/fetcher/AstrophysicsDataSystemTest.java new file mode 100644 index 00000000000..282e4b27cd1 --- /dev/null +++ b/src/test/java/net/sf/jabref/logic/importer/fetcher/AstrophysicsDataSystemTest.java @@ -0,0 +1,78 @@ +package net.sf.jabref.logic.importer.fetcher; + +import java.util.Collections; +import java.util.List; + +import net.sf.jabref.logic.bibtex.FieldContentParserPreferences; +import net.sf.jabref.logic.importer.ImportFormatPreferences; +import net.sf.jabref.model.entry.BibEntry; +import net.sf.jabref.model.entry.BibtexEntryTypes; + +import org.junit.Before; +import org.junit.Test; + +import static net.sf.jabref.logic.util.OS.NEWLINE; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class AstrophysicsDataSystemTest { + + AstrophysicsDataSystem fetcher; + BibEntry diezSliceTheoremEntry; + + @Before + public void setUp() throws Exception { + ImportFormatPreferences importFormatPreferences = mock(ImportFormatPreferences.class); + when(importFormatPreferences.getFieldContentParserPreferences()).thenReturn( + mock(FieldContentParserPreferences.class)); + fetcher = new AstrophysicsDataSystem(importFormatPreferences); + + diezSliceTheoremEntry = new BibEntry(); + diezSliceTheoremEntry.setType(BibtexEntryTypes.ARTICLE); + diezSliceTheoremEntry.setCiteKey("2014arXiv1405.2249D"); + diezSliceTheoremEntry.setField("author", "Diez, T."); + diezSliceTheoremEntry.setField("title", "Slice theorem for Fr$\\backslash$'echet group actions and covariant symplectic field theory"); + diezSliceTheoremEntry.setField("year", "2014"); + diezSliceTheoremEntry.setField("archiveprefix", "arXiv"); + diezSliceTheoremEntry.setField("eprint", "1405.2249"); + diezSliceTheoremEntry.setField("journal", "ArXiv e-prints"); + diezSliceTheoremEntry.setField("keywords", "Mathematical Physics, Mathematics - Differential Geometry, Mathematics - Symplectic Geometry, 58B99, 58Z05, 58B25, 22E65, 58D19, 53D20, 53D42"); + diezSliceTheoremEntry.setField("month", "#may#"); + diezSliceTheoremEntry.setField("primaryclass", "math-ph"); + diezSliceTheoremEntry.setField("abstract", + "A general slice theorem for the action of a Fr$\\backslash$'echet Lie group on a" + NEWLINE + + "Fr$\\backslash$'echet manifolds is established. The Nash-Moser theorem provides the" + NEWLINE + + "fundamental tool to generalize the result of Palais to this" + NEWLINE + + "infinite-dimensional setting. The presented slice theorem is illustrated" + NEWLINE + + "by its application to gauge theories: the action of the gauge" + NEWLINE + + "transformation group admits smooth slices at every point and thus the" + NEWLINE + + "gauge orbit space is stratified by Fr$\\backslash$'echet manifolds. Furthermore, a" + NEWLINE + + "covariant and symplectic formulation of classical field theory is" + NEWLINE + + "proposed and extensively discussed. At the root of this novel framework" + NEWLINE + + "is the incorporation of field degrees of freedom F and spacetime M into" + NEWLINE + + "the product manifold F * M. The induced bigrading of differential forms" + NEWLINE + + "is used in order to carry over the usual symplectic theory to this new" + NEWLINE + + "setting. The examples of the Klein-Gordon field and general Yang-Mills" + NEWLINE + + "theory illustrate that the presented approach conveniently handles the" + NEWLINE + + "occurring symmetries." + NEWLINE); + } + + @Test + public void searchByQueryFindsEntry() throws Exception { + List fetchedEntries = fetcher.performSearch("Diez slice theorem"); + assertEquals(Collections.singletonList(diezSliceTheoremEntry), fetchedEntries); + } + + @Test + public void searchByEntryFindsEntry() throws Exception { + BibEntry searchEntry = new BibEntry(); + searchEntry.setField("title", "slice theorem"); + searchEntry.setField("author", "Diez"); + + List fetchedEntries = fetcher.performSearch(searchEntry); + assertFalse(fetchedEntries.isEmpty()); + assertEquals(diezSliceTheoremEntry, fetchedEntries.get(0)); + } +} diff --git a/src/test/java/net/sf/jabref/logic/importer/fetcher/GvkFetcherTest.java b/src/test/java/net/sf/jabref/logic/importer/fetcher/GvkFetcherTest.java index 285b9cee6af..a106fc2b3fd 100644 --- a/src/test/java/net/sf/jabref/logic/importer/fetcher/GvkFetcherTest.java +++ b/src/test/java/net/sf/jabref/logic/importer/fetcher/GvkFetcherTest.java @@ -74,7 +74,7 @@ public void simpleSearchQueryStringCorrect() throws FetcherException { @Test public void simpleSearchQueryURLCorrect() throws MalformedURLException, URISyntaxException, FetcherException { String query = "java jdk"; - URL url = fetcher.getQueryURL(query); + URL url = fetcher.getURLForQuery(query); assertEquals("http://sru.gbv.de/gvk?version=1.1&operation=searchRetrieve&query=pica.all%3Djava+jdk&maximumRecords=50&recordSchema=picaxml&sortKeys=Year%2C%2C1", url.toString()); } @@ -88,7 +88,7 @@ public void complexSearchQueryStringCorrect() throws FetcherException { @Test public void complexSearchQueryURLCorrect() throws MalformedURLException, URISyntaxException, FetcherException { String query = "kon java tit jdk"; - URL url = fetcher.getQueryURL(query); + URL url = fetcher.getURLForQuery(query); assertEquals("http://sru.gbv.de/gvk?version=1.1&operation=searchRetrieve&query=pica.kon%3Djava+and+pica.tit%3Djdk&maximumRecords=50&recordSchema=picaxml&sortKeys=Year%2C%2C1", url.toString()); } diff --git a/src/test/java/net/sf/jabref/logic/importer/fetcher/GVKParserTest.java b/src/test/java/net/sf/jabref/logic/importer/fetcher/GvkParserTest.java similarity index 66% rename from src/test/java/net/sf/jabref/logic/importer/fetcher/GVKParserTest.java rename to src/test/java/net/sf/jabref/logic/importer/fetcher/GvkParserTest.java index 2d2a47bfff3..ae3184b4ffe 100644 --- a/src/test/java/net/sf/jabref/logic/importer/fetcher/GVKParserTest.java +++ b/src/test/java/net/sf/jabref/logic/importer/fetcher/GvkParserTest.java @@ -1,33 +1,29 @@ package net.sf.jabref.logic.importer.fetcher; -import java.io.IOException; import java.io.InputStream; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Optional; -import javax.xml.parsers.ParserConfigurationException; - import net.sf.jabref.logic.bibtex.BibEntryAssert; +import net.sf.jabref.logic.importer.fileformat.GvkParser; import net.sf.jabref.model.entry.BibEntry; import org.junit.Assert; import org.junit.Test; -import org.xml.sax.SAXException; -public class GVKParserTest { +public class GvkParserTest { - private void doTest(String xmlName, int expectedSize, List resourceNames) - throws ParserConfigurationException, SAXException, IOException { - try (InputStream is = GVKParser.class.getResourceAsStream(xmlName)) { - GVKParser parser = new GVKParser(); + private void doTest(String xmlName, int expectedSize, List resourceNames) throws Exception { + try (InputStream is = GvkParserTest.class.getResourceAsStream(xmlName)) { + GvkParser parser = new GvkParser(); List entries = parser.parseEntries(is); Assert.assertNotNull(entries); Assert.assertEquals(expectedSize, entries.size()); int i = 0; for (String resourceName : resourceNames) { - BibEntryAssert.assertEquals(GVKParser.class, resourceName, entries.get(i)); + BibEntryAssert.assertEquals(GvkParserTest.class, resourceName, entries.get(i)); i++; } } @@ -35,12 +31,12 @@ private void doTest(String xmlName, int expectedSize, List resourceNames @Test public void emptyResult() throws Exception { - doTest("gvk_empty_result_becaue_of_bad_query.xml", 0, Collections.emptyList()); + doTest("gvk_empty_result_because_of_bad_query.xml", 0, Collections.emptyList()); } @Test public void resultFor797485368() throws Exception { - doTest("gvk_result_for_797485368.xml", 1, Arrays.asList("gvk_result_for_797485368.bib")); + doTest("gvk_result_for_797485368.xml", 1, Collections.singletonList("gvk_result_for_797485368.bib")); } @Test @@ -49,9 +45,9 @@ public void testGMP() throws Exception { } @Test - public void subTitleTest() throws IOException, ParserConfigurationException, SAXException { - try (InputStream is = GVKParser.class.getResourceAsStream("gvk_artificial_subtitle_test.xml")) { - GVKParser parser = new GVKParser(); + public void subTitleTest() throws Exception { + try (InputStream is = GvkParserTest.class.getResourceAsStream("gvk_artificial_subtitle_test.xml")) { + GvkParser parser = new GvkParser(); List entries = parser.parseEntries(is); Assert.assertNotNull(entries); Assert.assertEquals(5, entries.size()); diff --git a/src/test/java/net/sf/jabref/logic/importer/fetcher/MathSciNetTest.java b/src/test/java/net/sf/jabref/logic/importer/fetcher/MathSciNetTest.java new file mode 100644 index 00000000000..87b6b121050 --- /dev/null +++ b/src/test/java/net/sf/jabref/logic/importer/fetcher/MathSciNetTest.java @@ -0,0 +1,69 @@ +package net.sf.jabref.logic.importer.fetcher; + +import java.util.List; + +import net.sf.jabref.logic.bibtex.FieldContentParserPreferences; +import net.sf.jabref.logic.importer.ImportFormatPreferences; +import net.sf.jabref.model.entry.BibEntry; +import net.sf.jabref.model.entry.BibtexEntryTypes; +import net.sf.jabref.support.DevEnvironment; + +import org.junit.Assume; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class MathSciNetTest { + + MathSciNet fetcher; + BibEntry ratiuEntry; + + @Before + public void setUp() throws Exception { + ImportFormatPreferences importFormatPreferences = mock(ImportFormatPreferences.class); + when(importFormatPreferences.getFieldContentParserPreferences()).thenReturn( + mock(FieldContentParserPreferences.class)); + fetcher = new MathSciNet(importFormatPreferences); + + ratiuEntry = new BibEntry(); + ratiuEntry.setType(BibtexEntryTypes.ARTICLE); + ratiuEntry.setCiteKey("MR3537908"); + ratiuEntry.setField("author", "Chechkin, Gregory A. and Ratiu, Tudor S. and Romanov, Maxim S. and Samokhin, Vyacheslav N."); + ratiuEntry.setField("title", "Existence and {U}niqueness {T}heorems for the {T}wo-{D}imensional {E}ricksen--{L}eslie {S}ystem"); + ratiuEntry.setField("journal", "Journal of Mathematical Fluid Mechanics"); + ratiuEntry.setField("volume", "18"); + ratiuEntry.setField("year", "2016"); + ratiuEntry.setField("number", "3"); + ratiuEntry.setField("pages", "571--589"); + ratiuEntry.setField("issn", "1422-6928"); + ratiuEntry.setField("keywords", "76A15 (35A01 35A02 35K61)"); + ratiuEntry.setField("mrnumber", "3537908"); + ratiuEntry.setField("doi", "10.1007/s00021-016-0250-0"); + } + + @Test + public void searchByEntryFindsEntry() throws Exception { + BibEntry searchEntry = new BibEntry(); + searchEntry.setField("title", "existence"); + searchEntry.setField("author", "Ratiu"); + searchEntry.setField("journal", "fluid"); + + List fetchedEntries = fetcher.performSearch(searchEntry); + assertFalse(fetchedEntries.isEmpty()); + assertEquals(ratiuEntry, fetchedEntries.get(0)); + } + + @Test + public void searchByQueryFindsEntry() throws Exception { + // CI has no subscription to zbMath and thus gets 401 response + Assume.assumeFalse(DevEnvironment.isCIServer()); + + List fetchedEntries = fetcher.performSearch("Two-Dimensional Ericksen Leslie System"); + assertFalse(fetchedEntries.isEmpty()); + assertEquals(ratiuEntry, fetchedEntries.get(0)); + } +} diff --git a/src/test/java/net/sf/jabref/logic/importer/fetcher/zbMATHTest.java b/src/test/java/net/sf/jabref/logic/importer/fetcher/zbMATHTest.java new file mode 100644 index 00000000000..6c457b3e1ae --- /dev/null +++ b/src/test/java/net/sf/jabref/logic/importer/fetcher/zbMATHTest.java @@ -0,0 +1,55 @@ +package net.sf.jabref.logic.importer.fetcher; + +import java.util.Collections; +import java.util.List; + +import net.sf.jabref.logic.bibtex.FieldContentParserPreferences; +import net.sf.jabref.logic.importer.ImportFormatPreferences; +import net.sf.jabref.model.entry.BibEntry; +import net.sf.jabref.model.entry.BibtexEntryTypes; +import net.sf.jabref.support.DevEnvironment; + +import org.junit.Assume; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class zbMATHTest { + private zbMATH fetcher; + private BibEntry donaldsonEntry; + + @Before + public void setUp() throws Exception { + ImportFormatPreferences importFormatPreferences = mock(ImportFormatPreferences.class); + when(importFormatPreferences.getFieldContentParserPreferences()).thenReturn( + mock(FieldContentParserPreferences.class)); + fetcher = new zbMATH(importFormatPreferences); + + donaldsonEntry = new BibEntry(); + donaldsonEntry.setType(BibtexEntryTypes.ARTICLE); + donaldsonEntry.setCiteKey("zbMATH03800580"); + donaldsonEntry.setField("author", "S.K. {Donaldson}"); + donaldsonEntry.setField("journal", "Journal of Differential Geometry"); + donaldsonEntry.setField("issn", "0022-040X; 1945-743X/e"); + donaldsonEntry.setField("language", "English"); + donaldsonEntry.setField("keywords", "57N13 57R10 53C05 58J99 57R65"); + donaldsonEntry.setField("pages", "279--315"); + donaldsonEntry.setField("publisher", "International Press of Boston, Somerville, MA"); + donaldsonEntry.setField("title", "An application of gauge theory to four dimensional topology."); + donaldsonEntry.setField("volume", "18"); + donaldsonEntry.setField("year", "1983"); + donaldsonEntry.setField("zbl", "0507.57010"); + } + + @Test + public void searchByQueryFindsEntry() throws Exception { + // CI has no subscription to zbMath and thus gets 401 response + Assume.assumeFalse(DevEnvironment.isCIServer()); + + List fetchedEntries = fetcher.performSearch("an:0507.57010"); + assertEquals(Collections.singletonList(donaldsonEntry), fetchedEntries); + } +} diff --git a/src/test/java/net/sf/jabref/logic/importer/fileformat/BibtexParserTest.java b/src/test/java/net/sf/jabref/logic/importer/fileformat/BibtexParserTest.java index b292299e920..8f971aae0c6 100644 --- a/src/test/java/net/sf/jabref/logic/importer/fileformat/BibtexParserTest.java +++ b/src/test/java/net/sf/jabref/logic/importer/fileformat/BibtexParserTest.java @@ -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 @@ -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}}"), @@ -1394,7 +1385,9 @@ 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" @@ -1402,10 +1395,8 @@ public void parseRecognizesSaveActionsAfterEntry() throws IOException { + " 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(); @@ -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()); diff --git a/src/test/java/net/sf/jabref/logic/util/io/RegExpFileSearchTests.java b/src/test/java/net/sf/jabref/logic/util/io/RegExpFileSearchTests.java index d09ebef1735..5c15c2db846 100644 --- a/src/test/java/net/sf/jabref/logic/util/io/RegExpFileSearchTests.java +++ b/src/test/java/net/sf/jabref/logic/util/io/RegExpFileSearchTests.java @@ -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; @@ -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 diff --git a/src/test/java/net/sf/jabref/testutils/AssertUtil.java b/src/test/java/net/sf/jabref/testutils/AssertUtil.java deleted file mode 100644 index 5b093335302..00000000000 --- a/src/test/java/net/sf/jabref/testutils/AssertUtil.java +++ /dev/null @@ -1,15 +0,0 @@ -package net.sf.jabref.testutils; - -import org.junit.Assert; - -public class AssertUtil { - - /** - * Will check if two paths are the same. - */ - public static void assertEqualPaths(String path1, String path2) { - Assert.assertNotNull("first path must not be null", path1); - Assert.assertNotNull("second path must not be null", path2); - Assert.assertEquals(path1.replaceAll("\\\\", "/"), path2.replaceAll("\\\\", "/")); - } -} diff --git a/src/test/resources/net/sf/jabref/logic/importer/fetcher/gvk_empty_result_becaue_of_bad_query.xml b/src/test/resources/net/sf/jabref/logic/importer/fetcher/gvk_empty_result_because_of_bad_query.xml similarity index 100% rename from src/test/resources/net/sf/jabref/logic/importer/fetcher/gvk_empty_result_becaue_of_bad_query.xml rename to src/test/resources/net/sf/jabref/logic/importer/fetcher/gvk_empty_result_because_of_bad_query.xml