325N/A/*
325N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.xml.internal.ws.util.localization;
325N/A
325N/Aimport java.text.MessageFormat;
325N/Aimport java.util.HashMap;
325N/Aimport java.util.Locale;
325N/Aimport java.util.MissingResourceException;
325N/Aimport java.util.ResourceBundle;
325N/A
325N/A/**
325N/A * Localizes the {@link Localizable} into a message
325N/A * by using a configured {@link Locale}.
325N/A *
325N/A * @author WS Development Team
325N/A */
325N/Apublic class Localizer {
325N/A
325N/A private final Locale _locale;
325N/A private final HashMap _resourceBundles;
325N/A
325N/A public Localizer() {
325N/A this(Locale.getDefault());
325N/A }
325N/A
325N/A public Localizer(Locale l) {
325N/A _locale = l;
325N/A _resourceBundles = new HashMap();
325N/A }
325N/A
325N/A public Locale getLocale() {
325N/A return _locale;
325N/A }
325N/A
325N/A public String localize(Localizable l) {
325N/A String key = l.getKey();
325N/A if (key == Localizable.NOT_LOCALIZABLE) {
325N/A // this message is not localizable
325N/A return (String) l.getArguments()[0];
325N/A }
325N/A String bundlename = l.getResourceBundleName();
325N/A
325N/A try {
325N/A ResourceBundle bundle =
325N/A (ResourceBundle) _resourceBundles.get(bundlename);
325N/A
325N/A if (bundle == null) {
325N/A try {
325N/A bundle = ResourceBundle.getBundle(bundlename, _locale);
325N/A } catch (MissingResourceException e) {
325N/A // work around a bug in the com.sun.enterprise.deployment.WebBundleArchivist:
325N/A // all files with an extension different from .class (hence all the .properties files)
325N/A // get copied to the top level directory instead of being in the package where they
325N/A // are defined
325N/A // so, since we can't find the bundle under its proper name, we look for it under
325N/A // the top-level package
325N/A
325N/A int i = bundlename.lastIndexOf('.');
325N/A if (i != -1) {
325N/A String alternateBundleName =
325N/A bundlename.substring(i + 1);
325N/A try {
325N/A bundle =
325N/A ResourceBundle.getBundle(
325N/A alternateBundleName,
325N/A _locale);
325N/A } catch (MissingResourceException e2) {
325N/A // give up
325N/A return getDefaultMessage(l);
325N/A }
325N/A }
325N/A }
325N/A
325N/A _resourceBundles.put(bundlename, bundle);
325N/A }
325N/A
325N/A if (bundle == null) {
325N/A return getDefaultMessage(l);
325N/A }
325N/A
325N/A if (key == null)
325N/A key = "undefined";
325N/A
325N/A String msg;
325N/A try {
325N/A msg = bundle.getString(key);
325N/A } catch (MissingResourceException e) {
325N/A // notice that this may throw a MissingResourceException of its own (caught below)
325N/A msg = bundle.getString("undefined");
325N/A }
325N/A
325N/A // localize all arguments to the given localizable object
325N/A Object[] args = l.getArguments();
325N/A for (int i = 0; i < args.length; ++i) {
325N/A if (args[i] instanceof Localizable)
325N/A args[i] = localize((Localizable) args[i]);
325N/A }
325N/A
325N/A String message = MessageFormat.format(msg, args);
325N/A return message;
325N/A
325N/A } catch (MissingResourceException e) {
325N/A return getDefaultMessage(l);
325N/A }
325N/A
325N/A }
325N/A
325N/A private String getDefaultMessage(Localizable l) {
325N/A String key = l.getKey();
325N/A Object[] args = l.getArguments();
325N/A StringBuilder sb = new StringBuilder();
325N/A sb.append("[failed to localize] ");
325N/A sb.append(key);
325N/A if (args != null) {
325N/A sb.append('(');
325N/A for (int i = 0; i < args.length; ++i) {
325N/A if (i != 0)
325N/A sb.append(", ");
325N/A sb.append(String.valueOf(args[i]));
325N/A }
325N/A sb.append(')');
325N/A }
325N/A return sb.toString();
325N/A }
325N/A
325N/A}