Skip to content

Fix citation key table #13151

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

Merged
merged 6 commits into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.jabref.logic.bibtex.comparator;

import java.util.Comparator;

import org.jabref.model.entry.BibEntry;

public class BibEntryByCitationKeyComparator implements Comparator<BibEntry> {
@Override
public int compare(BibEntry e1, BibEntry e2) {
boolean e1HasCitationKey = e1.hasCitationKey();
boolean e2HasCitationKey = e2.hasCitationKey();

if (!e1HasCitationKey && !e2HasCitationKey) {
return 0;
}

if (e1HasCitationKey && !e2HasCitationKey) {
return -1;
}

if (!e1HasCitationKey && e2HasCitationKey) {
return 1;
}

assert e1HasCitationKey && e2HasCitationKey;
Copy link

Choose a reason for hiding this comment

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

Using assertions for control flow is not recommended as they can be disabled at runtime. Consider using a more robust method to ensure both entries have citation keys.


return e1.getCitationKey().get().compareTo(e2.getCitationKey().get());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.jabref.logic.bibtex.comparator;

import java.util.Comparator;
import java.util.Iterator;

import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.Field;

/**
* Sorts entries by the number of fields and then by the field names.
*/
public class BibEntryByFieldsComparator implements Comparator<BibEntry> {
@Override
public int compare(BibEntry e1, BibEntry e2) {
int sizeComparison = e1.getFields().size() - e2.getFields().size();
if (sizeComparison != 0) {
return sizeComparison;
}
Iterator<String> it1 = e1.getFields().stream().map(Field::getName).sorted().iterator();
Iterator<String> it2 = e2.getFields().stream().map(Field::getName).sorted().iterator();
while (it1.hasNext() && it2.hasNext()) {
int fieldComparison = it1.next().compareTo(it2.next());
if (fieldComparison != 0) {
return fieldComparison;
}
}
assert !it1.hasNext() && !it2.hasNext();
Copy link

Choose a reason for hiding this comment

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

Using assertions for control flow is not recommended as they can be disabled at runtime, leading to unexpected behavior. Consider using a different approach to ensure iterators are exhausted.

return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.SequencedCollection;
import java.util.Set;

import org.jabref.logic.bibtex.comparator.BibEntryByCitationKeyComparator;
import org.jabref.logic.bibtex.comparator.BibEntryByFieldsComparator;
import org.jabref.logic.bibtex.comparator.FieldComparatorStack;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.types.EntryType;
Expand Down Expand Up @@ -55,38 +57,23 @@ public Result check(List<BibEntry> entries) {
return;
}

List<BibEntry> sortedEntries = entryTypeToEntriesMap
List<Comparator<BibEntry>> comparators = List.of(
new BibEntryByCitationKeyComparator(),
new BibEntryByFieldsComparator());
FieldComparatorStack<BibEntry> comparatorStack = new FieldComparatorStack<>(comparators);

List<BibEntry> differingEntries = entryTypeToEntriesMap
.get(entryType).stream()
.filter(entry -> !entry.getFields().equals(commonFields))
.sorted(getBibEntryComparator()).toList();
resultMap.put(entryType, new EntryTypeResult(uniqueFields, sortedEntries));
.sorted(comparatorStack)
.toList();

resultMap.put(entryType, new EntryTypeResult(uniqueFields, differingEntries));
});

return new Result(resultMap);
}

/**
* Sorts entries by the number of fields and then by the field names.
*/
private static Comparator<BibEntry> getBibEntryComparator() {
return (e1, e2) -> {
int sizeComparison = e1.getFields().size() - e2.getFields().size();
if (sizeComparison != 0) {
return sizeComparison;
}
Iterator<String> it1 = e1.getFields().stream().map(Field::getName).sorted().iterator();
Iterator<String> it2 = e2.getFields().stream().map(Field::getName).sorted().iterator();
while (it1.hasNext() && it2.hasNext()) {
int fieldComparison = it1.next().compareTo(it2.next());
if (fieldComparison != 0) {
return fieldComparison;
}
}
assert !it1.hasNext() && !it2.hasNext();
return 0;
};
}

private static void collectEntriesIntoMaps(List<BibEntry> entries, Map<EntryType, Set<Field>> entryTypeToFieldsInAnyEntryMap, Map<EntryType, Set<Field>> entryTypeToFieldsInAllEntriesMap, Map<EntryType, Set<BibEntry>> entryTypeToEntriesMap) {
entries.forEach(entry -> {
EntryType entryType = entry.getType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.StringJoiner;
Expand Down Expand Up @@ -53,46 +54,43 @@ public void writeFindings() throws IOException {
super.writeFindings();

if (!isPorcelain) {
int widthSymbol = Localization.lang("Symbol").length();
int widthMeaning = Collections.max(List.of(
Localization.lang("Meaning").length(),
Localization.lang("required field is present").length(),
Localization.lang("optional field is present").length(),
Localization.lang("unknown field is present").length(),
Localization.lang("field is absent").length()
));

writer.write("\n");
writer.write("%s | %s\n".formatted(REQUIRED_FIELD_AT_ENTRY_TYPE_CELL_ENTRY, Localization.lang("required field is present")));
writer.write("%s | %s\n".formatted(OPTIONAL_FIELD_AT_ENTRY_TYPE_CELL_ENTRY, Localization.lang("optional field is present")));
writer.write("%s | %s\n".formatted(UNKNOWN_FIELD_AT_ENTRY_TYPE_CELL_ENTRY, Localization.lang("unknown field is present")));
writer.write("%s | %s\n".formatted(UNSET_FIELD_AT_ENTRY_TYPE_CELL_ENTRY, Localization.lang("field is absent")));
writer.write(("| %-" + widthSymbol + "s | %-" + widthMeaning + "s |\n").formatted(Localization.lang("Symbol"), Localization.lang("Meaning")));
writer.write(("| " + "-".repeat(widthSymbol) + " | " + "-".repeat(widthMeaning) + " |\n").formatted("--", "--"));
writer.write(("| %-" + widthSymbol + "s | %-" + widthMeaning + "s |\n").formatted(REQUIRED_FIELD_AT_ENTRY_TYPE_CELL_ENTRY, Localization.lang("required field is present")));
writer.write(("| %-" + widthSymbol + "s | %-" + widthMeaning + "s |\n").formatted(OPTIONAL_FIELD_AT_ENTRY_TYPE_CELL_ENTRY, Localization.lang("optional field is present")));
writer.write(("| %-" + widthSymbol + "s | %-" + widthMeaning + "s |\n").formatted(UNKNOWN_FIELD_AT_ENTRY_TYPE_CELL_ENTRY, Localization.lang("unknown field is present")));
writer.write(("| %-" + widthSymbol + "s | %-" + widthMeaning + "s |\n").formatted(UNSET_FIELD_AT_ENTRY_TYPE_CELL_ENTRY, Localization.lang("field is absent")));
}
}

private void initializeColumnWidths() {
columnWidths = new ArrayList<>(columnNames.size());

Integer max = getColumnWidthOfEntryTypes();
columnWidths.add(max);
int entryTypeWidth = "entry type".length();
int citationKeyWidth = "citation key".length();

max = getColumnWidthOfCitationKeys(max);
columnWidths.add(max);
for (var keysAndValue : result.entryTypeToResultMap().entrySet()) {
entryTypeWidth = Math.max(entryTypeWidth, keysAndValue.getKey().getDisplayName().length());
for (var entry : keysAndValue.getValue().sortedEntries()) {
citationKeyWidth = Math.max(citationKeyWidth, entry.getCitationKey().orElse("").length());
}
}

columnWidths.add(entryTypeWidth);
columnWidths.add(citationKeyWidth);
columnWidths.addAll(columnNames.stream().skip(2).map(String::length).toList());
}

private Integer getColumnWidthOfEntryTypes() {
int max = result.entryTypeToResultMap().keySet()
.stream()
.map(entryType -> entryType.getDisplayName().length())
.max(Integer::compareTo)
.get();
max = Math.max(max, "entry type".length());
return max;
}

private Integer getColumnWidthOfCitationKeys(Integer max) {
result.entryTypeToResultMap().values()
.stream()
.flatMap(entryTypeResult -> entryTypeResult.sortedEntries().stream())
.map(entry -> entry.getCitationKey().orElse("").length())
.max(Integer::compareTo)
.get();
return Math.max(max, "citation key".length());
}

@Override
protected void writeBibEntry(BibEntry bibEntry, String entryType, Set<Field> requiredFields, Set<Field> optionalFields) throws IOException {
List<String> theRecord = getFindingsAsList(bibEntry, entryType, requiredFields, optionalFields);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ void checkSimpleLibrary(@TempDir Path tempDir) throws IOException {
| Article | first | o | - |
| Article | second | - | ? |

x | required field is present
o | optional field is present
? | unknown field is present
- | field is absent
| Symbol | Meaning |
| ------ | ------------------------- |
| x | required field is present |
| o | optional field is present |
| ? | unknown field is present |
| - | field is absent |
""", Files.readString(txtFile).replace("\r\n", "\n"));
}

Expand Down Expand Up @@ -81,10 +83,45 @@ void checkDifferentOutputSymbols(@TempDir Path tempDir) throws IOException {
| ---------- | ------------ | ------ | ----- | ----- |
| Article | first | ? | o | x |

x | required field is present
o | optional field is present
? | unknown field is present
- | field is absent
| Symbol | Meaning |
| ------ | ------------------------- |
| x | required field is present |
| o | optional field is present |
| ? | unknown field is present |
| - | field is absent |
""", Files.readString(txtFile).replace("\r\n", "\n"));
}

@Test
void checkVeryLongCitationKey(@TempDir Path tempDir) throws IOException {
UnknownField customField = new UnknownField("custom");
BibEntry first = new BibEntry(StandardEntryType.Article, "first-very-long-key")
.withField(StandardField.AUTHOR, "Author One") // required
.withField(StandardField.TITLE, "Title") // required
.withField(StandardField.PAGES, "some pages") // optional
.withField(customField, "custom"); // unknown
BibEntry second = new BibEntry(StandardEntryType.Article, "second")
.withField(StandardField.AUTHOR, "Author One");
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(List.of(first, second));

Path txtFile = tempDir.resolve("checkDifferentOutputSymbols-result.txt");
try (Writer writer = new OutputStreamWriter(Files.newOutputStream(txtFile));
BibliographyConsistencyCheckResultTxtWriter BibliographyConsistencyCheckResultTxtWriter = new BibliographyConsistencyCheckResultTxtWriter(result, writer, false)) {
BibliographyConsistencyCheckResultTxtWriter.writeFindings();
}
assertEquals("""
Field Presence Consistency Check Result

| entry type | citation key | Custom | Pages | Title |
| ---------- | ------------------- | ------ | ----- | ----- |
| Article | first-very-long-key | ? | o | x |

| Symbol | Meaning |
| ------ | ------------------------- |
| x | required field is present |
| o | optional field is present |
| ? | unknown field is present |
| - | field is absent |
""", Files.readString(txtFile).replace("\r\n", "\n"));
}

Expand All @@ -106,11 +143,16 @@ void checkComplexLibrary(@TempDir Path tempDir) throws IOException {
.withField(StandardField.AUTHOR, "Author One")
.withField(StandardField.YEAR, "2024")
.withField(StandardField.PUBLISHER, "publisher");
// Entry added to check for alphabetical ordering of citation keys
BibEntry fifth = new BibEntry(StandardEntryType.InProceedings, "fifth")
.withField(StandardField.AUTHOR, "Author One")
.withField(StandardField.LOCATION, "location")
.withField(StandardField.YEAR, "2024");
BibEntry sixth = new BibEntry(StandardEntryType.InProceedings, "sixth")
.withField(StandardField.AUTHOR, "Author One")
.withField(StandardField.YEAR, "2024");

BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(List.of(first, second, third, fourth, fifth));
BibliographyConsistencyCheck.Result result = new BibliographyConsistencyCheck().check(List.of(first, second, third, fourth, fifth, sixth));

Path txtFile = tempDir.resolve("checkSimpleLibrary-result.txt");
try (Writer writer = new OutputStreamWriter(Files.newOutputStream(txtFile));
Expand All @@ -120,17 +162,19 @@ void checkComplexLibrary(@TempDir Path tempDir) throws IOException {
assertEquals("""
Field Presence Consistency Check Result

| entry type | citation key | Location | Pages | Publisher |
| ------------- | ------------- | -------- | ----- | --------- |
| Article | first | - | o | - |
| Article | second | - | - | ? |
| InProceedings | fourth | - | - | o |
| InProceedings | third | ? | o | - |

x | required field is present
o | optional field is present
? | unknown field is present
- | field is absent
| entry type | citation key | Location | Pages | Publisher |
| ------------- | ------------ | -------- | ----- | --------- |
| Article | first | - | o | - |
| Article | second | - | - | ? |
| InProceedings | fourth | - | - | o |
| InProceedings | third | ? | o | - |

| Symbol | Meaning |
| ------ | ------------------------- |
| x | required field is present |
| o | optional field is present |
| ? | unknown field is present |
| - | field is absent |
""", Files.readString(txtFile).replace("\r\n", "\n"));
}

Expand Down