Skip to content

Commit fb1c9a9

Browse files
committed
merge group trees, prevent duplicate group assignment in entry
Add new BibDesk group Fix IOB for change listeing
1 parent 6525a9e commit fb1c9a9

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

src/main/java/org/jabref/logic/bibtex/comparator/BibDatabaseDiff.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ private static List<BibEntryDiff> compareEntries(List<BibEntry> originalEntries,
8989
}
9090
}
9191

92+
// Prevent IndexOutOfBoundException
93+
if (newEntries.isEmpty()) {
94+
return differences;
95+
}
9296
BibEntry bestEntry = newEntries.get(bestMatchIndex);
9397
if (bestMatch > MATCH_THRESHOLD
9498
|| hasEqualCitationKey(originalEntry, bestEntry)

src/main/java/org/jabref/logic/importer/fileformat/BibtexParser.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Objects;
2020
import java.util.Optional;
2121
import java.util.Set;
22+
import java.util.function.Predicate;
2223
import java.util.regex.Pattern;
2324

2425
import javax.xml.parsers.DocumentBuilderFactory;
@@ -65,6 +66,9 @@
6566
import org.w3c.dom.NodeList;
6667
import org.xml.sax.SAXException;
6768

69+
import static org.jabref.logic.util.MetadataSerializationConfiguration.GROUP_QUOTE_CHAR;
70+
import static org.jabref.logic.util.MetadataSerializationConfiguration.GROUP_TYPE_SUFFIX;
71+
6872
/**
6973
* Class for importing BibTeX-files.
7074
* <p>
@@ -259,7 +263,23 @@ private ParserResult parseFileContent() throws IOException {
259263
meta,
260264
importFormatPreferences.bibEntryPreferences().getKeywordSeparator());
261265
if (bibDeskGroupTreeNode != null) {
262-
metaData.setGroups(bibDeskGroupTreeNode);
266+
metaData.getGroups().ifPresentOrElse(existingGroupTree -> {
267+
var existingGroups = meta.get(MetaData.GROUPSTREE);
268+
// We only have one Group BibDeskGroup with n children
269+
// instead of iterating through the whole group structure every time we just search in the metadata for the group name
270+
var groupsToAdd = bibDeskGroupTreeNode.getChildren()
271+
.stream().
272+
filter(Predicate.not(groupTreeNode -> existingGroups.contains(GROUP_TYPE_SUFFIX + groupTreeNode.getName() + GROUP_QUOTE_CHAR)));
273+
groupsToAdd.forEach(existingGroupTree::addChild);
274+
275+
},
276+
// metadata does not contain any groups, so we need to create an AllEntriesGroup and add the other groups as children
277+
() -> {
278+
GroupTreeNode rootNode = new GroupTreeNode(DefaultGroupsFactory.getAllEntriesGroup());
279+
bibDeskGroupTreeNode.moveTo(rootNode);
280+
metaData.setGroups(rootNode);
281+
}
282+
);
263283
}
264284
parserResult.setMetaData(metaData);
265285
} catch (ParseException exception) {
@@ -313,7 +333,6 @@ private void parseAndAddEntry(String type) {
313333
} catch (IOException ex) {
314334
// This makes the parser more robust:
315335
// If an exception is thrown when parsing an entry, drop the entry and try to resume parsing.
316-
317336
LOGGER.warn("Could not parse entry", ex);
318337
parserResult.addWarning(Localization.lang("Error occurred when parsing entry") + ": '" + ex.getMessage()
319338
+ "'. " + "\n\n" + Localization.lang("JabRef skipped the entry."));
@@ -381,7 +400,8 @@ private void addBibDeskGroupEntriesToJabRefGroups() {
381400
Optional<String> groupValue = bibEntry.flatMap(entry -> entry.getField(StandardField.GROUPS));
382401
if (groupValue.isEmpty()) { // if the citation does not belong to a group already
383402
bibEntry.flatMap(entry -> entry.setField(StandardField.GROUPS, groupName));
384-
} else { // if the citation does belong to a group already, we concatenate
403+
} else if (!groupValue.get().contains(groupName)) {
404+
// if the citation does belong to a group already and is not yet assigned to the same group, we concatenate
385405
String concatGroup = groupValue.get() + "," + groupName;
386406
bibEntry.flatMap(entryByCitationKey -> entryByCitationKey.setField(StandardField.GROUPS, concatGroup));
387407
}
@@ -401,7 +421,7 @@ private void parseBibDeskComment(String comment, Map<String, String> meta) throw
401421

402422
NodeList dictList = doc.getElementsByTagName("dict");
403423
meta.putIfAbsent(MetaData.DATABASE_TYPE, "bibtex;");
404-
bibDeskGroupTreeNode = new GroupTreeNode(DefaultGroupsFactory.getAllEntriesGroup());
424+
bibDeskGroupTreeNode = GroupTreeNode.fromGroup(new ExplicitGroup("BibDeskGroups", GroupHierarchyType.INDEPENDENT, importFormatPreferences.bibEntryPreferences().getKeywordSeparator()));
405425

406426
// Since each static group has their own dict element, we iterate through them
407427
for (int i = 0; i < dictList.getLength(); i++) {

src/main/java/org/jabref/logic/util/MetadataSerializationConfiguration.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ public class MetadataSerializationConfiguration {
1818
*/
1919
public static final char GROUP_QUOTE_CHAR = '\\';
2020

21+
/**
22+
* Group Type suffix (part of the GroupType)
23+
*/
24+
public static final String GROUP_TYPE_SUFFIX = ":";
25+
2126
/**
2227
* For separating units (e.g. name and hierarchic context) in the string representation
2328
*/

src/main/java/org/jabref/model/groups/GroupTreeNode.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ public List<BibEntry> getEntriesInGroup(List<BibEntry> entries) {
197197
return result;
198198
}
199199

200+
/**
201+
* Get the name of the underlying group
202+
* @return String the name of the group
203+
*/
200204
public String getName() {
201205
return group.getName();
202206
}

0 commit comments

Comments
 (0)