View Javadoc

1   /*
2    * JarInspector - Copyright (C) 2004 Che Inc., Rosario Argentina
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Library General Public
6    * License as published by the Free Software Foundation; either
7    * version 2 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Library General Public License for more details.
13   *
14   * You should have received a copy of the GNU Library General Public
15   * License along with this library; if not, write to the Free
16   * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17   */
18  
19  package inc.che.common.resource;
20  
21  // Imports
22  
23  import inc.che.common.string.StringUtil;
24  
25  import java.io.ByteArrayOutputStream;
26  import java.io.IOException;
27  import java.io.InputStream;
28  import java.util.ArrayList;
29  import java.util.HashMap;
30  import java.util.Locale;
31  import java.util.MissingResourceException;
32  import java.util.ResourceBundle;
33  
34  import javax.swing.ImageIcon;
35  
36  import org.apache.log4j.Logger;
37  
38  /***
39   *  <b>ResourceManager für Textmeldungen</b>
40   * @version $Id: ResourceManager.java,v 1.1 2005/03/06 12:57:02 stevemcmee Exp $
41   * @author <address> Steve McMee &lt;stevemcmee@users.sourceforge.net&gt; </address>
42   */
43  
44  public final class ResourceManager {
45  
46      /*** CVS ID of this file */
47      public static final String CVS_ID =
48          "$Id: ResourceManager.java,v 1.1 2005/03/06 12:57:02 stevemcmee Exp $";
49  
50      /***
51       * logger instance for this class
52       */
53  
54      private static Logger log = Logger.getLogger(ResourceManager.class);
55  
56      private ResourceBundle resourceBundle = null;
57      private String resourceFile = null;
58      private Locale locale;
59  
60      /*** The Managers */
61  
62      private static HashMap managers = null;
63  
64      private static ArrayList keysNotFound = new ArrayList();
65  
66      /***
67       * forbidden Construcor
68       */
69  
70      private ResourceManager() {
71      }
72  
73      public static ResourceManager getCommonsResourceManager() {
74          return ResourceManager.getResourceManager(
75              StringResources.TEXT_RESOURCES);
76      }
77      public static ResourceManager getResourceManager(String resourceFile) {
78  
79          if (managers == null) {
80              managers = new HashMap();
81          }
82          if (managers.get(resourceFile) == null) {
83              ResourceManager manager = new ResourceManager();
84              manager.resourceBundle = ResourceBundle.getBundle(resourceFile);
85              manager.locale = manager.resourceBundle.getLocale();
86              manager.resourceFile = resourceFile;
87              managers.put(resourceFile, manager);
88          }
89          return (ResourceManager) managers.get(resourceFile);
90  
91      }
92  
93      public void setLocale(Locale locale) {
94          if (log.isDebugEnabled()) {
95              log.debug("setLocale(" + locale + ")");
96          }
97          if (log.isDebugEnabled()) {
98              log.debug("resourceFile:" + resourceFile);
99          }
100         this.locale = locale;
101         this.resourceBundle = ResourceBundle.getBundle(resourceFile, locale);
102     }
103     public Locale getLocale() {
104         return locale;
105     }
106 
107     public String getText(String id) {
108         return getText(id, null);
109     }
110 
111     public String getText(String id, String[] parameter) {
112         if (log.isDebugEnabled()) {
113             log.debug("getText(" + id + ")");
114         }
115         String text = null;
116         try {
117             text = resourceBundle.getString(id);
118         } catch (MissingResourceException ex) {
119             ex.printStackTrace();
120             keysNotFound.add(id);
121             log.error("Cannot find TextresourceID " + id);
122         }
123         if (text == null) {
124             text = "[Cannot find TextresourceID '" + id + "']";
125         } else {
126             if (parameter != null) {
127                 for (int i = 0; i < parameter.length; i++) {
128                     if (parameter[i] != null) {
129                         text =
130                             StringUtil.replace(
131                                 text,
132                                 "%" + (i + 1),
133                                 parameter[i]);
134                     }
135                 }
136             }
137         }
138         if (log.isDebugEnabled()) {
139             log.debug("getText(" + id + ")->" + text);
140         }
141         return text;
142     }
143 
144     public InputStream getResource(String name) {
145         String filename = getText(name);
146         return ResourceManager.class.getClassLoader().getResourceAsStream(
147             filename);
148     }
149     public ImageIcon getIconByGifName(String name) {
150         ByteArrayOutputStream imageStream = new ByteArrayOutputStream();
151         ImageIcon icon = null;
152         try {
153 
154             String filename = getText(name);
155             InputStream is =
156                 ResourceManager.class.getClassLoader().getResourceAsStream(
157                     filename);
158             if (is != null) {
159                 int c = -1;
160                 while ((c = is.read()) != -1) {
161                     imageStream.write(c);
162                 }
163                 is.close();
164                 imageStream.close();
165                 icon = new ImageIcon(imageStream.toByteArray());
166             } else {
167                 log.error("Cannot load image " + name + " " + filename);
168             }
169         } catch (IOException ex) {
170             log.error("Cannot load image " + name, ex);
171         }
172         return icon;
173     }
174 
175     public ImageIcon getIconByGifPath(String name) {
176         ByteArrayOutputStream imageStream = new ByteArrayOutputStream();
177         ImageIcon icon = null;
178         try {
179             String filename = name;
180             InputStream is =
181                 ResourceManager.class.getClassLoader().getResourceAsStream(
182                     filename);
183             if (is != null) {
184                 int c = -1;
185                 while ((c = is.read()) != -1) {
186                     imageStream.write(c);
187                 }
188                 is.close();
189                 imageStream.close();
190                 icon = new ImageIcon(imageStream.toByteArray());
191             } else {
192                 log.error("Cannot load image " + name);
193             }
194         } catch (IOException ex) {
195             log.error("Cannot load image " + name, ex);
196         }
197         return icon;
198     }
199 
200     public ArrayList getMissingKeys() {
201         return this.keysNotFound;
202     }
203 }