From 6f2610ec4d7a79dde17f9b3d02225c62aa52cfd8 Mon Sep 17 00:00:00 2001 From: gmungoc Date: Tue, 15 Sep 2015 12:26:34 +0100 Subject: [PATCH] JAL-1859 refactoring / debugging added --- src/jalview/bin/JalviewLite.java | 10 +- .../javascript/MouseOverStructureListener.java | 101 +++++++++++++------- 2 files changed, 74 insertions(+), 37 deletions(-) diff --git a/src/jalview/bin/JalviewLite.java b/src/jalview/bin/JalviewLite.java index 7112381..e736009 100644 --- a/src/jalview/bin/JalviewLite.java +++ b/src/jalview/bin/JalviewLite.java @@ -2400,10 +2400,11 @@ public class JalviewLite extends Applet implements * Try relative to document base */ URL documentBase = getDocumentBase(); + System.err.println("Trying documentbase: " + documentBase); String url = applet.resolveUrlForLocalOrAbsolute(f, documentBase); if (urlExists(url)) { - if (debug) + if (true/* debug */) { System.err.println("Prepended document base '" + documentBase + "' to make: '" + url + "'"); @@ -2415,10 +2416,11 @@ public class JalviewLite extends Applet implements * Try relative to codebase */ URL codeBase = getCodeBase(); + System.err.println("Trying codebase: " + codeBase); url = applet.resolveUrlForLocalOrAbsolute(f, codeBase); if (urlExists(url)) { - if (debug) + if (true/* debug */) { System.err.println("Prepended codebase '" + codeBase + "' to make: '" + url + "'"); @@ -2924,13 +2926,13 @@ public class JalviewLite extends Applet implements { url = localref + url; } - if (debug) + if (true/* debug */) { System.err.println("URL: " + localref.toString()); System.err.println("URL.getFile: " + localref.getFile()); System.err.println("URL.getPath: " + localref.getPath()); System.err.println("URL.getQuery: " + localref.getQuery()); - System.err.println("returning " + url); + System.err.println("resolveUrlForLocalOrAbsolute returning " + url); } return url; } diff --git a/src/jalview/javascript/MouseOverStructureListener.java b/src/jalview/javascript/MouseOverStructureListener.java index 2ef957e..88fba02 100644 --- a/src/jalview/javascript/MouseOverStructureListener.java +++ b/src/jalview/javascript/MouseOverStructureListener.java @@ -33,6 +33,9 @@ import jalview.structure.StructureMapping; import jalview.structure.StructureMappingcommandSet; import jalview.structure.StructureSelectionManager; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; import java.util.ArrayList; import java.util.List; @@ -87,45 +90,77 @@ public class MouseOverStructureListener extends JSFunctionExec implements { for (int i = 0; i < modelSet.length; i++) { - // resolve a real filename - try - { - if (new java.net.URL(modelSet[i]).openConnection() != null) - { - continue; - } - } catch (Exception x) - { - } - ; - try - { - String db = jvlite.getDocumentBase().toString(); - db = db.substring(0, db.lastIndexOf("/")); - if (new java.net.URL(db + "/" + modelSet[i]).openConnection() != null) - { - modelSet[i] = db + "/" + modelSet[i]; - continue; - } - } catch (Exception x) - { - } - ; + modelSet[i] = resolveModelFile(modelSet[i]); + } + } + } + + /** + * Returns the first of file, file prefixed by document base, file prefixed by + * codebase which can be resolved to a valid URL. If none can, returns the + * input parameter value. + * + * @param file + */ + public String resolveModelFile(String file) + { + if (isValidUrl(file)) + { + return file; + } + + String db = jvlite.getDocumentBase().toString(); + db = db.substring(0, db.lastIndexOf("/")); + String docBaseFile = db + "/" + file; + if (isValidUrl(docBaseFile)) + { + return docBaseFile; + } + + String cb = jvlite.getCodeBase() + file; + if (isValidUrl(cb)) + { + return cb; + } + + return file; + } + + /** + * Returns true if it is possible to open an input stream at the given URL, + * else false. The input stream is closed. + * + * @param file + * @return + */ + private boolean isValidUrl(String file) + { + InputStream is = null; + try + { + is = new URL(file).openStream(); + if (is != null) + { + return true; + } + } catch (IOException x) + { + // MalformedURLException, FileNotFoundException + return false; + } finally + { + if (is != null) + { try { - if (new java.net.URL(jvlite.getCodeBase() + modelSet[i]) - .openConnection() != null) - { - modelSet[i] = jvlite.getCodeBase() + modelSet[i]; - continue; - } - } catch (Exception x) + is.close(); + } catch (IOException e) { + // ignore } - ; - } } + return false; } @Override -- 1.7.10.2