Source Code Cross Referenced for ResourceManager.java in  » Installer » IzPack » com » izforge » izpack » installer » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Installer » IzPack » com.izforge.izpack.installer 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
003:         * 
004:         * http://izpack.org/
005:         * http://izpack.codehaus.org/
006:         * 
007:         * Copyright 2002 Marcus Stursberg
008:         * 
009:         * Licensed under the Apache License, Version 2.0 (the "License");
010:         * you may not use this file except in compliance with the License.
011:         * You may obtain a copy of the License at
012:         * 
013:         *     http://www.apache.org/licenses/LICENSE-2.0
014:         *     
015:         * Unless required by applicable law or agreed to in writing, software
016:         * distributed under the License is distributed on an "AS IS" BASIS,
017:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018:         * See the License for the specific language governing permissions and
019:         * limitations under the License.
020:         */
021:
022:        package com.izforge.izpack.installer;
023:
024:        import java.io.ByteArrayOutputStream;
025:        import java.io.IOException;
026:        import java.io.InputStream;
027:        import java.net.URL;
028:
029:        import javax.swing.ImageIcon;
030:
031:        /**
032:         * With this ResourceManager you are able to get resources from the jar file.
033:         * 
034:         * All resources are loaded language dependent as it's done in java.util.ResourceBundle. To set a
035:         * language dependent resource just append '_' and the locale to the end of the Resourcename<br>
036:         * <br>
037:         * Example:
038:         * <li> InfoPanel.info - for default value</li>
039:         * <li> InfoPanel.info_deu - for german value</li>
040:         * <li> InfoPanel.info_eng - for english value</li>
041:         * <br>
042:         * 
043:         * This class is almost a singleton. It is created once using <code>create</code> by the installer
044:         * and later, the instance is retrieved using <code>getInstance</code>.
045:         * 
046:         * @author Marcus Stursberg
047:         */
048:        public class ResourceManager {
049:
050:            /**
051:             * Contains the current language of the installer The locale is taken from
052:             * InstallData#installData#getAttribute("langpack") If there is no language set, the language is
053:             * english.
054:             */
055:            private String locale = "";
056:
057:            /** The base path where to find the resources */
058:            protected final String resourceBasePath = "/res/";
059:
060:            /** Contains the given InstallData */
061:            private AutomatedInstallData installData;
062:
063:            /** The instance of this class. */
064:            private static ResourceManager instance = null;
065:
066:            /**
067:             * Constructor. Protected because this is a singleton.
068:             * 
069:             * @param data - the current installData
070:             */
071:            protected ResourceManager(AutomatedInstallData data) {
072:                this .installData = data;
073:                if (data.localeISO3 != null) {
074:                    this .locale = data.localeISO3;
075:                } else {
076:                    // try to figure out ourself
077:                    this .locale = installData.xmlData.getAttribute("langpack",
078:                            "eng");
079:                }
080:            }
081:
082:            /**
083:             * Create the resource manager.
084:             * 
085:             * This method should be called only once. If it is called a second time, the already existing
086:             * instance is returned. The resource manager should be called <b>after</b> the language has
087:             * been set in {@link AutomatedInstallData#localeISO3}
088:             * 
089:             * @param data the installation information
090:             * @return the created instance
091:             */
092:            public static ResourceManager create(AutomatedInstallData data) {
093:                if (ResourceManager.instance == null)
094:                    ResourceManager.instance = new ResourceManager(data);
095:
096:                return ResourceManager.instance;
097:            }
098:
099:            /**
100:             * Return the resource manager.
101:             * 
102:             * @return the resource manager instance, null if no instance has been created
103:             */
104:            public static ResourceManager getInstance() {
105:                return ResourceManager.instance;
106:            }
107:
108:            /**
109:             * This method is used to get the language dependent path of the given resource. If there is a
110:             * resource for the current language the path of the language dependen resource is returnd. If
111:             * there's no resource for the current lanuage the default path is returned.
112:             * 
113:             * @param resource Resource to load language dependen
114:             * @return the language dependent path of the given resource
115:             * @throws ResourceNotFoundException If the resource is not found
116:             */
117:            private String getLanguageResourceString(String resource)
118:                    throws ResourceNotFoundException {
119:                InputStream in;
120:                String resourcePath = this .resourceBasePath + resource + "_"
121:                        + this .locale;
122:                in = ResourceManager.class.getResourceAsStream(resourcePath);
123:                if (in != null)
124:                    return resourcePath;
125:                else {
126:                    // if there's no language dependent resource found
127:                    resourcePath = this .resourceBasePath + resource;
128:                    in = ResourceManager.class
129:                            .getResourceAsStream(resourcePath);
130:                    if (in != null)
131:                        return resourcePath;
132:                    else
133:                        throw new ResourceNotFoundException(
134:                                "Can not find Resource " + resource
135:                                        + " for language " + this .locale);
136:                }
137:            }
138:
139:            /**
140:             * Returns an InputStream contains the given Resource The Resource is loaded language dependen
141:             * by the informations from <code>this.locale</code> If there is no Resource for the current
142:             * language found, the default Resource is given.
143:             * 
144:             * @param resource The resource to load
145:             * @return an InputStream contains the requested resource
146:             * @exception ResourceNotFoundException Description of the Exception
147:             * @throws ResourceNotFoundException thrown if there is no resource found
148:             */
149:            public InputStream getInputStream(String resource)
150:                    throws ResourceNotFoundException {
151:                String resourcepath = this .getLanguageResourceString(resource);
152:                // System.out.println ("reading resource "+resourcepath);
153:                return ResourceManager.class.getResourceAsStream(resourcepath);
154:            }
155:
156:            /**
157:             * Returns a URL refers to the given Resource
158:             * 
159:             * @param resource the resource to load
160:             * @return A languagedependen URL spezifies the requested resource
161:             * @exception ResourceNotFoundException Description of the Exception
162:             * @throws ResourceNotFoundException thrown if there is no resource found
163:             */
164:            public URL getURL(String resource) throws ResourceNotFoundException {
165:                try {
166:                    return this .getClass().getResource(
167:                            this .getLanguageResourceString(resource + "_"
168:                                    + installData.localeISO3));
169:                } catch (Exception ex) {
170:                    return this .getClass().getResource(
171:                            this .getLanguageResourceString(resource));
172:                }
173:            }
174:
175:            /**
176:             * Returns a text resource from the jar file. The resource is loaded by
177:             * ResourceManager#getResource and then converted into text.
178:             * 
179:             * @param resource - a text resource to load
180:             * @param encoding - the encoding, which should be used to read the resource
181:             * @return a String contains the text of the resource
182:             * @throws ResourceNotFoundException if the resource can not be found
183:             * @throws IOException if the resource can not be loaded
184:             */
185:            // Maybe we can add a text parser for this method
186:            public String getTextResource(String resource, String encoding)
187:                    throws ResourceNotFoundException, IOException {
188:                InputStream in = null;
189:                try {
190:                    in = this .getInputStream(resource + "_"
191:                            + this .installData.localeISO3);
192:                } catch (Exception ex) {
193:                    in = this .getInputStream(resource);
194:                }
195:
196:                ByteArrayOutputStream infoData = new ByteArrayOutputStream();
197:                byte[] buffer = new byte[5120];
198:                int bytesInBuffer;
199:                while ((bytesInBuffer = in.read(buffer)) != -1)
200:                    infoData.write(buffer, 0, bytesInBuffer);
201:
202:                if (encoding != null) {
203:                    return infoData.toString(encoding);
204:                } else {
205:                    return infoData.toString();
206:                }
207:            }
208:
209:            /**
210:             * Returns a text resource from the jar file. The resource is loaded by
211:             * ResourceManager#getResource and then converted into text.
212:             * 
213:             * @param resource - a text resource to load
214:             * @return a String contains the text of the resource
215:             * @throws ResourceNotFoundException if the resource can not be found
216:             * @throws IOException if the resource can not be loaded
217:             */
218:            // Maybe we can add a text parser for this method
219:            public String getTextResource(String resource)
220:                    throws ResourceNotFoundException, IOException {
221:                return this .getTextResource(resource, null);
222:            }
223:
224:            /**
225:             * Returns a laguage dependent ImageIcon for the given Resource
226:             * 
227:             * @param resource resrouce of the Icon
228:             * @return a ImageIcon loaded from the given Resource
229:             * @throws ResourceNotFoundException thrown when the resource can not be found
230:             * @throws IOException if the resource can not be loaded
231:             */
232:            public ImageIcon getImageIconResource(String resource)
233:                    throws ResourceNotFoundException, IOException {
234:                return new ImageIcon(this .getURL(resource));
235:            }
236:
237:            /**
238:             * Sets the locale for the resourcefiles. The locale is taken from
239:             * InstallData#installData#getAttribute("langpack") If there is no language set, the default
240:             * language is english.
241:             * 
242:             * @param locale of the resourcefile
243:             */
244:            public void setLocale(String locale) {
245:                this .locale = locale;
246:            }
247:
248:            /**
249:             * Returns the locale for the resourcefiles. The locale is taken from
250:             * InstallData#installData#getAttribute("langpack") If there is no language set, the default
251:             * language is english.
252:             * 
253:             * @return the current language
254:             */
255:            public String getLocale() {
256:                return this.locale;
257:            }
258:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.