ICU-2571 updates to localeconverter

X-SVN-Rev: 10649
This commit is contained in:
Ram Viswanadha 2002-12-12 23:53:19 +00:00
parent a40a17dd19
commit 63bcd3ab6e
2 changed files with 364 additions and 0 deletions

View File

@ -0,0 +1,156 @@
package com.ibm.icu.dev.tool.localeconverter;
import java.io.*;
import java.util.*;
import com.ibm.icu.impl.ICUListResourceBundle;
/**
* @author ram
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
public class ConvertICUListResourceBundle {
private static final byte OPT_11 = (byte)0x01;
private static final byte OPT_12 = (byte)0x02;
private static final byte OPT_ICU = (byte)0x04;
private static final byte OPT_PACKAGE = (byte)0x08;
private static final byte OPT_BUNDLE = (byte)0x10;
private static final byte OPT_UNKNOWN = (byte)0x80;
private static final String USER_OPTIONS[] = {
"-11",
"-12",
"-icu",
"-package",
"-bundle-name"
};
public static void main(String args[]) {
try {
new ConvertICUListResourceBundle(args, System.out);
} catch (Throwable t) {
System.err.println("Unknown error: "+t);
}
}
public ConvertICUListResourceBundle(String args[], PrintStream out) {
process(args, out);
}
public void process(String args[], PrintStream out) {
short options = identifyOptions(args);
if ((args.length < 1) || ((options & OPT_UNKNOWN) != 0)) {
printUsage();
} else {
String localeName = null;
String packagename = null;
String bundleName = null;
for (int i = 0; i < args.length; i++) {
final String thisArg = args[i];
if(args[i].equalsIgnoreCase("-package")){
i++;
packagename = args[i];
}else if(args[i].equalsIgnoreCase("-icu")){
}else if (!args[i].startsWith("-")) {
localeName = args[i];
}else if(args[i].equalsIgnoreCase("-bundle-name")){
bundleName = args[++i];
}
}
final Hashtable data = new Hashtable();
final String localeElements = packagename
+ (String)((bundleName != null) ? "."+ bundleName : ".LocaleElements" )
+ (String)((localeName != null) ? "_"+ localeName : "");
final String DateFormatZoneData = packagename+".DateFormatZoneData" +
(String)((localeName != null) ? "_"+localeName : "");
addLocaleData(localeElements, data);
//addLocaleData(DateFormatZoneData, data);
Locale locale;
if(localeName==null){
locale = localeFromString("root");
}else{
locale = localeFromString(localeName);
}
if ((options & OPT_11) != 0) {
new Java1LocaleWriter(out, System.err).write(locale, data);
}
if ((options & OPT_12) != 0) {
new JavaLocaleWriter(out, System.err).write(locale, data);
}
if ((options & OPT_ICU) != 0) {
new ICU3LocaleWriter(getBundle(localeElements),out, System.err).write(locale);
}
}
}
private ListResourceBundle getBundle(final String bundleClassName){
try {
final Class bundleClass = Class.forName(bundleClassName);
final ListResourceBundle bundle = (ListResourceBundle)bundleClass.newInstance();
return bundle;
} catch (ClassNotFoundException e) {
System.err.println("Could not find bundle class for bundle: "+bundleClassName);
} catch (InstantiationException e) {
System.err.println("Could not create bundle instance for bundle: "+bundleClassName);
} catch (IllegalAccessException e) {
System.err.println("Could not create bundle instance for bundle: "+bundleClassName);
}
return null;
}
private void addLocaleData(final String bundleClassName, final Hashtable data) {
ResourceBundle bundle = getBundle(bundleClassName);
Enumeration keys = bundle.getKeys();
while(keys.hasMoreElements()){
String key = (String) keys.nextElement();
Object o = bundle.getObject(key);
data.put(key, o);
}
}
private void printUsage() {
System.err.println("Usage: ConvertICUListResourceBundle [-11] [-12] [-icu] [-package] <package name> [-bundle-name] <bundle name> localeName");
}
private short identifyOptions(String[] options) {
short result = 0;
for (int j = 0; j < options.length; j++) {
String option = options[j];
if (option.startsWith("-")) {
boolean optionRecognized = false;
for (short i = 0; i < USER_OPTIONS.length; i++) {
if (USER_OPTIONS[i].equals(option)) {
result |= (short)(1 << i);
optionRecognized = true;
break;
}
}
if (!optionRecognized) {
result |= OPT_UNKNOWN;
}
}
}
return result;
}
private Locale localeFromString(final String localeName) {
if (localeName == null) return new Locale("", "", "");
String language = localeName;
String country = "";
String variant = "";
int ndx = language.indexOf('_');
if (ndx >= 0) {
country = language.substring(ndx+1);
language = language.substring(0, ndx);
}
ndx = country.indexOf('_');
if (ndx >= 0) {
variant = country.substring(ndx+1);
country = country.substring(0, ndx);
}
return new Locale(language, country, variant);
}
}

View File

@ -0,0 +1,208 @@
package com.ibm.icu.dev.tool.localeconverter;
import java.io.*;
import java.util.*;
/**
* @author ram
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
public class ICU3LocaleWriter extends LocaleWriter {
public ICU3LocaleWriter(PrintStream out) {
super(out);
}
private ListResourceBundle bundle = null;
protected void open(Locale locale) {
print(locale.toString());
println(" {");
indent();
}
public ICU3LocaleWriter(PrintStream out, PrintStream err) {
super(out, err);
}
public ICU3LocaleWriter(ListResourceBundle bundle,PrintStream out, PrintStream err) {
super(out, err);
this.bundle = bundle;
}
public void write(Locale locale){
open(locale);
Enumeration keys = bundle.getKeys();
while(keys.hasMoreElements()){
String key = (String) keys.nextElement();
Object o = bundle.getObject(key);
write(key, o);
}
close();
}
public void write(String key, Object o){
if(key!=null) print(key);
if(o instanceof String){
write((String) o);
}else if( o instanceof Integer[]){
write((Integer[]) o);
}else if(o instanceof byte[]){
write((byte[]) o);
}else if(o instanceof Integer){
write((Integer) o);
}else if(o instanceof Object[][]){
write((Object[][]) o);
}else if(o instanceof Object[]){
write((Object[]) o);
}
}
protected void write(String str){
print("{");
printString(str);
println("}");
}
protected void write(Integer[] o){
println(":intvector { ");
indent();
if(o!=null){
for(int i=0; i< o.length; i++){
print(o[i].toString());
println(",");
}
}
outdent();
println(" }");
}
private String toHex(byte b){
int i =(int)((char) (b & 0xFF));
String temp = Integer.toHexString(i);
if(temp.length()<2){
return "0"+temp;
}else{
return temp;
}
}
protected void write(byte[] o){
print(":bin{ ");
indent();
if(o!=null){
for(int i=0; i< o.length; i++){
print(toHex(o[i]));
}
}
outdent();
println(" }");
}
protected void write(Integer o){
print(":int { ");
indent();
if(o!=null){
print(o.toString());
}
outdent();
println(" }");
}
protected void write(Object[] o){
String key = null;
println(":array { ");
indent();
for(int i=0; i<o.length;i++){
if(o[i] instanceof String){
printString((String)o[i]);
}else{
write(key,o[i]);
}
println(",");
}
outdent();
println(" }");
}
protected void write(Object[][] o){
String key = null;
println(":table {");
indent();
for(int i=0; i< o.length;i++){
if(o[i][1] instanceof String){
print((String) o[i][0]);
print("{");
printString((String) o[i][1]);
println("}");
}else{
write((String) o[i][0], o[i][1]);
println("");
}
}
outdent();
println(" }");
}
///////////////////////////////////////////////////
/// Only for compatibility with super class
/// not required otherwise
//////////////////////////////////////////////////
/////// BEGIN //////////////////
protected void write(String tag, String value) {
print(tag);
print(" { ");
printString(value);
println(" }");
}
protected void write(String tag, String[] value) {
if (tag != null) {
print(tag);
println(" { ");
} else {
println("{");
}
indent();
for (int i = 0; i < value.length; i++) {
printString(value[i]);
println(",");
}
outdent();
println("}");
}
protected void write2D(String tag, String[][] value) {
print(tag);
println(" { ");
indent();
for (int i = 0; i < value.length; i++) {
write(null, value[i]);
}
outdent();
println("}");
}
protected void writeTagged(String tag, Object[][] value) {
print(tag);
println(" { ");
indent();
for (int i = 0; i < value.length; i++) {
write((String)value[i][0], value[i][1]);
}
outdent();
println("}");
}
protected void writeTagged(String tag, String[][] value) {
print(tag);
println(" { ");
indent();
for (int i = 0; i < value.length; i++) {
write(value[i][0], value[i][1]);
}
outdent();
println("}");
}
//////////////////// END //////////////////////////////////
protected void close() {
outdent();
println("}");
super.closeFileHandle();
}
protected String getStringJoiningCharacter() {
return "";
}
}