diff --git a/CHANGELOG.md b/CHANGELOG.md
index bdcbfa43487..12f856d00cf 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -27,7 +27,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- We fixed an error in the CrossRef fetcher that occurred if one of the fetched entries had no title
- We fixed an issue that prevented new entries to be automatically assigned to the currently selected group [#2783](https://github.com/JabRef/jabref/issues/2783).
- We fixed a bug that only allowed parsing positive timezones from a FileAnnotation [#2839](https://github.com/JabRef/jabref/issues/22839)
-
+- We fixed a bug that did not allow the correct re-export of the MS-Office XML field `msbib-accessed` with a different date format [#2859](https://github.com/JabRef/jabref/issues/2859).
### Removed
diff --git a/src/main/java/org/jabref/logic/msbib/MSBibEntry.java b/src/main/java/org/jabref/logic/msbib/MSBibEntry.java
index 7e1cb3d2758..2155ee5a56f 100644
--- a/src/main/java/org/jabref/logic/msbib/MSBibEntry.java
+++ b/src/main/java/org/jabref/logic/msbib/MSBibEntry.java
@@ -10,6 +10,7 @@
import org.jabref.model.entry.Author;
import org.jabref.model.entry.AuthorList;
+import org.jabref.model.entry.Date;
import org.jabref.model.strings.StringUtil;
import org.w3c.dom.Document;
@@ -26,14 +27,6 @@
*/
class MSBibEntry {
- /**
- * Allows 20.3-2007|||20/3- 2007 etc.
- * (\d{1,2})\s?[.,-/]\s?(\d{1,2})\s?[.,-/]\s?(\d{2,4})
- * 1-2 DIGITS SPACE SEPERATOR SPACE 1-2 DIGITS SPACE SEPERATOR SPACE 2-4 DIGITS
- */
- private static final Pattern DATE_PATTERN = Pattern
- .compile("(\\d{1,2})\\s*[.,-/]\\s*(\\d{1,2})\\s*[.,-/]\\s*(\\d{2,4})");
-
// MSBib fields and values
public Map fields = new HashMap<>();
public List authors;
@@ -165,22 +158,11 @@ private void populateFromXml(Element entry) {
String dayAccessed = getXmlElementTextContent("DayAccessed", entry);
String yearAccessed = getXmlElementTextContent("YearAccessed", entry);
- StringBuilder sbDateAccesed = new StringBuilder();
- if (monthAccessed != null) {
- sbDateAccesed.append(monthAccessed);
- sbDateAccesed.append(' ');
- }
- if (dayAccessed != null) {
- sbDateAccesed.append(dayAccessed);
- sbDateAccesed.append(", ");
- }
- if (yearAccessed != null) {
- sbDateAccesed.append(yearAccessed);
- }
- dateAccessed = sbDateAccesed.toString().trim();
- if (dateAccessed.isEmpty() || ",".equals(dateAccessed)) {
- dateAccessed = null;
- }
+ Optional parsedDateAcessed = Date.parse(Optional.ofNullable(yearAccessed),
+ Optional.ofNullable(monthAccessed),
+ Optional.ofNullable(dayAccessed));
+
+ parsedDateAcessed.map(Date::getNormalized).ifPresent(date -> dateAccessed = date);
NodeList nodeLst = entry.getElementsByTagNameNS("*", "Author");
if (nodeLst.getLength() > 0) {
@@ -262,15 +244,7 @@ public Element getEntryDom(Document document) {
addField(document, rootNode, entry.getKey(), entry.getValue());
}
- // based on bibtex content
- if (dateAccessed != null) {
- Matcher matcher = DATE_PATTERN.matcher(dateAccessed);
- if (matcher.matches() && (matcher.groupCount() >= 3)) {
- addField(document, rootNode, "Month" + "Accessed", matcher.group(1));
- addField(document, rootNode, "Day" + "Accessed", matcher.group(2));
- addField(document, rootNode, "Year" + "Accessed", matcher.group(3));
- }
- }
+ Optional.ofNullable(dateAccessed).ifPresent(field -> addDateAcessedFields(document, rootNode));
Element allAuthors = document.createElementNS(MSBibDatabase.NAMESPACE, MSBibDatabase.PREFIX + "Author");
@@ -361,6 +335,23 @@ private void addAuthor(Document document, Element allAuthors, String entryName,
}
+ private void addDateAcessedFields(Document document, Element rootNode) {
+ Optional parsedDateAcesseField = Date.parse(dateAccessed);
+ parsedDateAcesseField.flatMap(Date::getYear).map(accYear -> accYear.toString()).ifPresent(yearAccessed -> {
+ addField(document, rootNode, "Year" + "Accessed", yearAccessed);
+ });
+
+ parsedDateAcesseField.flatMap(Date::getMonth)
+ .map(accMonth -> accMonth.getTwoDigitNumber()).ifPresent(monthAcessed -> {
+ addField(document, rootNode, "Month" + "Accessed", monthAcessed);
+
+ });
+ parsedDateAcesseField.flatMap(Date::getDay).map(accDay -> accDay.toString()).ifPresent(dayAccessed -> {
+ addField(document, rootNode, "Day" + "Accessed", dayAccessed);
+ });
+
+ }
+
private void addAddress(Document document, Element parent, String addressToSplit) {
if (addressToSplit == null) {
return;
diff --git a/src/main/java/org/jabref/model/entry/Date.java b/src/main/java/org/jabref/model/entry/Date.java
index 5b4db035bfd..08bb3d7f552 100644
--- a/src/main/java/org/jabref/model/entry/Date.java
+++ b/src/main/java/org/jabref/model/entry/Date.java
@@ -31,21 +31,25 @@ public Date(TemporalAccessor date) {
* The code is essentially taken from http://stackoverflow.com/questions/4024544/how-to-parse-dates-in-multiple-formats-using-simpledateformat.
*/
public static Optional parse(String dateString) {
- List formatStrings = Arrays.asList("uuuu-M-d", "uuuu-M", "d-M-uuuu", "M/uu", "M/uuuu", "MMMM d, uuuu", "MMMM, uuuu",
+ Objects.requireNonNull(dateString);
+ List formatStrings = Arrays.asList("uuuu-M-d", "uuuu-M", "d-M-uuuu", "M/uu", "M/uuuu", "MMMM d, uuuu",
+ "MMMM, uuuu",
"d.M.uuuu", "uuuu.M.d", "uuuu");
- for (String formatString : formatStrings) {
- try {
- TemporalAccessor parsedDate = DateTimeFormatter.ofPattern(formatString).parse(dateString);
- return Optional.of(new Date(parsedDate));
- } catch (DateTimeParseException ignored) {
- // Ignored
+
+ for (String formatString : formatStrings) {
+ try {
+ TemporalAccessor parsedDate = DateTimeFormatter.ofPattern(formatString).parse(dateString);
+ return Optional.of(new Date(parsedDate));
+ } catch (DateTimeParseException ignored) {
+ // Ignored
+ }
}
- }
return Optional.empty();
}
- public static Optional parse(Optional yearValue, Optional monthValue, Optional dayValue) {
+ public static Optional parse(Optional yearValue, Optional monthValue,
+ Optional dayValue) {
Optional year = yearValue.flatMap(Date::convertToInt).map(Year::of);
Optional month = monthValue.flatMap(Month::parse);
Optional day = dayValue.flatMap(Date::convertToInt);
@@ -107,8 +111,12 @@ public TemporalAccessor toTemporalAccessor() {
@Override
public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
+ if (this == o) {
+ return true;
+ }
+ if ((o == null) || (getClass() != o.getClass())) {
+ return false;
+ }
Date date1 = (Date) o;
return Objects.equals(getYear(), date1.getYear()) &&
Objects.equals(getMonth(), date1.getMonth()) &&
diff --git a/src/test/java/org/jabref/model/entry/DateTest.java b/src/test/java/org/jabref/model/entry/DateTest.java
index 5c55cc92179..3192ef78554 100644
--- a/src/test/java/org/jabref/model/entry/DateTest.java
+++ b/src/test/java/org/jabref/model/entry/DateTest.java
@@ -14,4 +14,9 @@ public void parseCorrectlyDayMonthYearDate() throws Exception {
Date expected = new Date(LocalDate.of(2014, 6, 19));
assertEquals(Optional.of(expected), Date.parse("19-06-2014"));
}
+
+ @Test(expected = NullPointerException.class)
+ public void parseDateNull() {
+ assertEquals(Optional.empty(), Date.parse(null));
+ }
}
diff --git a/src/test/resources/org/jabref/logic/exporter/MsBibExportFormatTest1.xml b/src/test/resources/org/jabref/logic/exporter/MsBibExportFormatTest1.xml
index 960fd22d6b3..d92aaaa3a05 100644
--- a/src/test/resources/org/jabref/logic/exporter/MsBibExportFormatTest1.xml
+++ b/src/test/resources/org/jabref/logic/exporter/MsBibExportFormatTest1.xml
@@ -24,8 +24,8 @@
10
abb
1000
-12
-10
+10
+12
2015
5
diff --git a/src/test/resources/org/jabref/logic/exporter/MsBibExportFormatTestDateAcessed.bib b/src/test/resources/org/jabref/logic/exporter/MsBibExportFormatTestDateAcessed.bib
new file mode 100644
index 00000000000..bc4edda355f
--- /dev/null
+++ b/src/test/resources/org/jabref/logic/exporter/MsBibExportFormatTestDateAcessed.bib
@@ -0,0 +1,12 @@
+% Encoding: UTF-8
+
+@Article{Testa,
+ author = {Sam and jason},
+ title = {Test},
+ journaltitle = {Test},
+ year = {2012},
+ date = {2016},
+ msbib-accessed = {2015-12-12},
+}
+
+@Comment{jabref-meta: databaseType:biblatex;}
diff --git a/src/test/resources/org/jabref/logic/exporter/MsBibExportFormatTestDateAcessed.xml b/src/test/resources/org/jabref/logic/exporter/MsBibExportFormatTestDateAcessed.xml
new file mode 100644
index 00000000000..03524e8a072
--- /dev/null
+++ b/src/test/resources/org/jabref/logic/exporter/MsBibExportFormatTestDateAcessed.xml
@@ -0,0 +1,27 @@
+
+
+
+2012
+article
+JournalArticle
+2015-12-12
+Test
+Testa
+2015
+12
+12
+
+
+
+
+Sam
+
+
+jason
+
+
+
+
+Test
+
+