dev@jsftemplating.java.net

Re: review/suggestion

From: Ken Paulsen <Ken.Paulsen_at_Sun.COM>
Date: Thu, 07 Sep 2006 16:45:55 -0700

There's a Utility class in jsftemplating that does this:

    com.sun.jsftemplating.util.MessageUtil

See the methods:

    getInstance()
    String getMessage(String baseName, String key)
    String getMessage(String baseName, String key, Object args[])
    String getMessage(Locale locale, String baseName, String key, Object
args[])

It includes message formatting, and caching of ResourceBundles in the
same way that $resource{} does. Do these methods meet your needs? If
not what's missing? NOTE: There's also a LogUtil.java class in the same
package that includes Logging api's (which also uses these message api's
when localizing log messages).

Ken


Senthil Chidambaram wrote:
> I'm attaching the file GuiUtil.java for your review, and to make sure
> what I'm doing is correct. I've added methods to pull out the strings
> from the properties file. I need this bcz. the FacesServlet isn't
> initialized when the login.jsp is invoked, so I can't use the
> getMessage() method which was already checked. Could you pls review,
> and let me know.
>
> thx
> Senthil
> ------------------------------------------------------------------------
>
>
> /*
> * The contents of this file are subject to the terms
> * of the Common Development and Distribution License
> * (the License). You may not use this file except in
> * compliance with the License.
> *
> * You can obtain a copy of the license at
> * https://glassfish.dev.java.net/public/CDDLv1.0.html or
> * glassfish/bootstrap/legal/CDDLv1.0.txt.
> * See the License for the specific language governing
> * permissions and limitations under the License.
> *
> * When distributing Covered Code, include this CDDL
> * Header Notice in each file and include the License file
> * at glassfish/bootstrap/legal/CDDLv1.0.txt.
> * If applicable, add the following below the CDDL Header,
> * with the fields enclosed by brackets [] replaced by
> * you own identifying information:
> * "Portions Copyrighted [year] [name of copyright owner]"
> *
> * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
> */
>
> /*
> * GuiUtil.java
> *
> * Created on August 10, 2006, 9:19 PM
> *
> * To change this template, choose Tools | Template Manager
> * and open the template in the editor.
> */
>
>
> package com.sun.enterprise.tools.admingui.util;
>
> import com.sun.jsftemplating.util.Util;
> import com.sun.jsftemplating.resource.ResourceBundleManager;
>
> import javax.faces.context.FacesContext;
>
> import java.util.Locale;
> import java.util.ResourceBundle;
> import java.util.MissingResourceException;
>
> import java.text.MessageFormat;
>
> import java.lang.Exception;
> import java.lang.NullPointerException;
>
>
> /**
> *
> * @author anilam
> */
> public class GuiUtil {
>
> /** Creates a new instance of GuiUtil */
> public GuiUtil() {
> }
>
>
> //return true if the String is null or is """
> public static boolean isEmpty(String str){
> return (str == null || "".equals(str) )? true : false;
> }
>
> /*
> * returns the strings from com.sun.enterprise.tools.admingui.Strings
> * if no such key exists, return the key itself.
> */
> public static String getMessage(String key){
>
> try{
> // Get the Resource Bundle
> System.out.println("INSIDE GET MESSGE");
> ResourceBundle bundle = (ResourceBundle) FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get(I18N_RESOURCE_BUNDLE);
> System.out.println("BUNDLE = "+ bundle);
>
> if (bundle == null){
> System.out.println("bundle is null");
> Locale locale = com.sun.jsftemplating.util.Util.getLocale(FacesContext.getCurrentInstance());
> bundle = ResourceBundleManager.getInstance().getBundle(RESOURCE_NAME, locale);
> // Store it in the Request Map
> FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put(I18N_RESOURCE_BUNDLE, bundle);
> }
> String ret = bundle.getString(key);
> System.out.println("RETURN = "+ret);
> return (ret==null) ? key : ret;
> }catch (NullPointerException ex){
> return "";
> }catch (Exception ex1){
> return key;
> }
> }
>
> /*
> * returns the strings from baseName
> * if no such key exists, return the key itself.
> */
> public static String getMessage(String baseName, String key, Object[] args, Locale locale) {
> if (key == null) {
> return null;
> }
> if(locale == null) {
> locale = Locale.getDefault();
> }
> ResourceBundle bundle = getResourceBundle(baseName, locale);
>
> if(args == null) {
> return getMessageFromBundle(key, bundle);
> }
> String pattern = getMessageFromBundle(key, bundle);
> MessageFormat mf = new MessageFormat(pattern);
>
> Object[] mfArgs = new Object[args.length];
> for(int i = 0; i < args.length; i++) {
> mfArgs[i] = getMessageFromBundle(args[i].toString(), bundle);
> }
> key = mf.format(mfArgs);
> return key;
> }
>
> private static String getMessageFromBundle(String key, ResourceBundle bundle) {
> try {
> key = bundle.getString(key);
> }
> catch (MissingResourceException ex) {
> }
> return key;
> }
>
>
> private static ResourceBundle getResourceBundle(String baseName, Locale locale){
> if (baseName == null){
> System.out.println("bundle is null");
> baseName = RESOURCE_NAME;
> }
> ResourceBundle bundle = null;
> try{
> // Get the Resource Bundle
> bundle = ResourceBundle.getBundle(baseName, locale);
> }catch (MissingResourceException ex){
> try {
> bundle = ResourceBundle.getBundle(baseName);
> }
> catch (MissingResourceException ex1){
> bundle = ResourceBundle.getBundle(RESOURCE_NAME);
> }
> }
> return bundle;
> }
>
> public static final String I18N_RESOURCE_BUNDLE = "__i18n_resource_bundle";
> public static final String RESOURCE_NAME = "com.sun.enterprise.tools.admingui.resources.Strings";
> }
>