diff --git a/CHANGELOG.md b/CHANGELOG.md index 81f826a4b72..a8f58603947 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -83,6 +83,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv - We fixed an issue where "Specify Bib(La)TeX" tab was not focused when Bib(La)TeX was in the clipboard [#13597](https://github.com/JabRef/jabref/issues/13597) - We fixed an issue whereby the 'About' dialog was not honouring the user's configured font preferences. [#13558](https://github.com/JabRef/jabref/issues/13558) - We fixed an issue where the Pagetotal column was sorting the values alphabetically instead of numerically. [#12533](https://github.com/JabRef/jabref/issues/12533) +- We fixed an issue where URLs starting with "www." (without a protocol) in file fields caused an `IllegalArgumentException: URI is not absolute` error. [#12186](https://github.com/JabRef/jabref/issues/12186) - We fixed the dark mode of the BibTeX Source dialog in the Citation Relations tab. [#13599](https://github.com/JabRef/jabref/issues/13599) - We fixed an issue where the LibreOffice integration did not support citation keys containing Unicode characters. [#13301](https://github.com/JabRef/jabref/issues/13301) - We fixed an issue where the "Search ShortScience" action did not convert LaTeX-formatted titles to Unicode. [#13418](https://github.com/JabRef/jabref/issues/13418) diff --git a/jablib/src/main/java/org/jabref/logic/util/URLUtil.java b/jablib/src/main/java/org/jabref/logic/util/URLUtil.java index a77e96829f6..323c2fbd523 100644 --- a/jablib/src/main/java/org/jabref/logic/util/URLUtil.java +++ b/jablib/src/main/java/org/jabref/logic/util/URLUtil.java @@ -81,6 +81,15 @@ public static String cleanGoogleSearchURL(String url) { /// @param url the String to check for a URL /// @return true if `url` contains a valid URL public static boolean isURL(String url) { + if (url == null || url.trim().isEmpty()) { + return false; + } + + // Check if the URL has a protocol (http://, https://, ftp://) + if (!URL_PATTERN.matcher(url).matches()) { + return false; + } + try { create(url); return true; @@ -100,8 +109,16 @@ public static URL create(String url) throws MalformedURLException { if (url == null || url.trim().isEmpty()) { throw new IllegalArgumentException("URL must not be null or empty."); } + + String trimmedUrl = url.trim(); + + // Add https:// prefix to URLs starting with www. to make them absolute + if (trimmedUrl.startsWith("www.")) { + trimmedUrl = "https://" + trimmedUrl; + } + try { - URI parsedUri = new URI(url.trim()); + URI parsedUri = new URI(trimmedUrl); if (!parsedUri.isAbsolute()) { throw new MalformedURLException("URI is not absolute: " + url); } diff --git a/jablib/src/test/java/org/jabref/logic/importer/util/FileFieldParserTest.java b/jablib/src/test/java/org/jabref/logic/importer/util/FileFieldParserTest.java index 87b70b7eb4f..c6f1f1dd48b 100644 --- a/jablib/src/test/java/org/jabref/logic/importer/util/FileFieldParserTest.java +++ b/jablib/src/test/java/org/jabref/logic/importer/util/FileFieldParserTest.java @@ -50,6 +50,12 @@ private static Stream stringsToParseTest() throws MalformedURLExcepti List.of(), "" ), + + // URL starting with www. (without protocol) + Arguments.of( + List.of(new LinkedFile("A test", URLUtil.create("https://www.yahoo.com/abc/cde.htm"), "URL")), + "A test:www.yahoo.com/abc/cde.htm:URL" + ), // correct input Arguments.of( diff --git a/jablib/src/test/java/org/jabref/logic/net/URLUtilTest.java b/jablib/src/test/java/org/jabref/logic/net/URLUtilTest.java index 83b8a07afba..d577c179f3d 100644 --- a/jablib/src/test/java/org/jabref/logic/net/URLUtilTest.java +++ b/jablib/src/test/java/org/jabref/logic/net/URLUtilTest.java @@ -118,9 +118,18 @@ void emptyUrl() { } @Test - void uriMissingScheme() { + void urlStartingWithWww() throws MalformedURLException { + // URLs starting with www. should be prefixed with https:// + URL result = URLUtil.create("www.example.com"); + assertNotNull(result); + assertEquals("https://www.example.com", result.toString()); + } + + @Test + void uriMissingSchemeAndNotStartingWithWww() { + // URLs not starting with www. and without a scheme should still throw an exception MalformedURLException exception = assertThrows(MalformedURLException.class, () -> - URLUtil.create("www.example.com")); + URLUtil.create("example.com")); assertTrue(exception.getMessage().contains("not absolute")); }