001: //** Copyright Statement ***************************************************
002: //The Salmon Open Framework for Internet Applications (SOFIA)
003: // Copyright (C) 1999 - 2002, Salmon LLC
004: //
005: // This program is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU General Public License version 2
007: // as published by the Free Software Foundation;
008: //
009: // This program is distributed in the hope that it will be useful,
010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: // GNU General Public License for more details.
013: //
014: // You should have received a copy of the GNU General Public License
015: // along with this program; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: //
018: // For more information please visit http://www.salmonllc.com
019: //** End Copyright Statement ***************************************************
020:
021: package com.salmonllc.localizer;
022:
023: /////////////////////////
024: //$Archive: /SOFIA/SourceCode/com/salmonllc/localizer/PropertiesLocalizer.java $
025: //$Author: Dan $
026: //$Revision: 9 $
027: //$Modtime: 8/24/04 1:03p $
028: /////////////////////////
029:
030: import java.io.*;
031:
032: import com.salmonllc.properties.Props;
033: import com.salmonllc.util.MessageLog;
034:
035: /**
036: * The Properties Localizer is a concrete implementation of the Localizer class. This will load the localization keys from a property file in the salmon.props.path
037: */
038: public class PropertiesLocalizer extends Localizer {
039:
040: private String _charSet;
041:
042: /**
043: * Constructs a new Localizer
044: */
045: public PropertiesLocalizer() {
046: this ("8859_1", false);
047: }
048:
049: /**
050: * Constructs a new Localizer using properties files of a particular character set
051: * @param charSet The character set the property files will be in
052: * @param translateEscapes A boolean flag indicating whether or not to translate escape characters into UniCode
053: */
054: public PropertiesLocalizer(String charSet, boolean translateEscapes) {
055: super (true, translateEscapes);
056: _charSet = charSet;
057: }
058:
059: /**
060: * Loads the data from a property file in the Salmon.props.path.
061: * The file name must be in the form appName.lang.properties where appName and lang are the properties passed to the method.
062: * If the file appName.lang.properties is not found, it will attempt to load a file called lang.properties.
063: * The method returns true if it succeeds and false if not.
064: */
065: protected boolean loadData(String appName, String language) {
066:
067: try {
068: String propsPath = Props.getPropsPath();
069: File f = new File(propsPath + File.separatorChar + appName
070: + "." + language + ".properties");
071: if (!f.exists())
072: f = new File(propsPath + File.separatorChar + language
073: + ".properties");
074: if (!f.exists())
075: return false;
076: BufferedReader in = new BufferedReader(
077: new InputStreamReader(new FileInputStream(f),
078: _charSet));
079: String line = in.readLine();
080: int pos = 0;
081: while (line != null) {
082: if (line.length() > 0 && line.charAt(0) != '#') {
083: pos = line.indexOf('=');
084: if (pos > -1) {
085: String key = line.substring(0, pos);
086: String val = line.substring(pos + 1);
087: addValue(key, val);
088: }
089: }
090: line = in.readLine();
091: }
092: in.close();
093: return true;
094: } catch (Exception e) {
095: MessageLog.writeErrorMessage("loadData", e, this );
096: }
097:
098: return false;
099: }
100:
101: /**
102: * @returns the character set
103: */
104: public String getCharSet() {
105: return _charSet;
106: }
107:
108: }
|