forked from JabRef/jabref
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change CSV export to separate all names using semicolon (JabRef#2762)
Add LayoutFormatter AuthorAndToSemicolonReplacer that replaces all " and " with "; ". This formatter is used to format authors and editors for the openoffice-csv layout.
- Loading branch information
Showing
5 changed files
with
136 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
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" |
85 changes: 85 additions & 0 deletions
85
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,85 @@ | ||
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.junit.Assert.assertTrue; | ||
import static org.mockito.Mockito.mock; | ||
|
||
public class CsvExportFormatTest { | ||
private IExportFormat exportFormat; | ||
public BibDatabaseContext databaseContext; | ||
public Charset charset; | ||
public List<BibEntry> entries; | ||
|
||
@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; | ||
|
||
BibEntry entry = new BibEntry(); | ||
entry.setField("title", "title1"); | ||
entry.setField("author", "Someone, Van Something"); | ||
entry.setCiteKey("mykey1"); | ||
|
||
BibEntry entry2 = new BibEntry(); | ||
entry2.setField("title", "title2"); | ||
entry2.setField("author", "von Neumann, John and Smith, John and Black Brown, Peter"); | ||
entry2.setCiteKey("mykey2"); | ||
|
||
BibEntry entry3 = new BibEntry(); | ||
entry3.setField("title", "title3"); | ||
entry3.setField("editor", "Smith, John and Black Brown, Peter"); | ||
entry3.setCiteKey("mykey3"); | ||
|
||
entries = Arrays.asList(entry, entry2, entry3); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
exportFormat = null; | ||
} | ||
|
||
@Test | ||
public void testAuthorsAreSeparatedBySemicolon() throws Exception { | ||
File tmpFile = testFolder.newFile(); | ||
String filename = tmpFile.getCanonicalPath(); | ||
|
||
exportFormat.performExport(databaseContext, filename, charset, entries); | ||
|
||
List<String> lines = Files.readAllLines(tmpFile.toPath()); | ||
assertEquals(4, lines.size()); | ||
System.out.println(lines.get(3)); | ||
assertTrue(lines.get(1).matches("^.*,\"Someone, Van Something\",.*$")); | ||
assertTrue(lines.get(2).matches("^.*,\"von Neumann, John; Smith, John; Black Brown, Peter\",.*$")); | ||
assertTrue(lines.get(3).matches("^.*,\"Smith, John; Black Brown, Peter\",.*$")); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
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,35 @@ | ||
package org.jabref.logic.layout.format; | ||
|
||
import org.jabref.logic.layout.LayoutFormatter; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class AuthorAndToSemicolonReplacerTest { | ||
|
||
/** | ||
* Test method for | ||
* {@link org.jabref.logic.layout.format.AuthorAndToSemicolonReplacer#format(java.lang.String)}. | ||
*/ | ||
@Test | ||
public void testFormat() { | ||
LayoutFormatter a = new AuthorAndToSemicolonReplacer(); | ||
|
||
// Empty case | ||
Assert.assertEquals("", a.format("")); | ||
|
||
// Single Names don't change | ||
Assert.assertEquals("Someone, Van Something", a.format("Someone, Van Something")); | ||
|
||
// Two names just one semicolon | ||
Assert.assertEquals("John Smith; Black Brown, Peter", a | ||
.format("John Smith and Black Brown, Peter")); | ||
|
||
// Three names put two semicolons | ||
Assert.assertEquals("von Neumann, John; Smith, John; Black Brown, Peter", a | ||
.format("von Neumann, John and Smith, John and Black Brown, Peter")); | ||
|
||
Assert.assertEquals("John von Neumann; John Smith; Peter Black Brown", a | ||
.format("John von Neumann and John Smith and Peter Black Brown")); | ||
} | ||
} |