+ }
+
+ /**
+ * try to discover how to access the given file as a valid datasource that
+ * will be identified as the given type.
+ *
+ * @param file
+ * @param format
+ * @return protocol that yields the data parsable as the given type
+ */
+ public static String resolveProtocol(String file, String format)
+ {
+ return resolveProtocol(file, format, false);
+ }
+
+ public static String resolveProtocol(String file, String format,
+ boolean debug)
+ {
+ // TODO: test thoroughly!
+ String protocol = null;
+ if (debug)
+ {
+ System.out.println("resolving datasource started with:\n>>file\n"
+ + file + ">>endfile");
+ }
+
+ // This might throw a security exception in certain browsers
+ // Netscape Communicator for instance.
+ try
+ {
+ boolean rtn = false;
+ InputStream is = System.getSecurityManager().getClass()
+ .getResourceAsStream("/" + file);
+ if (is != null)
+ {
+ rtn = true;
+ is.close();
+ }
+ if (debug)
+ {
+ System.err.println("Resource '" + file + "' was "
+ + (rtn ? "" : "not") + " located by classloader.");
+ }
+ ;
+ if (rtn)
+ {
+ protocol = AppletFormatAdapter.CLASSLOADER;
+ }
+
+ } catch (Exception ex)
+ {
+ System.err
+ .println("Exception checking resources: " + file + " " + ex);
+ }
+
+ if (file.indexOf("://") > -1)
+ {
+ protocol = AppletFormatAdapter.URL;
+ }
+ else
+ {
+ // skipping codebase prepend check.
+ protocol = AppletFormatAdapter.FILE;
+ }
+ FileParse fp = null;
+ try
+ {
+ if (debug)
+ {
+ System.out.println("Trying to get contents of resource as "
+ + protocol + ":");
+ }
+ fp = new FileParse(file, protocol);
+ if (!fp.isValid())
+ {
+ fp = null;
+ }
+ else
+ {
+ if (debug)
+ {
+ System.out.println("Successful.");
+ }
+ }
+ } catch (Exception e)
+ {
+ if (debug)
+ {
+ System.err.println("Exception when accessing content: " + e);
+ }
+ fp = null;
+ }
+ if (fp == null)
+ {
+ if (debug)
+ {
+ System.out.println("Accessing as paste.");
+ }
+ protocol = AppletFormatAdapter.PASTE;
+ fp = null;
+ try
+ {
+ fp = new FileParse(file, protocol);
+ if (!fp.isValid())
+ {
+ fp = null;
+ }
+ } catch (Exception e)
+ {
+ System.err.println("Failed to access content as paste!");
+ e.printStackTrace();
+ fp = null;
+ }
+ }
+ if (fp == null)
+ {
+ return null;
+ }
+ if (format == null || format.length() == 0)
+ {
+ return protocol;
+ }
+ else
+ {
+ try
+ {
+ String idformat = new jalview.io.IdentifyFile().Identify(file,
+ protocol);
+ if (idformat == null)
+ {
+ if (debug)
+ {
+ System.out.println("Format not identified. Inaccessible file.");
+ }
+ return null;
+ }
+ if (debug)
+ {
+ System.out.println("Format identified as " + idformat
+ + "and expected as " + format);
+ }
+ if (idformat.equals(format))
+ {
+ if (debug)
+ {
+ System.out.println("Protocol identified as " + protocol);
+ }
+ return protocol;
+ }
+ else
+ {
+ if (debug)
+ {
+ System.out
+ .println("File deemed not accessible via " + protocol);
+ }
+ fp.close();
+ return null;
+ }
+ } catch (Exception e)
+ {
+ if (debug)
+ {
+ System.err.println("File deemed not accessible via " + protocol);
+ e.printStackTrace();
+ }
+ ;
+
+ }
+ }
+ return null;