Пример #1
0
  /**
   * Fetches a resource from the local repository.
   *
   * @param path the path to the resource (ie. "config/miso.properties"). This should not begin with
   *     a slash.
   * @exception IOException thrown if a problem occurs locating or reading the resource.
   */
  public InputStream getResource(String path) throws IOException {
    String localePath = getLocalePath(path);
    InputStream in;

    // first look for this resource in our default resource bundle
    for (ResourceBundle bundle : _default) {
      // Try a localized version first.
      if (localePath != null) {
        in = bundle.getResource(localePath);
        if (in != null) {
          return in;
        }
      }
      // If that didn't work, try generic.
      in = bundle.getResource(path);
      if (in != null) {
        return in;
      }
    }

    // fallback next to an unpacked resource file
    File file = getResourceFile(path);
    if (file != null && file.exists()) {
      return new FileInputStream(file);
    }

    // if we still didn't find anything, try the classloader; first try a locale-specific file
    if (localePath != null) {
      in = getInputStreamFromClasspath(PathUtil.appendPath(_rootPath, localePath));
      if (in != null) {
        return in;
      }
    }

    // if we didn't find that, try locale-neutral
    in = getInputStreamFromClasspath(PathUtil.appendPath(_rootPath, path));
    if (in != null) {
      return in;
    }

    // if we still haven't found it, we throw an exception
    throw new FileNotFoundException("Unable to locate resource [path=" + path + "]");
  }
Пример #2
0
  /**
   * Fetches and decodes the specified resource into a {@link BufferedImage}.
   *
   * @exception FileNotFoundException thrown if the resource could not be located in any of the
   *     bundles in the specified set, or if the specified set does not exist.
   * @exception IOException thrown if a problem occurs locating or reading the resource.
   */
  public BufferedImage getImageResource(String path) throws IOException {
    String localePath = getLocalePath(path);

    // first look for this resource in our default resource bundle
    for (ResourceBundle bundle : _default) {
      // try a localized version first
      BufferedImage image;
      if (localePath != null) {
        image = bundle.getImageResource(localePath, false);
        if (image != null) {
          return image;
        }
      }
      // if we didn't find that, try generic
      image = bundle.getImageResource(path, false);
      if (image != null) {
        return image;
      }
    }

    // fallback next to an unpacked resource file
    File file = getResourceFile(path);
    if (file != null && file.exists()) {
      return loadImage(file, path.endsWith(FastImageIO.FILE_SUFFIX));
    }

    // if we still didn't find anything, try the classloader
    InputStream in;
    if (localePath != null) {
      in = getInputStreamFromClasspath(PathUtil.appendPath(_rootPath, localePath));
      if (in != null) {
        return loadImage(in);
      }
    }
    in = getInputStreamFromClasspath(PathUtil.appendPath(_rootPath, path));
    if (in != null) {
      return loadImage(in);
    }

    // if we still haven't found it, we throw an exception
    throw new FileNotFoundException("Unable to locate image resource [path=" + path + "]");
  }