View Javadoc

1   /*
2       StatCvs - CVS statistics generation 
3       Copyright (C) 2002  Lukasz Pekacki <lukasz@pekacki.de>
4       http://statcvs.sf.net/
5       
6       This library is free software; you can redistribute it and/or
7       modify it under the terms of the GNU Lesser General Public
8       License as published by the Free Software Foundation; either
9       version 2.1 of the License, or (at your option) any later version.
10  
11      This library is distributed in the hope that it will be useful,
12      but WITHOUT ANY WARRANTY; without even the implied warranty of
13      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14      Lesser General Public License for more details.
15  
16      You should have received a copy of the GNU Lesser General Public
17      License along with this library; if not, write to the Free Software
18      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19      
20  	$RCSfile: Messages.java,v $ 
21  	Created on $Date: 2008/04/02 11:22:16 $ 
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                              // do nofin...
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 }