Statistics
| Revision:

root / import / ext3D / trunk / install-extension3d / IzPack / src / lib / com / izforge / izpack / installer / ResourceManager.java @ 15280

History | View | Annotate | Download (9.1 KB)

1 15280 rgaitan
/*
2
 *  $Id: ResourceManager.java,v 1.1 2006/06/14 07:29:07 cesar Exp $
3
 *  IzPack
4
 *  Copyright (C) 2001,2002 Marcus Stursberg
5
 *
6
 *  File :               ResourceManager.java
7
 *  Description :        Class to get resources from the installer
8
 *  Author's email :     marcus@emsty.de
9
 *  Author's Website :   http://www.emasty.de
10
 *
11
 *  This program is free software; you can redistribute it and/or
12
 *  modify it under the terms of the GNU General Public License
13
 *  as published by the Free Software Foundation; either version 2
14
 *  of the License, or any later version.
15
 *
16
 *  This program is distributed in the hope that it will be useful,
17
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 *  GNU General Public License for more details.
20
 *
21
 *  You should have received a copy of the GNU General Public License
22
 *  along with this program; if not, write to the Free Software
23
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24
 */
25
package com.izforge.izpack.installer;
26
27
import java.io.ByteArrayOutputStream;
28
import java.io.IOException;
29
import java.io.InputStream;
30
import java.net.URL;
31
32
import javax.swing.ImageIcon;
33
34
/**
35
 *  With this ResourceManager you are able to get resources from the jar file.
36
 *
37
 *  All resources are loaded language dependent as it's done in
38
 *  java.util.ResourceBundle. To set a language dependent resource just append
39
 *  '_' and the locale to the end of the Resourcename<br>
40
 *  <br>
41
 *  Example:
42
 *  <li> InfoPanel.info - for default value</li>
43
 *  <li> InfoPanel.info_deu - for german value</li>
44
 *  <li> InfoPanel.info_eng - for english value</li> <br>
45
 *
46
 * This class is almost a singleton. It is created once using
47
 * <code>create</code> by the installer and later, the instance is retrieved
48
 * using <code>getInstance</code>.
49
 *
50
 * @author     Marcus Stursberg
51
 */
52
public class ResourceManager
53
{
54
55
  /**
56
   *  Contains the current language of the installer The locale is taken from
57
   *  InstallData#installData#getAttribute("langpack") If there is no language
58
   *  set, the language is english.
59
   */
60
  private String locale = "";
61
62
  /**  The base path where to find the resources */
63
  protected final String resourceBasePath = "/res/";
64
65
  /**  Contains the given InstallData */
66
  private AutomatedInstallData installData;
67
68
  /**  The instance of this class. */
69
  private static ResourceManager instance = null;
70
71
72
  /**
73
   *  Constructor. Protected because this is a singleton.
74
   *
75
   * @param  data  - the current installData
76
   */
77
  protected ResourceManager(AutomatedInstallData data)
78
  {
79
    this.installData = data;
80
    if (data.localeISO3 != null)
81
    {
82
      this.locale = data.localeISO3;
83
    }
84
    else
85
    {
86
      // try to figure out ourself
87
      this.locale = installData.xmlData.getAttribute("langpack", "eng");
88
    }
89
  }
90
91
  /**
92
   * Create the resource manager.
93
   *
94
   * This method should be called only once. If it is called a second
95
   * time, the already existing instance is returned. The resource manager
96
   * should be called <b>after</b> the language has been set in
97
   * {@link AutomatedInstallData#localeISO3}
98
   *
99
   * @param data the installation information
100
   * @return the created instance
101
   */
102
  public static ResourceManager create (AutomatedInstallData data)
103
  {
104
    if (ResourceManager.instance == null)
105
      ResourceManager.instance = new ResourceManager (data);
106
107
    return ResourceManager.instance;
108
  }
109
110
  /**
111
   * Return the resource manager.
112
   *
113
   * @return the resource manager instance, null if no instance has been
114
   *         created
115
   */
116
  public static ResourceManager getInstance ()
117
  {
118
    return ResourceManager.instance;
119
  }
120
121
  /**
122
   *  This method is used to get the language dependent path of the given
123
   *  resource. If there is a resource for the current language the path of the
124
   *  language dependen resource is returnd. If there's no resource for the
125
   *  current lanuage the default path is returned.
126
   *
127
   * @param  resource                    Resource to load language dependen
128
   * @return                             the language dependent path of the
129
   *      given resource
130
   * @throws  ResourceNotFoundException  If the resource is not found
131
   */
132
  private String getLanguageResourceString(String resource) throws ResourceNotFoundException
133
  {
134
    InputStream in;
135
    String resourcePath = this.resourceBasePath + resource + "_" + this.locale;
136
    in = ResourceManager.class.getResourceAsStream(resourcePath);
137
    if (in != null)
138
      return resourcePath;
139
    else
140
    {
141
      // if there's no language dependent resource found
142
      resourcePath = this.resourceBasePath + resource;
143
      in = ResourceManager.class.getResourceAsStream(resourcePath);
144
      if (in != null)
145
        return resourcePath;
146
      else
147
        throw new ResourceNotFoundException("Can not find Resource " +
148
          resource + " for language " + this.locale);
149
    }
150
  }
151
152
153
  /**
154
   *  Returns an InputStream contains the given Resource The Resource is loaded
155
   *  language dependen by the informations from <code>this.locale</code> If
156
   *  there is no Resource for the current language found, the default Resource
157
   *  is given.
158
   *
159
   * @param  resource                                    The resource to load
160
   * @return                                             an InputStream contains
161
   *      the requested resource
162
   * @exception  ResourceNotFoundException               Description of the
163
   *      Exception
164
   * @throws  ResourceManager.ResourceNotFoundException  thrown if there is no
165
   *      resource found
166
   */
167
  public InputStream getInputStream(String resource) throws ResourceNotFoundException
168
  {
169
    String resourcepath = this.getLanguageResourceString(resource);
170
    //System.out.println ("reading resource "+resourcepath);
171
    return ResourceManager.class.getResourceAsStream(resourcepath);
172
  }
173
174
175
  /**
176
   *  Returns a URL refers to the given Resource
177
   *
178
   * @param  resource                                    the resource to load
179
   * @return                                             A languagedependen URL
180
   *      spezifies the requested resource
181
   * @exception  ResourceNotFoundException               Description of the
182
   *      Exception
183
   * @throws  ResourceManager.ResourceNotFoundException  thrown if there is no
184
   *      resource found
185
   */
186
  public URL getURL(String resource) throws ResourceNotFoundException
187
  {
188
    try
189
    {
190
      return this.getClass().getResource(this.getLanguageResourceString(resource + "_" + installData.localeISO3));
191
    }
192
    catch (Exception ex)
193
    {
194
      return this.getClass().getResource(this.getLanguageResourceString(resource));
195
    }
196
  }
197
198
199
  /**
200
   *  Returns a text resource from the jar file. The resource is loaded by
201
   *  ResourceManager#getResource and then converted into text.
202
   *
203
   * @param  resource                    - a text resource to load
204
   * @return                             a String contains the text of the
205
   *      resource
206
   * @throws  ResourceNotFoundException  if the resource can not be found
207
   * @throws  IOException                if the resource can not be loaded
208
   */
209
  //Maybe we can add a text parser for this method
210
  public String getTextResource(String resource) throws ResourceNotFoundException,
211
    IOException
212
  {
213
    InputStream in = null;
214
    try
215
    {
216
      in = this.getInputStream(resource + "_" + this.installData.localeISO3);
217
    }
218
    catch (Exception ex)
219
    {
220
      in = this.getInputStream(resource);
221
    }
222
223
    ByteArrayOutputStream infoData = new ByteArrayOutputStream();
224
    byte[] buffer = new byte[5120];
225
    int bytesInBuffer;
226
    while ((bytesInBuffer = in.read(buffer)) != -1)
227
      infoData.write(buffer, 0, bytesInBuffer);
228
229
    return infoData.toString();
230
  }
231
232
233
  /**
234
   *  Returns a laguage dependent ImageIcon for the given Resource
235
   *
236
   * @param  resource                    resrouce of the Icon
237
   * @return                             a ImageIcon loaded from the given
238
   *      Resource
239
   * @throws  ResourceNotFoundException  thrown when the resource can not be
240
   *      found
241
   * @throws  IOException                if the resource can not be loaded
242
   */
243
  public ImageIcon getImageIconResource(String resource) throws ResourceNotFoundException,
244
    IOException
245
  {
246
    return new ImageIcon(this.getURL(resource));
247
  }
248
249
250
  /**
251
   *  Sets the locale for the resourcefiles. The locale is taken from
252
   *  InstallData#installData#getAttribute("langpack") If there is no language
253
   *  set, the default language is english.
254
   *
255
   * @param  locale  of the resourcefile
256
   */
257
  public void setLocale(String locale)
258
  {
259
    this.locale = locale;
260
  }
261
262
263
  /**
264
   *  Returns the locale for the resourcefiles. The locale is taken from
265
   *  InstallData#installData#getAttribute("langpack") If there is no language
266
   *  set, the default language is english.
267
   *
268
   * @return    the current language
269
   */
270
  public String getLocale()
271
  {
272
    return this.locale;
273
  }
274
}