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: package com.sun.xml.ws.util.localization;
038:
039: import java.text.MessageFormat;
040: import java.util.HashMap;
041: import java.util.Locale;
042: import java.util.MissingResourceException;
043: import java.util.ResourceBundle;
044:
045: /**
046: * Localizes the {@link Localizable} into a message
047: * by using a configured {@link Locale}.
048: *
049: * @author WS Development Team
050: */
051: public class Localizer {
052:
053: private final Locale _locale;
054: private final HashMap _resourceBundles;
055:
056: public Localizer() {
057: this (Locale.getDefault());
058: }
059:
060: public Localizer(Locale l) {
061: _locale = l;
062: _resourceBundles = new HashMap();
063: }
064:
065: public Locale getLocale() {
066: return _locale;
067: }
068:
069: public String localize(Localizable l) {
070: String key = l.getKey();
071: if (key == Localizable.NOT_LOCALIZABLE) {
072: // this message is not localizable
073: return (String) l.getArguments()[0];
074: }
075: String bundlename = l.getResourceBundleName();
076:
077: try {
078: ResourceBundle bundle = (ResourceBundle) _resourceBundles
079: .get(bundlename);
080:
081: if (bundle == null) {
082: try {
083: bundle = ResourceBundle.getBundle(bundlename,
084: _locale);
085: } catch (MissingResourceException e) {
086: // work around a bug in the com.sun.enterprise.deployment.WebBundleArchivist:
087: // all files with an extension different from .class (hence all the .properties files)
088: // get copied to the top level directory instead of being in the package where they
089: // are defined
090: // so, since we can't find the bundle under its proper name, we look for it under
091: // the top-level package
092:
093: int i = bundlename.lastIndexOf('.');
094: if (i != -1) {
095: String alternateBundleName = bundlename
096: .substring(i + 1);
097: try {
098: bundle = ResourceBundle.getBundle(
099: alternateBundleName, _locale);
100: } catch (MissingResourceException e2) {
101: // give up
102: return getDefaultMessage(l);
103: }
104: }
105: }
106:
107: _resourceBundles.put(bundlename, bundle);
108: }
109:
110: if (bundle == null) {
111: return getDefaultMessage(l);
112: }
113:
114: if (key == null)
115: key = "undefined";
116:
117: String msg;
118: try {
119: msg = bundle.getString(key);
120: } catch (MissingResourceException e) {
121: // notice that this may throw a MissingResourceException of its own (caught below)
122: msg = bundle.getString("undefined");
123: }
124:
125: // localize all arguments to the given localizable object
126: Object[] args = l.getArguments();
127: for (int i = 0; i < args.length; ++i) {
128: if (args[i] instanceof Localizable)
129: args[i] = localize((Localizable) args[i]);
130: }
131:
132: String message = MessageFormat.format(msg, args);
133: return message;
134:
135: } catch (MissingResourceException e) {
136: return getDefaultMessage(l);
137: }
138:
139: }
140:
141: private String getDefaultMessage(Localizable l) {
142: String key = l.getKey();
143: Object[] args = l.getArguments();
144: StringBuilder sb = new StringBuilder();
145: sb.append("[failed to localize] ");
146: sb.append(key);
147: if (args != null) {
148: sb.append('(');
149: for (int i = 0; i < args.length; ++i) {
150: if (i != 0)
151: sb.append(", ");
152: sb.append(String.valueOf(args[i]));
153: }
154: sb.append(')');
155: }
156: return sb.toString();
157: }
158:
159: }
|