Esempio n. 1
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 rset, String path) throws IOException {
    // grab the resource bundles in the specified resource set
    ResourceBundle[] bundles = getResourceSet(rset);
    if (bundles == null) {
      throw new FileNotFoundException(
          "Unable to locate image resource [set=" + rset + ", path=" + path + "]");
    }

    String localePath = getLocalePath(path);
    // look for the resource in any of the bundles
    for (ResourceBundle bundle : bundles) {
      BufferedImage image;
      // try a localized version first
      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;
      }
    }

    throw new FileNotFoundException(
        "Unable to locate image resource [set=" + rset + ", path=" + path + "]");
  }
Esempio n. 2
0
  /**
   * Returns an input stream from which the requested resource can be loaded. <em>Note:</em> this
   * performs a linear search of all of the bundles in the set and returns the first resource found
   * with the specified path, thus it is not extremely efficient and will behave unexpectedly if you
   * use the same paths in different resource bundles.
   *
   * @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 InputStream getResource(String rset, String path) throws IOException {
    // grab the resource bundles in the specified resource set
    ResourceBundle[] bundles = getResourceSet(rset);
    if (bundles == null) {
      throw new FileNotFoundException(
          "Unable to locate resource [set=" + rset + ", path=" + path + "]");
    }

    String localePath = getLocalePath(path);
    // look for the resource in any of the bundles
    for (ResourceBundle bundle : bundles) {
      InputStream in;
      // Try a localized version first.
      if (localePath != null) {
        in = bundle.getResource(localePath);
        if (in != null) {
          return in;
        }
      }
      // If we didn't find that, try a generic.
      in = bundle.getResource(path);
      if (in != null) {
        return in;
      }
    }

    throw new FileNotFoundException(
        "Unable to locate resource [set=" + rset + ", path=" + path + "]");
  }
Esempio n. 3
0
  public static ResourceBundle loadLocale(String localeString) {

    JarFile jarFile;
    try {
      jarFile = new JarFile(MyPetPlugin.getPlugin().getFile());
    } catch (IOException ignored) {
      return null;
    }

    ResourceBundle newLocale = null;
    try {
      JarEntry jarEntry = jarFile.getJarEntry("locale/MyPet_" + localeString + ".properties");
      if (jarEntry != null) {
        java.util.ResourceBundle defaultBundle =
            new PropertyResourceBundle(
                new InputStreamReader(jarFile.getInputStream(jarEntry), "UTF-8"));
        newLocale = new ResourceBundle(defaultBundle);
      } else {
        throw new IOException();
      }
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
      DebugLogger.printThrowable(e);
    } catch (IOException ignored) {
    }

    File localeFile =
        new File(
            MyPetPlugin.getPlugin().getDataFolder()
                + File.separator
                + "locale"
                + File.separator
                + "MyPet_"
                + localeString
                + ".properties");
    if (localeFile.exists()) {
      if (newLocale == null) {
        newLocale = new ResourceBundle();
      }
      try {
        java.util.ResourceBundle optionalBundle =
            new PropertyResourceBundle(
                new InputStreamReader(new FileInputStream(localeFile), "UTF-8"));
        newLocale.addExtensionBundle(optionalBundle);
      } catch (IOException e) {
        e.printStackTrace();
        DebugLogger.printThrowable(e);
      }
    }
    return newLocale;
  }
  public void testPropertiesFileNotAssociatedWhileLanguageCodeNotRecognized() {
    final PsiFile file = myFixture.addFileToProject("some_property_file.properties", "");
    final PsiFile file2 = myFixture.addFileToProject("some_property_fil.properties", "");
    final PropertiesFile propertiesFile = PropertiesImplUtil.getPropertiesFile(file);
    assertNotNull(propertiesFile);
    final ResourceBundle resourceBundle = propertiesFile.getResourceBundle();
    assertSize(1, resourceBundle.getPropertiesFiles());

    final PropertiesFile propertiesFile2 = PropertiesImplUtil.getPropertiesFile(file2);
    assertNotNull(propertiesFile2);
    final ResourceBundle resourceBundle2 = propertiesFile.getResourceBundle();
    assertSize(1, resourceBundle2.getPropertiesFiles());

    assertEquals(PropertiesUtil.DEFAULT_LOCALE, propertiesFile.getLocale());
  }
Esempio n. 5
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 + "]");
  }
Esempio n. 6
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 + "]");
  }
 ResourceBundleKeyRenameValidator(
     Project project,
     ResourceBundleEditor editor,
     ResourceBundle bundle,
     String oldPropertyName) {
   myEditor = editor;
   myOldPropertyName = oldPropertyName;
   for (PropertiesFile file : bundle.getPropertiesFiles(project)) {
     for (IProperty property : file.getProperties()) {
       myExistingProperties.add(property.getKey());
     }
   }
   myExistingProperties.remove(oldPropertyName);
 }
  public void testCombineToCustomResourceBundleAndDissociateAfter() {
    final PropertiesFile file =
        PropertiesImplUtil.getPropertiesFile(
            myFixture.addFileToProject("resources-dev/my-app-dev.properties", ""));
    final PropertiesFile file2 =
        PropertiesImplUtil.getPropertiesFile(
            myFixture.addFileToProject("resources-prod/my-app-prod.properties", ""));
    assertNotNull(file);
    assertNotNull(file2);
    assertOneElement(file.getResourceBundle().getPropertiesFiles());
    assertOneElement(file2.getResourceBundle().getPropertiesFiles());

    final ResourceBundleManager resourceBundleBaseNameManager =
        ResourceBundleManager.getInstance(getProject());
    final String newBaseName = "my-app";
    resourceBundleBaseNameManager.combineToResourceBundle(list(file, file2), newBaseName);
    final ResourceBundle resourceBundle = file.getResourceBundle();
    assertEquals(2, resourceBundle.getPropertiesFiles().size());
    assertEquals(newBaseName, resourceBundle.getBaseName());

    resourceBundleBaseNameManager.dissociateResourceBundle(resourceBundle);
    assertOneElement(file.getResourceBundle().getPropertiesFiles());
    assertOneElement(file2.getResourceBundle().getPropertiesFiles());
  }
  public void testPropertiesFilesDefaultCombiningToResourceBundle() {
    final PsiFile file = myFixture.addFileToProject("prop_core_en.properties", "");
    final PsiFile file2 = myFixture.addFileToProject("prop_core_fi.properties", "");
    final PropertiesFile propertiesFile = PropertiesImplUtil.getPropertiesFile(file);
    final PropertiesFile propertiesFile2 = PropertiesImplUtil.getPropertiesFile(file2);
    assertNotNull(propertiesFile);
    assertNotNull(propertiesFile2);
    final ResourceBundle bundle = propertiesFile.getResourceBundle();
    final ResourceBundle bundle2 = propertiesFile2.getResourceBundle();
    assertTrue(bundle.equals(bundle2));
    assertSize(2, bundle.getPropertiesFiles());
    assertTrue(bundle.getDefaultPropertiesFile().equals(bundle2.getDefaultPropertiesFile()));
    assertEquals("prop_core", bundle.getBaseName());

    assertNotSame(
        propertiesFile.getLocale().getLanguage(), propertiesFile.getLocale().getDisplayLanguage());
    assertNotSame(
        propertiesFile2.getLocale().getLanguage(),
        propertiesFile2.getLocale().getDisplayLanguage());
  }
  public void testLanguageCodeNotRecognized() {
    final PsiFile file = myFixture.addFileToProject("p.properties", "");
    final PsiFile file2 = myFixture.addFileToProject("p_asd.properties", "");

    final PropertiesFile propertiesFile = PropertiesImplUtil.getPropertiesFile(file);
    final PropertiesFile propertiesFile2 = PropertiesImplUtil.getPropertiesFile(file2);
    assertNotNull(propertiesFile);
    assertNotNull(propertiesFile2);
    final ResourceBundle bundle = propertiesFile.getResourceBundle();
    final ResourceBundle bundle2 = propertiesFile2.getResourceBundle();
    assertSize(1, bundle.getPropertiesFiles());
    assertSize(1, bundle2.getPropertiesFiles());
    assertEquals("p", bundle.getBaseName());
    assertEquals("p_asd", bundle2.getBaseName());

    final ResourceBundleManager manager = ResourceBundleManager.getInstance(getProject());
    final ArrayList<PropertiesFile> rawBundle =
        ContainerUtil.newArrayList(propertiesFile, propertiesFile2);
    final String suggestedBaseName = PropertiesUtil.getDefaultBaseName(rawBundle);
    assertEquals("p", suggestedBaseName);
    manager.combineToResourceBundle(rawBundle, suggestedBaseName);

    assertEquals("asd", propertiesFile2.getLocale().getLanguage());
  }