1 | /*
|
---|
2 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
3 | * you may not use this file except in compliance with the License.
|
---|
4 | * You may obtain a copy of the License at
|
---|
5 | *
|
---|
6 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
7 | *
|
---|
8 | * Unless required by applicable law or agreed to in writing, software
|
---|
9 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
11 | * See the License for the specific language governing permissions and
|
---|
12 | * limitations under the License.
|
---|
13 | */
|
---|
14 | package org.tp23.antinstaller;
|
---|
15 |
|
---|
16 | import java.text.MessageFormat;
|
---|
17 | import java.util.MissingResourceException;
|
---|
18 | import java.util.ResourceBundle;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Helper class that will catch missing bundle entries and handle formatting
|
---|
22 | * of message strings
|
---|
23 | *
|
---|
24 | * @author mwilson
|
---|
25 | * @version $Id
|
---|
26 | * @since 0.7.4 patch 7
|
---|
27 | */
|
---|
28 | public 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 | /**
|
---|
53 | * Return a formatted message string.
|
---|
54 | * @param key resource bundle key
|
---|
55 | * @param objArray array of objects that will be used with formatting
|
---|
56 | * string
|
---|
57 | * @return formatted string or error message if the resource bundle
|
---|
58 | * entry is missing
|
---|
59 | */
|
---|
60 | public String getMessage( String key, Object objArray[] )
|
---|
61 | {
|
---|
62 | String message;
|
---|
63 |
|
---|
64 | //Replace null values with empty strings - safer
|
---|
65 | 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 | //Handle missing resource below...
|
---|
82 | }
|
---|
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 |
|
---|
100 | message = strBuffer.toString();
|
---|
101 | }
|
---|
102 | else
|
---|
103 | {
|
---|
104 | MessageFormat msgformat = new MessageFormat( formatStr );
|
---|
105 | message = msgformat.format( ( (Object) ( objArray ) ) );
|
---|
106 | }
|
---|
107 |
|
---|
108 | return message;
|
---|
109 | }
|
---|
110 |
|
---|
111 | }
|
---|