From 112c96673db8e5d8bc5680d67459ffa36968df30 Mon Sep 17 00:00:00 2001
From: Phillipus
Date: Thu, 21 Nov 2024 08:48:54 +0000
Subject: [PATCH] Don't use deprecated Java URL constructors
- These are deprecated in Java 20
- See https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/net/URL.html#constructor-deprecation
- Use URI.toURL()
- Fix HTML report URL not opening Windows network paths on Firefox
---
.../canvas/policies/CanvasDNDEditPolicy.java | 12 +++----
.../actions/CheckForNewVersionAction.java | 14 +++-----
.../editor/actions/WebBrowserAction.java | 5 ++-
.../archimatetool/editor/utils/HTMLUtils.java | 34 ++++++-------------
.../reports/html/HTMLReportExporter.java | 8 +++--
5 files changed, 30 insertions(+), 43 deletions(-)
diff --git a/com.archimatetool.canvas/src/com/archimatetool/canvas/policies/CanvasDNDEditPolicy.java b/com.archimatetool.canvas/src/com/archimatetool/canvas/policies/CanvasDNDEditPolicy.java
index 2b6362d90..8ff73fb50 100644
--- a/com.archimatetool.canvas/src/com/archimatetool/canvas/policies/CanvasDNDEditPolicy.java
+++ b/com.archimatetool.canvas/src/com/archimatetool/canvas/policies/CanvasDNDEditPolicy.java
@@ -8,8 +8,8 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.net.URI;
import java.net.URISyntaxException;
-import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.util.ArrayList;
@@ -121,11 +121,11 @@ private Command getURLDropCommand(DiagramDropRequest request) throws IOException
}
File file = null;
- URL url = new URL(s);
+ URI uri = new URI(s);
- // Local file:/// URL for a file that exists
- if("file".equals(url.getProtocol())) {
- file = new File(url.toURI());
+ // Local file:/// URI for a file that exists
+ if("file".equals(uri.getScheme())) {
+ file = new File(uri);
}
// Online URL that we should download to a temporary file
else {
@@ -134,7 +134,7 @@ private Command getURLDropCommand(DiagramDropRequest request) throws IOException
file.deleteOnExit();
try(FileOutputStream fos = new FileOutputStream(file)) {
- try(ReadableByteChannel rbc = Channels.newChannel(url.openStream())) {
+ try(ReadableByteChannel rbc = Channels.newChannel(uri.toURL().openStream())) {
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
}
}
diff --git a/com.archimatetool.editor/src/com/archimatetool/editor/actions/CheckForNewVersionAction.java b/com.archimatetool.editor/src/com/archimatetool/editor/actions/CheckForNewVersionAction.java
index 8a0397bfb..e0fc05194 100644
--- a/com.archimatetool.editor/src/com/archimatetool/editor/actions/CheckForNewVersionAction.java
+++ b/com.archimatetool.editor/src/com/archimatetool/editor/actions/CheckForNewVersionAction.java
@@ -9,7 +9,8 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
-import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
@@ -69,8 +70,7 @@ public void run() {
return;
}
- URL url = new URL(versionFile);
- String newVersion = getOnlineVersion(url);
+ String newVersion = getOnlineVersion(new URI(versionFile).toURL());
// Get this app's main version number
String thisVersion = ArchiPlugin.INSTANCE.getVersion();
@@ -94,8 +94,7 @@ public void run() {
IWorkbenchBrowserSupport support = PlatformUI.getWorkbench().getBrowserSupport();
IWebBrowser browser = support.getExternalBrowser();
if(browser != null) {
- URL url2 = new URL(downloadURL);
- browser.openURL(url2);
+ browser.openURL(new URI(downloadURL).toURL());
}
}
}
@@ -103,10 +102,7 @@ public void run() {
MessageDialog.openInformation(null, Messages.CheckForNewVersionAction_1, Messages.CheckForNewVersionAction_4);
}
}
- catch(MalformedURLException ex) {
- ex.printStackTrace();
- }
- catch(IOException ex) {
+ catch(URISyntaxException | IOException ex) {
ex.printStackTrace();
showErrorMessage(Messages.CheckForNewVersionAction_5 + " " + ex.getMessage()); //$NON-NLS-1$
return;
diff --git a/com.archimatetool.editor/src/com/archimatetool/editor/actions/WebBrowserAction.java b/com.archimatetool.editor/src/com/archimatetool/editor/actions/WebBrowserAction.java
index 6face2c5e..4ad43c579 100644
--- a/com.archimatetool.editor/src/com/archimatetool/editor/actions/WebBrowserAction.java
+++ b/com.archimatetool.editor/src/com/archimatetool/editor/actions/WebBrowserAction.java
@@ -5,7 +5,7 @@
*/
package com.archimatetool.editor.actions;
-import java.net.URL;
+import java.net.URI;
import org.eclipse.jface.action.Action;
import org.eclipse.ui.PlatformUI;
@@ -33,8 +33,7 @@ public void run() {
try {
IWebBrowser browser = support.getExternalBrowser();
if(browser != null) {
- URL url = new URL(fUrl);
- browser.openURL(url);
+ browser.openURL(new URI(fUrl).toURL());
}
}
catch(Exception ex) {
diff --git a/com.archimatetool.editor/src/com/archimatetool/editor/utils/HTMLUtils.java b/com.archimatetool.editor/src/com/archimatetool/editor/utils/HTMLUtils.java
index 7fb542678..2a7d88713 100644
--- a/com.archimatetool.editor/src/com/archimatetool/editor/utils/HTMLUtils.java
+++ b/com.archimatetool.editor/src/com/archimatetool/editor/utils/HTMLUtils.java
@@ -6,7 +6,8 @@
package com.archimatetool.editor.utils;
import java.net.MalformedURLException;
-import java.net.URL;
+import java.net.URI;
+import java.net.URISyntaxException;
import java.util.regex.Pattern;
import org.eclipse.ui.PartInitException;
@@ -20,12 +21,13 @@
*
* @author Phillip Beauvoir
*/
+@SuppressWarnings("nls")
public class HTMLUtils {
/**
* The reg expression for HTML links
*/
- public static final String HTML_LINK_REGEX = "(http|https|ftp|file)://\\S+"; //$NON-NLS-1$
+ public static final String HTML_LINK_REGEX = "(http|https|ftp|file)://\\S+";
/**
* The compiled pattern to match HTML links
@@ -35,7 +37,7 @@ public class HTMLUtils {
/**
* The reg expression for HTML tags
*/
- public static final String HTML_TAG_REGEX = "<[^>]+>"; //$NON-NLS-1$
+ public static final String HTML_TAG_REGEX = "<[^>]+>";
/**
* The compiled pattern to match HTML tags
@@ -44,41 +46,27 @@ public class HTMLUtils {
/**
* Strip tags out of a String
- * @param str
- * @return
*/
public static String stripTags(String str) {
if (str == null || str.indexOf('<') == -1 || str.indexOf('>') == -1) {
return str;
}
- str = HTML_TAG_REGEX_PATTERN.matcher(str).replaceAll(""); //$NON-NLS-1$
+ str = HTML_TAG_REGEX_PATTERN.matcher(str).replaceAll("");
return str;
}
/**
* Open a link in a Browser
- * @param href
- * @throws PartInitException
- * @throws MalformedURLException
*/
public static void openLinkInBrowser(String href) throws PartInitException, MalformedURLException {
IWorkbenchBrowserSupport support = PlatformUI.getWorkbench().getBrowserSupport();
IWebBrowser browser = support.getExternalBrowser();
- browser.openURL(new URL(urlEncodeForSpaces(href.toCharArray())));
- }
-
- private static String urlEncodeForSpaces(char[] input) {
- StringBuffer retu = new StringBuffer(input.length);
- for(int i = 0; i < input.length; i++) {
- if(input[i] == ' ') {
- retu.append("%20"); //$NON-NLS-1$
- }
- else {
- retu.append(input[i]);
- }
+ try {
+ browser.openURL(new URI(href.replaceAll(" ", "%20")).toURL());
+ }
+ catch(URISyntaxException ex) {
+ throw new MalformedURLException(ex.toString());
}
- return retu.toString();
}
-
}
diff --git a/com.archimatetool.reports/src/com/archimatetool/reports/html/HTMLReportExporter.java b/com.archimatetool.reports/src/com/archimatetool/reports/html/HTMLReportExporter.java
index a67ca3636..3e9de5122 100644
--- a/com.archimatetool.reports/src/com/archimatetool/reports/html/HTMLReportExporter.java
+++ b/com.archimatetool.reports/src/com/archimatetool/reports/html/HTMLReportExporter.java
@@ -9,6 +9,7 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
+import java.net.URI;
import java.net.URL;
import java.util.HashMap;
import java.util.Hashtable;
@@ -145,8 +146,11 @@ else if(exception[0] != null) {
// Open it in external Browser
IWorkbenchBrowserSupport support = PlatformUI.getWorkbench().getBrowserSupport();
IWebBrowser browser = support.getExternalBrowser();
- // This method supports network URLs
- browser.openURL(new URL("file", null, file[0].getAbsolutePath().replace(" ", "%20"))); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+ // Firefox needs 5 forward slashes for Windows network paths so use 3 and construct URL like this
+ String path = "file:///" + file[0].getAbsolutePath().replaceAll(" ", "%20").replaceAll("\\\\", "/"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$//$NON-NLS-5$
+ browser.openURL(new URI(path).toURL());
+ // This works but not for Windows network paths on Firefox
+ // browser.openURL(file[0].toURI().toURL());
}
public void preview() throws Exception {