Better, Faster, Freer

The LXR Cross Referencer

source navigation ]
diff markup ]
identifier search ]
general search ]
 
 
Architecture: i386 ]
Version: HEAD ]

001 /*************************************************************************
002  *
003  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004  * 
005  * Copyright 2008 by Sun Microsystems, Inc.
006  *
007  * OpenOffice.org - a multi-platform office productivity suite
008  *
009  * $RCSfile: ResourceManager.java,v $
010  * $Revision: 1.3 $
011  *
012  * This file is part of OpenOffice.org.
013  *
014  * OpenOffice.org is free software: you can redistribute it and/or modify
015  * it under the terms of the GNU Lesser General Public License version 3
016  * only, as published by the Free Software Foundation.
017  *
018  * OpenOffice.org is distributed in the hope that it will be useful,
019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
021  * GNU Lesser General Public License version 3 for more details
022  * (a copy is included in the LICENSE file that accompanied this code).
023  *
024  * You should have received a copy of the GNU Lesser General Public License
025  * version 3 along with OpenOffice.org.  If not, see
026  * <http://www.openoffice.org/license.html>
027  * for a copy of the LGPLv3 License.
028  *
029  ************************************************************************/
030 
031 package org.openoffice.setup;
032 
033 import org.openoffice.setup.SetupData.SetupDataProvider;
034 import java.io.File;
035 import java.net.MalformedURLException;
036 import java.net.URL;
037 import java.util.Enumeration;
038 import java.util.HashMap;
039 import java.util.Locale;
040 import java.util.MissingResourceException;
041 import java.util.PropertyResourceBundle;
042 import java.util.ResourceBundle;
043 import javax.swing.ImageIcon;
044 
045 public class ResourceManager {
046     
047     static PropertyResourceBundle stringResourceBundle;
048     static PropertyResourceBundle fileNameResourceBundle;
049     static HashMap setupFiles = new HashMap();  // required, because it is not possible to set values in fileNameResourceBundle
050     
051     private ResourceManager() {
052     }
053     
054     static public void checkFileExistence(File htmlDirectory) {
055 
056         for (Enumeration e = fileNameResourceBundle.getKeys(); e.hasMoreElements(); ) {
057             String key = (String) e.nextElement();            
058             String fileName = (String)(fileNameResourceBundle.getObject(key));
059 
060             if ( ! fileName.endsWith("html") ) {
061                 // no check of existence for non-html files
062                 setupFiles.put(key, fileName);            
063                 // System.err.println("Using file: " + fileName);
064             }
065 
066             if ( fileName.endsWith("html") ) {
067                 boolean fileExists = true;
068             
069                 File file = new File(htmlDirectory, fileName);
070                 File newFile = null;
071 
072                 if ( file.exists() ) {
073                     setupFiles.put(key, fileName);                
074                     // System.err.println("Using file: " + fileName);
075                 } else {
076                     fileExists = false;
077                     // try to use english version
078                     int pos1 = fileName.lastIndexOf("_");
079                 
080                     if ( pos1 > 0 ) {
081                         int pos2 = fileName.lastIndexOf(".");
082                         String newFileName = fileName.substring(0, pos1) + fileName.substring(pos2, fileName.length());
083                         newFile = new File(htmlDirectory, newFileName);
084                         if ( newFile.exists() ) {
085                             fileExists = true;
086                             setupFiles.put(key, newFileName);                
087                             // System.err.println("Using file: " + fileName);
088                         } else {
089                             // Introducing fallback to a very special simple html page
090                             String simplePage = "Excuse.html";
091                             File simpleFile = new File(htmlDirectory, simplePage);
092                             if ( simpleFile.exists() ) {
093                                 fileExists = true;
094                                 setupFiles.put(key, simplePage);
095                                 // System.err.println("Using file: " + fileName);
096                             }                
097                         }
098                     }
099                 }
100             
101                 if ( ! fileExists ) {
102                     if ( newFile != null ) {
103                         System.err.println("ERROR: Neither file \"" + file.getPath() +
104                                            "\" nor file \"" + newFile.getPath() + "\" do exist!");
105                     } else {
106                         System.err.println("ERROR: File \"" + file.getPath() + "\" does not exist!");
107                     }
108                     System.exit(1);
109                 }
110             }      
111         }   
112     }
113     
114     static public String getString(String key) {
115         String value = (String)(stringResourceBundle.getObject(key));
116         if (value != null && (value.indexOf('$') >= 0)) {
117             value = SetupDataProvider.replaceMacros(value);
118         }
119         return value;
120     }
121 
122     static public String getFileName(String key) {
123         String value = (String)setupFiles.get(key);
124         // String value = (String)(fileNameResourceBundle.getObject(key));
125         return value;
126     }
127     
128     static public ImageIcon getIcon(String key) {
129         
130         String name = getFileName(key);
131         
132         try {
133             Class c = Class.forName("org.openoffice.setup.ResourceManager");
134             URL url = c.getResource(name);
135             if (url != null) {
136                 return new ImageIcon(url);
137             } else {
138                 System.err.println("Error: file not found: " + name);
139             }             
140         } catch (ClassNotFoundException e) {
141             System.err.println(e);
142         }
143 
144         return new ImageIcon();
145     }
146 
147     static public ImageIcon getIconFromPath(File file) {
148         
149         try {
150             URL url = file.toURL();
151             if (url != null) {
152                 return new ImageIcon(url);
153             } else {
154                 System.err.println("Error: file not found: " + file.getPath());
155             }             
156         } catch (MalformedURLException e) {
157             System.err.println(e);
158         }
159 
160         return new ImageIcon();
161     }
162 
163     static {
164         Locale locale = Locale.getDefault();
165         System.err.println("System locale: " + locale );
166         try {
167             stringResourceBundle = (PropertyResourceBundle) ResourceBundle.getBundle("org.openoffice.setup.setupstrings", locale);
168             fileNameResourceBundle = (PropertyResourceBundle) ResourceBundle.getBundle("org.openoffice.setup.setupfiles", locale);
169         } catch (MissingResourceException ex) {
170             ex.printStackTrace();
171             System.exit(1);
172         }
173     }
174 }

source navigation ] diff markup ] identifier search ] general search ]

This page was automatically generated by the LXR engine.
The LXR team
Valid HTML 4.01!