Skip to content

Commit

Permalink
Fix for issue 2762: Change CSV export to separate all names using sem…
Browse files Browse the repository at this point in the history
…icolon (#2793)

* Change CSV export to separate all names using semicolon (#2762)

Add LayoutFormatter AuthorAndToSemicolonReplacer that replaces all " and " with "; ". This formatter is used to format authors and editors for the openoffice-csv layout.

* add changelog entry for author and editor separation in csv exports (#2762)

* split test cases for CsvExportFormatTest and AuthorAndToSemicolonReplacerTest (#2793)

* add reference to issue 2762 in changelog (#2793)
  • Loading branch information
125m125 authored and tobiasdiez committed Apr 25, 2017
1 parent 4e25cc6 commit b8cd026
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ We refer to [GitHub issues](/~https://github.com/JabRef/jabref/issues) by using `#
- Continued to redesign the user interface: this time the editor got a fresh coat of paint:
- The buttons were changed to icons.
- Removed the hidden feature that a double click in the editor inserted the current date.
- All authors and editors are separated using semicolons when exporting to csv. [#2762](/~https://github.com/JabRef/jabref/issues/2762)


### Fixed
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/jabref/logic/layout/LayoutEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.jabref.logic.formatter.bibtexfields.HtmlToLatexFormatter;
import org.jabref.logic.formatter.bibtexfields.UnicodeToLatexFormatter;
import org.jabref.logic.layout.format.AuthorAbbreviator;
import org.jabref.logic.layout.format.AuthorAndToSemicolonReplacer;
import org.jabref.logic.layout.format.AuthorAndsCommaReplacer;
import org.jabref.logic.layout.format.AuthorAndsReplacer;
import org.jabref.logic.layout.format.AuthorFirstAbbrLastCommas;
Expand Down Expand Up @@ -403,6 +404,8 @@ private LayoutFormatter getLayoutFormatterByName(String name) throws Exception {
return new OOPreFormatter();
case "AuthorAbbreviator":
return new AuthorAbbreviator();
case "AuthorAndToSemicolonReplacer":
return new AuthorAndToSemicolonReplacer();
case "AuthorAndsCommaReplacer":
return new AuthorAndsCommaReplacer();
case "AuthorAndsReplacer":
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.jabref.logic.layout.format;

import org.jabref.logic.layout.LayoutFormatter;

public class AuthorAndToSemicolonReplacer implements LayoutFormatter {

@Override
public String format(String fieldText) {
return fieldText.replaceAll(" and ", "; ");
}

}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
\format[GetOpenOfficeType]{\entrytype},"\begin{isbn}\isbn\end{isbn}","\bibtexkey","\format[AuthorLastFirst,AuthorAndsReplacer]{\author}","\format[RemoveBrackets,RemoveWhitespace]{\title}","\journal",\volume,\number,"\month","\pages",\year,"\address","\note","\url","\booktitle","\chapter","\edition","\series","\format[AuthorLastFirst,AuthorAndsReplacer]{\editor}","\publisher","\begin{reporttype}\reporttype\end{reporttype}","\howpublished","\institution","\organization","\school","\annote","\format[Replace(\n, )]{\abstract}","\comment","\keywords","\format[FileLink(pdf)]{\file}","\key"
\format[GetOpenOfficeType]{\entrytype},"\begin{isbn}\isbn\end{isbn}","\bibtexkey","\format[AuthorLastFirst,AuthorAndToSemicolonReplacer]{\author}","\format[RemoveBrackets,RemoveWhitespace]{\title}","\journal",\volume,\number,"\month","\pages",\year,"\address","\note","\url","\booktitle","\chapter","\edition","\series","\format[AuthorLastFirst,AuthorAndToSemicolonReplacer]{\editor}","\publisher","\begin{reporttype}\reporttype\end{reporttype}","\howpublished","\institution","\organization","\school","\annote","\format[Replace(\n, )]{\abstract}","\comment","\keywords","\format[FileLink(pdf)]{\file}","\key"
120 changes: 120 additions & 0 deletions src/test/java/org/jabref/logic/exporter/CsvExportFormatTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package org.jabref.logic.exporter;

import java.io.File;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jabref.logic.layout.LayoutFormatterPreferences;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;

import com.google.common.base.Charsets;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.mockito.Answers;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;

public class CsvExportFormatTest {
private IExportFormat exportFormat;
public BibDatabaseContext databaseContext;
public Charset charset;

@Rule
public TemporaryFolder testFolder = new TemporaryFolder();

@Before
public void setUp() {
Map<String, ExportFormat> customFormats = new HashMap<>();
LayoutFormatterPreferences layoutPreferences = mock(LayoutFormatterPreferences.class, Answers.RETURNS_DEEP_STUBS);
SavePreferences savePreferences = mock(SavePreferences.class);
ExportFormats.initAllExports(customFormats, layoutPreferences, savePreferences);

exportFormat = ExportFormats.getExportFormat("oocsv");

databaseContext = new BibDatabaseContext();
charset = Charsets.UTF_8;
}

@After
public void tearDown() {
exportFormat = null;
}

@Test
public void testPerformExportForSingleAuthor() throws Exception {
File tmpFile = testFolder.newFile();
String filename = tmpFile.getCanonicalPath();
BibEntry entry = new BibEntry();
entry.setField("author", "Someone, Van Something");
List<BibEntry> entries = Arrays.asList(entry);

exportFormat.performExport(databaseContext, filename, charset, entries);

List<String> lines = Files.readAllLines(tmpFile.toPath());
assertEquals(2, lines.size());
assertEquals(
"10,\"\",\"\",\"Someone, Van Something\",\"\",\"\",,,\"\",\"\",,\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\"",
lines.get(1));
}

@Test
public void testPerformExportForMultipleAuthors() throws Exception {
File tmpFile = testFolder.newFile();
String filename = tmpFile.getCanonicalPath();
BibEntry entry = new BibEntry();
entry.setField("author", "von Neumann, John and Smith, John and Black Brown, Peter");
List<BibEntry> entries = Arrays.asList(entry);

exportFormat.performExport(databaseContext, filename, charset, entries);

List<String> lines = Files.readAllLines(tmpFile.toPath());
assertEquals(2, lines.size());
assertEquals(
"10,\"\",\"\",\"von Neumann, John; Smith, John; Black Brown, Peter\",\"\",\"\",,,\"\",\"\",,\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\"",
lines.get(1));
}

@Test
public void testPerformExportForSingleEditor() throws Exception {
File tmpFile = testFolder.newFile();
String filename = tmpFile.getCanonicalPath();
BibEntry entry = new BibEntry();
entry.setField("editor", "Someone, Van Something");
List<BibEntry> entries = Arrays.asList(entry);

exportFormat.performExport(databaseContext, filename, charset, entries);

List<String> lines = Files.readAllLines(tmpFile.toPath());
assertEquals(2, lines.size());
assertEquals(
"10,\"\",\"\",\"\",\"\",\"\",,,\"\",\"\",,\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"Someone, Van Something\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\"",
lines.get(1));
}

@Test
public void testPerformExportForMultipleEditors() throws Exception {
File tmpFile = testFolder.newFile();
String filename = tmpFile.getCanonicalPath();
BibEntry entry = new BibEntry();
entry.setField("editor", "von Neumann, John and Smith, John and Black Brown, Peter");
List<BibEntry> entries = Arrays.asList(entry);

exportFormat.performExport(databaseContext, filename, charset, entries);

List<String> lines = Files.readAllLines(tmpFile.toPath());
assertEquals(2, lines.size());
assertEquals(
"10,\"\",\"\",\"\",\"\",\"\",,,\"\",\"\",,\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"von Neumann, John; Smith, John; Black Brown, Peter\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\"",
lines.get(1));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.jabref.logic.layout.format;

import java.util.Arrays;
import java.util.Collection;

import org.jabref.logic.layout.LayoutFormatter;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class AuthorAndToSemicolonReplacerTest {

@Parameter(value = 0)
public String input;

@Parameter(value = 1)
public String expected;

@Parameters(name = "{index}: format(\"{0}\")=\"{1}\"")
public static Collection<Object[]> data() {

return Arrays.asList(new Object[][] {
{"",""},
{"Someone, Van Something", "Someone, Van Something"},
{"John Smith and Black Brown, Peter", "John Smith; Black Brown, Peter"},
{"von Neumann, John and Smith, John and Black Brown, Peter", "von Neumann, John; Smith, John; Black Brown, Peter"},
{"John von Neumann and John Smith and Peter Black Brown","John von Neumann; John Smith; Peter Black Brown"},
});
}

@Test
public void testFormat() {
LayoutFormatter a = new AuthorAndToSemicolonReplacer();

Assert.assertEquals(expected, a.format(input));
}
}

0 comments on commit b8cd026

Please sign in to comment.