-
-
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
[WIP] Proper copying of entries #1677
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -342,7 +342,33 @@ public synchronized boolean hasStringLabel(String label) { | |
*/ | ||
public String resolveForStrings(String content) { | ||
Objects.requireNonNull(content, "Content for resolveForStrings must not be null."); | ||
return resolveContent(content, new HashSet<>()); | ||
return resolveContent(content, new HashSet<>(), null); | ||
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. Please don't pass |
||
} | ||
|
||
/** | ||
* Get all strings used in the entries. | ||
*/ | ||
public Collection<BibtexString> getUsedStrings(Collection<BibEntry> entries) { | ||
List<BibtexString> result = new ArrayList<>(); | ||
Set<String> allUsedIds = new HashSet<>(); | ||
|
||
// All entries | ||
for (BibEntry entry : entries) { | ||
for (String fieldContent : entry.getFieldValues()) { | ||
resolveContent(fieldContent, new HashSet<>(), allUsedIds); | ||
} | ||
} | ||
|
||
// Preamble | ||
if (preamble != null) { | ||
resolveContent(preamble, new HashSet<>(), allUsedIds); | ||
} | ||
|
||
for (String stringId : allUsedIds) { | ||
result.add((BibtexString) bibtexStrings.get(stringId).clone()); | ||
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. why do you use clone here? |
||
} | ||
|
||
return result; | ||
} | ||
|
||
/** | ||
|
@@ -400,7 +426,7 @@ public BibEntry resolveForStrings(BibEntry entry, boolean inPlace) { | |
* care not to follow a circular reference pattern. | ||
* If the string is undefined, returns null. | ||
*/ | ||
private String resolveString(String label, Set<String> usedIds) { | ||
private String resolveString(String label, Set<String> usedIds, Set<String> allUsedIds) { | ||
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. Please add Please add 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. What is the difference between |
||
for (BibtexString string : bibtexStrings.values()) { | ||
if (string.getName().equalsIgnoreCase(label)) { | ||
// First check if this string label has been resolved | ||
|
@@ -413,11 +439,14 @@ private String resolveString(String label, Set<String> usedIds) { | |
} | ||
// If not, log this string's ID now. | ||
usedIds.add(string.getId()); | ||
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. Here you get the id of the string and above in getUsedStrings you get the string from the id. |
||
if (allUsedIds != null) { | ||
allUsedIds.add(string.getId()); | ||
} | ||
|
||
// Ok, we found the string. Now we must make sure we | ||
// resolve any references to other strings in this one. | ||
String result = string.getContent(); | ||
result = resolveContent(result, usedIds); | ||
result = resolveContent(result, usedIds, allUsedIds); | ||
|
||
// Finished with recursing this branch, so we remove our | ||
// ID again: | ||
|
@@ -439,7 +468,8 @@ private String resolveString(String label, Set<String> usedIds) { | |
|
||
private static final Pattern RESOLVE_CONTENT_PATTERN = Pattern.compile(".*#[^#]+#.*"); | ||
|
||
private String resolveContent(String result, Set<String> usedIds) { | ||
|
||
private String resolveContent(String result, Set<String> usedIds, Set<String> allUsedIds) { | ||
String res = result; | ||
if (RESOLVE_CONTENT_PATTERN.matcher(res).matches()) { | ||
StringBuilder newRes = new StringBuilder(); | ||
|
@@ -457,7 +487,7 @@ private String resolveContent(String result, Set<String> usedIds) { | |
// We found the boundaries of the string ref, | ||
// now resolve that one. | ||
String refLabel = res.substring(next + 1, stringEnd); | ||
String resolved = resolveString(refLabel, usedIds); | ||
String resolved = resolveString(refLabel, usedIds, allUsedIds); | ||
|
||
if (resolved == null) { | ||
// Could not resolve string. Display the # | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,10 @@ | |
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import net.sf.jabref.Globals; | ||
import net.sf.jabref.importer.ParserResult; | ||
|
@@ -317,4 +320,52 @@ public void resolveForStringsOddHashMarkAtTheEnd() { | |
database.addString(string); | ||
assertEquals(database.resolveForStrings("AAA#AAA#AAA#"), "AAAaaaAAA#"); | ||
} | ||
|
||
@Test | ||
public void getUsedStringsSingleStringWithString() { | ||
BibEntry entry = new BibEntry(IdGenerator.next()); | ||
entry.setField("author", "#AAA#"); | ||
BibtexString string = new BibtexString(IdGenerator.next(), "AAA", "Some other #BBB#"); | ||
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. Please move the string initalization and adding them to the db to the @setup method. |
||
BibtexString string2 = new BibtexString(IdGenerator.next(), "BBB", "Some more text"); | ||
BibtexString string3 = new BibtexString(IdGenerator.next(), "CCC", "Even more text"); | ||
database.addString(string); | ||
database.addString(string2); | ||
database.addString(string3); | ||
database.insertEntry(entry); | ||
List<BibtexString> usedStrings = (List<BibtexString>) database.getUsedStrings(Arrays.asList(entry)); | ||
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. create overload of getUsedStrings for one entry and use Collections.singletonList. |
||
assertEquals(2, usedStrings.size()); | ||
assertTrue((string.getName() == usedStrings.get(0).getName()) | ||
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. Can't we put all strings in a set and just compare two sets? 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. That might indeed be a better solution. The problem at the moment is really that I do not know which order they are added in (or rather, I want the test to be independent of that). 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. Yeah, therefore I asked for a Set. The Set is independent of the order - or did I get your comment wrong? |
||
|| (string.getName() == usedStrings.get(1).getName())); | ||
assertTrue((string2.getName() == usedStrings.get(0).getName()) | ||
|| (string2.getName() == usedStrings.get(1).getName())); | ||
assertTrue((string.getContent() == usedStrings.get(0).getContent()) | ||
|| (string.getContent() == usedStrings.get(1).getContent())); | ||
assertTrue((string2.getContent() == usedStrings.get(0).getContent()) | ||
|| (string2.getContent() == usedStrings.get(1).getContent())); | ||
} | ||
|
||
@Test | ||
public void getUsedStringsSingleString() { | ||
BibEntry entry = new BibEntry(IdGenerator.next()); | ||
entry.setField("author", "#AAA#"); | ||
BibtexString string = new BibtexString(IdGenerator.next(), "AAA", "Some other text"); | ||
BibtexString string2 = new BibtexString(IdGenerator.next(), "BBB", "Some more text"); | ||
database.addString(string); | ||
database.addString(string2); | ||
database.insertEntry(entry); | ||
List<BibtexString> usedStrings = (List<BibtexString>) database.getUsedStrings(Arrays.asList(entry)); | ||
assertEquals(string.getName(), usedStrings.get(0).getName()); | ||
assertEquals(string.getContent(), usedStrings.get(0).getContent()); | ||
} | ||
|
||
@Test | ||
public void getUsedStringsNoString() { | ||
BibEntry entry = new BibEntry(IdGenerator.next()); | ||
entry.setField("author", "Oscar Gustafsson"); | ||
BibtexString string = new BibtexString(IdGenerator.next(), "AAA", "Some other text"); | ||
database.addString(string); | ||
database.insertEntry(entry); | ||
Collection<BibtexString> usedStrings = database.getUsedStrings(Arrays.asList(entry)); | ||
assertEquals(Collections.emptyList(), usedStrings); | ||
} | ||
} |
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.
Is it possible to add a test case covering that?