KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > izforge > izpack > installer > ResourceManager


1 /*
2  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
3  *
4  * http://www.izforge.com/izpack/
5  * http://developer.berlios.de/projects/izpack/
6  *
7  * Copyright 2002 Marcus Stursberg
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21
22 package com.izforge.izpack.installer;
23
24 import java.io.ByteArrayOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.net.URL JavaDoc;
28
29 import javax.swing.ImageIcon JavaDoc;
30
31 /**
32  * With this ResourceManager you are able to get resources from the jar file.
33  *
34  * All resources are loaded language dependent as it's done in java.util.ResourceBundle. To set a
35  * language dependent resource just append '_' and the locale to the end of the Resourcename<br>
36  * <br>
37  * Example:
38  * <li> InfoPanel.info - for default value</li>
39  * <li> InfoPanel.info_deu - for german value</li>
40  * <li> InfoPanel.info_eng - for english value</li>
41  * <br>
42  *
43  * This class is almost a singleton. It is created once using <code>create</code> by the installer
44  * and later, the instance is retrieved using <code>getInstance</code>.
45  *
46  * @author Marcus Stursberg
47  */

48 public class ResourceManager
49 {
50
51     /**
52      * Contains the current language of the installer The locale is taken from
53      * InstallData#installData#getAttribute("langpack") If there is no language set, the language is
54      * english.
55      */

56     private String JavaDoc locale = "";
57
58     /** The base path where to find the resources */
59     protected final String JavaDoc resourceBasePath = "/res/";
60
61     /** Contains the given InstallData */
62     private AutomatedInstallData installData;
63
64     /** The instance of this class. */
65     private static ResourceManager instance = null;
66
67     /**
68      * Constructor. Protected because this is a singleton.
69      *
70      * @param data - the current installData
71      */

72     protected ResourceManager(AutomatedInstallData data)
73     {
74         this.installData = data;
75         if (data.localeISO3 != null)
76         {
77             this.locale = data.localeISO3;
78         }
79         else
80         {
81             // try to figure out ourself
82
this.locale = installData.xmlData.getAttribute("langpack", "eng");
83         }
84     }
85
86     /**
87      * Create the resource manager.
88      *
89      * This method should be called only once. If it is called a second time, the already existing
90      * instance is returned. The resource manager should be called <b>after</b> the language has
91      * been set in {@link AutomatedInstallData#localeISO3}
92      *
93      * @param data the installation information
94      * @return the created instance
95      */

96     public static ResourceManager create(AutomatedInstallData data)
97     {
98         if (ResourceManager.instance == null) ResourceManager.instance = new ResourceManager(data);
99
100         return ResourceManager.instance;
101     }
102
103     /**
104      * Return the resource manager.
105      *
106      * @return the resource manager instance, null if no instance has been created
107      */

108     public static ResourceManager getInstance()
109     {
110         return ResourceManager.instance;
111     }
112
113     /**
114      * This method is used to get the language dependent path of the given resource. If there is a
115      * resource for the current language the path of the language dependen resource is returnd. If
116      * there's no resource for the current lanuage the default path is returned.
117      *
118      * @param resource Resource to load language dependen
119      * @return the language dependent path of the given resource
120      * @throws ResourceNotFoundException If the resource is not found
121      */

122     private String JavaDoc getLanguageResourceString(String JavaDoc resource) throws ResourceNotFoundException
123     {
124         InputStream JavaDoc in;
125         String JavaDoc resourcePath = this.resourceBasePath + resource + "_" + this.locale;
126         in = ResourceManager.class.getResourceAsStream(resourcePath);
127         if (in != null)
128             return resourcePath;
129         else
130         {
131             // if there's no language dependent resource found
132
resourcePath = this.resourceBasePath + resource;
133             in = ResourceManager.class.getResourceAsStream(resourcePath);
134             if (in != null)
135                 return resourcePath;
136             else
137                 throw new ResourceNotFoundException("Can not find Resource " + resource
138                         + " for language " + this.locale);
139         }
140     }
141
142     /**
143      * Returns an InputStream contains the given Resource The Resource is loaded language dependen
144      * by the informations from <code>this.locale</code> If there is no Resource for the current
145      * language found, the default Resource is given.
146      *
147      * @param resource The resource to load
148      * @return an InputStream contains the requested resource
149      * @exception ResourceNotFoundException Description of the Exception
150      * @throws ResourceNotFoundException thrown if there is no resource found
151      */

152     public InputStream JavaDoc getInputStream(String JavaDoc resource) throws ResourceNotFoundException
153     {
154         String JavaDoc resourcepath = this.getLanguageResourceString(resource);
155         // System.out.println ("reading resource "+resourcepath);
156
return ResourceManager.class.getResourceAsStream(resourcepath);
157     }
158
159     /**
160      * Returns a URL refers to the given Resource
161      *
162      * @param resource the resource to load
163      * @return A languagedependen URL spezifies the requested resource
164      * @exception ResourceNotFoundException Description of the Exception
165      * @throws ResourceNotFoundException thrown if there is no resource found
166      */

167     public URL JavaDoc getURL(String JavaDoc resource) throws ResourceNotFoundException
168     {
169         try
170         {
171             return this.getClass().getResource(
172                     this.getLanguageResourceString(resource + "_" + installData.localeISO3));
173         }
174         catch (Exception JavaDoc ex)
175         {
176             return this.getClass().getResource(this.getLanguageResourceString(resource));
177         }
178     }
179
180     /**
181      * Returns a text resource from the jar file. The resource is loaded by
182      * ResourceManager#getResource and then converted into text.
183      *
184      * @param resource - a text resource to load
185      * @param encoding - the encoding, which should be used to read the resource
186      * @return a String contains the text of the resource
187      * @throws ResourceNotFoundException if the resource can not be found
188      * @throws IOException if the resource can not be loaded
189      */

190     // Maybe we can add a text parser for this method
191
public String JavaDoc getTextResource(String JavaDoc resource, String JavaDoc encoding) throws ResourceNotFoundException, IOException JavaDoc
192     {
193         InputStream JavaDoc in = null;
194         try
195         {
196             in = this.getInputStream(resource + "_" + this.installData.localeISO3);
197         }
198         catch (Exception JavaDoc ex)
199         {
200             in = this.getInputStream(resource);
201         }
202
203         ByteArrayOutputStream JavaDoc infoData = new ByteArrayOutputStream JavaDoc();
204         byte[] buffer = new byte[5120];
205         int bytesInBuffer;
206         while ((bytesInBuffer = in.read(buffer)) != -1)
207             infoData.write(buffer, 0, bytesInBuffer);
208
209         if (encoding != null){
210             return infoData.toString(encoding);
211         }
212         else {
213             return infoData.toString();
214         }
215     }
216     
217     /**
218      * Returns a text resource from the jar file. The resource is loaded by
219      * ResourceManager#getResource and then converted into text.
220      *
221      * @param resource - a text resource to load
222      * @return a String contains the text of the resource
223      * @throws ResourceNotFoundException if the resource can not be found
224      * @throws IOException if the resource can not be loaded
225      */

226     // Maybe we can add a text parser for this method
227
public String JavaDoc getTextResource(String JavaDoc resource) throws ResourceNotFoundException, IOException JavaDoc
228     {
229        return this.getTextResource(resource,null);
230     }
231
232     /**
233      * Returns a laguage dependent ImageIcon for the given Resource
234      *
235      * @param resource resrouce of the Icon
236      * @return a ImageIcon loaded from the given Resource
237      * @throws ResourceNotFoundException thrown when the resource can not be found
238      * @throws IOException if the resource can not be loaded
239      */

240     public ImageIcon JavaDoc getImageIconResource(String JavaDoc resource) throws ResourceNotFoundException,
241             IOException JavaDoc
242     {
243         return new ImageIcon JavaDoc(this.getURL(resource));
244     }
245
246     /**
247      * Sets the locale for the resourcefiles. The locale is taken from
248      * InstallData#installData#getAttribute("langpack") If there is no language set, the default
249      * language is english.
250      *
251      * @param locale of the resourcefile
252      */

253     public void setLocale(String JavaDoc locale)
254     {
255         this.locale = locale;
256     }
257
258     /**
259      * Returns the locale for the resourcefiles. The locale is taken from
260      * InstallData#installData#getAttribute("langpack") If there is no language set, the default
261      * language is english.
262      *
263      * @return the current language
264      */

265     public String JavaDoc getLocale()
266     {
267         return this.locale;
268     }
269 }
270
Popular Tags