xref: /AOO41X/main/javainstaller2/src/JavaSetup/org/openoffice/setup/ResourceManager.java (revision 67e470dafe1997e73f56ff7ff4878983707e3e07)
1*9a1eeea9SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*9a1eeea9SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*9a1eeea9SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*9a1eeea9SAndrew Rist  * distributed with this work for additional information
6*9a1eeea9SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*9a1eeea9SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*9a1eeea9SAndrew Rist  * "License"); you may not use this file except in compliance
9*9a1eeea9SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*9a1eeea9SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*9a1eeea9SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*9a1eeea9SAndrew Rist  * software distributed under the License is distributed on an
15*9a1eeea9SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9a1eeea9SAndrew Rist  * KIND, either express or implied.  See the License for the
17*9a1eeea9SAndrew Rist  * specific language governing permissions and limitations
18*9a1eeea9SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*9a1eeea9SAndrew Rist  *************************************************************/
21*9a1eeea9SAndrew Rist 
22*9a1eeea9SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package org.openoffice.setup;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import org.openoffice.setup.SetupData.SetupDataProvider;
27cdf0e10cSrcweir import java.io.File;
28cdf0e10cSrcweir import java.net.MalformedURLException;
29cdf0e10cSrcweir import java.net.URL;
30cdf0e10cSrcweir import java.util.Enumeration;
31cdf0e10cSrcweir import java.util.HashMap;
32cdf0e10cSrcweir import java.util.Locale;
33cdf0e10cSrcweir import java.util.MissingResourceException;
34cdf0e10cSrcweir import java.util.PropertyResourceBundle;
35cdf0e10cSrcweir import java.util.ResourceBundle;
36cdf0e10cSrcweir import javax.swing.ImageIcon;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir public class ResourceManager {
39cdf0e10cSrcweir 
40cdf0e10cSrcweir     static PropertyResourceBundle stringResourceBundle;
41cdf0e10cSrcweir     static PropertyResourceBundle fileNameResourceBundle;
42cdf0e10cSrcweir     static HashMap setupFiles = new HashMap();  // required, because it is not possible to set values in fileNameResourceBundle
43cdf0e10cSrcweir 
ResourceManager()44cdf0e10cSrcweir     private ResourceManager() {
45cdf0e10cSrcweir     }
46cdf0e10cSrcweir 
checkFileExistence(File htmlDirectory)47cdf0e10cSrcweir     static public void checkFileExistence(File htmlDirectory) {
48cdf0e10cSrcweir 
49cdf0e10cSrcweir         for (Enumeration e = fileNameResourceBundle.getKeys(); e.hasMoreElements(); ) {
50cdf0e10cSrcweir             String key = (String) e.nextElement();
51cdf0e10cSrcweir             String fileName = (String)(fileNameResourceBundle.getObject(key));
52cdf0e10cSrcweir 
53cdf0e10cSrcweir             if ( ! fileName.endsWith("html") ) {
54cdf0e10cSrcweir                 // no check of existence for non-html files
55cdf0e10cSrcweir                 setupFiles.put(key, fileName);
56cdf0e10cSrcweir                 // System.err.println("Using file: " + fileName);
57cdf0e10cSrcweir             }
58cdf0e10cSrcweir 
59cdf0e10cSrcweir             if ( fileName.endsWith("html") ) {
60cdf0e10cSrcweir                 boolean fileExists = true;
61cdf0e10cSrcweir 
62cdf0e10cSrcweir                 File file = new File(htmlDirectory, fileName);
63cdf0e10cSrcweir                 File newFile = null;
64cdf0e10cSrcweir 
65cdf0e10cSrcweir                 if ( file.exists() ) {
66cdf0e10cSrcweir                     setupFiles.put(key, fileName);
67cdf0e10cSrcweir                     // System.err.println("Using file: " + fileName);
68cdf0e10cSrcweir                 } else {
69cdf0e10cSrcweir                     fileExists = false;
70cdf0e10cSrcweir                     // try to use english version
71cdf0e10cSrcweir                     int pos1 = fileName.lastIndexOf("_");
72cdf0e10cSrcweir 
73cdf0e10cSrcweir                     if ( pos1 > 0 ) {
74cdf0e10cSrcweir                         int pos2 = fileName.lastIndexOf(".");
75cdf0e10cSrcweir                         String newFileName = fileName.substring(0, pos1) + fileName.substring(pos2, fileName.length());
76cdf0e10cSrcweir                         newFile = new File(htmlDirectory, newFileName);
77cdf0e10cSrcweir                         if ( newFile.exists() ) {
78cdf0e10cSrcweir                             fileExists = true;
79cdf0e10cSrcweir                             setupFiles.put(key, newFileName);
80cdf0e10cSrcweir                             // System.err.println("Using file: " + fileName);
81cdf0e10cSrcweir                         } else {
82cdf0e10cSrcweir                             // Introducing fallback to a very special simple html page
83cdf0e10cSrcweir                             String simplePage = "Excuse.html";
84cdf0e10cSrcweir                             File simpleFile = new File(htmlDirectory, simplePage);
85cdf0e10cSrcweir                             if ( simpleFile.exists() ) {
86cdf0e10cSrcweir                                 fileExists = true;
87cdf0e10cSrcweir                                 setupFiles.put(key, simplePage);
88cdf0e10cSrcweir                                 // System.err.println("Using file: " + fileName);
89cdf0e10cSrcweir                             }
90cdf0e10cSrcweir                         }
91cdf0e10cSrcweir                     }
92cdf0e10cSrcweir                 }
93cdf0e10cSrcweir 
94cdf0e10cSrcweir                 if ( ! fileExists ) {
95cdf0e10cSrcweir                     if ( newFile != null ) {
96cdf0e10cSrcweir                         System.err.println("ERROR: Neither file \"" + file.getPath() +
97cdf0e10cSrcweir                                            "\" nor file \"" + newFile.getPath() + "\" do exist!");
98cdf0e10cSrcweir                     } else {
99cdf0e10cSrcweir                         System.err.println("ERROR: File \"" + file.getPath() + "\" does not exist!");
100cdf0e10cSrcweir                     }
101cdf0e10cSrcweir                     System.exit(1);
102cdf0e10cSrcweir                 }
103cdf0e10cSrcweir             }
104cdf0e10cSrcweir         }
105cdf0e10cSrcweir     }
106cdf0e10cSrcweir 
getString(String key)107cdf0e10cSrcweir     static public String getString(String key) {
108cdf0e10cSrcweir         String value = (String)(stringResourceBundle.getObject(key));
109cdf0e10cSrcweir         if (value != null && (value.indexOf('$') >= 0)) {
110cdf0e10cSrcweir             value = SetupDataProvider.replaceMacros(value);
111cdf0e10cSrcweir         }
112cdf0e10cSrcweir         return value;
113cdf0e10cSrcweir     }
114cdf0e10cSrcweir 
getFileName(String key)115cdf0e10cSrcweir     static public String getFileName(String key) {
116cdf0e10cSrcweir         String value = (String)setupFiles.get(key);
117cdf0e10cSrcweir         // String value = (String)(fileNameResourceBundle.getObject(key));
118cdf0e10cSrcweir         return value;
119cdf0e10cSrcweir     }
120cdf0e10cSrcweir 
getIcon(String key)121cdf0e10cSrcweir     static public ImageIcon getIcon(String key) {
122cdf0e10cSrcweir 
123cdf0e10cSrcweir         String name = getFileName(key);
124cdf0e10cSrcweir 
125cdf0e10cSrcweir         try {
126cdf0e10cSrcweir             Class c = Class.forName("org.openoffice.setup.ResourceManager");
127cdf0e10cSrcweir             URL url = c.getResource(name);
128cdf0e10cSrcweir             if (url != null) {
129cdf0e10cSrcweir                 return new ImageIcon(url);
130cdf0e10cSrcweir             } else {
131cdf0e10cSrcweir                 System.err.println("Error: file not found: " + name);
132cdf0e10cSrcweir             }
133cdf0e10cSrcweir         } catch (ClassNotFoundException e) {
134cdf0e10cSrcweir             System.err.println(e);
135cdf0e10cSrcweir         }
136cdf0e10cSrcweir 
137cdf0e10cSrcweir         return new ImageIcon();
138cdf0e10cSrcweir     }
139cdf0e10cSrcweir 
getIconFromPath(File file)140cdf0e10cSrcweir     static public ImageIcon getIconFromPath(File file) {
141cdf0e10cSrcweir 
142cdf0e10cSrcweir         try {
143cdf0e10cSrcweir             URL url = file.toURL();
144cdf0e10cSrcweir             if (url != null) {
145cdf0e10cSrcweir                 return new ImageIcon(url);
146cdf0e10cSrcweir             } else {
147cdf0e10cSrcweir                 System.err.println("Error: file not found: " + file.getPath());
148cdf0e10cSrcweir             }
149cdf0e10cSrcweir         } catch (MalformedURLException e) {
150cdf0e10cSrcweir             System.err.println(e);
151cdf0e10cSrcweir         }
152cdf0e10cSrcweir 
153cdf0e10cSrcweir         return new ImageIcon();
154cdf0e10cSrcweir     }
155cdf0e10cSrcweir 
156cdf0e10cSrcweir     static {
157cdf0e10cSrcweir         Locale locale = Locale.getDefault();
158cdf0e10cSrcweir         System.err.println("System locale: " + locale );
159cdf0e10cSrcweir         try {
160cdf0e10cSrcweir             stringResourceBundle = (PropertyResourceBundle) ResourceBundle.getBundle("org.openoffice.setup.setupstrings", locale);
161cdf0e10cSrcweir             fileNameResourceBundle = (PropertyResourceBundle) ResourceBundle.getBundle("org.openoffice.setup.setupfiles", locale);
162cdf0e10cSrcweir         } catch (MissingResourceException ex) {
163cdf0e10cSrcweir             ex.printStackTrace();
164cdf0e10cSrcweir             System.exit(1);
165cdf0e10cSrcweir         }
166cdf0e10cSrcweir     }
167cdf0e10cSrcweir }
168