View Javadoc
1   /*
2    * Copyright (c) 1996, 1997, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  package sun.applet;
27  
28  import java.util.ResourceBundle;
29  import java.util.MissingResourceException;
30  import java.text.MessageFormat;
31  
32  /**
33   * An hanlder of localized messages.
34   *
35   * @author      Koji Uno
36   */
37  class AppletMessageHandler {
38      private static ResourceBundle rb;
39      private String baseKey = null;
40  
41      static {
42          try {
43              rb = ResourceBundle.getBundle
44                  ("sun.applet.resources.MsgAppletViewer");
45          } catch (MissingResourceException e) {
46              System.out.println(e.getMessage());
47              System.exit(1);
48          }
49      }
50  
51      AppletMessageHandler(String baseKey) {
52          this.baseKey = baseKey;
53      }
54  
55      String getMessage(String key) {
56          return (String)rb.getString(getQualifiedKey(key));
57      }
58  
59      String getMessage(String key, Object arg){
60          String basemsgfmt = (String)rb.getString(getQualifiedKey(key));
61          MessageFormat msgfmt = new MessageFormat(basemsgfmt);
62          Object msgobj[] = new Object[1];
63          if (arg == null) {
64              arg = "null"; // mimic java.io.PrintStream.print(String)
65          }
66          msgobj[0] = arg;
67          return msgfmt.format(msgobj);
68      }
69  
70      String getMessage(String key, Object arg1, Object arg2) {
71          String basemsgfmt = (String)rb.getString(getQualifiedKey(key));
72          MessageFormat msgfmt = new MessageFormat(basemsgfmt);
73          Object msgobj[] = new Object[2];
74          if (arg1 == null) {
75              arg1 = "null";
76          }
77          if (arg2 == null) {
78              arg2 = "null";
79          }
80          msgobj[0] = arg1;
81          msgobj[1] = arg2;
82          return msgfmt.format(msgobj);
83      }
84  
85      String getMessage(String key, Object arg1, Object arg2, Object arg3) {
86          String basemsgfmt = (String)rb.getString(getQualifiedKey(key));
87          MessageFormat msgfmt = new MessageFormat(basemsgfmt);
88          Object msgobj[] = new Object[3];
89          if (arg1 == null) {
90              arg1 = "null";
91          }
92          if (arg2 == null) {
93              arg2 = "null";
94          }
95          if (arg3 == null) {
96              arg3 = "null";
97          }
98          msgobj[0] = arg1;
99          msgobj[1] = arg2;
100         msgobj[2] = arg3;
101         return msgfmt.format(msgobj);
102     }
103 
104     String getMessage(String key, Object arg[]) {
105         String basemsgfmt = (String)rb.getString(getQualifiedKey(key));
106         MessageFormat msgfmt = new MessageFormat(basemsgfmt);
107         return msgfmt.format(arg);
108     }
109 
110     String getQualifiedKey(String subKey) {
111         return baseKey + "." + subKey;
112     }
113 }