-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix for issue 2762: Change CSV export to separate all names using sem…
…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
1 parent
4e25cc6
commit b8cd026
Showing
6 changed files
with
178 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/main/java/org/jabref/logic/layout/format/AuthorAndToSemicolonReplacer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ", "; "); | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
src/main/resources/resource/layout/openoffice/openoffice-csv.layout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
120
src/test/java/org/jabref/logic/exporter/CsvExportFormatTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
src/test/java/org/jabref/logic/layout/format/AuthorAndToSemicolonReplacerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |