-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Fixes #3472: Files starting with BibTeX key of a similar entry are no longer found by mistake #3509
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
26c4462
Refactor auto link classes
tobiasdiez 456241c
Fixes #3472: Files starting with BibTeX key of a similar entry are no…
tobiasdiez 3784d90
Simplify code
tobiasdiez 18a6688
Implement feedback, fix tests and checkstyle
tobiasdiez fb207b2
Minor changes
tobiasdiez 88cdb43
Merge branch 'master' into fix3472
tobiasdiez 8f30b75
Merge branch 'fix3472' of https://github.com/JabRef/jabref into fix3472
tobiasdiez c45de80
Fix changelog
tobiasdiez e661b76
Really fix changelog
tobiasdiez 9d03922
Fix build
tobiasdiez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,18 +5,19 @@ | |
import java.nio.file.Path; | ||
import java.nio.file.attribute.BasicFileAttributes; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.function.BiPredicate; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import org.jabref.logic.bibtexkeypattern.BibtexKeyPatternUtil; | ||
import org.jabref.model.entry.BibEntry; | ||
import org.jabref.model.strings.StringUtil; | ||
import org.jabref.model.util.FileHelper; | ||
|
||
import org.apache.commons.logging.Log; | ||
|
@@ -32,54 +33,57 @@ class CiteKeyBasedFileFinder implements FileFinder { | |
} | ||
|
||
@Override | ||
public Map<BibEntry, List<Path>> findAssociatedFiles(List<BibEntry> entries, List<Path> directories, List<String> extensions) { | ||
public List<Path> findAssociatedFiles(BibEntry entry, List<Path> directories, List<String> extensions) { | ||
Objects.requireNonNull(directories); | ||
Objects.requireNonNull(entries); | ||
Objects.requireNonNull(entry); | ||
|
||
Map<BibEntry, List<Path>> result = new HashMap<>(); | ||
Optional<String> citeKeyOptional = entry.getCiteKeyOptional(); | ||
if (StringUtil.isBlank(citeKeyOptional)) { | ||
return Collections.emptyList(); | ||
} | ||
String citeKey = citeKeyOptional.get(); | ||
|
||
List<Path> result = new ArrayList<>(); | ||
|
||
// First scan directories | ||
Set<Path> filesWithExtension = findFilesByExtension(directories, extensions); | ||
|
||
// Initialize Result-Set | ||
for (BibEntry entry : entries) { | ||
result.put(entry, new ArrayList<>()); | ||
} | ||
|
||
// Now look for keys | ||
nextFile: | ||
for (Path file : filesWithExtension) { | ||
String name = file.getFileName().toString(); | ||
int dot = name.lastIndexOf('.'); | ||
// First, look for exact matches: | ||
for (BibEntry entry : entries) { | ||
Optional<String> citeKey = entry.getCiteKeyOptional(); | ||
if ((citeKey.isPresent()) && !citeKey.get().isEmpty() && (dot > 0) | ||
&& name.substring(0, dot).equals(citeKey.get())) { | ||
result.get(entry).add(file); | ||
continue nextFile; | ||
} | ||
String nameWithoutExtension = FileUtil.getBaseName(name); | ||
|
||
// First, look for exact matches | ||
if (nameWithoutExtension.equals(citeKey)) { | ||
result.add(file); | ||
continue; | ||
} | ||
// If we get here, we did not find any exact matches. If non-exact | ||
// matches are allowed, try to find one: | ||
// If we get here, we did not find any exact matches. If non-exact matches are allowed, try to find one | ||
if (!exactKeyOnly) { | ||
for (BibEntry entry : entries) { | ||
Optional<String> citeKey = entry.getCiteKeyOptional(); | ||
if ((citeKey.isPresent()) && !citeKey.get().isEmpty() && name.startsWith(citeKey.get())) { | ||
result.get(entry).add(file); | ||
continue nextFile; | ||
} | ||
if (matches(name, citeKey)) { | ||
result.add(file); | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
return result.stream().sorted().collect(Collectors.toList()); | ||
} | ||
|
||
private boolean matches(String filename, String citeKey) { | ||
boolean startsWithKey = filename.startsWith(citeKey); | ||
if (startsWithKey) { | ||
// The file name starts with the key, that's already a good start | ||
// However, we do not want to match "JabRefa" for "JabRef" since this is probably a file belonging to another entry published in the same time / same name | ||
char charAfterKey = filename.charAt(citeKey.length()); | ||
return !StringUtil.contains(BibtexKeyPatternUtil.CHARS, charAfterKey); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better direclty use the java method: String.contains(Character.toString(c)) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And also take a look at the codacy |
||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Returns a list of all files in the given directories which have one of the given extension. | ||
*/ | ||
public Set<Path> findFilesByExtension(List<Path> directories, List<String> extensions) { | ||
private Set<Path> findFilesByExtension(List<Path> directories, List<String> extensions) { | ||
Objects.requireNonNull(extensions, "Extensions must not be null!"); | ||
|
||
BiPredicate<Path, BasicFileAttributes> isFileWithCorrectExtension = (path, attributes) -> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am against creating a second constructor. It was my initial idea in #3368 and guess who supposed the current alternative? 😆
See your comment #3368 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand what you mean. The linked comment is about the list of entries and not about how the
AutoSetFileLinksUtil
gets the things needed (like directories and preferences). I think, the current setup makes sense: on initialization, specify everything the auto set functionality needs to know. Why don't you like these constructors?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah sorry, I could not see all code in the line on my mobile, so I was a b9it confused. Forget about my remark,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem!