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: package com.salmonllc.localizer;
021:
022: import java.util.Enumeration;
023:
024: import com.salmonllc.util.VectorSort;
025:
026: /////////////////////////
027: //$Archive: /SOFIA/SourceCode/com/salmonllc/localizer/Localizer.java $
028: //$Author: Dan $
029: //$Revision: 7 $
030: //$Modtime: 1/16/03 2:31p $
031: /////////////////////////
032:
033: /**
034: * This class is the base class for all localizers in the framework. A localizer is a class used to load resources for a particular language and applicaton
035: */
036: public abstract class Localizer {
037:
038: private KeyList _list = new KeyList();
039: private boolean _needsSort = false;
040: private boolean _doSort = false;
041: private boolean _translateEscapes = false;
042:
043: /**
044: * Creates a new Localizer object. If elements added to the list will be sorted, pass a false as sortRequired and this will allow the class to skip the sorting step before searching for the first resource.
045: */
046: public Localizer(boolean sortRequired) {
047: _doSort = sortRequired;
048: _needsSort = sortRequired;
049: _translateEscapes = false;
050: }
051:
052: /**
053: * Creates a new Localizer object. If elements added to the list will be sorted, pass a false as sortRequired and this will allow the class to skip the sorting step before searching for the first resource. You can also control whether or not escape sequences are translated to unicode characters with the translateEscapes flag (default = false).
054: */
055: public Localizer(boolean sortRequired, boolean translateEscapes) {
056: this (sortRequired);
057: _translateEscapes = translateEscapes;
058: }
059:
060: private class LocaleValue {
061: public String key;
062: public String value;
063:
064: public LocaleValue(String key, String value) {
065: this .key = key;
066: this .value = value;
067: }
068:
069: public String toString() {
070: return key + " " + value;
071: }
072: }
073:
074: private class KeyList extends VectorSort {
075: public boolean compare(Object o1, Object o2) {
076: String key1 = ((LocaleValue) o1).key;
077: String key2 = ((LocaleValue) o2).key;
078: if (key1 == null || key2 == null)
079: return false;
080: else
081: return key1.compareTo(key2) < 0;
082: }
083:
084: private int getValueIndex(String key) {
085: if (key == null)
086: return -1;
087: else if (size() < 1)
088: return -1;
089:
090: int start = 0;
091: int end = size() - 1;
092: int comp = -1;
093: while (comp != 0) {
094: int mid = (start + end) / 2;
095: comp = key
096: .compareTo(((LocaleValue) elementAt(mid)).key);
097: if (comp == 0)
098: return mid;
099: else {
100: if (start >= end)
101: return -1;
102: else if (comp > 0)
103: start = mid + 1;
104: else
105: end = mid - 1;
106: }
107: }
108: return -1;
109: }
110:
111: }
112:
113: private class LocaleEnum implements Enumeration {
114:
115: private Enumeration _listEnum;
116: private boolean _keys;
117:
118: private LocaleEnum(KeyList l, boolean keys) {
119: _listEnum = l.elements();
120: _keys = keys;
121: }
122:
123: public boolean hasMoreElements() {
124: return _listEnum.hasMoreElements();
125: }
126:
127: public Object nextElement() {
128: LocaleValue o = (LocaleValue) _listEnum.nextElement();
129: if (o == null)
130: return null;
131: else
132: return _keys ? o.key : o.value;
133: }
134:
135: }
136:
137: /**
138: * Adds a value to the list
139: */
140: protected synchronized void addValue(String key, String value) {
141: if (key != null && value != null) {
142: if (_translateEscapes == true)
143: value = convert(value);
144: _list.addElement(new LocaleValue(key, value));
145: _needsSort = _doSort;
146: }
147: }
148:
149: //converts slash characters to double byte
150: private String convert(String theString) {
151: char aChar;
152: int len = theString.length();
153: StringBuffer outBuffer = new StringBuffer(len);
154:
155: for (int x = 0; x < len;) {
156: aChar = theString.charAt(x++);
157: if (aChar == '\\') {
158: aChar = theString.charAt(x++);
159: if (aChar == 'u') {
160: // Read the xxxx
161: int value = 0;
162: for (int i = 0; i < 4; i++) {
163: aChar = theString.charAt(x++);
164: switch (aChar) {
165: case '0':
166: case '1':
167: case '2':
168: case '3':
169: case '4':
170: case '5':
171: case '6':
172: case '7':
173: case '8':
174: case '9':
175: value = (value << 4) + aChar - '0';
176: break;
177: case 'a':
178: case 'b':
179: case 'c':
180: case 'd':
181: case 'e':
182: case 'f':
183: value = (value << 4) + 10 + aChar - 'a';
184: break;
185: case 'A':
186: case 'B':
187: case 'C':
188: case 'D':
189: case 'E':
190: case 'F':
191: value = (value << 4) + 10 + aChar - 'A';
192: break;
193: default:
194: throw new IllegalArgumentException(
195: "Malformed \\uxxxx encoding.");
196: }
197: }
198: outBuffer.append((char) value);
199: } else {
200: if (aChar == 't')
201: aChar = '\t';
202: else if (aChar == 'r')
203: aChar = '\r';
204: else if (aChar == 'n')
205: aChar = '\n';
206: else if (aChar == 'f')
207: aChar = '\f';
208: outBuffer.append(aChar);
209: }
210: } else
211: outBuffer.append(aChar);
212: }
213: return outBuffer.toString();
214: }
215:
216: /**
217: * Returns the number of resources in the localizer
218: */
219: public int getSize() {
220: return _list.size();
221: }
222:
223: /**
224: * Returns the value at the specified key or null if no value is found
225: */
226: public String getResource(String key) {
227: if (key != null) {
228: if (_needsSort) {
229: _list.sort();
230: _needsSort = false;
231: }
232: int index = _list.getValueIndex(key);
233: if (index == -1)
234: return null;
235: else
236: return ((LocaleValue) _list.elementAt(index)).value;
237: } else
238: return null;
239: }
240:
241: /**
242: * Returns an enumeration of keys in the list
243: */
244: public synchronized Enumeration getKeys() {
245: if (_needsSort) {
246: _needsSort = _doSort;
247: _list.sort();
248: }
249: return new LocaleEnum(_list, true);
250: }
251:
252: /**
253: * Returns an enumeration of resources in the list
254: */
255: public synchronized Enumeration getResources() {
256: if (_needsSort) {
257: _needsSort = _doSort;
258: _list.sort();
259: }
260: return new LocaleEnum(_list, false);
261: }
262:
263: /**
264: * This method must be implemented by subclasses of the Localizer to load the data for one application and language
265: */
266: protected abstract boolean loadData(String appName, String language);
267:
268: }
|