Skip to content

Commit

Permalink
Change CSV export to separate all names using semicolon (JabRef#2762)
Browse files Browse the repository at this point in the history
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
125m125 committed Apr 25, 2017
1 parent 4e25cc6 commit 17248ec
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 1 deletion.
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"
85 changes: 85 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,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\",.*$"));
}
}
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"));
}
}

0 comments on commit 17248ec

Please sign in to comment.