Skip to content

Commit

Permalink
JabRef#8491 : Enhance Date Field Support
Browse files Browse the repository at this point in the history
  • Loading branch information
addak committed Feb 26, 2022
1 parent 4d76136 commit 923bff1
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 26 deletions.
78 changes: 59 additions & 19 deletions src/main/java/org/jabref/logic/xmp/DublinCoreExtractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@

import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalField;
import java.util.*;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.apache.commons.lang3.LocaleUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.xmpbox.type.AbstractComplexProperty;
import org.apache.xmpbox.type.Attribute;
import org.apache.xmpbox.type.ComplexPropertyContainer;
import org.eclipse.jgit.annotations.NonNull;
import org.jabref.logic.TypedBibEntry;
import org.jabref.model.database.BibDatabaseMode;
import org.jabref.model.entry.Author;
Expand All @@ -26,7 +28,6 @@
import org.jabref.model.entry.field.FieldFactory;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.field.UnknownField;
import org.jabref.model.entry.types.EntryType;
import org.jabref.model.entry.types.EntryTypeFactory;
import org.jabref.model.schema.DublinCoreSchemaCustom;
import org.jabref.model.strings.StringUtil;
Expand Down Expand Up @@ -92,20 +93,41 @@ private void extractAuthor() {
private void extractYearAndMonth() {
List<String> dates = dcSchema.getUnqualifiedSequenceValueList("date");
if ((dates != null) && !dates.isEmpty()) {
String date = dates.get(0).trim();

String dateString = dates.get(0).trim();
var dateFormat = ((DublinCoreSchemaCustom) dcSchema).getDateAttributeFormatQualifier();

Calendar calender = null;
try {
calender = DateConverter.toCalendar(date);
calender = DateConverter.toCalendar(dateString);
} catch (IOException ignored) {
// Ignored
}
if (calender != null) {
bibEntry.setField(StandardField.YEAR, String.valueOf(calender.get(Calendar.YEAR)));
int monthNumber = calender.get(Calendar.MONTH) + 1;
// not the 1st of January
if (!((monthNumber == 1) && (calender.get(Calendar.DAY_OF_MONTH) == 1))) {
Month.getMonthByNumber(monthNumber)
.ifPresent(month -> bibEntry.setMonth(month));

if (Objects.nonNull(calender)) {
if (StringUtils.isNotEmpty(dateFormat)) {
LocalDate date = LocalDate.ofInstant(calender.toInstant(), ZoneId.systemDefault());
switch (dateFormat) {
case "YYYY-MM-DD":
bibEntry.setField(StandardField.DAY, Integer.toString(date.getDayOfMonth()));
case "YYYY-MM":
Month.getMonthByNumber(date.getMonthValue())
.ifPresent(month -> bibEntry.setMonth(month));
case "YYYY":
bibEntry.setField(StandardField.YEAR, String.valueOf(date.getYear()));
break;
default:

}

} else {
bibEntry.setField(StandardField.YEAR, String.valueOf(calender.get(Calendar.YEAR)));
int monthNumber = calender.get(Calendar.MONTH) + 1;
// not the 1st of January
if (!((monthNumber == 1) && (calender.get(Calendar.DAY_OF_MONTH) == 1))) {
Month.getMonthByNumber(monthNumber)
.ifPresent(month -> bibEntry.setMonth(month));
}
}
}
}
Expand Down Expand Up @@ -329,7 +351,19 @@ private void fillCreator(String creators) {
*/
private void fillDate() {
bibEntry.getFieldOrAlias(StandardField.DATE)
.ifPresent(publicationDate -> dcSchema.addUnqualifiedSequenceValue("date", publicationDate));
.ifPresent(publicationDate -> {
dcSchema.addUnqualifiedSequenceValue("date", publicationDate);
String[] f = publicationDate.split("-");
String format;
if(f.length == 3){
format = "YYYY-MM-DD";
} else if (f.length == 2){
format = "YYYY-MM";
} else{
format = "YYYY";
}
((DublinCoreSchemaCustom)dcSchema).setDateAttributeFormatQualifier(format);
});
}

/**
Expand Down Expand Up @@ -439,6 +473,8 @@ public void fillDublinCoreSchema() {

Set<Entry<Field, String>> fieldValues = new TreeSet<>(Comparator.comparing(fieldStringEntry -> fieldStringEntry.getKey().getName()));
fieldValues.addAll(bibEntry.getFieldMap().entrySet());

boolean hasStandardYearField = fieldValues.stream().anyMatch(field -> StandardField.YEAR.equals(field.getKey()));
for (Entry<Field, String> field : fieldValues) {
if (useXmpPrivacyFilter && xmpPreferences.getXmpPrivacyFilter().contains(field.getKey())) {
continue;
Expand Down Expand Up @@ -475,6 +511,10 @@ public void fillDublinCoreSchema() {
case LANGUAGE:
this.fillLanguages(field.getValue());
break;
case MONTH:
case DAY:
if (hasStandardYearField)
break;
default:
this.fillCustomField(field.getKey(), field.getValue());
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/logic/xmp/XmpUtilReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.apache.pdfbox.pdmodel.PDDocumentInformation;
import org.apache.pdfbox.pdmodel.common.PDMetadata;
import org.apache.xmpbox.XMPMetadata;
import org.apache.xmpbox.schema.DublinCoreSchema;
import org.jabref.model.schema.DublinCoreSchemaCustom;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -77,8 +77,8 @@ public static List<BibEntry> readXmp(Path path, XmpPreferences xmpPreferences)
if (!xmpMetaList.isEmpty()) {
// Only support Dublin Core since JabRef 4.2
for (XMPMetadata xmpMeta : xmpMetaList) {
DublinCoreSchema dcSchema = xmpMeta.getDublinCoreSchema();

DublinCoreSchemaCustom dcSchema = DublinCoreSchemaCustom.copyDublinCoreSchema(xmpMeta.getDublinCoreSchema());
if (dcSchema != null) {
DublinCoreExtractor dcExtractor = new DublinCoreExtractor(dcSchema, xmpPreferences, new BibEntry());
Optional<BibEntry> entry = dcExtractor.extractBibtexEntry();
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/jabref/logic/xmp/XmpUtilWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.xmpbox.XMPMetadata;
import org.apache.xmpbox.schema.DublinCoreSchema;
import org.apache.xmpbox.xml.XmpSerializer;
import org.jabref.model.schema.DublinCoreSchemaCustom;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -160,7 +161,7 @@ private static void writeDublinCore(PDDocument document,
meta.removeSchema(meta.getDublinCoreSchema());

for (BibEntry entry : resolvedEntries) {
DublinCoreSchema dcSchema = meta.createAndAddDublinCoreSchema();
DublinCoreSchemaCustom dcSchema = DublinCoreSchemaCustom.copyDublinCoreSchema(meta.createAndAddDublinCoreSchema());
XmpUtilWriter.writeToDCSchema(dcSchema, entry, null, xmpPreferences);
}

Expand Down
6 changes: 2 additions & 4 deletions src/test/java/org/jabref/logic/xmp/XmpUtilWriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

import javax.xml.transform.TransformerException;

import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.Date;
import org.jabref.model.entry.Month;
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.field.UnknownField;
import org.jabref.model.entry.types.EntryType;
import org.jabref.model.entry.types.StandardEntryType;

import org.apache.pdfbox.pdmodel.PDDocument;
Expand Down Expand Up @@ -74,7 +72,7 @@ private void initBibEntries() {
vapnik2000.setField(StandardField.DOI, "10.1007/978-1-4757-3264-1");
vapnik2000.setField(StandardField.OWNER, "Ich");
vapnik2000.setField(StandardField.LANGUAGE,"English, Japanese");
vapnik2000.setField(StandardField.YEAR, "2006");
vapnik2000.setDate(new Date(2314,11,1));

vapnik2000.setField(new UnknownField(DC_COVERAGE),"coverageField");
vapnik2000.setField(new UnknownField((DC_SOURCE)), "JabRef");
Expand Down

0 comments on commit 923bff1

Please sign in to comment.