Skip to content

Commit

Permalink
fix NPE on empty field conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Helm committed Oct 2, 2022
1 parent fca9fb9 commit 2cd4363
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve

### Fixed

- We fixed an issue where converting empty fields led to an exception [#9200](https://github.com/JabRef/jabref/issues/9200)
- We fixed an issue where author names with tilde accents (for example ñ) were marked as "Names are not in the standard BibTex format" [#8071](https://github.com/JabRef/jabref/issues/8071)
- We fixed an issue where the possibility to generate a subdatabase from an aux file was writing empty files when called from the commandline [#9115](https://github.com/JabRef/jabref/issues/9115), [forum#3516](https://discourse.jabref.org/t/export-subdatabase-from-aux-file-on-macos-command-line/3516)
- We fixed the display of issue, number, eid and pages fields in the entry preview. [#8607](https://github.com/JabRef/jabref/pull/8607), [#8372](https://github.com/JabRef/jabref/issues/8372), [Koppor#514](https://github.com/koppor/jabref/issues/514), [forum#2390](https://discourse.jabref.org/t/unable-to-edit-my-bibtex-file-that-i-used-before-vers-5-1/2390), [forum#3462](https://discourse.jabref.org/t/jabref-5-6-need-help-with-export-from-jabref-to-microsoft-word-entry-preview-of-apa-7-not-rendering-correctly/3462)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class HtmlToLatexFormatter extends Formatter implements LayoutFormatter {

@Override
public String format(String text) {
String result = Objects.requireNonNull(text);
String result = Objects.requireNonNullElse(text, "");

if (result.isEmpty()) {
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import org.apache.commons.lang3.StringEscapeUtils;

import java.util.Objects;

@ApacheCommonsLang3Allowed("There is no equivalent in Google's Guava")
public class HtmlToUnicodeFormatter extends Formatter implements LayoutFormatter {

Expand All @@ -33,6 +35,7 @@ public String getExampleInput() {
@Override
public String format(String fieldText) {
// StringEscapeUtils converts characters and regex kills tags
return StringEscapeUtils.unescapeHtml4(fieldText).replaceAll("<[^>]*>", "");
String result = Objects.requireNonNullElse(fieldText, "");
return StringEscapeUtils.unescapeHtml4(result).replaceAll("<[^>]*>", "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ public class UnicodeToLatexFormatter extends Formatter implements LayoutFormatte

@Override
public String format(String text) {
String result = Objects.requireNonNull(text);

String result = Objects.requireNonNullElse(text, "");
if (result.isEmpty()) {
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public class LatexToUnicodeAdapter {
* @return a String with LaTeX resolved into Unicode, or the original String if the LaTeX could not be parsed
*/
public static String format(String inField) {
Objects.requireNonNull(inField);
return parse(inField).orElse(Normalizer.normalize(inField, Normalizer.Form.NFC));
String result = Objects.requireNonNullElse(inField, "");
return parse(result).orElse(Normalizer.normalize(result, Normalizer.Form.NFC));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public void testBasic() {

@Test
public void testHTML() {
assertEquals("", formatter.format(null));
assertEquals("", formatter.format(""));
assertEquals("{\\\"{a}}", formatter.format("&auml;"));
assertEquals("{\\\"{a}}", formatter.format("&#228;"));
assertEquals("{\\\"{a}}", formatter.format("&#xe4;"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class HtmlToUnicodeFormatterTest {

private static Stream<Arguments> data() {
return Stream.of(
Arguments.of("", ""),
Arguments.of("", null),
Arguments.of("abc", "abc"),
Arguments.of("åäö", "&aring;&auml;&ouml;"),
Arguments.of("í", "i&#x301;"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ void setUp() {

private static Stream<Arguments> testCases() {
return Stream.of(
Arguments.of("", null), // empty string input
Arguments.of("", ""), // empty string input
Arguments.of("abc", "abc"), // non unicode input
Arguments.of("{{\\aa}}{\\\"{a}}{\\\"{o}}", "\u00E5\u00E4\u00F6"), // multiple unicodes input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ void keepUnknownCommandWithoutArgument() {
assertEquals("\\aaaa", formatter.format("\\aaaa"));
}

@Test
void keepUnknownCommandWithNullArgument() {
assertEquals("", formatter.format(null));
}

@Test
void keepUnknownCommandWithArgument() {
assertEquals("\\aaaa{bbbb}", formatter.format("\\aaaa{bbbb}"));
Expand Down

0 comments on commit 2cd4363

Please sign in to comment.