Skip to content

Commit

Permalink
Merge pull request #2731 from JabRef/fixDateParsing
Browse files Browse the repository at this point in the history
Improve date parsing
  • Loading branch information
koppor authored Apr 17, 2017
2 parents 781c824 + 7c34880 commit 723af6d
Show file tree
Hide file tree
Showing 30 changed files with 635 additions and 621 deletions.
26 changes: 15 additions & 11 deletions src/main/java/org/jabref/gui/entryeditor/FieldExtraComponents.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import javax.swing.JButton;
import javax.swing.JComboBox;
Expand Down Expand Up @@ -43,7 +47,7 @@
import org.jabref.model.entry.FieldName;
import org.jabref.model.entry.FieldProperty;
import org.jabref.model.entry.InternalBibtexFields;
import org.jabref.model.entry.MonthUtil;
import org.jabref.model.entry.Month;
import org.jabref.model.entry.identifier.DOI;
import org.jabref.model.entry.identifier.ISBN;
import org.jabref.preferences.JabRefPreferences;
Expand Down Expand Up @@ -378,19 +382,19 @@ public static Optional<JComponent> getYesNoExtraComponent(FieldEditor fieldEdito
* @return
*/
public static Optional<JComponent> getMonthExtraComponent(FieldEditor fieldEditor, EntryEditor entryEditor, BibDatabaseMode type) {
final String[] options = new String[13];
options[0] = Localization.lang("Select");
for (int i = 1; i <= 12; i++) {
options[i] = MonthUtil.getMonthByNumber(i).fullName;
}
JComboBox<String> month = new JComboBox<>(options);
List<String> monthNames = Arrays.stream(Month.values()).map(Month::getFullName).collect(Collectors.toList());
List<String> options = new ArrayList<>(13);
options.add(Localization.lang("Select"));
options.addAll(monthNames);

JComboBox<String> month = new JComboBox<>(options.toArray(new String[0]));
month.addActionListener(actionEvent -> {
int monthnumber = month.getSelectedIndex();
if (monthnumber >= 1) {
int monthNumber = month.getSelectedIndex();
if (monthNumber >= 1) {
if (type == BibDatabaseMode.BIBLATEX) {
fieldEditor.setText(String.valueOf(monthnumber));
fieldEditor.setText(String.valueOf(monthNumber));
} else {
fieldEditor.setText(MonthUtil.getMonthByNumber(monthnumber).bibtexFormat);
fieldEditor.setText(Month.getMonthByNumber(monthNumber).get().getJabRefFormat());
}
} else {
fieldEditor.setText("");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.Locale;
import java.util.Optional;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
Expand All @@ -24,7 +25,7 @@
import org.jabref.logic.l10n.Localization;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.FieldName;
import org.jabref.model.entry.MonthUtil;
import org.jabref.model.entry.Month;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -188,10 +189,8 @@ protected BibEntry importOai2Entry(String key) throws IOException, SAXException
entry.setField(FieldName.YEAR, "20" + fixedKey.substring(0, 2));

int monthNumber = Integer.parseInt(fixedKey.substring(2, 4));
MonthUtil.Month month = MonthUtil.getMonthByNumber(monthNumber);
if (month.isValid()) {
entry.setField(FieldName.MONTH, month.bibtexFormat);
}
Optional<Month> month = Month.getMonthByNumber(monthNumber);
month.ifPresent(entry::setMonth);
}
}
return entry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ public String format(String content, String fieldName) throws InvalidFieldValueE
return formatAndResolveStrings(result, fieldName);
}

/**
* This method handles # in the field content to get valid bibtex strings
*
* For instance, <code>#jan# - #feb#</code> gets <code>jan #{ - } # feb</code> (see @link{org.jabref.logic.bibtex.LatexFieldFormatterTests#makeHashEnclosedWordsRealStringsInMonthField()})
*/
private String formatAndResolveStrings(String content, String fieldName) throws InvalidFieldValueException {
stringBuilder = new StringBuilder();
checkBraces(content);
Expand Down Expand Up @@ -163,7 +168,6 @@ private String formatAndResolveStrings(String content, String fieldName) throws
pivot = pos2 + 1;
} else {
pivot = pos1 + 1;
//if (tell++ > 10) System.exit(0);
}
}

Expand Down Expand Up @@ -233,7 +237,7 @@ private void writeText(String text, int startPos, int endPos) {
inCommandName = false;
inCommand = true;
} else {
// Or simply the end of this command altogether:
// Or simply the end of this command alltogether:
commandName.delete(0, commandName.length());
inCommandName = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.jabref.model.entry.FieldName;
import org.jabref.model.entry.FieldProperty;
import org.jabref.model.entry.InternalBibtexFields;
import org.jabref.model.entry.MonthUtil;
import org.jabref.model.entry.Month;
import org.jabref.model.metadata.SaveOrderConfig;
import org.jabref.model.strings.StringUtil;

Expand Down Expand Up @@ -118,7 +118,9 @@ public int compare(BibEntry e1, BibEntry e2) {
int comparisonResult = Integer.compare(f1year, f2year);
return comparisonResult * multiplier;
} else if (fieldType == FieldType.MONTH) {
return Integer.compare(MonthUtil.getMonth(f1).number, MonthUtil.getMonth(f2).number) * multiplier;
int month1 = Month.parse(f1).map(Month::getNumber).orElse(-1);
int month2 = Month.parse(f2).map(Month::getNumber).orElse(-1);
return Integer.compare(month1, month2) * multiplier;
}

if (isNumeric) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package org.jabref.logic.formatter.bibtexfields;

import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.TemporalAccessor;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import org.jabref.logic.l10n.Localization;
import org.jabref.model.cleanup.Formatter;
import org.jabref.model.entry.Date;

/**
* This class transforms date to the format yyyy-mm-dd or yyyy-mm..
Expand All @@ -34,13 +30,8 @@ public String getKey() {
*/
@Override
public String format(String value) {
Optional<TemporalAccessor> parsedDate = tryParseDate(value);
if (!parsedDate.isPresent()) {
return value;
}

DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("uuuu-MM[-dd]");
return dateFormatter.format(parsedDate.get());
Optional<Date> parsedDate = Date.parse(value);
return parsedDate.map(Date::getNormalized).orElse(value);
}

@Override
Expand All @@ -53,29 +44,6 @@ public String getExampleInput() {
return "29.11.2003";
}

/*
* Try to parse the following formats
* "M/y" (covers 9/15, 9/2015, and 09/2015)
* "MMMM (dd), yyyy" (covers September 1, 2015 and September, 2015)
* "yyyy-MM-dd" (covers 2009-1-15)
* "d.M.uuuu" (covers 15.1.2015)
* "uuuu.M.d" (covers 2015.1.15)
* The code is essentially taken from http://stackoverflow.com/questions/4024544/how-to-parse-dates-in-multiple-formats-using-simpledateformat.
*/
private Optional<TemporalAccessor> tryParseDate(String dateString) {
List<String> formatStrings = Arrays.asList("uuuu-M-d", "uuuu-M", "M/uu", "M/uuuu", "MMMM d, uuuu", "MMMM, uuuu",
"d.M.uuuu", "uuuu.M.d");
for (String formatString : formatStrings) {
try {
return Optional.of(DateTimeFormatter.ofPattern(formatString).parse(dateString));
} catch (DateTimeParseException ignored) {
// Ignored
}
}

return Optional.empty();
}

@Override
public int hashCode() {
return defaultHashCode();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package org.jabref.logic.formatter.bibtexfields;

import java.util.Objects;
import java.util.Optional;

import org.jabref.logic.l10n.Localization;
import org.jabref.model.cleanup.Formatter;
import org.jabref.model.entry.MonthUtil;
import org.jabref.model.entry.Month;

public class NormalizeMonthFormatter implements Formatter {

Expand All @@ -21,12 +22,8 @@ public String getKey() {
@Override
public String format(String value) {
Objects.requireNonNull(value);
MonthUtil.Month month = MonthUtil.getMonth(value);
if (month.isValid()) {
return month.bibtexFormat;
} else {
return value;
}
Optional<Month> month = Month.parse(value);
return month.map(Month::getJabRefFormat).orElse(value);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

import org.jabref.logic.formatter.bibtexfields.ClearFormatter;
import org.jabref.logic.formatter.bibtexfields.NormalizeMonthFormatter;
import org.jabref.logic.help.HelpFile;
import org.jabref.logic.importer.FetcherException;
import org.jabref.logic.importer.IdBasedParserFetcher;
Expand All @@ -26,8 +28,9 @@
import org.jabref.logic.importer.SearchBasedFetcher;
import org.jabref.logic.importer.fileformat.MedlineImporter;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.cleanup.FieldFormatterCleanup;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.MonthUtil;
import org.jabref.model.entry.FieldName;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -144,10 +147,11 @@ public Parser getParser() {

@Override
public void doPostCleanup(BibEntry entry) {
entry.clearField("journal-abbreviation");
entry.clearField("status");
entry.clearField("copyright");
entry.getField("month").ifPresent(month -> entry.setField("month", MonthUtil.getMonth(month).bibtexFormat));
new FieldFormatterCleanup("journal-abbreviation", new ClearFormatter()).cleanup(entry);
new FieldFormatterCleanup("status", new ClearFormatter()).cleanup(entry);
new FieldFormatterCleanup("copyright", new ClearFormatter()).cleanup(entry);

new FieldFormatterCleanup(FieldName.MONTH, new NormalizeMonthFormatter()).cleanup(entry);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -17,7 +18,7 @@
import org.jabref.logic.util.FileExtensions;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.FieldName;
import org.jabref.model.entry.MonthUtil;
import org.jabref.model.entry.Month;

/**
* Importer for the ISI Web of Science, INSPEC and Medline format.
Expand Down Expand Up @@ -338,19 +339,19 @@ public static String parseMonth(String value) {

String[] parts = value.split("\\s|\\-");
for (String part1 : parts) {
MonthUtil.Month month = MonthUtil.getMonthByShortName(part1.toLowerCase(Locale.ROOT));
if (month.isValid()) {
return month.bibtexFormat;
Optional<Month> month = Month.getMonthByShortName(part1.toLowerCase(Locale.ROOT));
if (month.isPresent()) {
return month.get().getJabRefFormat();
}
}

// Try two digit month
for (String part : parts) {
try {
int number = Integer.parseInt(part);
MonthUtil.Month month = MonthUtil.getMonthByNumber(number);
if (month.isValid()) {
return month.bibtexFormat;
Optional<Month> month = Month.getMonthByNumber(number);
if (month.isPresent()) {
return month.get().getJabRefFormat();
}
} catch (NumberFormatException ignored) {
// Ignored
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@

import java.io.BufferedReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Objects;

Expand All @@ -18,6 +13,7 @@
import org.jabref.logic.importer.ParserResult;
import org.jabref.logic.util.FileExtensions;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.Date;
import org.jabref.model.entry.FieldName;

import org.apache.commons.logging.Log;
Expand Down Expand Up @@ -148,14 +144,12 @@ public class RepecNepImporter extends Importer {
private static final Log LOGGER = LogFactory.getLog(RepecNepImporter.class);

private static final Collection<String> RECOGNIZED_FIELDS = Arrays.asList("Keywords", "JEL", "Date", "URL", "By");

private final ImportFormatPreferences importFormatPreferences;
private int line;
private String lastLine = "";
private String preLine = "";
private boolean inOverviewSection;

private final ImportFormatPreferences importFormatPreferences;


public RepecNepImporter(ImportFormatPreferences importFormatPreferences) {
this.importFormatPreferences = importFormatPreferences;
Expand Down Expand Up @@ -347,30 +341,10 @@ private void parseAdditionalFields(BibEntry be, boolean multilineUrlFieldAllowed
} else if ("JEL".equals(keyword)) {
be.setField("jel", readMultipleLines(in));

// parse date field
} else if (keyword.startsWith("Date")) {
Date date = null;
// parse date field
String content = readMultipleLines(in);
String[] recognizedDateFormats = new String[]{"yyyy-MM-dd", "yyyy-MM", "yyyy"};
int i = 0;
for (; (i < recognizedDateFormats.length) && (date == null); i++) {
try {
date = new SimpleDateFormat(recognizedDateFormats[i]).parse(content);
} catch (ParseException e) {
// wrong format
}
}

Calendar cal = new GregorianCalendar();
cal.setTime(date == null ? new Date() : date);
be.setField(FieldName.YEAR, String.valueOf(cal.get(Calendar.YEAR)));
if ((date != null) && recognizedDateFormats[i - 1].contains("MM")) {
be.setField(FieldName.MONTH, String.valueOf(cal.get(Calendar.MONTH) + 1));
}
if ((date != null) && recognizedDateFormats[i - 1].contains("dd")) {
be.setField(FieldName.DAY, String.valueOf(cal.get(Calendar.DAY_OF_MONTH)));
}

Date.parse(content).ifPresent(be::setDate);
// parse URL field
} else if (keyword.startsWith("URL")) {
String content;
Expand Down
Loading

0 comments on commit 723af6d

Please sign in to comment.