aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-duplications/src/test/files/java/MessageResources.java
blob: fbed9f7e738d058649e8cc6f591597b22c39da40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/*
 * $Id: MessageResources.java 471754 2006-11-06 14:55:09Z husted $
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.struts.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.Serializable;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Locale;

/**
 * General purpose abstract class that describes an API for retrieving
 * Locale-sensitive messages from underlying resource locations of an
 * unspecified design, and optionally utilizing the <code>MessageFormat</code>
 * class to produce internationalized messages with parametric replacement.
 * <p> Calls to <code>getMessage()</code> variants without a
 * <code>Locale</code> argument are presumed to be requesting a message string
 * in the default <code>Locale</code> for this JVM. <p> Calls to
 * <code>getMessage()</code> with an unknown key, or an unknown
 * <code>Locale</code> will return <code>null</code> if the
 * <code>returnNull</code> property is set to <code>true</code>.  Otherwise, a
 * suitable error message will be returned instead. <p> <strong>IMPLEMENTATION
 * NOTE</strong> - Classes that extend this class must be Serializable so that
 * instances may be used in distributable application server environments.
 *
 * @version $Rev: 471754 $ $Date: 2005-08-29 23:57:50 -0400 (Mon, 29 Aug 2005)
 *          $
 */
public abstract class MessageResources implements Serializable {
  // ------------------------------------------------------------- Properties

  /**
   * Commons Logging instance.
   */
  protected static Log log = LogFactory.getLog(MessageResources.class);

  // --------------------------------------------------------- Static Methods

  /**
   * The default MessageResourcesFactory used to create MessageResources
   * instances.
   */
  protected static MessageResourcesFactory defaultFactory = null;

  /**
   * The configuration parameter used to initialize this MessageResources.
   */
  protected String config = null;

  /**
   * The default Locale for our environment.
   */
  protected Locale defaultLocale = Locale.getDefault();

  /**
   * The <code>MessageResourcesFactory</code> that created this instance.
   */
  protected MessageResourcesFactory factory = null;

  /**
   * The set of previously created MessageFormat objects, keyed by the key
   * computed in <code>messageKey()</code>.
   */
  protected HashMap formats = new HashMap();

  /**
   * Indicate is a <code>null</code> is returned instead of an error message
   * string when an unknown Locale or key is requested.
   */
  protected boolean returnNull = false;

  /**
   * Indicates whether 'escape processing' should be performed on the error
   * message string.
   */
  private boolean escape = true;

  // ----------------------------------------------------------- Constructors

  /**
   * Construct a new MessageResources according to the specified
   * parameters.
   *
   * @param factory The MessageResourcesFactory that created us
   * @param config  The configuration parameter for this MessageResources
   */
  public MessageResources(MessageResourcesFactory factory, String config) {
    this(factory, config, false);
  }

  /**
   * Construct a new MessageResources according to the specified
   * parameters.
   *
   * @param factory    The MessageResourcesFactory that created us
   * @param config     The configuration parameter for this
   *                   MessageResources
   * @param returnNull The returnNull property we should initialize with
   */
  public MessageResources(MessageResourcesFactory factory, String config,
                          boolean returnNull) {
    super();
    this.factory = factory;
    this.config = config;
    this.returnNull = returnNull;
  }

  /**
   * The configuration parameter used to initialize this MessageResources.
   *
   * @return parameter used to initialize this MessageResources
   */
  public String getConfig() {
    return (this.config);
  }

  /**
   * The <code>MessageResourcesFactory</code> that created this instance.
   *
   * @return <code>MessageResourcesFactory</code> that created instance
   */
  public MessageResourcesFactory getFactory() {
    return (this.factory);
  }

  /**
   * Indicates that a <code>null</code> is returned instead of an error
   * message string if an unknown Locale or key is requested.
   *
   * @return true if null is returned if unknown key or locale is requested
   */
  public boolean getReturnNull() {
    return (this.returnNull);
  }

  /**
   * Indicates that a <code>null</code> is returned instead of an error
   * message string if an unknown Locale or key is requested.
   *
   * @param returnNull true Indicates that a <code>null</code> is returned
   *                   if an unknown Locale or key is requested.
   */
  public void setReturnNull(boolean returnNull) {
    this.returnNull = returnNull;
  }

  /**
   * Indicates whether 'escape processing' should be performed on the error
   * message string.
   *
   * @since Struts 1.2.8
   */
  public boolean isEscape() {
    return escape;
  }

  /**
   * Set whether 'escape processing' should be performed on the error
   * message string.
   *
   * @since Struts 1.2.8
   */
  public void setEscape(boolean escape) {
    this.escape = escape;
  }

  // --------------------------------------------------------- Public Methods

  /**
   * Returns a text message for the specified key, for the default Locale.
   *
   * @param key The message key to look up
   */
  public String getMessage(String key) {
    return this.getMessage((Locale) null, key, null);
  }

  /**
   * Returns a text message after parametric replacement of the specified
   * parameter placeholders.
   *
   * @param key  The message key to look up
   * @param args An array of replacement parameters for placeholders
   */
  public String getMessage(String key, Object[] args) {
    return this.getMessage((Locale) null, key, args);
  }

  /**
   * Returns a text message after parametric replacement of the specified
   * parameter placeholders.
   *
   * @param key  The message key to look up
   * @param arg0 The replacement for placeholder {0} in the message
   */
  public String getMessage(String key, Object arg0) {
    return this.getMessage((Locale) null, key, arg0);
  }

  /**
   * Returns a text message after parametric replacement of the specified
   * parameter placeholders.
   *
   * @param key  The message key to look up
   * @param arg0 The replacement for placeholder {0} in the message
   * @param arg1 The replacement for placeholder {1} in the message
   */
  public String getMessage(String key, Object arg0, Object arg1) {
    return this.getMessage((Locale) null, key, arg0, arg1);
  }

  /**
   * Returns a text message after parametric replacement of the specified
   * parameter placeholders.
   *
   * @param key  The message key to look up
   * @param arg0 The replacement for placeholder {0} in the message
   * @param arg1 The replacement for placeholder {1} in the message
   * @param arg2 The replacement for placeholder {2} in the message
   */
  public String getMessage(String key, Object arg0, Object arg1, Object arg2) {
    return this.getMessage((Locale) null, key, arg0, arg1, arg2);
  }

  /**
   * Returns a text message after parametric replacement of the specified
   * parameter placeholders.
   *
   * @param key  The message key to look up
   * @param arg0 The replacement for placeholder {0} in the message
   * @param arg1 The replacement for placeholder {1} in the message
   * @param arg2 The replacement for placeholder {2} in the message
   * @param arg3 The replacement for placeholder {3} in the message
   */
  public String getMessage(String key, Object arg0, Object arg1, Object arg2,
                           Object arg3) {
    return this.getMessage((Locale) null, key, arg0, arg1, arg2, arg3);
  }

  /**
   * Returns a text message for the specified key, for the default Locale. A
   * null string result will be returned by this method if no relevant
   * message resource is found for this key or Locale, if the
   * <code>returnNull</code> property is set.  Otherwise, an appropriate
   * error message will be returned. <p> This method must be implemented by
   * a concrete subclass.
   *
   * @param locale The requested message Locale, or <code>null</code> for
   *               the system default Locale
   * @param key    The message key to look up
   */
  public abstract String getMessage(Locale locale, String key);

  /**
   * Returns a text message after parametric replacement of the specified
   * parameter placeholders.  A null string result will be returned by this
   * method if no resource bundle has been configured.
   *
   * @param locale The requested message Locale, or <code>null</code> for
   *               the system default Locale
   * @param key    The message key to look up
   * @param args   An array of replacement parameters for placeholders
   */
  public String getMessage(Locale locale, String key, Object[] args) {
    // Cache MessageFormat instances as they are accessed
    if (locale == null) {
      locale = defaultLocale;
    }

    MessageFormat format = null;
    String formatKey = messageKey(locale, key);

    synchronized (formats) {
      format = (MessageFormat) formats.get(formatKey);

      if (format == null) {
        String formatString = getMessage(locale, key);

        if (formatString == null) {
          return returnNull ? null : ("???" + formatKey + "???");
        }

        format = new MessageFormat(escape(formatString));
        format.setLocale(locale);
        formats.put(formatKey, format);
      }
    }

    return format.format(args);
  }

  /**
   * Returns a text message after parametric replacement of the specified
   * parameter placeholders.  A null string result will never be returned by
   * this method.
   *
   * @param locale The requested message Locale, or <code>null</code> for
   *               the system default Locale
   * @param key    The message key to look up
   * @param arg0   The replacement for placeholder {0} in the message
   */
  public String getMessage(Locale locale, String key, Object arg0) {
    return this.getMessage(locale, key, new Object[]{arg0});
  }

  /**
   * Returns a text message after parametric replacement of the specified
   * parameter placeholders.  A null string result will never be returned by
   * this method.
   *
   * @param locale The requested message Locale, or <code>null</code> for
   *               the system default Locale
   * @param key    The message key to look up
   * @param arg0   The replacement for placeholder {0} in the message
   * @param arg1   The replacement for placeholder {1} in the message
   */
  public String getMessage(Locale locale, String key, Object arg0, Object arg1) {
    return this.getMessage(locale, key, new Object[]{arg0, arg1});
  }

  /**
   * Returns a text message after parametric replacement of the specified
   * parameter placeholders.  A null string result will never be returned by
   * this method.
   *
   * @param locale The requested message Locale, or <code>null</code> for
   *               the system default Locale
   * @param key    The message key to look up
   * @param arg0   The replacement for placeholder {0} in the message
   * @param arg1   The replacement for placeholder {1} in the message
   * @param arg2   The replacement for placeholder {2} in the message
   */
  public String getMessage(Locale locale, String key, Object arg0,
                           Object arg1, Object arg2) {
    return this.getMessage(locale, key, new Object[]{arg0, arg1, arg2});
  }

  /**
   * Returns a text message after parametric replacement of the specified
   * parameter placeholders.  A null string result will never be returned by
   * this method.
   *
   * @param locale The requested message Locale, or <code>null</code> for
   *               the system default Locale
   * @param key    The message key to look up
   * @param arg0   The replacement for placeholder {0} in the message
   * @param arg1   The replacement for placeholder {1} in the message
   * @param arg2   The replacement for placeholder {2} in the message
   * @param arg3   The replacement for placeholder {3} in the message
   */
  public String getMessage(Locale locale, String key, Object arg0,
                           Object arg1, Object arg2, Object arg3) {
    return this.getMessage(locale, key,
        new Object[]{arg0, arg1, arg2, arg3});
  }

  /**
   * Return <code>true</code> if there is a defined message for the
   * specified key in the system default locale.
   *
   * @param key The message key to look up
   */
  public boolean isPresent(String key) {
    return this.isPresent(null, key);
  }

  /**
   * Return <code>true</code> if there is a defined message for the
   * specified key in the specified Locale.
   *
   * @param locale The requested message Locale, or <code>null</code> for
   *               the system default Locale
   * @param key    The message key to look up
   */
  public boolean isPresent(Locale locale, String key) {
    String message = getMessage(locale, key);

    if (message == null) {
      return false;
    } else if (message.startsWith("???") && message.endsWith("???")) {
      return false; // FIXME - Only valid for default implementation
    } else {
      return true;
    }
  }

  // ------------------------------------------------------ Protected Methods

  /**
   * Escape any single quote characters that are included in the specified
   * message string.
   *
   * @param string The string to be escaped
   */
  protected String escape(String string) {
    if (!isEscape()) {
      return string;
    }

    if ((string == null) || (string.indexOf('\'') < 0)) {
      return string;
    }

    int n = string.length();
    StringBuffer sb = new StringBuffer(n);

    for (int i = 0; i < n; i++) {
      char ch = string.charAt(i);

      if (ch == '\'') {
        sb.append('\'');
      }

      sb.append(ch);
    }

    return sb.toString();
  }

  /**
   * Compute and return a key to be used in caching information by a Locale.
   * <strong>NOTE</strong> - The locale key for the default Locale in our
   * environment is a zero length String.
   *
   * @param locale The locale for which a key is desired
   */
  protected String localeKey(Locale locale) {
    return (locale == null) ? "" : locale.toString();
  }

  /**
   * Compute and return a key to be used in caching information by Locale
   * and message key.
   *
   * @param locale The Locale for which this format key is calculated
   * @param key    The message key for which this format key is calculated
   */
  protected String messageKey(Locale locale, String key) {
    return (localeKey(locale) + "." + key);
  }

  /**
   * Compute and return a key to be used in caching information by locale
   * key and message key.
   *
   * @param localeKey The locale key for which this cache key is calculated
   * @param key       The message key for which this cache key is
   *                  calculated
   */
  protected String messageKey(String localeKey, String key) {
    return (localeKey + "." + key);
  }

  /**
   * Create and return an instance of <code>MessageResources</code> for the
   * created by the default <code>MessageResourcesFactory</code>.
   *
   * @param config Configuration parameter for this message bundle.
   */
  public synchronized static MessageResources getMessageResources(
      String config) {
    if (defaultFactory == null) {
      defaultFactory = MessageResourcesFactory.createFactory();
    }

    return defaultFactory.createResources(config);
  }

  /**
   * Log a message to the Writer that has been configured for our use.
   *
   * @param message The message to be logged
   */
  public void log(String message) {
    log.debug(message);
  }

  /**
   * Log a message and exception to the Writer that has been configured for
   * our use.
   *
   * @param message   The message to be logged
   * @param throwable The exception to be logged
   */
  public void log(String message, Throwable throwable) {
    log.debug(message, throwable);
  }
}