KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.HashMap JavaDoc;
27 import java.util.Locale JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.apache.log4j.Logger;
31
32
33 /**
34  *
35  *
36  * @author <a HREF="mailto:meat_for_the_butcher@yahoo.com">Patrik Nyborg</a>
37  */

38 public class StringManagerFactory
39 {
40
41     private final static Logger logger = Logger.getLogger(StringManagerFactory.class.getName());
42
43     private static final String JavaDoc SYSTEM = ".SystemStrings";
44     private static final String JavaDoc PRESENTATION = ".PresentationStrings";
45
46     // All created StringManager instances.
47
private static Map JavaDoc managers = new HashMap JavaDoc();
48
49
50   /**
51    * Static class; don't allow instantiation.
52    */

53   private StringManagerFactory() {}
54
55
56
57   // --- [Public] --------------------------------------------------------------
58

59   /**
60    *
61    */

62   public static synchronized StringManager getSystemStringManager(String JavaDoc packageName) {
63     return getStringManager(packageName, SYSTEM, Locale.ENGLISH);
64   }
65
66   /**
67    *
68    */

69   public static synchronized StringManager getPresentationStringManager(String JavaDoc packageName, Locale JavaDoc locale) {
70     return getStringManager(packageName, PRESENTATION, locale);
71   }
72
73   /**
74    *
75    */

76   public static synchronized StringManager getPresentationStringManager(String JavaDoc packageNames[], Locale JavaDoc locale)
77   {
78     logger.info("Getting stringManager");
79     final ChainedStringManager chain = new ChainedStringManager();
80     for(int i=0; i<packageNames.length; ++i) {
81       chain.add(getPresentationStringManager(packageNames[i], locale));
82     }
83     return chain;
84   }
85
86
87
88   // --- [X implementation] ----------------------------------------------------
89
// --- [X Overrides] ---------------------------------------------------------
90
// --- [Package protected] ---------------------------------------------------
91
// --- [Private] -------------------------------------------------------------
92

93   /**
94    *
95    */

96   private static StringManager getStringManager(String JavaDoc packageName, String JavaDoc suffix, Locale JavaDoc locale) {
97     logger.info("packageName:" + packageName);
98     final String JavaDoc name = getName(packageName, suffix, locale);
99     final String JavaDoc bundleName = getBundleName(packageName, suffix);
100
101     StringManager manager = (StringManager) managers.get(name);
102     if(manager == null) {
103       manager = new SimpleStringManager(bundleName, locale);
104       managers.put(name, manager);
105     }
106     return manager;
107   }
108
109   /**
110    *
111    */

112   
113   private final static String JavaDoc getName(String JavaDoc packageName, String JavaDoc suffix, Locale JavaDoc locale)
114   {
115     return packageName + suffix + "_" + locale.toString() + ".properties";
116   }
117
118   /**
119    *
120    */

121   private final static String JavaDoc getBundleName(String JavaDoc packageName, String JavaDoc suffix) {
122     return packageName + suffix;
123   }
124
125
126
127   // --- [Protected] -----------------------------------------------------------
128
// --- [Inner classes] -------------------------------------------------------
129
}
Popular Tags