1
14package org.tp23.antinstaller;
15
16import java.text.MessageFormat;
17import java.util.MissingResourceException;
18import java.util.ResourceBundle;
19
20
28public class ResourceBundleHelper
29{
30 private String bundleName;
31 private ResourceBundle resourceBundle;
32
33 private static final Object[] EMPTY_OBJ_ARRAY = new Object[0];
34 private static final String EMPTY_STRING = "";
35
36 public ResourceBundleHelper( final String bundleName )
37 {
38 this.bundleName = bundleName;
39 resourceBundle = ResourceBundle.getBundle( bundleName );
40 }
41
42 public String getMessage( final String key )
43 {
44 return getMessage( key, EMPTY_OBJ_ARRAY );
45 }
46
47 public String getMessage( final String key, final Object obj )
48 {
49 return getMessage( key, new Object[] { obj } );
50 }
51
52
60 public String getMessage( String key, Object objArray[] )
61 {
62 String message;
63
64 for( int i = 0; i < objArray.length; i++ )
66 {
67 if( objArray[i] == null )
68 {
69 objArray[i] = EMPTY_STRING;
70 }
71 }
72
73 String formatStr = null;
74
75 try
76 {
77 formatStr = resourceBundle.getString( key );
78 }
79 catch( MissingResourceException missingResExc )
80 {
81 }
83
84 if( formatStr == null )
85 {
86 StringBuffer strBuffer = new StringBuffer( "Message entry with key " );
87 strBuffer.append( key );
88 strBuffer.append( " is missing from resource bundle " );
89 strBuffer.append( bundleName );
90 strBuffer.append( "\n" );
91 strBuffer.append( bundleName );
92 strBuffer.append( ":: " );
93 strBuffer.append( key );
94 for( int i = 0; i < objArray.length; i++ )
95 {
96 strBuffer.append( " " );
97 strBuffer.append( objArray[i].toString() );
98 }
99
00 message = strBuffer.toString();
01 }
02 else
03 {
04 MessageFormat msgformat = new MessageFormat( formatStr );
05 message = msgformat.format( ( (Object) ( objArray ) ) );
06 }
07
08 return message;
09 }
10
11}
12