01: /*
02: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
03: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
04: *
05: * This code is free software; you can redistribute it and/or modify it
06: * under the terms of the GNU General Public License version 2 only, as
07: * published by the Free Software Foundation. Sun designates this
08: * particular file as subject to the "Classpath" exception as provided
09: * by Sun in the LICENSE file that accompanied this code.
10: *
11: * This code is distributed in the hope that it will be useful, but WITHOUT
12: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14: * version 2 for more details (a copy is included in the LICENSE file that
15: * accompanied this code).
16: *
17: * You should have received a copy of the GNU General Public License version
18: * 2 along with this work; if not, write to the Free Software Foundation,
19: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20: *
21: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22: * CA 95054 USA or visit www.sun.com if you need additional information or
23: * have any questions.
24: */
25:
26: package com.sun.xml.internal.rngom.util;
27:
28: import java.text.MessageFormat;
29: import java.util.MissingResourceException;
30: import java.util.ResourceBundle;
31:
32: /**
33: * Localizes messages.
34: */
35: public class Localizer {
36: private final Class cls;
37: private ResourceBundle bundle;
38: /**
39: * If non-null, any resources that weren't found in this localizer
40: * will be delegated to the parent.
41: */
42: private final Localizer parent;
43:
44: public Localizer(Class cls) {
45: this (null, cls);
46: }
47:
48: public Localizer(Localizer parent, Class cls) {
49: this .parent = parent;
50: this .cls = cls;
51: }
52:
53: private String getString(String key) {
54: try {
55: return getBundle().getString(key);
56: } catch (MissingResourceException e) {
57: // delegation
58: if (parent != null)
59: return parent.getString(key);
60: else
61: throw e;
62: }
63: }
64:
65: public String message(String key) {
66: return MessageFormat.format(getString(key), new Object[] {});
67: }
68:
69: public String message(String key, Object arg) {
70: return MessageFormat.format(getString(key),
71: new Object[] { arg });
72: }
73:
74: public String message(String key, Object arg1, Object arg2) {
75: return MessageFormat.format(getString(key), new Object[] {
76: arg1, arg2 });
77: }
78:
79: public String message(String key, Object[] args) {
80: return MessageFormat.format(getString(key), args);
81: }
82:
83: private ResourceBundle getBundle() {
84: if (bundle == null) {
85: String s = cls.getName();
86: int i = s.lastIndexOf('.');
87: if (i > 0)
88: s = s.substring(0, i + 1);
89: else
90: s = "";
91: bundle = ResourceBundle.getBundle(s + "Messages");
92: }
93: return bundle;
94: }
95: }
|