* 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 + "'");
* 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 + "'");
{
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;
}
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;
{
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