Resource utils : ResourceBundle « I18N « Java






Resource utils

        
/**
 *Copyright [2008] [dennis zhuang]
 *Licensed under the Apache License, Version 2.0 (the "License");
 *you may not use this file except in compliance with the License. 
 *You may obtain a copy of the License at 
 *             http://www.apache.org/licenses/LICENSE-2.0 
 *Unless required by applicable law or agreed to in writing, 
 *software distributed under the License is distributed on an "AS IS" BASIS, 
 *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
 *either express or implied. See the License for the specific language governing permissions and limitations under the License
 */
//package com.google.code.yanf4j.util;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.util.Properties;

/**
 * Resource utils
 * 
 * @author dennis
 * 
 */
public class ResourcesUtils extends Object {

  /** */
  /**
   * Returns the URL of the resource on the classpath
   * 
   * @param resource
   *            The resource to find
   * @throws IOException
   *             If the resource cannot be found or read
   * @return The resource
   */
  public static URL getResourceURL(String resource) throws IOException {
    URL url = null;
    ClassLoader loader = ResourcesUtils.class.getClassLoader();
    if (loader != null) {
      url = loader.getResource(resource);
    }
    if (url == null) {
      url = ClassLoader.getSystemResource(resource);
    }
    if (url == null) {
      throw new IOException("Could not find resource " + resource);
    }
    return url;
  }

  /** */
  /**
   * Returns the URL of the resource on the classpath
   * 
   * @param loader
   *            The classloader used to load the resource
   * @param resource
   *            The resource to find
   * @throws IOException
   *             If the resource cannot be found or read
   * @return The resource
   */
  public static URL getResourceURL(ClassLoader loader, String resource)
      throws IOException {
    URL url = null;
    if (loader != null) {
      url = loader.getResource(resource);
    }
    if (url == null) {
      url = ClassLoader.getSystemResource(resource);
    }
    if (url == null) {
      throw new IOException("Could not find resource " + resource);
    }
    return url;
  }

  /** */
  /**
   * Returns a resource on the classpath as a Stream object
   * 
   * @param resource
   *            The resource to find
   * @throws IOException
   *             If the resource cannot be found or read
   * @return The resource
   */
  public static InputStream getResourceAsStream(String resource)
      throws IOException {
    InputStream in = null;
    ClassLoader loader = ResourcesUtils.class.getClassLoader();
    if (loader != null) {
      in = loader.getResourceAsStream(resource);
    }
    if (in == null) {
      in = ClassLoader.getSystemResourceAsStream(resource);
    }
    if (in == null) {
      throw new IOException("Could not find resource " + resource);
    }
    return in;
  }

  /** */
  /**
   * Returns a resource on the classpath as a Stream object
   * 
   * @param loader
   *            The classloader used to load the resource
   * @param resource
   *            The resource to find
   * @throws IOException
   *             If the resource cannot be found or read
   * @return The resource
   */
  public static InputStream getResourceAsStream(ClassLoader loader,
      String resource) throws IOException {
    InputStream in = null;
    if (loader != null) {
      in = loader.getResourceAsStream(resource);
    }
    if (in == null) {
      in = ClassLoader.getSystemResourceAsStream(resource);
    }
    if (in == null) {
      throw new IOException("Could not find resource " + resource);
    }
    return in;
  }

  /** */
  /**
   * Returns a resource on the classpath as a Properties object
   * 
   * @param resource
   *            The resource to find
   * @throws IOException
   *             If the resource cannot be found or read
   * @return The resource
   */
  public static Properties getResourceAsProperties(String resource)
      throws IOException {
    Properties props = new Properties();
    InputStream in = null;
    String propfile = resource;
    in = getResourceAsStream(propfile);
    props.load(in);
    in.close();
    return props;
  }

  /** */
  /**
   * Returns a resource on the classpath as a Properties object
   * 
   * @param loader
   *            The classloader used to load the resource
   * @param resource
   *            The resource to find
   * @throws IOException
   *             If the resource cannot be found or read
   * @return The resource
   */
  public static Properties getResourceAsProperties(ClassLoader loader,
      String resource) throws IOException {
    Properties props = new Properties();
    InputStream in = null;
    String propfile = resource;
    in = getResourceAsStream(loader, propfile);
    props.load(in);
    in.close();
    return props;
  }

  /** */
  /**
   * Returns a resource on the classpath as a Reader object
   * 
   * @param resource
   *            The resource to find
   * @throws IOException
   *             If the resource cannot be found or read
   * @return The resource
   */
  public static InputStreamReader getResourceAsReader(String resource)
      throws IOException {
    return new InputStreamReader(getResourceAsStream(resource));
  }

  /** */
  /**
   * Returns a resource on the classpath as a Reader object
   * 
   * @param loader
   *            The classloader used to load the resource
   * @param resource
   *            The resource to find
   * @throws IOException
   *             If the resource cannot be found or read
   * @return The resource
   */
  public static Reader getResourceAsReader(ClassLoader loader, String resource)
      throws IOException {
    return new InputStreamReader(getResourceAsStream(loader, resource));
  }

  /** */
  /**
   * Returns a resource on the classpath as a File object
   * 
   * @param resource
   *            The resource to find
   * @throws IOException
   *             If the resource cannot be found or read
   * @return The resource
   */
  public static File getResourceAsFile(String resource) throws IOException {
    return new File(getResourceURL(resource).getFile());
  }

  /** */
  /**
   * Returns a resource on the classpath as a File object
   * 
   * @param loader
   *            The classloader used to load the resource
   * @param resource
   *            The resource to find
   * @throws IOException
   *             If the resource cannot be found or read
   * @return The resource
   */
  public static File getResourceAsFile(ClassLoader loader, String resource)
      throws IOException {
    return new File(getResourceURL(loader, resource).getFile());
  }

}

   
    
    
    
    
    
    
    
  








Related examples in the same category

1.Java I18N: IntroductionJava I18N: Introduction
2.Load resource with ResourceBundle.getBundle
3.Load a Resource Bundle
4.Localizing Messages with a property-file type resource bundle
5.Matching Line Boundaries in a Regular Expression
6.Convert ResourceBundle to Properties
7.Get resource bundle for a certain localeGet resource bundle for a certain locale
8.Convert ResourceBundle to Map
9.Use ResourceBundle for i18n
10.Load resources based upon client environment at startup
11.Load resources via a resources file
12.Java file based resource bundle
13.Resource Bundle by Locale
14.Resource Bundle in Java code
15.List Resource Bundle Creator
16.Simple Resource Bundle based on Java code
17.Create one button, internationalizedly
18.Java Internationalization: load string from properties Java Internationalization: load string from properties
19.Isolating Locale-Specific Data with Resource Bundles Isolating Locale-Specific Data with Resource Bundles
20.Property To List Resource Bundle
21.Which Bundle Comes First
22.Localized JOptionPane
23.Big Demo for I18N
24.Popup in FrenchPopup in French
25.ResourceBundle with ParametersResourceBundle with Parameters
26.ResourceBundle with parameter positionResourceBundle with parameter position
27.Load a given resource.
28.Helper class for constructing messages from bundles
29.Methods for receving messages from resource bundles
30.Convert Class To Resource Path
31.Transform the class-relative resource name into a global name by appending it to the classes package name.
32.ResourceBundle: avoid a performance penalty by superfluous resource (and classes loaded by Class.forName) lookups on web server in applets.
33.Read ResourceBundles with custom encodings
34.Method to convert a ResourceBundle to a Map object.
35.Method to convert a ResourceBundle to a Properties object.