KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > util > SimpleStringManager


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.util;
25
26 import java.text.MessageFormat JavaDoc;
27 import java.util.Locale JavaDoc;
28 import java.util.MissingResourceException JavaDoc;
29 import java.util.ResourceBundle JavaDoc;
30
31 import org.apache.log4j.Logger;
32 import org.infoglue.cms.exception.Bug;
33 import org.infoglue.cms.exception.ConfigurationError;
34
35 /**
36  *
37  */

38 public class SimpleStringManager implements StringManager
39 {
40
41     private final static Logger logger = Logger.getLogger(SimpleStringManager.class.getName());
42
43     // The ResourceBundle for this StringManager.
44
private ResourceBundle JavaDoc bundle;
45
46  
47     SimpleStringManager(String JavaDoc bundleName, Locale JavaDoc locale)
48     {
49         if(locale == null || locale.getLanguage() == null || locale.getLanguage().equalsIgnoreCase(""))
50         {
51             logger.info("No locale sent in - must be a bug:" + locale);
52             locale = Locale.ENGLISH;
53         }
54         
55         try
56         {
57             logger.info("Created a SimpleStringManager for package bundleName" + bundleName + ":" + locale);
58             this.bundle = ResourceBundle.getBundle(bundleName, locale);
59         }
60         catch(MissingResourceException JavaDoc e)
61         {
62             throw new ConfigurationError("Unable to find resource bundle: " + e.getMessage(), e);
63         }
64         catch(NullPointerException JavaDoc e)
65         {
66             throw new Bug("Unable to create resource bundle.", e);
67         }
68     }
69
70
71
72   // --- [Public] --------------------------------------------------------------
73
// --- [org.infoglue.cms.util.StringManager implementation] -----------------
74

75   /**
76    *
77    */

78     public final String JavaDoc getString(String JavaDoc key)
79     {
80         try
81         {
82             //logger.info("Trying to find a string for key " + key + " in " + this);
83
return this.bundle.getString(key);
84         }
85         catch(MissingResourceException JavaDoc e)
86         {
87             logger.warn("Error trying to find a string for key " + key, e);
88             throw new ConfigurationError("Key not found.", e);
89         }
90         catch(Throwable JavaDoc t)
91         {
92             logger.warn("Error trying to find a string for key " + key);
93             throw new Bug("Unable to fetch the value for the specified key.", t);
94         }
95     }
96
97   /**
98    *
99    */

100   public final String JavaDoc getString(String JavaDoc key, Object JavaDoc args[]) {
101     return MessageFormat.format(getString(key), args);
102   }
103
104   /**
105    *
106    */

107   public final String JavaDoc getString(String JavaDoc key, Object JavaDoc arg) {
108     return getString(key, new Object JavaDoc[]{ arg });
109   }
110
111   /**
112    *
113    */

114   public final String JavaDoc getString(String JavaDoc key, Object JavaDoc arg1, Object JavaDoc arg2) {
115     return getString(key, new Object JavaDoc[]{ arg1, arg2 });
116   }
117
118   /**
119    *
120    */

121   public final String JavaDoc getString(String JavaDoc key, Object JavaDoc arg1, Object JavaDoc arg2, Object JavaDoc arg3) {
122     return getString(key, new Object JavaDoc[]{ arg1, arg2, arg3 });
123   }
124
125   // --- [X Overrides] ---------------------------------------------------------
126
// --- [Package protected] ---------------------------------------------------
127

128   /**
129    *
130    */

131   boolean containsKey(String JavaDoc key) {
132     try {
133       this.bundle.getString(key);
134       return true;
135     } catch(MissingResourceException JavaDoc e) {
136       return false;
137     }
138   }
139
140
141
142   // --- [Private] -------------------------------------------------------------
143
// --- [Protected] -----------------------------------------------------------
144
// --- [Inner classes] -------------------------------------------------------
145
}
146   
147
148
149
Popular Tags