001: /*
002: * Portions Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.xml.internal.ws.util.localization;
027:
028: import java.text.MessageFormat;
029: import java.util.HashMap;
030: import java.util.Locale;
031: import java.util.MissingResourceException;
032: import java.util.ResourceBundle;
033:
034: /**
035: * Localizes the {@link Localizable} into a message
036: * by using a configured {@link Locale}.
037: *
038: * @author WS Development Team
039: */
040: public class Localizer {
041:
042: private final Locale _locale;
043: private final HashMap _resourceBundles;
044:
045: public Localizer() {
046: this (Locale.getDefault());
047: }
048:
049: public Localizer(Locale l) {
050: _locale = l;
051: _resourceBundles = new HashMap();
052: }
053:
054: public Locale getLocale() {
055: return _locale;
056: }
057:
058: public String localize(Localizable l) {
059: String bundlename = l.getResourceBundleName();
060:
061: try {
062: ResourceBundle bundle = (ResourceBundle) _resourceBundles
063: .get(bundlename);
064:
065: if (bundle == null) {
066: try {
067: bundle = ResourceBundle.getBundle(bundlename,
068: _locale);
069: } catch (MissingResourceException e) {
070: // work around a bug in the com.sun.enterprise.deployment.WebBundleArchivist:
071: // all files with an extension different from .class (hence all the .properties files)
072: // get copied to the top level directory instead of being in the package where they
073: // are defined
074: // so, since we can't find the bundle under its proper name, we look for it under
075: // the top-level package
076:
077: int i = bundlename.lastIndexOf('.');
078: if (i != -1) {
079: String alternateBundleName = bundlename
080: .substring(i + 1);
081: try {
082: bundle = ResourceBundle.getBundle(
083: alternateBundleName, _locale);
084: } catch (MissingResourceException e2) {
085: // give up
086: return getDefaultMessage(l);
087: }
088: }
089: }
090:
091: _resourceBundles.put(bundlename, bundle);
092: }
093:
094: if (bundle == null) {
095: return getDefaultMessage(l);
096: }
097:
098: String key = l.getKey();
099: if (key == Localizable.NOT_LOCALIZABLE) {
100: // this message is not localizable
101: return (String) l.getArguments()[0];
102: }
103:
104: if (key == null)
105: key = "undefined";
106:
107: String msg;
108: try {
109: msg = bundle.getString(key);
110: } catch (MissingResourceException e) {
111: // notice that this may throw a MissingResourceException of its own (caught below)
112: msg = bundle.getString("undefined");
113: }
114:
115: // localize all arguments to the given localizable object
116: Object[] args = l.getArguments();
117: for (int i = 0; i < args.length; ++i) {
118: if (args[i] instanceof Localizable)
119: args[i] = localize((Localizable) args[i]);
120: }
121:
122: String message = MessageFormat.format(msg, args);
123: return message;
124:
125: } catch (MissingResourceException e) {
126: return getDefaultMessage(l);
127: }
128:
129: }
130:
131: private String getDefaultMessage(Localizable l) {
132: String key = l.getKey();
133: Object[] args = l.getArguments();
134: StringBuilder sb = new StringBuilder();
135: sb.append("[failed to localize] ");
136: sb.append(key);
137: if (args != null) {
138: sb.append('(');
139: for (int i = 0; i < args.length; ++i) {
140: if (i != 0)
141: sb.append(", ");
142: sb.append(String.valueOf(args[i]));
143: }
144: sb.append(')');
145: }
146: return sb.toString();
147: }
148:
149: }
|