| 1 |
|
package com.atlassian.core.util; |
| 2 |
|
|
| 3 |
|
import com.opensymphony.util.TextUtils; |
| 4 |
|
import org.apache.log4j.Category; |
| 5 |
|
import org.w3c.dom.Document; |
| 6 |
|
import org.w3c.dom.Element; |
| 7 |
|
import org.xml.sax.SAXException; |
| 8 |
|
|
| 9 |
|
import javax.xml.parsers.DocumentBuilder; |
| 10 |
|
import javax.xml.parsers.DocumentBuilderFactory; |
| 11 |
|
import javax.xml.parsers.ParserConfigurationException; |
| 12 |
|
import java.io.IOException; |
| 13 |
|
import java.net.URL; |
| 14 |
|
import java.util.*; |
| 15 |
|
|
|
|
|
| 0% |
Uncovered Elements: 37 (37) |
Complexity: 10 |
Complexity Density: 0.4 |
|
| 16 |
|
public class LocaleUtils |
| 17 |
|
{ |
| 18 |
|
private static final Category log = Category.getInstance(LocaleUtils.class); |
| 19 |
|
|
| 20 |
|
public static final String LANGUAGE_DESCRIPTOR_FILENAME = "language-descriptor.xml"; |
| 21 |
|
public static final String LOCALE_TAG_NAME = "locale"; |
| 22 |
|
|
| 23 |
|
public List installedLocales; |
| 24 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
|
@link |
| 29 |
|
@link |
| 30 |
|
|
| 31 |
|
@throws |
| 32 |
|
|
|
|
|
| 0% |
Uncovered Elements: 25 (25) |
Complexity: 7 |
Complexity Density: 0.37 |
|
| 33 |
0
|
public List getInstalledLocales() throws IOException... |
| 34 |
|
{ |
| 35 |
0
|
if (installedLocales == null) |
| 36 |
|
{ |
| 37 |
0
|
installedLocales = new ArrayList(); |
| 38 |
0
|
URL url = null; |
| 39 |
0
|
final Enumeration localeDescriptors = ClassLoaderUtils.getResources(LANGUAGE_DESCRIPTOR_FILENAME, this.getClass()); |
| 40 |
0
|
while (localeDescriptors.hasMoreElements()) |
| 41 |
|
{ |
| 42 |
0
|
try |
| 43 |
|
{ |
| 44 |
0
|
url = (URL) localeDescriptors.nextElement(); |
| 45 |
0
|
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); |
| 46 |
0
|
Document xmlDoc = db.parse(url.openConnection().getInputStream()); |
| 47 |
0
|
Element root = xmlDoc.getDocumentElement(); |
| 48 |
0
|
String locale = XMLUtils.getContainedText(root, LOCALE_TAG_NAME); |
| 49 |
0
|
if (TextUtils.stringSet(locale)) |
| 50 |
|
{ |
| 51 |
0
|
installedLocales.add(getLocale(locale)); |
| 52 |
|
} |
| 53 |
|
else |
| 54 |
|
{ |
| 55 |
0
|
throw new IllegalArgumentException("The " + LOCALE_TAG_NAME + " element must be set in " + url); |
| 56 |
|
} |
| 57 |
|
} |
| 58 |
|
catch (IOException e) |
| 59 |
|
{ |
| 60 |
0
|
log.error("Error while reading language descriptor '" + url + "'.", e); |
| 61 |
|
} |
| 62 |
|
catch (ParserConfigurationException e) |
| 63 |
|
{ |
| 64 |
0
|
log.error("Error while parsing language descriptor '" + url + "'.", e); |
| 65 |
|
} |
| 66 |
|
catch (SAXException e) |
| 67 |
|
{ |
| 68 |
0
|
log.error("Error while parsing language descriptor '" + url + "'.", e); |
| 69 |
|
} |
| 70 |
|
} |
| 71 |
|
|
| 72 |
0
|
Collections.sort(installedLocales, new LocaleComparator()); |
| 73 |
|
} |
| 74 |
|
|
| 75 |
0
|
return installedLocales; |
| 76 |
|
} |
| 77 |
|
|
| 78 |
|
|
| 79 |
|
|
| 80 |
|
|
| 81 |
|
@param |
| 82 |
|
@return |
| 83 |
|
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
| 84 |
0
|
public Locale getLocale(String locale)... |
| 85 |
|
{ |
| 86 |
0
|
if (TextUtils.stringSet(locale)) |
| 87 |
|
{ |
| 88 |
0
|
int _pos = locale.indexOf("_"); |
| 89 |
0
|
if (_pos != -1) |
| 90 |
|
{ |
| 91 |
0
|
return new Locale(locale.substring(0, _pos), locale.substring(_pos + 1)); |
| 92 |
|
} |
| 93 |
|
else |
| 94 |
|
{ |
| 95 |
0
|
return new Locale(locale, ""); |
| 96 |
|
} |
| 97 |
|
} |
| 98 |
0
|
return null; |
| 99 |
|
} |
| 100 |
|
} |