Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the DOI connection Error by Automatically Removing all Special Characters at the end of DOI #8215

Closed
wants to merge 14 commits into from
Closed
2 changes: 1 addition & 1 deletion .idea/runConfigurations/JabRef_Main.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve

### Fixed

- We fixed an issue where an invalid DOI should not show "Connection error" [#8127](https://github.com/JabRef/jabref/issues/8127)
- We fixed an issue where an invalid DOI should not show "Connection error" [#8127](https://github.com/JabRef/jabref/issues/8127)
- We fixed an issue where an exception ocurred when a linked online file was edited in the entry editor [#8008](https://github.com/JabRef/jabref/issues/8008)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this duplicted line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, will do. Thanks very much.

- We fixed an issue where an exception occurred when a linked online file was edited in the entry editor [#8008](https://github.com/JabRef/jabref/issues/8008)
- We fixed an issue when checking for a new version when JabRef is used behind a corporate proxy. [#7884](https://github.com/JabRef/jabref/issues/7884)
- We fixed some icons that were drawn in the wrong color when JabRef used a custom theme. [#7853](https://github.com/JabRef/jabref/issues/7853)
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/org/jabref/model/entry/identifier/DOI.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ public DOI(String doi) {
// Remove whitespace
String trimmedDoi = doi.trim();

// Remove non-valid special character at the end
trimmedDoi = removeScharDOI(trimmedDoi);

// HTTP URL decoding
if (doi.matches(HTTP_EXP) || doi.matches(SHORT_DOI_HTTP_EXP)) {
try {
Expand Down Expand Up @@ -141,6 +144,32 @@ public DOI(String doi) {
}
}

/**
* Removes all special characters at the end of the DOI string
*
* @param doiStr the DOI/Short DOI string
* @return an DOI string with the special character at the end removed
*/
public static String removeScharDOI(String doiStr) {
char[] lcharDoi = doiStr.toCharArray();
int i = lcharDoi.length - 1;
// valid DOI characters are a-z(97-122), A-Z(65-90), 0-9(48-57), -(45), .(46), _(95), ;(59), ( (40), ) (41), /(47)
for (; i >= 0; i--) {
if ((lcharDoi[i] >= 45 && lcharDoi[i] <= 47) ||
(lcharDoi[i] >= 48 && lcharDoi[i] <= 57) ||
(lcharDoi[i] >= 65 && lcharDoi[i] <= 90) ||
(lcharDoi[i] >= 97 && lcharDoi[i] <= 122) ||
lcharDoi[i] == 95 ||
lcharDoi[i] == 59 ||
lcharDoi[i] == 40 ||
lcharDoi[i] == 41
) {
break;
}
}
return doiStr.substring(0, i + 1);
}

/**
* Creates an Optional&lt;DOI> from various schemes including URL, URN, and plain DOIs.
* <p>
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/org/jabref/model/entry/identifier/DOITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,21 @@ public void rejectMissingDivider() {
public void rejectMissingDividerInShortDoi() {
assertThrows(IllegalArgumentException.class, () -> new DOI("10gf4gqc end"));
}

private static Stream<Arguments> testECharData() {
return Stream.of(
// Arguments.of("10.1006/jmbi.1998.2354", new DOI("10.1006/jmbi.1998.2354").getDOI()),
Arguments.of(DOI.removeScharDOI("10.1007/978-3-319-47590-5_8?"), "10.1007/978-3-319-47590-5_8"),
Arguments.of(DOI.removeScharDOI("10.1007/978-3-319-47590-5_8?$"), "10.1007/978-3-319-47590-5_8"),
Arguments.of(DOI.removeScharDOI("10.1007/978-3-319-47590-5_8"), "10.1007/978-3-319-47590-5_8"),
Arguments.of(DOI.removeScharDOI("10.1007/978-3-319-47590-?%"), "10.1007/978-3-319-47590-"),
Arguments.of(DOI.removeScharDOI("10.1007/*?"), "10.1007/")
);
}

@ParameterizedTest
@MethodSource("testECharData")
public void testEquals2(String expected, String input) {
assertEquals(expected, input);
}
}