Skip to content

Commit

Permalink
Fix bug olap4j-76, "Still missing codes from LCID-to-Locale mapping".
Browse files Browse the repository at this point in the history
git-svn-id: https://olap4j.svn.sourceforge.net/svnroot/olap4j/trunk@551 c6a108a4-781c-0410-a6c6-c2d559e19af0
  • Loading branch information
julianhyde committed Oct 16, 2012
1 parent 7bc91ff commit b77b2f1
Show file tree
Hide file tree
Showing 4 changed files with 2,329 additions and 22 deletions.
4 changes: 4 additions & 0 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ src/org/olap4j/mdx/parser/impl/TokenMgrError.java,
src/org/olap4j/driver/xmla/XmlaOlap4jDriverVersion.java" />

<target name="checkIsJdk15" if="jdk15.not.present">
<!--
<fail message="The 'dist' target (used to make an official release) must be run under JDK 1.5." />
-->
</target>

<condition property="jdk15.present">
Expand Down Expand Up @@ -177,6 +179,8 @@ VERSION.txt" />
excludes="
org/olap4j/driver/**,
META-INF/**"/>
<zipfileset dir="${src.dir}" prefix=""
includes="org/olap4j/impl/nls.properties"/>
</zip>
</target>

Expand Down
114 changes: 94 additions & 20 deletions src/org/olap4j/impl/LcidLocale.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
*/
package org.olap4j.impl;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.*;

/**
Expand All @@ -28,10 +31,10 @@
* @author jhyde
*/
public class LcidLocale {
final Map<Short, String> lcidLocaleMap = new HashMap<Short, String>();
final Map<String, Short> localeToLcidMap = new HashMap<String, Short>();
final Map<Short, Info> lcidLocaleMap = new HashMap<Short, Info>();
final Map<String, Info> localeToLcidMap = new HashMap<String, Info>();

private static final Object[] LOCALE_DATA = {
private static final Object[] LOCALE_DATA_ = {
"", (short) 0x007f, "invariant culture",
"ar", (short) 0x0001, "Arabic",
"bg", (short) 0x0002, "Bulgarian",
Expand Down Expand Up @@ -268,19 +271,56 @@ public class LcidLocale {
private static LcidLocale INSTANCE;

private LcidLocale() {
for (int i = 0; i < LOCALE_DATA.length;) {
String localeName = (String) LOCALE_DATA[i++];
Short lcid = (Short) LOCALE_DATA[i++];
Olap4jUtil.discard((String) LOCALE_DATA[i++]); // description
lcidLocaleMap.put(lcid, localeName);
localeToLcidMap.put(localeName, lcid);
URL resource = getClass().getResource("nls.properties");
InputStream inputStream = null;
try {
inputStream = resource.openStream();
Properties properties = new Properties();
properties.load(inputStream);
@SuppressWarnings("unchecked")
final Map<String, String> map = (Map) properties;
for (Map.Entry<String, String> entry : map.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
if (key.endsWith(".lcid")) {
final String root =
key.substring(0, key.length() - ".lcid".length());
String lcname =
root.substring(root.indexOf('.') + 1).replace('-', '_');
short lcid = Short.parseShort(value.substring(2), 16);
String locale = map.get(root + ".locale");
String language = map.get(root + ".language");
String languageLocal = map.get(root + ".languageLocal");
String codepageAnsi = map.get(root + ".codepageAnsi");
String codepageOem = map.get(root + ".codepageOem");
String regionAbbrev = map.get(root + ".regionAbbrev");
String languageAbbrev = map.get(root + ".languageAbbrev");
Info info =
new Info(
lcid,
lcname,
locale,
language,
languageLocal,
Short.parseShort(codepageAnsi),
Short.parseShort(codepageOem),
regionAbbrev,
languageAbbrev);
lcidLocaleMap.put(lcid, info);
localeToLcidMap.put(lcname, info);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
// ignore
}
}
}
assert LOCALE_DATA.length % 3 == 0;
assert lcidLocaleMap.size() == LOCALE_DATA.length / 3;

// Off by one because Spanish (traditional) and Spanish (modern) share
// locale "es_ES".
assert localeToLcidMap.size() == LOCALE_DATA.length / 3 - 1;
}

/**
Expand All @@ -301,11 +341,11 @@ static LcidLocale instance() {
* @throws RuntimeException if LCID is not valid
*/
private Locale toLocale(short lcid) {
final String s = lcidLocaleMap.get(lcid);
final Info s = lcidLocaleMap.get(lcid);
if (s == null) {
throw new RuntimeException("Unknown LCID " + lcid);
}
return parseLocale(s);
return parseLocale(s.locale);
}

/**
Expand Down Expand Up @@ -348,9 +388,9 @@ public static short localeToLcid(Locale locale) {
private short toLcid(String localeName) {
final String localeName0 = localeName;
for (;;) {
final Short lcid = localeToLcidMap.get(localeName);
if (lcid != null) {
return lcid;
final Info info = localeToLcidMap.get(localeName);
if (info != null) {
return info.lcid;
}
final int underscore = localeName.lastIndexOf('_');
if (underscore < 0) {
Expand Down Expand Up @@ -382,6 +422,40 @@ public static Locale parseLocale(String localeString) {
"bad locale string '" + localeString + "'");
}
}

public static class Info {
public final short lcid;
public final String locale;
public final String localeFull;
public final String language;
public final String languageLocal;
public final short codepageAnsi;
public final short codepageOem;
public final String regionAbbrev;
public final String languageAbbrev;

public Info(
short lcid,
String locale,
String localeFull,
String language,
String languageLocal,
short codepageAnsi,
short codepageOem,
String regionAbbrev,
String languageAbbrev)
{
this.lcid = lcid;
this.locale = locale;
this.localeFull = localeFull;
this.language = language;
this.languageLocal = languageLocal;
this.codepageAnsi = codepageAnsi;
this.codepageOem = codepageOem;
this.regionAbbrev = regionAbbrev;
this.languageAbbrev = languageAbbrev;
}
}
}

// End LcidLocale.java
Loading

0 comments on commit b77b2f1

Please sign in to comment.