Code with Finding: |
class JigsawHttpServletRequest { /** * Returns an <code>Enumeration</code> of <code>Locale</code> objects * indicating, in decreasing order starting with the preferred locale, the * locales that are acceptable to the client based on the Accept-Language * header. * If the client request doesn't provide an Accept-Language header, * this method returns an <code>Enumeration</code> containing one * <code>Locale</code>, the default locale for the server. * * @return an <code>Enumeration</code> of preferred * <code>Locale</code> objects for the client */ public Enumeration getLocales() { HttpAcceptLanguage languages[] = request.getAcceptLanguage(); if (languages == null) { Vector def = new Vector(); def.addElement(Locale.getDefault()); return def.elements(); }
//LinkedList is better, but we must be JDK1.1 compliant Vector locales = new Vector();
for (int i = 0 ; i < languages.length ; i++) { HttpAcceptLanguage language = languages[i]; double quality = language.getQuality(); String lang = language.getLanguage(); String country = ""; int idx = lang.indexOf('-'); if (idx > -1) { country = lang.substring(idx + 1).trim(); lang = lang.substring(0, idx).trim(); } // insert the Locale in ordered list int qidx = 0; int size = locales.size(); if (size > 0) { QLocale ql = (QLocale) locales.firstElement(); while ((qidx < size) && (ql.getLanguageQuality() >= quality)) { try { ql = (QLocale) locales.elementAt(++qidx); } catch (ArrayIndexOutOfBoundsException ex) { //end of vector, so append } } locales.insertElementAt(new QLocale(lang, country, quality), qidx); } else { locales.addElement(new QLocale(lang, country, quality)); } } // because Locale is final :( int size = locales.size(); Vector vlocale = new Vector(size); for (int i = 0 ; i < size ; i ++) { vlocale.addElement(((QLocale)locales.elementAt(i)).getLocale()); } return vlocale.elements(); }
}
|