/** Returns the next language in the list or null if there aren't any. */ public String getNextLanguage() { _ndx++; if (_ndx >= _list.size()) return null; Lang l = (Lang) _list.elementAt(_ndx); return l.lang; }
/** * Constructs a new LanguagePreference object. The passed string should be an http Accept-Language * header. */ public LanguagePreferences(String langString) { Hashtable specific = new Hashtable(); StringTokenizer t = new StringTokenizer(langString, ","); float ndx = 0.000099F; while (t.hasMoreTokens()) { String tok = t.nextToken().trim(); String lang = null; float weight = 1.0F; int pos = tok.indexOf(';'); if (pos == -1) lang = tok; else { lang = tok.substring(0, pos); pos = tok.indexOf('='); if (pos > -1) { String work = tok.substring(pos + 1); try { weight = Float.parseFloat(work); } catch (Exception e) { } ; } } if (lang == null) continue; Lang l = new Lang(); l.lang = lang.toLowerCase(); l.weight = weight + ndx; ndx -= 0.000001F; _list.add(l); pos = lang.indexOf("-"); if (pos == -1) specific.put(lang, lang); } _list.sort(); int size = _list.size(); for (int i = 0; i < size; i++) { Lang l = (Lang) _list.elementAt(i); String lang = l.lang; int pos = lang.indexOf('-'); if (pos > -1) { lang = lang.substring(0, pos); if (!specific.containsKey(lang)) { l = new Lang(); l.lang = lang; _list.add(l); specific.put(lang, lang); } } } }