001: package com.salmonllc.localizer;
002:
003: //** Copyright Statement ***************************************************
004: //The Salmon Open Framework for Internet Applications (SOFIA)
005: // Copyright (C) 1999 - 2002, Salmon LLC
006: //
007: // This program is free software; you can redistribute it and/or
008: // modify it under the terms of the GNU General Public License version 2
009: // as published by the Free Software Foundation;
010: //
011: // This program is distributed in the hope that it will be useful,
012: // but WITHOUT ANY WARRANTY; without even the implied warranty of
013: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: // GNU General Public License for more details.
015: //
016: // You should have received a copy of the GNU General Public License
017: // along with this program; if not, write to the Free Software
018: // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: //
020: // For more information please visit http://www.salmonllc.com
021: //** End Copyright Statement ***************************************************
022:
023: /////////////////////////
024: //$Archive: /SOFIA/SourceCode/com/salmonllc/localizer/LanguagePreferences.java $
025: //$Author: Srufle $
026: //$Revision: 6 $
027: //$Modtime: 4/15/03 2:24p $
028: /////////////////////////
029: import java.io.Serializable;
030: import java.util.Hashtable;
031: import java.util.StringTokenizer;
032:
033: import com.salmonllc.util.VectorSort;
034:
035: /**
036: * This class is used to parse an HTTP language preferences string to determine which languages the user prefers
037: */
038: public class LanguagePreferences implements Serializable {
039: private LangList _list = new LangList();
040: private int _ndx = 0;
041:
042: private class Lang implements Serializable {
043: String lang;
044: float weight;
045: }
046:
047: private class LangList extends VectorSort implements Serializable {
048: public boolean compare(Object o1, Object o2) {
049: Lang l1 = (Lang) o1;
050: Lang l2 = (Lang) o2;
051: return l1.weight > l2.weight;
052: }
053: }
054:
055: /**
056: * Constructs a new LanguagePreference object. The passed string should be an http Accept-Language header.
057: */
058: public LanguagePreferences(String langString) {
059: Hashtable specific = new Hashtable();
060: StringTokenizer t = new StringTokenizer(langString, ",");
061: float ndx = 0.000099F;
062: while (t.hasMoreTokens()) {
063: String tok = t.nextToken().trim();
064: String lang = null;
065: float weight = 1.0F;
066: int pos = tok.indexOf(';');
067: if (pos == -1)
068: lang = tok;
069: else {
070: lang = tok.substring(0, pos);
071: pos = tok.indexOf('=');
072: if (pos > -1) {
073: String work = tok.substring(pos + 1);
074: try {
075: weight = Float.parseFloat(work);
076: } catch (Exception e) {
077: }
078: ;
079: }
080: }
081: if (lang == null)
082: continue;
083: Lang l = new Lang();
084: l.lang = lang.toLowerCase();
085: l.weight = weight + ndx;
086: ndx -= 0.000001F;
087: _list.add(l);
088: pos = lang.indexOf("-");
089: if (pos == -1)
090: specific.put(lang, lang);
091: }
092: _list.sort();
093:
094: int size = _list.size();
095: for (int i = 0; i < size; i++) {
096: Lang l = (Lang) _list.elementAt(i);
097: String lang = l.lang;
098: int pos = lang.indexOf('-');
099: if (pos > -1) {
100: lang = lang.substring(0, pos);
101: if (!specific.containsKey(lang)) {
102: l = new Lang();
103: l.lang = lang;
104: _list.add(l);
105: specific.put(lang, lang);
106: }
107: }
108: }
109: }
110:
111: /**
112: * Returns the first language in the list or null if there aren't any
113: */
114: public String getFirstLanguage() {
115: _ndx = 0;
116: if (_ndx >= _list.size())
117: return null;
118:
119: Lang l = (Lang) _list.elementAt(_ndx);
120: return l.lang;
121: }
122:
123: /**
124: * Returns the next language in the list or null if there aren't any.
125: */
126: public String getNextLanguage() {
127: _ndx++;
128: if (_ndx >= _list.size())
129: return null;
130:
131: Lang l = (Lang) _list.elementAt(_ndx);
132: return l.lang;
133: }
134: }
|