View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/util/MessageResource.java,v 1.10 2004/08/18 12:26:09 hkollmann Exp $
3    * $Revision: 1.10 $
4    * $Date: 2004/08/18 12:26:09 $
5    *
6    * DbForms - a Rapid Application Development Framework
7    * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8    *
9    * This library is free software; you can redistribute it and/or
10   * modify it under the terms of the GNU Lesser General Public
11   * License as published by the Free Software Foundation; either
12   * version 2.1 of the License, or (at your option) any later version.
13   *
14   * This library is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this library; if not, write to the Free Software
21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22   */
23  
24  package org.dbforms.util;
25  
26  import java.util.HashMap;
27  import java.util.Locale;
28  import java.util.ResourceBundle;
29  
30  
31  
32  /***
33   * base class for handling message resources
34   *
35   * @author Henner Kollmann
36   */
37  public class MessageResource {
38     /***
39      * Use of HashMap for allowing null value (ReourceBundle) and avoiding to
40      * call getBundle each time if resources file is not present.
41      */
42     private HashMap hashResources = new HashMap();
43     private String  subClass = null;
44  
45     /***
46      * Creates a new MessageResource object.
47      *
48      * @param subClass DOCUMENT ME!
49      */
50     public MessageResource(String subClass) {
51        this.subClass = subClass;
52     }
53  
54     /***
55      * Retrieve message from ResourceBundle.  If the ResourceBundle is not yet
56      * cached, cache it and retreive message.
57      *
58      * @param msgmsg </code> : Message key to lookup.
59      * @param locloc </code> : Locale object to map message with good
60      *        ResourceBundle.
61      *
62      * @return <code>String</code> : Message resolve, null if not found.
63      */
64     public String getMessage(String msg,
65                              Locale loc) {
66        if (subClass == null) {
67           return null;
68        }
69  
70        if (loc == null) {
71           return null;
72        }
73  
74        ResourceBundle rb = null;
75  
76        // Faster than String (immuable) concatenation
77        String key = new StringBuffer().append(loc.getLanguage())
78                                       .append("_")
79                                       .append(loc.getCountry())
80                                       .append("_")
81                                       .append(loc.getVariant())
82                                       .toString();
83  
84        if (hashResources.containsKey(key)) {
85           rb = (ResourceBundle) hashResources.get(key);
86        } else {
87           try {
88              rb = ResourceBundle.getBundle(subClass, loc);
89           } catch (Exception e) {
90              rb = null;
91           }
92  
93           // Put the ResourceBundle or null value in HashMap with the key
94           hashResources.put(key, rb);
95        }
96  
97        String s = null;
98  
99        if (rb != null) {
100          try {
101             s = rb.getString(msg);
102          } catch (Exception e) {
103             s = null;
104          }
105       }
106 
107       return s;
108    }
109 
110 
111    /***
112     * Retrieve message from ResourceBundle and replace parameter "{x}" with
113     * values in parms array.
114     *
115     * @param msgmsg </code> : Message key to lookup.
116     * @param locloc </code> : Locale object to map message with good
117     *        ResourceBundle.
118     * @param parmsparms[] </code> : Parameters to replace "{x}" in message .
119     *
120     * @return <code>String</code> : Message resolve with parameter replaced,
121     *         null if message key not found.
122     */
123    public String getMessage(String   msg,
124                             Locale   loc,
125                             String[] parms) {
126       String result = getMessage(msg, loc);
127 
128       if (result == null) {
129          return null;
130       }
131 
132       String search = null;
133 
134       for (int i = 0; i < parms.length; i++) {
135          search = "{" + i + "}";
136          result = replaceAll(result, search, parms[i]);
137       }
138 
139       return result;
140    }
141 
142 
143    /***
144     * DOCUMENT ME!
145     *
146     * @return DOCUMENT ME!
147     */
148    public String getSubClass() {
149       return subClass;
150    }
151 
152 
153    /***
154     * Replace all expression {...} by the appropriate string.
155     *
156     * @param strstr </code> : Original string.
157     * @param searchsearch </code> : Expression to search.
158     * @param replacereplace </code> : Replacement string.
159     *
160     * @return <code>String</code> : The string with all expression replaced.
161     */
162    private String replaceAll(String str,
163                              String search,
164                              String replace) {
165       StringBuffer result = null;
166       int          oldpos = 0;
167 
168       do {
169          int pos = str.indexOf(search, oldpos);
170 
171          if (pos < 0) {
172             break;
173          }
174 
175          if (result == null) {
176             result = new StringBuffer();
177          }
178 
179          result.append(str.substring(oldpos, pos));
180 
181          result.append(replace);
182 
183          pos += search.length();
184          oldpos = pos;
185       } while (true);
186 
187       if (oldpos == 0) {
188          return str;
189       } else {
190          result.append(str.substring(oldpos));
191 
192          return new String(result);
193       }
194    }
195 }