Skip to content

Commit

Permalink
Fix checkstyle, compiling regular expressions and magic values
Browse files Browse the repository at this point in the history
  • Loading branch information
u7302654 committed Oct 24, 2024
1 parent 15631ec commit 48dec44
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/main/java/org/jabref/logic/importer/AuthorListParser.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
package org.jabref.logic.importer;

import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -32,6 +40,11 @@ public class AuthorListParser {

private static final Pattern STARTS_WITH_CAPITAL_LETTER_DOT_OR_DASH = Pattern.compile("^[A-Z](\\.[ -]| ?-)");

private static final Pattern EXTENDED_NAME_FORMAT_PATTERN = Pattern.compile("(\\w+)\\s*=\\s*([^,]+)(?:,\\s*|$)");

private static final int AUTHOR_SEPARATOR_LENGTH = 5; // Length of " and "

private static final int NAME_SPLIT_INCREMENT = 4; // Increment after processing " and "
/**
* the raw bibtex author/editor field
*/
Expand Down Expand Up @@ -190,7 +203,7 @@ public AuthorList parse(@NonNull String listOfNames) {
*/
private Optional<Author> parseExtendedNameFormat(String authorString) {
Map<String, String> nameParts = new HashMap<>();
Matcher matcher = Pattern.compile("(\\w+)\\s*=\\s*([^,]+)(?:,\\s*|$)").matcher(authorString);
Matcher matcher = EXTENDED_NAME_FORMAT_PATTERN.matcher(authorString);
while (matcher.find()) {
nameParts.put(matcher.group(1).trim(), matcher.group(2).trim());
}
Expand All @@ -212,12 +225,9 @@ private Optional<Author> parseExtendedNameFormat(String authorString) {

// abbreviate given name
String givenNameAbbreviated = abbreviateGivenName(givenName);

// create Author object
return Optional.of(new Author(givenName, givenNameAbbreviated, namePrefix, familyName, nameSuffix));
}


/**
* Abbreviates the given name by taking the first letter of each word and appending a dot.
* Handles cases where the given name is already in an abbreviated format.
Expand Down Expand Up @@ -263,17 +273,16 @@ private List<String> splitAuthors(String authorList) {
bracesLevel++;
} else if (c == '}') {
bracesLevel--;
} else if (i <= authorList.length() - 5 && authorList.substring(i, i + 5).equals(" and ") && bracesLevel == 0) {
} else if (i <= authorList.length() - AUTHOR_SEPARATOR_LENGTH && authorList.substring(i, i + AUTHOR_SEPARATOR_LENGTH).equals(" and ") && bracesLevel == 0) {
authors.add(authorList.substring(start, i));
i += 4;
i += NAME_SPLIT_INCREMENT;
start = i + 1;
}
}
authors.add(authorList.substring(start));
return authors;
}


/**
* Handle cases names in order Firstname Lastname, separated by <code>","</code> and a final <code>", and "</code>
* E.g, <code>"I. Podadera, J. M. Carmona, A. Ibarra, and J. Molla"</code>
Expand Down

0 comments on commit 48dec44

Please sign in to comment.