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