001: /*
002: *******************************************************************************
003: * Copyright (C) 2002-2004, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007: package com.ibm.icu.dev.tool.localeconverter;
008:
009: import java.io.*;
010: import java.util.*;
011:
012: /**
013:
014: The ConvertJavaLocale application converts java locales to
015: Java and ICU Locale files. It's usage is as follows
016:
017: ConvertJavaLocale [-11] [-12] [-icu] [-package] locale...
018:
019: Usage
020:
021: -11
022: If this option is specified, data is output in
023: Java 1.1.x locale format.
024:
025: -12
026: If this option is specified, data is output in
027: Java 1.2.x locale format. If an output format
028: is not specified, -12 is the default.
029:
030: -icu
031: If this option is specified, data is output in
032: ICU locale format.
033:
034: locale
035: The locale to convert
036:
037:
038: */
039:
040: public class ConvertJavaLocale {
041: private static final byte OPT_11 = (byte) 0x01;
042: private static final byte OPT_12 = (byte) 0x02;
043: private static final byte OPT_ICU = (byte) 0x04;
044: private static final byte OPT_PACKAGE = (byte) 0x08;
045: private static final byte OPT_UNKNOWN = (byte) 0x80;
046: private static final String USER_OPTIONS[] = { "-11", "-12",
047: "-icu", "-package" };
048:
049: private static final String[] tags = { "LocaleString", "LocaleID",
050: "ShortLanguage", "ShortCountry", "Languages", "Countries",
051: "MonthNames", "MonthAbbreviations", "DayNames",
052: "DayAbbreviations", "AmPmMarkers", "Eras",
053: "NumberPatterns", "NumberElements", "CurrencyElements",
054: "DateTimePatterns", "DateTimeElements", "collations",
055: "zoneStrings", "localPatternChars", };
056:
057: public static void main(String args[]) {
058: try {
059: new ConvertJavaLocale(args, System.out);
060: } catch (Throwable t) {
061: System.err.println("Unknown error: " + t);
062: }
063: }
064:
065: public ConvertJavaLocale(String args[], PrintStream out) {
066: process(args, out);
067: }
068:
069: public void process(String args[], PrintStream out) {
070: short options = identifyOptions(args);
071: if ((args.length < 1) || ((options & OPT_UNKNOWN) != 0)) {
072: printUsage();
073: } else {
074: String localeName = null;
075: String packagename = null;
076: for (int i = 0; i < args.length; i++) {
077: if (args[i].equalsIgnoreCase("-package")) {
078: i++;
079: packagename = args[i];
080: } else if (args[i].equalsIgnoreCase("-icu")) {
081: } else if (!args[i].startsWith("-")) {
082: localeName = args[i];
083: }
084: }
085: final Hashtable data = new Hashtable();
086: final String localeElements;
087: final String dateFormatZoneData;
088:
089: if (localeName != null) {
090: if (!localeName.equals("root")) {
091: localeElements = packagename + ".LocaleElements"
092: + "_" + localeName;
093: dateFormatZoneData = packagename
094: + ".DateFormatZoneData" + "_" + localeName;
095: } else {
096: localeElements = packagename + ".LocaleElements";
097: dateFormatZoneData = packagename
098: + ".DateFormatZoneData";
099: }
100: } else {
101: printUsage();
102: return;
103: }
104: addLocaleData(localeElements, data);
105: addLocaleData(dateFormatZoneData, data);
106: final Locale locale = localeFromString(localeName);
107: if ((options & OPT_11) != 0) {
108: new Java1LocaleWriter(out, System.err).write(locale,
109: data);
110: }
111: if ((options & OPT_12) != 0) {
112: new JavaLocaleWriter(out, System.err).write(locale,
113: data);
114: }
115: if ((options & OPT_ICU) != 0) {
116: new ICULocaleWriter(out, System.err)
117: .write(locale, data);
118: }
119: }
120: }
121:
122: private void addLocaleData(final String bundleClassName,
123: final Hashtable data) {
124: try {
125: final Class bundleClass = Class.forName(bundleClassName);
126: final ResourceBundle bundle = (ResourceBundle) bundleClass
127: .newInstance();
128: for (int i = 0; i < tags.length; i++) {
129: try {
130: final Object resource = bundle.getObject(tags[i]);
131: data.put(tags[i], resource);
132: } catch (MissingResourceException e) {
133: }
134: }
135: } catch (ClassNotFoundException e) {
136: System.err
137: .println("Could not find bundle class for bundle: "
138: + bundleClassName);
139: } catch (InstantiationException e) {
140: System.err
141: .println("Could not create bundle instance for bundle: "
142: + bundleClassName);
143: } catch (IllegalAccessException e) {
144: System.err
145: .println("Could not create bundle instance for bundle: "
146: + bundleClassName);
147: }
148: }
149:
150: private void printUsage() {
151: System.err
152: .println("Usage: ConvertJavaLocale [-11] [-12] [-icu] [-package] <package name> localeName");
153: }
154:
155: private short identifyOptions(String[] options) {
156: short result = 0;
157: for (int j = 0; j < options.length; j++) {
158: String option = options[j];
159: if (option.startsWith("-")) {
160: boolean optionRecognized = false;
161: for (short i = 0; i < USER_OPTIONS.length; i++) {
162: if (USER_OPTIONS[i].equals(option)) {
163: result |= (short) (1 << i);
164: optionRecognized = true;
165: break;
166: }
167: }
168: if (!optionRecognized) {
169: result |= OPT_UNKNOWN;
170: }
171: }
172: }
173: return result;
174: }
175:
176: private Locale localeFromString(final String localeName) {
177: if (localeName == null)
178: return new Locale("", "", "");
179: String language = localeName;
180: String country = "";
181: String variant = "";
182:
183: int ndx = language.indexOf('_');
184: if (ndx >= 0) {
185: country = language.substring(ndx + 1);
186: language = language.substring(0, ndx);
187: }
188: ndx = country.indexOf('_');
189: if (ndx >= 0) {
190: variant = country.substring(ndx + 1);
191: country = country.substring(0, ndx);
192: }
193: return new Locale(language, country, variant);
194: }
195: }
|