Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup FindFile and asssociated tests #1596

Merged
merged 12 commits into from
Jul 25, 2016
14 changes: 9 additions & 5 deletions src/main/java/net/sf/jabref/external/AutoSetLinks.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class AutoSetLinks {
* @param entries the entries for which links should be set
* @param databaseContext the database for which links are set
*/
public static void autoSetLinks(Collection<BibEntry> entries, BibDatabaseContext databaseContext) {
public static void autoSetLinks(List<BibEntry> entries, BibDatabaseContext databaseContext) {
autoSetLinks(entries, null, null, null, databaseContext, null, null);
}

Expand Down Expand Up @@ -69,7 +69,9 @@ public static void autoSetLinks(Collection<BibEntry> entries, BibDatabaseContext
* parameter can be null, which means that no progress update will be shown.
* @return the thread performing the automatically setting
*/
public static Runnable autoSetLinks(final Collection<BibEntry> entries, final NamedCompound ce, final Set<BibEntry> changedEntries, final FileListTableModel singleTableModel, final BibDatabaseContext databaseContext, final ActionListener callback, final JDialog diag) {
public static Runnable autoSetLinks(final List<BibEntry> entries, final NamedCompound ce,
final Set<BibEntry> changedEntries, final FileListTableModel singleTableModel,
final BibDatabaseContext databaseContext, final ActionListener callback, final JDialog diag) {
final Collection<ExternalFileType> types = ExternalFileTypes.getInstance().getExternalFileTypeSelection();
if (diag != null) {
final JProgressBar prog = new JProgressBar(JProgressBar.HORIZONTAL, 0, types.size() - 1);
Expand All @@ -94,7 +96,7 @@ public void run() {
dirs.addAll(dirsS.stream().map(File::new).collect(Collectors.toList()));

// determine extensions
Collection<String> extensions = new ArrayList<>();
List<String> extensions = new ArrayList<>();
for (final ExternalFileType type : types) {
extensions.add(type.getExtension());
}
Expand All @@ -105,7 +107,8 @@ public void run() {
String regExp = Globals.prefs.get(JabRefPreferences.REG_EXP_SEARCH_EXPRESSION_KEY);
result = RegExpFileSearch.findFilesForSet(entries, extensions, dirs, regExp);
} else {
result = FileUtil.findAssociatedFiles(entries, extensions, dirs, Globals.prefs);
boolean autoLinkExactKeyOnly = Globals.prefs.getBoolean(JabRefPreferences.AUTOLINK_EXACT_KEY_ONLY);
result = FileUtil.findAssociatedFiles(entries, extensions, dirs, autoLinkExactKeyOnly);
}

boolean foundAny = false;
Expand Down Expand Up @@ -210,7 +213,8 @@ public void run() {
* parameter can be null, which means that no progress update will be shown.
* @return the runnable able to perform the automatically setting
*/
public static Runnable autoSetLinks(final BibEntry entry, final FileListTableModel singleTableModel, final BibDatabaseContext databaseContext, final ActionListener callback, final JDialog diag) {
public static Runnable autoSetLinks(final BibEntry entry, final FileListTableModel singleTableModel,
final BibDatabaseContext databaseContext, final ActionListener callback, final JDialog diag) {
return autoSetLinks(Collections.singletonList(entry), null, null, singleTableModel, databaseContext, callback,
diag);
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/net/sf/jabref/external/ExternalFileTypes.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.sf.jabref.external;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.Set;
Expand Down Expand Up @@ -104,7 +103,7 @@ public static List<ExternalFileType> getDefaultExternalFileTypes() {
return list;
}

public Collection<ExternalFileType> getExternalFileTypeSelection() {
public Set<ExternalFileType> getExternalFileTypeSelection() {
return externalFileTypes;
}

Expand Down
12 changes: 5 additions & 7 deletions src/main/java/net/sf/jabref/external/RegExpFileSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
Expand Down Expand Up @@ -63,8 +62,8 @@ public class RegExpFileSearch {
* @param regExp The expression deciding which names are acceptable.
* @return A map linking each given entry to a list of files matching the given criteria.
*/
public static Map<BibEntry, List<File>> findFilesForSet(Collection<BibEntry> entries,
Collection<String> extensions, List<File> directories, String regExp) {
public static Map<BibEntry, List<File>> findFilesForSet(List<BibEntry> entries, List<String> extensions,
List<File> directories, String regExp) {

Map<BibEntry, List<File>> res = new HashMap<>();
for (BibEntry entry : entries) {
Expand All @@ -82,8 +81,8 @@ public static Map<BibEntry, List<File>> findFilesForSet(Collection<BibEntry> ent
* @param regularExpression The expression deciding which names are acceptable.
* @return A list of files paths matching the given criteria.
*/
private static List<File> findFiles(BibEntry entry, Collection<String> extensions,
Collection<File> directories, String regularExpression) {
private static List<File> findFiles(BibEntry entry, List<String> extensions, List<File> directories,
String regularExpression) {

String extensionRegExp = '(' + String.join("|", extensions) + ')';

Expand Down Expand Up @@ -128,8 +127,7 @@ private static List<File> findFiles(BibEntry entry, Collection<String> extension
* @return Will return the first file found to match the given criteria or
* null if none was found.
*/
private static List<File> findFile(BibEntry entry, Collection<File> dirs, String file,
String extensionRegExp) {
private static List<File> findFile(BibEntry entry, List<File> dirs, String file, String extensionRegExp) {
List<File> res = new ArrayList<>();
for (File directory : dirs) {
res.addAll(findFile(entry, directory.getPath(), file, extensionRegExp));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public void run() {

// First we try to autoset fields
if (autoSet) {
Collection<BibEntry> entries = new ArrayList<>(sel);
List<BibEntry> entries = new ArrayList<>(sel);

// Start the automatically setting process:
Runnable r = AutoSetLinks.autoSetLinks(entries, ce, changedEntries, null, panel.getBibDatabaseContext(), null, null);
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/net/sf/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.TimerTask;

import javax.swing.AbstractAction;
Expand Down Expand Up @@ -2402,9 +2403,9 @@ public Optional<String> searchAndOpen() {
of stuff below. It is now disabled by default. */

// see if we can fall back to a filename based on the bibtex key
final Collection<BibEntry> entries = Collections.singleton(entry);
final List<BibEntry> entries = Collections.singletonList(entry);

final Collection<ExternalFileType> types = ExternalFileTypes.getInstance().getExternalFileTypeSelection();
final Set<ExternalFileType> types = ExternalFileTypes.getInstance().getExternalFileTypeSelection();
final List<File> dirs = new ArrayList<>();
if (!basePanel.getBibDatabaseContext().getFileDirectory().isEmpty()) {
final List<String> mdDirs = basePanel.getBibDatabaseContext().getFileDirectory();
Expand All @@ -2423,7 +2424,8 @@ public Optional<String> searchAndOpen() {
String regExp = Globals.prefs.get(JabRefPreferences.REG_EXP_SEARCH_EXPRESSION_KEY);
result = RegExpFileSearch.findFilesForSet(entries, extensions, dirs, regExp);
} else {
result = FileUtil.findAssociatedFiles(entries, extensions, dirs, Globals.prefs);
boolean autoLinkExactKeyOnly = Globals.prefs.getBoolean(JabRefPreferences.AUTOLINK_EXACT_KEY_ONLY);
result = FileUtil.findAssociatedFiles(entries, extensions, dirs, autoLinkExactKeyOnly);
}
if (result.containsKey(entry)) {
final List<File> res = result.get(entry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

Expand Down Expand Up @@ -434,8 +433,7 @@ private boolean editListEntry(FileListEntry entry, boolean openBrowse) {
public void autoSetLinks() {
auto.setEnabled(false);

Collection<BibEntry> entries = new ArrayList<>();
entries.addAll(frame.getCurrentBasePanel().getSelectedEntries());
List<BibEntry> entries = new ArrayList<>(frame.getCurrentBasePanel().getSelectedEntries());

// filesystem lookup
JDialog dialog = new JDialog(frame, true);
Expand Down
52 changes: 28 additions & 24 deletions src/main/java/net/sf/jabref/logic/util/io/FileFinder.java
Original file line number Diff line number Diff line change
@@ -1,43 +1,47 @@
package net.sf.jabref.logic.util.io;

import java.io.File;
import java.util.Collection;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.function.BiPredicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class FileFinder {

public static Set<File> findFiles(Collection<String> extensions, Collection<File> directories) {
Set<File> result = new HashSet<>();
private static final Log LOGGER = LogFactory.getLog(FileFinder.class);

for (File directory : directories) {
result.addAll(FileFinder.findFiles(extensions, directory));
}

return result;
}
public static Set<File> findFiles(List<String> extensions, List<File> directories) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there cases where the result is explicitly checked for an empty Set and some warning related to permissions is shown?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I misread the code... It should be OK.

Objects.requireNonNull(directories, "Directories must not be null!");
Objects.requireNonNull(extensions, "Extensions must not be null!");

BiPredicate<Path, BasicFileAttributes> isDirectoryAndContainsExtension = (path,
attr) -> !Files.isDirectory(path)
&& extensions.contains(FileUtil.getFileExtension(path.toFile()).orElse(""));

private static Collection<? extends File> findFiles(Collection<String> extensions, File directory) {
Set<File> result = new HashSet<>();
for (File directory : directories) {

File[] children = directory.listFiles();
if (children == null) {
return result; // No permission?
}
try (Stream<File> files = Files.find(directory.toPath(), Integer.MAX_VALUE, isDirectoryAndContainsExtension)
.map(x -> x.toFile())) {
result.addAll(files.collect(Collectors.toSet()));

for (File child : children) {
if (child.isDirectory()) {
result.addAll(FileFinder.findFiles(extensions, child));
} else {
FileUtil.getFileExtension(child).ifPresent(extension -> {
if (extensions.contains(extension)) {
result.add(child);
}
});
} catch (IOException e) {
LOGGER.error("Problem in finding files", e);
}
}

return result;
}

}
}
8 changes: 3 additions & 5 deletions src/main/java/net/sf/jabref/logic/util/io/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -317,8 +316,8 @@ private static File shortenFileName(File fileName, String directory) {
}
}

public static Map<BibEntry, List<File>> findAssociatedFiles(Collection<BibEntry> entries,
Collection<String> extensions, Collection<File> directories, JabRefPreferences prefs) {
public static Map<BibEntry, List<File>> findAssociatedFiles(List<BibEntry> entries,
List<String> extensions, List<File> directories, boolean autolinkExactKeyOnly) {
Map<BibEntry, List<File>> result = new HashMap<>();

// First scan directories
Expand All @@ -329,7 +328,6 @@ public static Map<BibEntry, List<File>> findAssociatedFiles(Collection<BibEntry>
result.put(entry, new ArrayList<>());
}

boolean exactOnly = prefs.getBoolean(JabRefPreferences.AUTOLINK_EXACT_KEY_ONLY);
// Now look for keys
nextFile: for (File file : filesWithExtension) {

Expand All @@ -345,7 +343,7 @@ public static Map<BibEntry, List<File>> findAssociatedFiles(Collection<BibEntry>
}
// If we get here, we didn't find any exact matches. If non-exact
// matches are allowed, try to find one:
if (!exactOnly) {
if (!autolinkExactKeyOnly) {
for (BibEntry entry : entries) {
String citeKey = entry.getCiteKey();
if ((citeKey != null) && !citeKey.isEmpty() && name.startsWith(citeKey)) {
Expand Down
116 changes: 0 additions & 116 deletions src/test/java/net/sf/jabref/FileBasedTestCase.java

This file was deleted.

Loading