Skip to content

Commit

Permalink
Fix JabRef#1271: Authors with compound first names are parsed properly
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasdiez committed Apr 29, 2016
1 parent 3f9b937 commit f778df6
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ to [sourceforge feature requests](https://sourceforge.net/p/jabref/features/) by
- Fixed [#1245](https://github.com/JabRef/jabref/issues/1245): Empty jstyle properties can now be specified as ""
- Fixed [#1259](https://github.com/JabRef/jabref/issues/1259): NPE when sorting tabs
- Fixed display bug in the cleanup dialog: field formatters are now correctly displayed using their name
- Fixed [#1245](https://github.com/JabRef/jabref/issues/1245): Empty jstyle properties can now be specified as ""
- Fixed [#1271](https://github.com/JabRef/jabref/issues/1271): Authors with compound first names are displayed properly

### Removed
- Removed possibility to export entries/databases to an `.sql` file, as the logic cannot easily use the correct escape logic
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/net/sf/jabref/model/entry/AuthorList.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package net.sf.jabref.model.entry;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.WeakHashMap;
Expand Down Expand Up @@ -156,6 +157,10 @@ protected AuthorList(List<Author> authors) {
this.authors = Objects.requireNonNull(authors);
}

protected AuthorList(Author author) {
this(Collections.singletonList(author));
}

/**
* Retrieve an AuthorList for the given string of authors or editors.
* <p>
Expand Down Expand Up @@ -416,7 +421,7 @@ public String getAsLastFirstNames(boolean abbreviate, boolean oxfordComma) {

@Override
public String toString() {
return getAsLastFirstNamesWithAnd(false);
return authors.toString();
}

/**
Expand Down
20 changes: 9 additions & 11 deletions src/main/java/net/sf/jabref/model/entry/AuthorListParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ public class AuthorListParser {
}


private static final int TOKEN_GROUP_LENGTH = 4; // number of entries for

// a token
private static final int TOKEN_GROUP_LENGTH = 4; // number of entries for a token

// the following are offsets of an entry in a group of entries for one token
private static final int OFFSET_TOKEN = 0; // String -- token itself;
Expand Down Expand Up @@ -137,6 +135,11 @@ private Author getAuthor() {
}
if (vonStart < 0) {
if (!tokenCase) {
int previousTermToken = tokens.size() - TOKEN_GROUP_LENGTH - TOKEN_GROUP_LENGTH + OFFSET_TOKEN_TERM;
if(previousTermToken >= 0 && tokens.get(previousTermToken).equals('-')) {
// We are in a first name which contained a hyphen
break;
}
vonStart = tokens.size() - TOKEN_GROUP_LENGTH;
break;
}
Expand Down Expand Up @@ -353,12 +356,12 @@ private int getToken() {
if (c == '{') {
bracesLevel++;
}
if ((c == '}') && (bracesLevel > 0)) {
bracesLevel--;
}
if (firstLetterIsFound && (tokenAbbr < 0) && (bracesLevel == 0)) {
tokenAbbr = tokenEnd;
}
if ((c == '}') && (bracesLevel > 0)) {
bracesLevel--;
}
if (!firstLetterIsFound && (currentBackslash < 0) && Character.isLetter(c)) {
if (bracesLevel == 0) {
tokenCase = Character.isUpperCase(c);
Expand Down Expand Up @@ -386,11 +389,6 @@ private int getToken() {
if ((bracesLevel == 0) && ((",;~-".indexOf(c) != -1) || Character.isWhitespace(c))) {
break;
}
// Morten Alver 18 Apr 2006: Removed check for hyphen '-' above to
// prevent
// problems with names like Bailey-Jones getting broken up and
// sorted wrong.
// Aaron Chen 14 Sep 2008: Enable hyphen check for first names like Chang-Chin
tokenEnd++;
}
if (tokenAbbr < 0) {
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/net/sf/jabref/model/entry/AuthorListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -580,4 +580,29 @@ public void testRemoveStartAndEndBraces() {
public void createCorrectInitials() {
Assert.assertEquals("J. G.", AuthorList.parse("Hornberg, Johann Gottfried").getAuthor(0).getFirstAbbr());
}

@Test
public void parseNameWithBracesAroundFirstName() throws Exception {
//TODO: Be more intelligent and abbreviate the first name correctly
Author expected = new Author("Tse-tung", "{Tse-tung}.", null, "Mao", null);
Assert.assertEquals(new AuthorList(expected), AuthorList.parse("{Tse-tung} Mao"));
}

@Test
public void parseNameWithBracesAroundLastName() throws Exception {
Author expected = new Author("Hans", "H.", null, "van den Bergen", null);
Assert.assertEquals(new AuthorList(expected), AuthorList.parse("{van den Bergen}, Hans"));
}

@Test
public void parseNameWithHyphenInFirstName() throws Exception {
Author expected = new Author("Tse-tung", "T.-t.", null, "Mao", null);
Assert.assertEquals(new AuthorList(expected), AuthorList.parse("Tse-tung Mao"));
}

@Test
public void parseNameWithHyphenInLastName() throws Exception {
Author expected = new Author("Firstname", "F.", null, "Bailey-Jones", null);
Assert.assertEquals(new AuthorList(expected), AuthorList.parse("Firstname Bailey-Jones"));
}
}

0 comments on commit f778df6

Please sign in to comment.