コード例 #1
0
ファイル: FILE.java プロジェクト: cowsandmilk/MoleculeViewer
  /**
   * Return a FILE object for reading the specified resource.
   *
   * <p>If the resource looks like it is a URL name (begins with something like 'http:' or 'ftp:')
   * then an attempt will be made to open it as a URL. Otherwise the resource will be opened as a
   * file.
   */
  public static FILE open(String resource) {
    FILE input = null;

    if (debug) {
      System.out.println("FILE.open tryfiles=" + tryFiles);
      System.out.println("FILE.open resource=" + resource);
    }

    if (input == null
        && (resource.endsWith(".properties")
            || resource.endsWith(".gif")
            || resource.endsWith(".jpg")
            || resource.endsWith(".jpeg"))) {
      // instantiate a file so we can use
      // getResourceAsStream()
      // one last chance it might be in a jar file
      InputStream inputStream = null;

      if (debug) {
        System.out.println("attempt to open as absolute resource");
      }
      inputStream = openResource("/" + resource);

      if (inputStream == null) {
        if (debug) {
          System.out.println("attempt to open as relative resource");
        }
        inputStream = openResource(resource);
      }

      if (inputStream != null) {
        if (debug) {
          System.out.println("opened as resource");
        }
        input = new FILE(inputStream);
      }
    }

    if (input == null && documentBase != null) {
      if (debug) {
        System.out.println("documentBase " + documentBase);
      }

      try {
        URL url = new URL(documentBase, resource);
        if (debug) {
          System.out.println("attempting to open url " + url);
        }
        input = open(url);
        if (debug && input != null) {
          System.out.println("url successfully opened");
        }
      } catch (Exception e) {
        if (debug) {
          System.out.println("**** URL exception " + e);
        }
      }
    }

    if (input == null && codeBase != null) {
      if (debug) {
        System.out.println("codeBase " + codeBase);
      }

      try {
        URL url = new URL(codeBase, resource);
        if (debug) {
          System.out.println("attempting to open url " + url);
        }
        input = open(url);
        if (debug && input != null) {
          System.out.println("url successfully opened");
        }
      } catch (Exception e) {
        if (debug) {
          System.out.println("**** URL exception " + e);
        }
      }
    }

    if (input == null && stringIsURL(resource)) {
      try {
        if (debug) {
          System.out.println("attempting to open as absolute url");
        }
        input = openURL(resource);
        if (debug && input != null) {
          System.out.println("successfully opened as absolute url");
        }
      } catch (Exception e) {
        input = null;
        if (debug) {
          System.out.println("**** absolute url exception " + e);
        }
      }
    }

    if (input == null && tryFiles) {
      try {
        if (debug) {
          System.out.println("attempting to open as file");
        }
        input = openFile(resource);
        if (debug && input != null) {
          System.out.println("successfully opened as file");
        }
      } catch (Exception e) {
        input = null;
        if (debug) {
          System.out.println("**** open file exception " + e);
        }
      }
    }

    if (input != null && debug) {
      System.out.println("opened as resource");
    }

    if (resource.endsWith(".gz")) {
      try {
        GZIPInputStream gis = new GZIPInputStream(input.inputStream);

        input.inputStream = gis;
      } catch (Exception e) {
        input = null;
        if (debug) {
          System.out.println("failed to open gzip'ed inputStream");
          System.out.println(e);
        }
      }
    }

    // finally try and see if there is a resource with
    // .gz extension that corresponds to the resource
    // we were asked to open
    // i.e. we were asked to open 1abc.pdb, but it
    // didn't exist. Does 1abc.pdb.gz exist instead?
    if (input == null && !resource.endsWith(".gz")) {
      input = open(resource + ".gz");

      if (input != null) {
        return input;
      }
    }

    return input;
  }