From 91771bf98242068c2473cb72c7f629cc13a938eb Mon Sep 17 00:00:00 2001 From: Tobias Diez Date: Mon, 10 Jul 2017 14:02:46 +0200 Subject: [PATCH 1/2] Remove
 tag from entries fetched using MathSciNet

---
 .../jabref/logic/importer/fetcher/MathSciNet.java    |  3 +++
 src/main/java/org/jabref/model/entry/BibEntry.java   | 12 +++++++-----
 .../org/jabref/model/entry/CanonicalBibtexEntry.java | 10 ++++++----
 3 files changed, 16 insertions(+), 9 deletions(-)

diff --git a/src/main/java/org/jabref/logic/importer/fetcher/MathSciNet.java b/src/main/java/org/jabref/logic/importer/fetcher/MathSciNet.java
index 4262d289954..eeacd2a0f70 100644
--- a/src/main/java/org/jabref/logic/importer/fetcher/MathSciNet.java
+++ b/src/main/java/org/jabref/logic/importer/fetcher/MathSciNet.java
@@ -108,5 +108,8 @@ public void doPostCleanup(BibEntry entry) {
         new MoveFieldCleanup("mrclass", FieldName.KEYWORDS).cleanup(entry);
         new FieldFormatterCleanup("mrreviewer", new ClearFormatter()).cleanup(entry);
         new FieldFormatterCleanup(FieldName.URL, new ClearFormatter()).cleanup(entry);
+
+        // Remove comments: MathSciNet prepends a 
 html tag
+        entry.setCommentsBeforeEntry("");
     }
 }
diff --git a/src/main/java/org/jabref/model/entry/BibEntry.java b/src/main/java/org/jabref/model/entry/BibEntry.java
index b887754be81..aa0bce82add 100644
--- a/src/main/java/org/jabref/model/entry/BibEntry.java
+++ b/src/main/java/org/jabref/model/entry/BibEntry.java
@@ -214,15 +214,15 @@ public String getType() {
     /**
      * Sets this entry's type.
      */
-    public void setType(EntryType type) {
-        this.setType(type.getName());
+    public void setType(String type) {
+        setType(type, EntryEventSource.LOCAL);
     }
 
     /**
      * Sets this entry's type.
      */
-    public void setType(String type) {
-        setType(type, EntryEventSource.LOCAL);
+    public void setType(EntryType type) {
+        this.setType(type.getName());
     }
 
     /**
@@ -700,7 +700,9 @@ public boolean equals(Object o) {
             return false;
         }
         BibEntry entry = (BibEntry) o;
-        return Objects.equals(type, entry.type) && Objects.equals(fields, entry.fields);
+        return Objects.equals(type, entry.type)
+                && Objects.equals(fields, entry.fields)
+                && Objects.equals(commentsBeforeEntry, entry.commentsBeforeEntry);
     }
 
     @Override
diff --git a/src/main/java/org/jabref/model/entry/CanonicalBibtexEntry.java b/src/main/java/org/jabref/model/entry/CanonicalBibtexEntry.java
index 04dbff71414..85fa98f469f 100644
--- a/src/main/java/org/jabref/model/entry/CanonicalBibtexEntry.java
+++ b/src/main/java/org/jabref/model/entry/CanonicalBibtexEntry.java
@@ -19,19 +19,21 @@ private CanonicalBibtexEntry() {
      *
      * Serializes all fields, even the JabRef internal ones. Does NOT serialize "KEY_FIELD" as field, but as key
      */
-    public static String getCanonicalRepresentation(BibEntry e) {
+    public static String getCanonicalRepresentation(BibEntry entry) {
         StringBuilder sb = new StringBuilder();
 
+        sb.append(entry.getUserComments());
+
         // generate first line: type and bibtex key
-        String citeKey = e.getCiteKeyOptional().orElse("");
-        sb.append(String.format("@%s{%s,", e.getType().toLowerCase(Locale.US), citeKey)).append('\n');
+        String citeKey = entry.getCiteKeyOptional().orElse("");
+        sb.append(String.format("@%s{%s,", entry.getType().toLowerCase(Locale.US), citeKey)).append('\n');
 
         // we have to introduce a new Map as fields are stored case-sensitive in JabRef (see https://github.com/koppor/jabref/issues/45).
         Map mapFieldToValue = new HashMap<>();
 
         // determine sorted fields -- all fields lower case
         SortedSet sortedFields = new TreeSet<>();
-        for (Entry field : e.getFieldMap().entrySet()) {
+        for (Entry field : entry.getFieldMap().entrySet()) {
             String fieldName = field.getKey();
             String fieldValue = field.getValue();
             // JabRef stores the key in the field KEY_FIELD, which must not be serialized

From 77c126874555236a34e90229dfd89eb283694eb7 Mon Sep 17 00:00:00 2001
From: Tobias Diez 
Date: Mon, 10 Jul 2017 14:27:25 +0200
Subject: [PATCH 2/2] Fix tests

---
 src/main/java/org/jabref/model/entry/BibEntry.java | 14 +++++++-------
 .../importer/fileformat/BibtexParserTest.java      |  3 ++-
 .../MedlinePlainImporterStringOutOfBounds.bib      |  3 ++-
 .../importer/fileformat/RepecNepImporterTest2.bib  |  2 --
 .../importer/fileformat/RepecNepImporterTest3.bib  |  2 --
 .../importer/fileformat/RisImporterTestScopus.bib  |  2 --
 6 files changed, 11 insertions(+), 15 deletions(-)

diff --git a/src/main/java/org/jabref/model/entry/BibEntry.java b/src/main/java/org/jabref/model/entry/BibEntry.java
index aa0bce82add..1f5e900a5c4 100644
--- a/src/main/java/org/jabref/model/entry/BibEntry.java
+++ b/src/main/java/org/jabref/model/entry/BibEntry.java
@@ -214,15 +214,15 @@ public String getType() {
     /**
      * Sets this entry's type.
      */
-    public void setType(String type) {
-        setType(type, EntryEventSource.LOCAL);
+    public void setType(EntryType type) {
+        this.setType(type.getName());
     }
 
     /**
      * Sets this entry's type.
      */
-    public void setType(EntryType type) {
-        this.setType(type.getName());
+    public void setType(String type) {
+        setType(type, EntryEventSource.LOCAL);
     }
 
     /**
@@ -605,7 +605,8 @@ public void setParsedSerialization(String parsedSerialization) {
     }
 
     public void setCommentsBeforeEntry(String parsedComments) {
-        this.commentsBeforeEntry = parsedComments;
+        // delete trailing whitespaces (between entry and text)
+        this.commentsBeforeEntry = REMOVE_TRAILING_WHITESPACE.matcher(parsedComments).replaceFirst("");
     }
 
     public boolean hasChanged() {
@@ -732,8 +733,7 @@ public BibEntry withField(String field, String value) {
     * Returns user comments (arbitrary text before the entry), if they exist. If not, returns the empty String
      */
     public String getUserComments() {
-        // delete trailing whitespaces (between entry and text) from stored serialization
-        return REMOVE_TRAILING_WHITESPACE.matcher(commentsBeforeEntry).replaceFirst("");
+        return commentsBeforeEntry;
     }
 
     public List getEntryLinkList(String fieldName, BibDatabase database) {
diff --git a/src/test/java/org/jabref/logic/importer/fileformat/BibtexParserTest.java b/src/test/java/org/jabref/logic/importer/fileformat/BibtexParserTest.java
index d2124b9473f..ece99a4723e 100644
--- a/src/test/java/org/jabref/logic/importer/fileformat/BibtexParserTest.java
+++ b/src/test/java/org/jabref/logic/importer/fileformat/BibtexParserTest.java
@@ -346,7 +346,8 @@ public void parseRecognizesEntryPrecedingComment() throws IOException {
 
         List parsed = result.getDatabase().getEntries();
 
-        BibEntry expected = new BibEntry("article").withField(BibEntry.KEY_FIELD, "test")
+        BibEntry expected = new BibEntry("article")
+                .withField(BibEntry.KEY_FIELD, "test")
                 .withField("author", "Ed von T@st");
         expected.setCommentsBeforeEntry(comment);
 
diff --git a/src/test/resources/org/jabref/logic/importer/fileformat/MedlinePlainImporterStringOutOfBounds.bib b/src/test/resources/org/jabref/logic/importer/fileformat/MedlinePlainImporterStringOutOfBounds.bib
index 005a90f4969..cf5f8e920e8 100644
--- a/src/test/resources/org/jabref/logic/importer/fileformat/MedlinePlainImporterStringOutOfBounds.bib
+++ b/src/test/resources/org/jabref/logic/importer/fileformat/MedlinePlainImporterStringOutOfBounds.bib
@@ -1,5 +1,6 @@
 @misc{,
   title = {This is a test title}
-}, @misc{,
+}
+@misc{,
   title = {This is also a test title}
 }
diff --git a/src/test/resources/org/jabref/logic/importer/fileformat/RepecNepImporterTest2.bib b/src/test/resources/org/jabref/logic/importer/fileformat/RepecNepImporterTest2.bib
index 9f24d3e4249..7b5161d9fb5 100644
--- a/src/test/resources/org/jabref/logic/importer/fileformat/RepecNepImporterTest2.bib
+++ b/src/test/resources/org/jabref/logic/importer/fileformat/RepecNepImporterTest2.bib
@@ -1,5 +1,3 @@
-% Encoding: UTF-8
-
 @TechReport{
   author =        {M?ller,Rudolf and Perea,Andr?s and Wolf,Sascha},
   title =         {Weak Monotonicity and Bayes-Nash Incentive Compatibility},
diff --git a/src/test/resources/org/jabref/logic/importer/fileformat/RepecNepImporterTest3.bib b/src/test/resources/org/jabref/logic/importer/fileformat/RepecNepImporterTest3.bib
index 25b69b52242..8d6cbb44118 100644
--- a/src/test/resources/org/jabref/logic/importer/fileformat/RepecNepImporterTest3.bib
+++ b/src/test/resources/org/jabref/logic/importer/fileformat/RepecNepImporterTest3.bib
@@ -1,5 +1,3 @@
-% Encoding: UTF-8
-
 @TechReport{,
   title = {Commercial Television and Voter Information},
   url =   {http://d.repec.org/n?u=RePEc:cla:levrem:784828000000000363&r=ict}
diff --git a/src/test/resources/org/jabref/logic/importer/fileformat/RisImporterTestScopus.bib b/src/test/resources/org/jabref/logic/importer/fileformat/RisImporterTestScopus.bib
index ab14c21b29b..fcdd9a0709b 100644
--- a/src/test/resources/org/jabref/logic/importer/fileformat/RisImporterTestScopus.bib
+++ b/src/test/resources/org/jabref/logic/importer/fileformat/RisImporterTestScopus.bib
@@ -1,5 +1,3 @@
-% Encoding: UTF-8
-
 @Article{,
   author   = {Federico, S. and Grillo, A. and Herzog, W.},
   title    = {A transversely isotropic composite with a statistical distribution of spheroidal inclusions: A geometrical approach to overall properties},