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.util.*;
010:
011: public class NeutralToJ1Converter extends LocaleConverter {
012: private static class Conversion {
013: private String propName;
014: private int ndx;
015:
016: protected Conversion() {
017: }
018:
019: public Conversion(String propName) {
020: this .propName = propName;
021: this .ndx = -1;
022: }
023:
024: public Conversion(String propName, int ndx) {
025: this .propName = propName;
026: this .ndx = ndx;
027: }
028:
029: public String getPropName() {
030: return propName;
031: }
032:
033: public String convert(Hashtable source) throws ConversionError {
034: Object sourceData = source.get(propName);
035: if (sourceData == null) {
036: return null;
037: }
038: if (ndx >= 0) {
039: if (sourceData instanceof String[]) {
040: sourceData = ((String[]) sourceData)[ndx];
041: } else if (sourceData instanceof String[][]) {
042: sourceData = ((String[][]) sourceData)[ndx];
043: }
044: }
045: if (sourceData instanceof String) {
046: return (String) sourceData;
047: } else if (sourceData instanceof String[]) {
048: String[] data = (String[]) sourceData;
049: StringBuffer result = new StringBuffer();
050: for (int i = 0; i < data.length; i++) {
051: if (i > 0)
052: result.append(';');
053: result.append(data[i]);
054: }
055: return result.toString();
056: } else {
057: throw new ConversionError("could not convert tag: "
058: + propName);
059: }
060: }
061: }
062:
063: private static class CollationConversion extends Conversion {
064: public String convert(Hashtable source) throws ConversionError {
065: Object[][] elements = (Object[][]) source.get("collations");
066: CollationItem[] items = (CollationItem[]) elements[2][1];
067: if (items == null) {
068: return "";
069: } else {
070: StringBuffer result = new StringBuffer();
071: for (int i = 0; i < items.length; i++) {
072: if (items[i] != null) {
073: result.append(items[i].toString());
074: }
075: }
076: return result.toString();
077: }
078: }
079: }
080:
081: private static final Conversion[] conversions = {
082: new Conversion("LocaleString"), /*locale id based on iso codes*/
083: new Conversion("LocaleID"), /*Windows id*/
084: new Conversion("ShortLanguage"), /*iso-3 abbrev lang name*/
085: new Conversion("ShortCountry"), /*iso-3 abbrev country name*/
086: new Conversion("Languages"), /*language names*/
087: new Conversion("Countries"), /*country names*/
088: new Conversion("MonthNames", 0), /*january*/
089: new Conversion("MonthNames", 1), /*february*/
090: new Conversion("MonthNames", 2), /*march*/
091: new Conversion("MonthNames", 3), /*april*/
092: new Conversion("MonthNames", 4), /*may*/
093: new Conversion("MonthNames", 5), /*june*/
094: new Conversion("MonthNames", 6), /*july*/
095: new Conversion("MonthNames", 7), /*august*/
096: new Conversion("MonthNames", 8), /*september*/
097: new Conversion("MonthNames", 9), /*october*/
098: new Conversion("MonthNames", 10), /*november*/
099: new Conversion("MonthNames", 11), /*december*/
100: new Conversion("MonthNames", 12), /*month 13 if applicable*/
101: new Conversion("MonthAbbreviations", 0), /*abb january*/
102: new Conversion("MonthAbbreviations", 1), /*abb february*/
103: new Conversion("MonthAbbreviations", 2), /*abb march*/
104: new Conversion("MonthAbbreviations", 3), /*abb april*/
105: new Conversion("MonthAbbreviations", 4), /*abb may*/
106: new Conversion("MonthAbbreviations", 5), /*abb june*/
107: new Conversion("MonthAbbreviations", 6), /*abb july*/
108: new Conversion("MonthAbbreviations", 7), /*abb august*/
109: new Conversion("MonthAbbreviations", 8), /*abb september*/
110: new Conversion("MonthAbbreviations", 9), /*abb october*/
111: new Conversion("MonthAbbreviations", 10), /*abb november*/
112: new Conversion("MonthAbbreviations", 11), /*abb december*/
113: new Conversion("MonthAbbreviations", 12), /*abb month 13 if applicable*/
114: new Conversion("DayNames", 0), /*Monday*/
115: new Conversion("DayNames", 1), /*Tuesday*/
116: new Conversion("DayNames", 2), /*Wednesday*/
117: new Conversion("DayNames", 3), /*Thursday*/
118: new Conversion("DayNames", 4), /*Friday*/
119: new Conversion("DayNames", 5), /*Saturday*/
120: new Conversion("DayNames", 6), /*Sunday*/
121: new Conversion("DayAbbreviations", 0), /*abb Monday*/
122: new Conversion("DayAbbreviations", 1), /*abb Tuesday*/
123: new Conversion("DayAbbreviations", 2), /*abb Wednesday*/
124: new Conversion("DayAbbreviations", 3), /*abb Thursday*/
125: new Conversion("DayAbbreviations", 4), /*abb Friday*/
126: new Conversion("DayAbbreviations", 5), /*abb Saturday*/
127: new Conversion("DayAbbreviations", 6), /*abb Sunday*/
128: new Conversion("AmPmMarkers", 0), /*am marker*/
129: new Conversion("AmPmMarkers", 1), /*pm marker*/
130: new Conversion("Eras"),/*era strings*/
131: new Conversion("NumberPatterns", 0), /*decimal pattern*/
132: new Conversion("NumberPatterns", 1), /*currency pattern*/
133: new Conversion("NumberPatterns", 2), /*percent pattern*/
134: new Conversion("NumberElements", 0), /*decimal separator*/
135: new Conversion("NumberElements", 1), /*group (thousands) separator*/
136: new Conversion("NumberElements", 2), /*list separator*/
137: new Conversion("NumberElements", 3), /*percent sign*/
138: new Conversion("NumberElements", 4), /*native 0 digit*/
139: new Conversion("NumberElements", 5), /*pattern digit*/
140: new Conversion("NumberElements", 6), /*minus sign*/
141: new Conversion("NumberElements", 7), /*exponential*/
142: new Conversion("CurrencyElements", 0), /*local currency symbol*/
143: new Conversion("CurrencyElements", 1), /*intl currency symbol*/
144: new Conversion("CurrencyElements", 2), /*monetary decimal separator*/
145: new Conversion("DateTimePatterns", 0), /*full time pattern*/
146: new Conversion("DateTimePatterns", 1), /*long time pattern*/
147: new Conversion("DateTimePatterns", 2), /*medium time pattern*/
148: new Conversion("DateTimePatterns", 3), /*short time pattern*/
149: new Conversion("DateTimePatterns", 4), /*full date pattern*/
150: new Conversion("DateTimePatterns", 5), /*long date pattern*/
151: new Conversion("DateTimePatterns", 6), /*medium date pattern*/
152: new Conversion("DateTimePatterns", 7), /*short date pattern*/
153: new Conversion("DateTimePatterns", 8), /*date-time pattern*/
154: new Conversion("DateTimeElements", 9), /*first day of week*/
155: new Conversion("DateTimeElements", 10), /*min days in first week*/
156: new CollationConversion(), /*collation order*/
157: };
158: private Locale locale;
159: private Locale parentLocale;
160: private ResourceBundle defaultData;
161:
162: public NeutralToJ1Converter(Locale locale) {
163: this .locale = locale;
164: String language = locale.toString();
165: String country = "";
166: String variant = "";
167:
168: int ndx = language.indexOf('_');
169: if (ndx >= 0) {
170: country = language.substring(ndx + 1);
171: language = language.substring(0, ndx);
172: }
173: ndx = country.indexOf('_');
174: if (ndx >= 0) {
175: variant = country.substring(ndx);
176: country = country.substring(0, ndx);
177: }
178:
179: if ("".equals(country)) {
180: language = "";
181: variant = "";
182: } else if ("".equals(variant)) {
183: country = "";
184: }
185:
186: parentLocale = new Locale(language, country, variant);
187: defaultData = ResourceBundle
188: .getBundle(
189: "com.ibm.icu.dev.tool.localeconverter.myLocaleElements",
190: parentLocale);
191: //{{INIT_CONTROLS
192: //}}
193: }
194:
195: /** convert the source table to the result */
196: protected void convert(Hashtable result, Hashtable source)
197: throws ConversionError {
198: Vector localeElements = new Vector();
199: for (int i = 0; i < conversions.length; i++) {
200: final Conversion conv = conversions[i];
201: final String newValue = conv.convert(source);
202: if (newValue != null) {
203: localeElements.addElement(newValue);
204: } else {
205: localeElements.addElement(defaultData.getObject(conv
206: .getPropName()));
207: }
208: }
209: result.put("LocaleElements", localeElements);
210: }
211: //{{DECLARE_CONTROLS
212: //}}
213: }
|