From: jprocter Date: Wed, 3 Aug 2011 13:37:41 +0000 (+0100) Subject: refactored resolvetocodebase code and reused for resource location as well as showURL... X-Git-Tag: Release_2_7~115^2 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=7ed076c4df6147646e3da63de1f8df108843545a;p=jalview.git refactored resolvetocodebase code and reused for resource location as well as showURL completion (JAL-786 and JAL-359) --- diff --git a/src/jalview/bin/JalviewLite.java b/src/jalview/bin/JalviewLite.java index 08229d6..24f3351 100644 --- a/src/jalview/bin/JalviewLite.java +++ b/src/jalview/bin/JalviewLite.java @@ -1865,7 +1865,7 @@ public class JalviewLite extends Applet implements StructureSelectionManagerProv { if (file.indexOf("://") == -1) { - String fl = applet.getDocumentBase() + file; + String fl = applet.resolveUrlForLocalOrAbsolute(file, getDocumentBase()); try { if (new java.net.URL(fl).openStream() != null) @@ -1881,20 +1881,17 @@ public class JalviewLite extends Applet implements StructureSelectionManagerProv { } ; - fl = applet.getCodeBase() + file; + fl = applet.resolveUrlForLocalOrAbsolute(file, getCodeBase()); try { if (new java.net.URL(fl).openStream() != null) { if (debug) { - if (debug) - { - System.err.println("Prepended codebase for resource: '" - + file + "'"); - } - return fl; + System.err.println("Prepended codebase for resource: '" + + file + "'"); } + return fl; } } catch (Exception x) { @@ -2282,6 +2279,27 @@ public class JalviewLite extends Applet implements StructureSelectionManagerProv showURL(helpUrl, "HELP"); } + /** + * form a complete URL given a path to a resource and a reference location on the same server + * @param url - an absolute path on the same server as localref or a document located relative to localref + * @param localref - a URL on the same server as url + * @return a complete URL for the resource located by url + */ + private String resolveUrlForLocalOrAbsolute(String url, URL localref) + { + String codebase = localref.toString(); + if (url.indexOf("/") == 0) + { + url = codebase.substring(0, codebase.length() + - localref.getFile().length()) + + url; + } + else + { + url = localref + url; + } + return url; + } /** * open a URL in the browser - resolving it according to relative refs and coping with javascript: protocol if necessary. @@ -2297,20 +2315,10 @@ public class JalviewLite extends Applet implements StructureSelectionManagerProv // TODO: verify (Bas Vroling bug) prepend codebase or server URL to // form valid URL // Should really use docbase, not codebase. - URL base = getDefaultParameter("resolvetocodebase", false) ? getDocumentBase() : getCodeBase(); - String codebase = base.toString(); - if (url.indexOf("/") == 0) - { - url = codebase.substring(0, codebase.length() - - base.getFile().length()) - + url; - } - else - { - url = base + url; - } + URL prepend; + url = resolveUrlForLocalOrAbsolute(url, prepend=getDefaultParameter("resolvetocodebase", false) ? getDocumentBase() : getCodeBase()); if (debug) { - System.err.println("Show url (prepended "+codebase+" - toggle resolvetocodebase if code/docbase resolution is wrong): " + url); + System.err.println("Show url (prepended "+prepend+" - toggle resolvetocodebase if code/docbase resolution is wrong): " + url); } } else