Skip to content

Commit

Permalink
Don't use deprecated Java URL constructors
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
Phillipus committed Nov 21, 2024
1 parent 80f4d17 commit 112c966
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand All @@ -94,19 +94,15 @@ 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());
}
}
}
else {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 112c966

Please sign in to comment.