001: /**
002: *******************************************************************************
003: * Copyright (C) 2002-2004, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */package com.ibm.icu.dev.tool.localeconverter;
007:
008: import java.io.*;
009: import java.util.*;
010:
011: /**
012: * @author ram
013: *
014: * To change this generated comment edit the template variable "typecomment":
015: * Window>Preferences>Java>Templates.
016: * To enable and disable the creation of type comments go to
017: * Window>Preferences>Java>Code Generation.
018: */
019: public class ConvertICUListResourceBundle {
020: private static final byte OPT_11 = (byte) 0x01;
021: private static final byte OPT_12 = (byte) 0x02;
022: private static final byte OPT_ICU = (byte) 0x04;
023: private static final byte OPT_PACKAGE = (byte) 0x08;
024: private static final byte OPT_BUNDLE = (byte) 0x10;
025: private static final byte OPT_UNKNOWN = (byte) 0x80;
026: private static final String USER_OPTIONS[] = { "-11", "-12",
027: "-icu", "-package", "-bundle-name" };
028:
029: public static void main(String args[]) {
030: try {
031: new ConvertICUListResourceBundle(args, System.out);
032: } catch (Throwable t) {
033: System.err.println("Unknown error: " + t);
034: }
035: }
036:
037: public ConvertICUListResourceBundle(String args[], PrintStream out) {
038: process(args, out);
039: }
040:
041: public void process(String args[], PrintStream out) {
042: short options = identifyOptions(args);
043: if ((args.length < 1) || ((options & OPT_UNKNOWN) != 0)) {
044: printUsage();
045: } else {
046: String localeName = null;
047: String packagename = null;
048: String bundleName = null;
049: for (int i = 0; i < args.length; i++) {
050: //final String thisArg = args[i];
051:
052: if (args[i].equalsIgnoreCase("-package")) {
053: i++;
054: packagename = args[i];
055: } else if (args[i].equalsIgnoreCase("-icu")) {
056: } else if (!args[i].startsWith("-")) {
057: localeName = args[i];
058: } else if (args[i].equalsIgnoreCase("-bundle-name")) {
059: bundleName = args[++i];
060: }
061: }
062: final Hashtable data = new Hashtable();
063: final String localeElements = packagename
064: + (String) ((bundleName != null) ? "." + bundleName
065: : ".LocaleElements")
066: + (String) ((localeName != null) ? "_" + localeName
067: : "");
068:
069: // final String DateFormatZoneData = packagename+".DateFormatZoneData" +
070: // (String)((localeName != null) ? "_"+localeName : "");
071:
072: addLocaleData(localeElements, data);
073: //addLocaleData(DateFormatZoneData, data);
074:
075: Locale locale;
076: if (localeName == null) {
077: locale = localeFromString("root");
078: } else {
079: locale = localeFromString(localeName);
080: }
081: if ((options & OPT_11) != 0) {
082: new Java1LocaleWriter(out, System.err).write(locale,
083: data);
084: }
085: if ((options & OPT_12) != 0) {
086: new JavaLocaleWriter(out, System.err).write(locale,
087: data);
088: }
089: if ((options & OPT_ICU) != 0) {
090: new ICU3LocaleWriter(getBundle(localeElements), out,
091: System.err).write(locale);
092: }
093: }
094: }
095:
096: private ListResourceBundle getBundle(final String bundleClassName) {
097: try {
098: final Class bundleClass = Class.forName(bundleClassName);
099: final ListResourceBundle bundle = (ListResourceBundle) bundleClass
100: .newInstance();
101: return bundle;
102:
103: } catch (ClassNotFoundException e) {
104: System.err
105: .println("Could not find bundle class for bundle: "
106: + bundleClassName);
107: } catch (InstantiationException e) {
108: System.err
109: .println("Could not create bundle instance for bundle: "
110: + bundleClassName);
111: } catch (IllegalAccessException e) {
112: System.err
113: .println("Could not create bundle instance for bundle: "
114: + bundleClassName);
115: }
116: return null;
117: }
118:
119: private void addLocaleData(final String bundleClassName,
120: final Hashtable data) {
121: ResourceBundle bundle = getBundle(bundleClassName);
122: Enumeration keys = bundle.getKeys();
123: while (keys.hasMoreElements()) {
124: String key = (String) keys.nextElement();
125: Object o = bundle.getObject(key);
126: data.put(key, o);
127: }
128: }
129:
130: private void printUsage() {
131: System.err
132: .println("Usage: ConvertICUListResourceBundle [-11] [-12] [-icu] [-package] <package name> [-bundle-name] <bundle name> localeName");
133: }
134:
135: private short identifyOptions(String[] options) {
136: short result = 0;
137: for (int j = 0; j < options.length; j++) {
138: String option = options[j];
139: if (option.startsWith("-")) {
140: boolean optionRecognized = false;
141: for (short i = 0; i < USER_OPTIONS.length; i++) {
142: if (USER_OPTIONS[i].equals(option)) {
143: result |= (short) (1 << i);
144: optionRecognized = true;
145: break;
146: }
147: }
148: if (!optionRecognized) {
149: result |= OPT_UNKNOWN;
150: }
151: }
152: }
153: return result;
154: }
155:
156: private Locale localeFromString(final String localeName) {
157: if (localeName == null)
158: return new Locale("", "", "");
159: String language = localeName;
160: String country = "";
161: String variant = "";
162:
163: int ndx = language.indexOf('_');
164: if (ndx >= 0) {
165: country = language.substring(ndx + 1);
166: language = language.substring(0, ndx);
167: }
168: ndx = country.indexOf('_');
169: if (ndx >= 0) {
170: variant = country.substring(ndx + 1);
171: country = country.substring(0, ndx);
172: }
173: return new Locale(language, country, variant);
174: }
175: }
|