package net.sourceforge.uiloader.ui.swing.languages; /* * Program : UILoader * Package : net.sourceforge.uiloader.ui.swing.languages * File : LanguageManager.java * Created : Jan 15, 2005 * By : brian * * Description : Handles String Internationlization * * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details ( see the LICENSE file ). * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ import net.sourceforge.uiloader.core.Constants; import org.w3c.dom.*; import org.xml.sax.*; import javax.xml.parsers.*; import java.io.*; /** * @author brian * @version 1.0 */ public class LanguageManager { class InvalidPhraseException extends Exception { }; class InvalidLanguageException extends Exception { }; class InvalidLanguageVersionException extends Exception { }; class LanguageNotLoadedException extends Exception { }; private LanguageParser languageParser = new LanguageParser(); /** * @param phraseToGet The identifier of the phrase to retrive * @return A phrase from the current language file as a string * @throws InvalidPhraseException * @throws LanguageNotLoadedException */ public String getPhrase(String phraseToGet) throws InvalidPhraseException, LanguageNotLoadedException { String phrase = null; try { phrase = this.languageParser.getPhrase(phraseToGet); } catch (InvalidPhraseException e) { throw new InvalidPhraseException(); } catch (LanguageNotLoadedException e) { throw new LanguageNotLoadedException(); } return phrase; } /** * Interface method for loading language files * * @param languageFile The Language File (XML Format) to load * @throws InvalidLanguageException * @throws InvalidLanguageVersionException * */ public void loadLanguage(File languageFile) throws InvalidLanguageException, InvalidLanguageVersionException { try { this.languageParser.loadLanguage(languageFile); } catch (InvalidLanguageException e) { throw new InvalidLanguageException(); } catch (InvalidLanguageVersionException e) { throw new InvalidLanguageVersionException(); } } private class LanguageParser { private DocumentBuilderFactory xmlFactory = null; private DocumentBuilder xmlBuilder = null; private Element root = null; private NodeList langNodes = null; private Document parsedFile = null; private String currentLanguage = null; private String fileVersion = null; /** * Returns the name of the current language * * @return Name of current language as a string or null if none is loaded */ public String getCurrentLanguage() { return currentLanguage; } /** * @param phraseToGet The identifier of the phrase to retrive * @return A phrase from the current language file as a string * @throws InvalidPhraseException * @throws LanguageNotLoadedException */ public String getPhrase(String phraseToGet) throws InvalidPhraseException, LanguageNotLoadedException { if (xmlFactory == null) { throw new LanguageNotLoadedException(); } return phraseToGet; } /** * Loads an XML File containing phrases into the parser * * @param languageFile The XML File Containing Phrases To Use * @throws InvalidLanguageException * @throws InvalidLanguageVersionException * */ public void loadLanguage(File languageFile) throws InvalidLanguageException, InvalidLanguageVersionException { try { if (xmlFactory == null) { xmlFactory = DocumentBuilderFactory.newInstance(); xmlFactory.setValidating(true); xmlBuilder = xmlFactory.newDocumentBuilder(); } if (parsedFile != null) { parsedFile = null; } parsedFile = xmlBuilder.parse(languageFile); root = parsedFile.getDocumentElement(); if (root.getTagName() != "UILoaderLanguage") { throw new InvalidLanguageException(); } else { langNodes = root.getChildNodes(); fileVersion = langNodes.item(0).toString(); if (fileVersion != Constants.PROGRAM_VERSION) { throw new InvalidLanguageVersionException(); } } } catch (ParserConfigurationException e) { //TODO : This is a prototype. Will standerdize the error handling with a central class later System.out.println("A Parser Configuration Exception Occured"); System.out.println("This Usually Means You Are Running On An Old JRE"); System.out.println("or that your language data file is corrupt"); System.out.println("Please Update Your JRE To The Latest Version"); System.out.println("----------------------------------------------"); System.out.println("If you continue to recieve this error please send the following to dcatz@nc.rr.com"); System.out.println("----------------------------------------------"); System.out.println("Begin Stack Trace"); System.out.println(e.getStackTrace()); System.exit(10); } catch (IOException e) { throw new InvalidLanguageException(); } catch (SAXException e) { throw new InvalidLanguageException(); } } } }