From 172e9a7ae584fdca8b3f3cc9a2374d29a2d286c8 Mon Sep 17 00:00:00 2001 From: vmichelan Date: Mon, 1 Jul 2019 19:57:23 -0300 Subject: [PATCH 1/4] Adds priority to date fields when importing ris files --- .../importer/fileformat/RisImporter.java | 67 ++++++++++++++----- 1 file changed, 50 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/jabref/logic/importer/fileformat/RisImporter.java b/src/main/java/org/jabref/logic/importer/fileformat/RisImporter.java index 3adfa827d19..97779a6dfdf 100644 --- a/src/main/java/org/jabref/logic/importer/fileformat/RisImporter.java +++ b/src/main/java/org/jabref/logic/importer/fileformat/RisImporter.java @@ -27,6 +27,8 @@ public class RisImporter extends Importer { private static final Pattern RECOGNIZED_FORMAT_PATTERN = Pattern.compile("TY - .*"); private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy"); + //stores all the date tags from highest to lowest priority + private final String[] dateTags = {"Y1", "PY", "DA", "Y2"}; @Override public String getName() { @@ -61,7 +63,9 @@ public ParserResult importDatabase(BufferedReader reader) throws IOException { for (String entry1 : entries) { - boolean foundDate = false; + String dateTag = ""; + String dateValue = ""; + int datePriority = dateTags.length; String type = ""; String author = ""; @@ -193,25 +197,20 @@ public ParserResult importDatabase(BufferedReader reader) throws IOException { } } else if ("UR".equals(tag) || "L2".equals(tag) || "LK".equals(tag)) { fields.put(FieldName.URL, value); - } else if (!foundDate && (("Y1".equals(tag) || "Y2".equals(tag) || "PY".equals(tag) || "DA".equals(tag)) && (value.length() >= 4))) { - String year = value.substring(0, 4); + } else if (isDateTag(tag) && value.length() >= 4) { + int tagPriority = getDatePriority(tag); - try { - Year.parse(year, formatter); - //if the year is parsebale we have found our date - fields.put(FieldName.YEAR, value.substring(0, 4)); - foundDate = true; - } catch (DateTimeParseException ex) { - //We can't parse the year, we ignore it - } + if (tagPriority < datePriority) { + String year = value.substring(0, 4); - String[] parts = value.split("/"); - if ((parts.length > 1) && !parts[1].isEmpty()) { try { - int monthNumber = Integer.parseInt(parts[1]); - month = Month.getMonthByNumber(monthNumber); - } catch (NumberFormatException ex) { - // The month part is unparseable, so we ignore it. + Year.parse(year, formatter); + //if the year is parsebale we have found a higher priority date + dateTag = tag; + dateValue = value; + datePriority = tagPriority; + } catch (DateTimeParseException ex) { + //We can't parse the year, we ignore it } } } else if ("KW".equals(tag)) { @@ -277,6 +276,21 @@ else if ("AV".equals(tag)) { fields.put(FieldName.PAGES, startPage + endPage); } + // if we found a date + if (dateTag.length() > 0) { + fields.put(FieldName.YEAR, dateValue.substring(0, 4)); + + String[] parts = dateValue.split("/"); + if ((parts.length > 1) && !parts[1].isEmpty()) { + try { + int monthNumber = Integer.parseInt(parts[1]); + month = Month.getMonthByNumber(monthNumber); + } catch (NumberFormatException ex) { + // The month part is unparseable, so we ignore it. + } + } + } + // Remove empty fields: fields.entrySet().removeIf(key -> (key.getValue() == null) || key.getValue().trim().isEmpty()); @@ -300,4 +314,23 @@ private void addDoi(Map hm, String val) { hm.put(FieldName.DOI, doi); } } + + private boolean isDateTag(String s) { + for (String dateTag : dateTags) { + if (dateTag.equals(s)) { + return true; + } + } + return false; + } + + private int getDatePriority(String date) { + int i; + for (i = 0; i < dateTags.length; i++) { + if (dateTags[i].equals(date)) { + break; + } + } + return i; + } } From 2f243c16f08f2cfeba6f1ab4651970d90eb55546 Mon Sep 17 00:00:00 2001 From: danielbom Date: Tue, 2 Jul 2019 14:33:30 -0300 Subject: [PATCH 2/4] Updates RisImport test cases. --- .../importer/fileformat/RisImporterTest3.bib | 1 + .../fileformat/RisImporterTestDate1.1.bib | 15 +++++++++++ .../fileformat/RisImporterTestDate1.1.ris | 27 +++++++++++++++++++ .../fileformat/RisImporterTestDate1.2.bib | 15 +++++++++++ .../fileformat/RisImporterTestDate1.2.ris | 27 +++++++++++++++++++ 5 files changed, 85 insertions(+) create mode 100644 src/test/resources/org/jabref/logic/importer/fileformat/RisImporterTestDate1.1.bib create mode 100644 src/test/resources/org/jabref/logic/importer/fileformat/RisImporterTestDate1.1.ris create mode 100644 src/test/resources/org/jabref/logic/importer/fileformat/RisImporterTestDate1.2.bib create mode 100644 src/test/resources/org/jabref/logic/importer/fileformat/RisImporterTestDate1.2.ris diff --git a/src/test/resources/org/jabref/logic/importer/fileformat/RisImporterTest3.bib b/src/test/resources/org/jabref/logic/importer/fileformat/RisImporterTest3.bib index 9bd2b47b290..642578e2d77 100644 --- a/src/test/resources/org/jabref/logic/importer/fileformat/RisImporterTest3.bib +++ b/src/test/resources/org/jabref/logic/importer/fileformat/RisImporterTest3.bib @@ -5,6 +5,7 @@ @incollection{ issn = {978-1-4822-5326-9}, journal = {Dermoscopy Image Analysis}, number = {0}, + month = {#sep#}, pages = {1-22}, publisher = {CRC Press}, series = {Digital Imaging and Computer Vision}, diff --git a/src/test/resources/org/jabref/logic/importer/fileformat/RisImporterTestDate1.1.bib b/src/test/resources/org/jabref/logic/importer/fileformat/RisImporterTestDate1.1.bib new file mode 100644 index 00000000000..9be8fcc17a6 --- /dev/null +++ b/src/test/resources/org/jabref/logic/importer/fileformat/RisImporterTestDate1.1.bib @@ -0,0 +1,15 @@ +@article{, + author = {Valle-Delgado, J. J. and Molina-Bolívar, J. A. and Galisteo-González, F. and Gálvez-Ruiz, M. J. and Feiler, A. and Rutland, M. W.}, + comment = {doi: 10.1063/1.1954747}, + doi = {10.1063/1.1954747}, + issn = {0021-9606}, + journal = {J. Chem. Phys.}, + month = {#jul#}, + number = {3}, + pages = {034708}, + publisher = {American Institute of Physics}, + title = {Hydration forces between silica surfaces: Experimental data and predictions from different theories}, + url = {https://doi.org/10.1063/1.1954747}, + volume = {123}, + year = {2005} +} \ No newline at end of file diff --git a/src/test/resources/org/jabref/logic/importer/fileformat/RisImporterTestDate1.1.ris b/src/test/resources/org/jabref/logic/importer/fileformat/RisImporterTestDate1.1.ris new file mode 100644 index 00000000000..8c280e69142 --- /dev/null +++ b/src/test/resources/org/jabref/logic/importer/fileformat/RisImporterTestDate1.1.ris @@ -0,0 +1,27 @@ + + +TY - JOUR +T1 - Hydration forces between silica surfaces: Experimental data and predictions from different theories +AU - Valle-Delgado,J. J. +AU - Molina-Bolívar,J. A. +AU - Galisteo-González,F. +AU - Gálvez-Ruiz,M. J. +AU - Feiler,A. +AU - Rutland,M. W. +Y2 - 2019/03/28 +Y1 - 2005/07/15 +PY - 2005 +DA - 2005/07/15 +N1 - doi: 10.1063/1.1954747 +DO - 10.1063/1.1954747 +T2 - The Journal of Chemical Physics +JF - The Journal of Chemical Physics +JO - J. Chem. Phys. +SP - 034708 +VL - 123 +IS - 3 +PB - American Institute of Physics +SN - 0021-9606 +M3 - doi: 10.1063/1.1954747 +UR - https://doi.org/10.1063/1.1954747 +ER - diff --git a/src/test/resources/org/jabref/logic/importer/fileformat/RisImporterTestDate1.2.bib b/src/test/resources/org/jabref/logic/importer/fileformat/RisImporterTestDate1.2.bib new file mode 100644 index 00000000000..9be8fcc17a6 --- /dev/null +++ b/src/test/resources/org/jabref/logic/importer/fileformat/RisImporterTestDate1.2.bib @@ -0,0 +1,15 @@ +@article{, + author = {Valle-Delgado, J. J. and Molina-Bolívar, J. A. and Galisteo-González, F. and Gálvez-Ruiz, M. J. and Feiler, A. and Rutland, M. W.}, + comment = {doi: 10.1063/1.1954747}, + doi = {10.1063/1.1954747}, + issn = {0021-9606}, + journal = {J. Chem. Phys.}, + month = {#jul#}, + number = {3}, + pages = {034708}, + publisher = {American Institute of Physics}, + title = {Hydration forces between silica surfaces: Experimental data and predictions from different theories}, + url = {https://doi.org/10.1063/1.1954747}, + volume = {123}, + year = {2005} +} \ No newline at end of file diff --git a/src/test/resources/org/jabref/logic/importer/fileformat/RisImporterTestDate1.2.ris b/src/test/resources/org/jabref/logic/importer/fileformat/RisImporterTestDate1.2.ris new file mode 100644 index 00000000000..10458c4c8fc --- /dev/null +++ b/src/test/resources/org/jabref/logic/importer/fileformat/RisImporterTestDate1.2.ris @@ -0,0 +1,27 @@ + + +TY - JOUR +T1 - Hydration forces between silica surfaces: Experimental data and predictions from different theories +AU - Valle-Delgado,J. J. +AU - Molina-Bolívar,J. A. +AU - Galisteo-González,F. +AU - Gálvez-Ruiz,M. J. +AU - Feiler,A. +AU - Rutland,M. W. +Y1 - 2005/07/15 +PY - 2005 +DA - 2005/07/15 +Y2 - 2019/03/28 +N1 - doi: 10.1063/1.1954747 +DO - 10.1063/1.1954747 +T2 - The Journal of Chemical Physics +JF - The Journal of Chemical Physics +JO - J. Chem. Phys. +SP - 034708 +VL - 123 +IS - 3 +PB - American Institute of Physics +SN - 0021-9606 +M3 - doi: 10.1063/1.1954747 +UR - https://doi.org/10.1063/1.1954747 +ER - From 89d5483806c05a0fa64c087882a27fc6884404e1 Mon Sep 17 00:00:00 2001 From: Victor Figueira Date: Thu, 4 Jul 2019 18:06:49 -0300 Subject: [PATCH 3/4] Changes array to arrayasList Abbreviates isDateTag() and getDatePriority() --- .../importer/fileformat/RisImporter.java | 26 ++++++------------- 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/jabref/logic/importer/fileformat/RisImporter.java b/src/main/java/org/jabref/logic/importer/fileformat/RisImporter.java index 97779a6dfdf..14bc3936da9 100644 --- a/src/main/java/org/jabref/logic/importer/fileformat/RisImporter.java +++ b/src/main/java/org/jabref/logic/importer/fileformat/RisImporter.java @@ -12,6 +12,7 @@ import java.util.Map; import java.util.Optional; import java.util.regex.Pattern; +import java.util.Arrays; import org.jabref.logic.importer.Importer; import org.jabref.logic.importer.ParserResult; @@ -28,8 +29,8 @@ public class RisImporter extends Importer { private static final Pattern RECOGNIZED_FORMAT_PATTERN = Pattern.compile("TY - .*"); private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy"); //stores all the date tags from highest to lowest priority - private final String[] dateTags = {"Y1", "PY", "DA", "Y2"}; - + private final List dateTags = Arrays.asList("Y1", "PY", "DA", "Y2"); + @Override public String getName() { return "RIS"; @@ -65,7 +66,7 @@ public ParserResult importDatabase(BufferedReader reader) throws IOException { String dateTag = ""; String dateValue = ""; - int datePriority = dateTags.length; + int datePriority = dateTags.size(); String type = ""; String author = ""; @@ -315,22 +316,11 @@ private void addDoi(Map hm, String val) { } } - private boolean isDateTag(String s) { - for (String dateTag : dateTags) { - if (dateTag.equals(s)) { - return true; - } - } - return false; + private boolean isDateTag(String searchTag) { + return dateTags.stream().anyMatch(tag -> tag.equals(searchTag)); } - private int getDatePriority(String date) { - int i; - for (i = 0; i < dateTags.length; i++) { - if (dateTags[i].equals(date)) { - break; - } - } - return i; + private int getDatePriority(String dateTag) { + return dateTags.indexOf(dateTag); } } From 97cfb72db8ebba9853b5eccc58de2fc14804414e Mon Sep 17 00:00:00 2001 From: vmichelan Date: Sun, 7 Jul 2019 16:02:36 -0300 Subject: [PATCH 4/4] Use indexOf to find out if a tag is a date --- .../importer/fileformat/RisImporter.java | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/jabref/logic/importer/fileformat/RisImporter.java b/src/main/java/org/jabref/logic/importer/fileformat/RisImporter.java index 14bc3936da9..ce5295d2512 100644 --- a/src/main/java/org/jabref/logic/importer/fileformat/RisImporter.java +++ b/src/main/java/org/jabref/logic/importer/fileformat/RisImporter.java @@ -6,13 +6,13 @@ import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Optional; import java.util.regex.Pattern; -import java.util.Arrays; import org.jabref.logic.importer.Importer; import org.jabref.logic.importer.ParserResult; @@ -28,8 +28,6 @@ public class RisImporter extends Importer { private static final Pattern RECOGNIZED_FORMAT_PATTERN = Pattern.compile("TY - .*"); private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy"); - //stores all the date tags from highest to lowest priority - private final List dateTags = Arrays.asList("Y1", "PY", "DA", "Y2"); @Override public String getName() { @@ -62,11 +60,15 @@ public ParserResult importDatabase(BufferedReader reader) throws IOException { String[] entries = linesAsString.replace("\u2013", "-").replace("\u2014", "--").replace("\u2015", "--") .split("ER -.*\\n"); + //stores all the date tags from highest to lowest priority + List dateTags = Arrays.asList("Y1", "PY", "DA", "Y2"); + for (String entry1 : entries) { String dateTag = ""; String dateValue = ""; int datePriority = dateTags.size(); + int tagPriority; String type = ""; String author = ""; @@ -198,8 +200,7 @@ public ParserResult importDatabase(BufferedReader reader) throws IOException { } } else if ("UR".equals(tag) || "L2".equals(tag) || "LK".equals(tag)) { fields.put(FieldName.URL, value); - } else if (isDateTag(tag) && value.length() >= 4) { - int tagPriority = getDatePriority(tag); + } else if ((tagPriority = dateTags.indexOf(tag)) != -1 && value.length() >= 4) { if (tagPriority < datePriority) { String year = value.substring(0, 4); @@ -315,12 +316,4 @@ private void addDoi(Map hm, String val) { hm.put(FieldName.DOI, doi); } } - - private boolean isDateTag(String searchTag) { - return dateTags.stream().anyMatch(tag -> tag.equals(searchTag)); - } - - private int getDatePriority(String dateTag) { - return dateTags.indexOf(dateTag); - } }