Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix export and import of MS office day/year/month acessed fields #2862

Merged
merged 4 commits into from
May 24, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
57 changes: 24 additions & 33 deletions src/main/java/org/jabref/logic/msbib/MSBibEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,14 +27,6 @@
*/
class MSBibEntry {

/**
* Allows 20.3-2007|||20/3- 2007 etc.
* <b>(\d{1,2})\s?[.,-/]\s?(\d{1,2})\s?[.,-/]\s?(\d{2,4})</b>
* 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<String, String> fields = new HashMap<>();
public List<MsBibAuthor> authors;
Expand Down Expand Up @@ -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<Date> 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) {
Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -361,6 +335,23 @@ private void addAuthor(Document document, Element allAuthors, String entryName,

}

private void addDateAcessedFields(Document document, Element rootNode) {
Optional<Date> 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;
Expand Down
30 changes: 19 additions & 11 deletions src/main/java/org/jabref/model/entry/Date.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Date> parse(String dateString) {
List<String> formatStrings = Arrays.asList("uuuu-M-d", "uuuu-M", "d-M-uuuu", "M/uu", "M/uuuu", "MMMM d, uuuu", "MMMM, uuuu",
Objects.requireNonNull(dateString);
List<String> 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<Date> parse(Optional<String> yearValue, Optional<String> monthValue, Optional<String> dayValue) {
public static Optional<Date> parse(Optional<String> yearValue, Optional<String> monthValue,
Optional<String> dayValue) {
Optional<Year> year = yearValue.flatMap(Date::convertToInt).map(Year::of);
Optional<Month> month = monthValue.flatMap(Month::parse);
Optional<Integer> day = dayValue.flatMap(Date::convertToInt);
Expand Down Expand Up @@ -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()) &&
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/org/jabref/model/entry/DateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
<b:NumberVolumes>10</b:NumberVolumes>
<b:AbbreviatedCaseNumber>abb</b:AbbreviatedCaseNumber>
<b:BIBTEX_Size>1000</b:BIBTEX_Size>
<b:MonthAccessed>12</b:MonthAccessed>
<b:DayAccessed>10</b:DayAccessed>
<b:MonthAccessed>10</b:MonthAccessed>
<b:DayAccessed>12</b:DayAccessed>
<b:YearAccessed>2015</b:YearAccessed>
<b:Day>5</b:Day>
<b:Author/>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<b:Sources xmlns:b="http://schemas.openxmlformats.org/officeDocument/2006/bibliography" xmlns="http://schemas.openxmlformats.org/officeDocument/2006/bibliography" SelectedStyle="">
<b:Source>
<b:Year>2012</b:Year>
<b:BIBTEX_Entry>article</b:BIBTEX_Entry>
<b:SourceType>JournalArticle</b:SourceType>
<b:Accessed>2015-12-12</b:Accessed>
<b:Title>Test</b:Title>
<b:Tag>Testa</b:Tag>
<b:YearAccessed>2015</b:YearAccessed>
<b:MonthAccessed>12</b:MonthAccessed>
<b:DayAccessed>12</b:DayAccessed>
<b:Author>
<b:Author>
<b:NameList>
<b:Person>
<b:Last>Sam</b:Last>
</b:Person>
<b:Person>
<b:Last>jason</b:Last>
</b:Person>
</b:NameList>
</b:Author>
</b:Author>
<b:JournalName>Test</b:JournalName>
</b:Source>
</b:Sources>