Package cookxml.common.setter

Source Code of cookxml.common.setter.ResourceBundleSetter

/*
* (c) Copyright 2004 by Heng Yuan
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* ITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package cookxml.common.setter;

import java.util.Locale;
import java.util.ResourceBundle;
import java.util.StringTokenizer;

import cookxml.common.converter.LocaleConverter;
import cookxml.core.DecodeEngine;
import cookxml.core.interfaces.Setter;
import cookxml.core.stringhook.ResourceBundleStringHook;

/**
* This setter sets the current resource bundle to be used.  It also setups the StringHook
* such that resources can be referenced within the XML document.
*
* @see cookxml.core.stringhook.ResourceBundleStringHook
* @since CookXml 1.0
* @version $Id: ResourceBundleSetter.java 240 2007-06-06 18:49:38Z coconut $
* @author Heng Yuan
*/
public class ResourceBundleSetter implements Setter
{
  public static String SEPARATOR = ";";

  public void setAttribute (String ns, String tag, String attrNS, String attr, Object obj, Object value, DecodeEngine decodeEngine)
  {
    if (value == null || !(value instanceof String))
      return;
    ResourceBundle bundle = getResourceBundle ((String)value);
    if (bundle == null)
      return;
    decodeEngine.setStringHook (new ResourceBundleStringHook (bundle));
  }

  /**
   * Get the resource bundle from the input, which consists of the resource bundle path and
   * the locale information, separated by the ";" character.
   * @param   input
   *          the resource location and locale for the resource bundle
   * @return  the ResourceBundle loaded.
   */
  public static ResourceBundle getResourceBundle (String input)
  {
    try
    {
      if (input == null || input.length () == 0)
        return null;
      StringTokenizer tokenizer = new StringTokenizer (input, SEPARATOR);
      String baseName = tokenizer.nextToken ();
      String locale = null;
      if (tokenizer.hasMoreTokens ())
        locale = tokenizer.nextToken ();
      ResourceBundle bundle;
      if (locale == null || locale.length () == 0)
        bundle = ResourceBundle.getBundle (baseName);
      else
      {
        Locale l = (Locale)LocaleConverter.getInstance ().convert (locale, null);
        if (l == null)
          bundle = ResourceBundle.getBundle (baseName);
        else
          bundle = ResourceBundle.getBundle (baseName, l);
      }
      return bundle;
    }
    catch (Exception ex)
    {
      return null;
    }
  }
}
TOP

Related Classes of cookxml.common.setter.ResourceBundleSetter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.