1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package net.sf.statcvs;
24
25 import java.util.MissingResourceException;
26 import java.util.ResourceBundle;
27
28 import net.sf.statcvs.output.ConfigurationOptions;
29
30 /**
31 * This class manages the externalization of strings that will
32 * possiby be presented to the user
33 * @author Lukasz Pekacki
34 * @version $Id: Messages.java,v 1.11 2008/04/02 11:22:16 benoitx Exp $
35 */
36 public class Messages {
37 /**
38 * Whitespace constant
39 */
40 public static final String WS = " ";
41 /**
42 * Newline constant
43 */
44 public static final String NL = "\n";
45
46 private static final String BUNDLE_NAME = "net.sf.statcvs.statcvs";
47 private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME);
48
49 private static ResourceBundle primaryResourceBundle = null;
50
51 /**
52 * Returns the value for the specified key. Key-value pairs are specified
53 * in the resourcebundle properties file, either the primary one or,
54 * it falls back on the original one.
55 * @param key key of the requested string
56 * @return String
57 */
58 public static String getString(final String key) {
59 final boolean xml = "xml".equalsIgnoreCase(ConfigurationOptions.getOutputFormat());
60 String val = null;
61 try {
62 if (primaryResourceBundle != null) {
63 try {
64 val = primaryResourceBundle.getString(key + (xml ? ".xml" : ""));
65 if (val != null) {
66 return val;
67 }
68 } catch (final MissingResourceException e) {
69 if (xml) {
70 try {
71 val = primaryResourceBundle.getString(key);
72 if (val != null) {
73 return val;
74 }
75 } catch (final MissingResourceException e2) {
76
77 }
78 }
79 }
80 }
81 val = RESOURCE_BUNDLE.getString(key + (xml ? ".xml" : ""));
82 return val;
83 } catch (final MissingResourceException e) {
84 try {
85 if (xml) {
86 val = RESOURCE_BUNDLE.getString(key);
87 if (val != null) {
88 return val;
89 }
90 }
91 return '!' + key + '!';
92 } catch (final MissingResourceException e2) {
93 return '!' + key + '!';
94 }
95 }
96 }
97
98 /**
99 * This method enables a user, typically of another project, to set
100 * a primary resource bundle. If no value or null, it will revert to
101 * the original bundle.
102 * @param primaryResourceName
103 */
104 public static void setPrimaryResource(final String primaryResourceName) {
105 primaryResourceBundle = ResourceBundle.getBundle(primaryResourceName);
106 }
107 }