001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: /*
038: * I18nUtilities.java
039: *
040: */
041:
042: package com.sun.jbi.jsf.util;
043:
044: import java.util.Locale;
045: import java.util.ResourceBundle;
046: import javax.el.ELContext;
047: import javax.el.ExpressionFactory;
048: import javax.el.ValueExpression;
049: import javax.faces.application.Application;
050: import javax.faces.component.UIComponent;
051: import javax.faces.context.FacesContext;
052: import java.util.logging.Logger;
053:
054: /**
055: *
056: * provides internationalization utilties
057: *
058: **/
059:
060: public final class I18nUtilities {
061: /*
062: *
063: */
064: public static final String ADMIN_CONSOLE_BUNDLE = "com.sun.enterprise.tools.admingui.resources.Strings";
065: public static final String JBI_ADMIN_GUI_BUNDLE = "com.sun.jbi.jsf.resources.Bundle";
066:
067: /**
068: * Controls printing of diagnostic messages to the log
069: */
070: private static Logger sLog = JBILogger.getInstance();
071:
072: // Will return the text for the given resource string and resource bundle file.
073: // If the given resource string is blank, then a blank string will be returned.
074: // If the given resource string does not exist, the the resource string will be returned
075: public static String getResourceString(String aResourceFile,
076: String aResourceString, String aDefaultString) {
077: Locale loc = FacesContext.getCurrentInstance().getViewRoot()
078: .getLocale();
079: String result = null;
080: String resourceString = "";
081: if (aResourceString == null) {
082: resourceString = aDefaultString.trim();
083: } else {
084: resourceString = aResourceString.trim();
085: }
086: if (resourceString.length() > 0) {
087: try {
088: ResourceBundle resource = ResourceBundle.getBundle(
089: aResourceFile, loc);
090: result = resource.getString(resourceString);
091: } catch (java.util.MissingResourceException e) {
092: result = resourceString;
093: }
094: }
095:
096: return result;
097: }
098:
099: /**
100: * provides an internationalized resource String
101: * @param aResourceKey
102: * @param aDefaultValue
103: * @return a resource value (or default value if key not found)
104: */
105: public static String getResourceString(String aResourceKey,
106: String aDefaultValue) {
107: String result = null;
108: String resourceFile = JBI_ADMIN_GUI_BUNDLE;
109: result = getResourceString(resourceFile, aResourceKey,
110: aDefaultValue);
111: sLog.fine("I18Utilities.getResourceString(" + aResourceKey
112: + ", " + aDefaultValue + "), " + resourceFile
113: + " result=" + result);
114:
115: // if the key is not found in the JBI console bundle,
116: // then try the admin-gui strings
117: if ((aResourceKey.equals(result))
118: || (aDefaultValue.equals(result))) {
119: resourceFile = ADMIN_CONSOLE_BUNDLE;
120: result = getResourceString(resourceFile, aResourceKey,
121: aDefaultValue);
122: sLog.fine("I18Utilities.getResourceString(" + aResourceKey
123: + ", " + aDefaultValue + "), " + resourceFile
124: + " result=" + result);
125: }
126: return result;
127: }
128:
129: /* This Method accepts a string and searched the resource Bundle file
130: * to get the matching Key for the value passed
131: *@param aResourceString String value passed whose key is needed
132: * from Resource Bundle file
133: *@return matching key for the passed resourceString value
134: */
135:
136: public static String getResourceString(String aResourceString) {
137: String resourceFile = ADMIN_CONSOLE_BUNDLE;
138: String str = getResourceString(aResourceString, "");
139: return str;
140: }
141:
142: public static String getStringPropertyUsingExpression(
143: String anExpression) {
144: String result = null;
145: FacesContext fCtx = FacesContext.getCurrentInstance();
146: ELContext elCtx = fCtx.getELContext();
147: ExpressionFactory ef = fCtx.getApplication()
148: .getExpressionFactory();
149: ValueExpression ve = ef.createValueExpression(elCtx,
150: anExpression, String.class);
151: result = (String) ve.getValue(elCtx);
152: return result;
153: }
154:
155: public static void setStringPropertyUsingExpression(
156: String aStringValue, String anExpression) {
157: FacesContext fCtx = FacesContext.getCurrentInstance();
158: ELContext elCtx = fCtx.getELContext();
159: ExpressionFactory ef = fCtx.getApplication()
160: .getExpressionFactory();
161: ValueExpression ve = ef.createValueExpression(elCtx,
162: anExpression, String.class);
163: ve.setValue(elCtx, aStringValue);
164: }
165:
166: }
|