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 ICU3LocaleWriter extends LocaleWriter {
020: public ICU3LocaleWriter(PrintStream out) {
021: super (out);
022: }
023:
024: private ListResourceBundle bundle = null;
025:
026: protected void open(Locale locale) {
027: print(locale.toString());
028: println(" {");
029: indent();
030: }
031:
032: public ICU3LocaleWriter(PrintStream out, PrintStream err) {
033: super (out, err);
034: }
035:
036: public ICU3LocaleWriter(ListResourceBundle bundle, PrintStream out,
037: PrintStream err) {
038: super (out, err);
039: this .bundle = bundle;
040: }
041:
042: public void write(Locale locale) {
043: open(locale);
044: Enumeration keys = bundle.getKeys();
045: while (keys.hasMoreElements()) {
046: String key = (String) keys.nextElement();
047: Object o = bundle.getObject(key);
048: write(key, o);
049: }
050: close();
051: }
052:
053: public void write(String key, Object o) {
054: if (key != null)
055: print(key);
056: if (o instanceof String) {
057: write((String) o);
058: } else if (o instanceof Integer[]) {
059: write((Integer[]) o);
060: } else if (o instanceof byte[]) {
061: write((byte[]) o);
062: } else if (o instanceof Integer) {
063: write((Integer) o);
064: } else if (o instanceof Object[][]) {
065: write((Object[][]) o);
066: } else if (o instanceof Object[]) {
067: write((Object[]) o);
068: }
069: }
070:
071: protected void write(String str) {
072: print("{");
073: printString(str);
074: println("}");
075: }
076:
077: protected void write(Integer[] o) {
078: println(":intvector { ");
079: indent();
080: if (o != null) {
081: for (int i = 0; i < o.length; i++) {
082: print(o[i].toString());
083: println(",");
084: }
085: }
086: outdent();
087: println(" }");
088: }
089:
090: private String toHex(byte b) {
091: int i = (int) ((char) (b & 0xFF));
092: String temp = Integer.toHexString(i);
093: if (temp.length() < 2) {
094: return "0" + temp;
095: } else {
096: return temp;
097: }
098: }
099:
100: protected void write(byte[] o) {
101: print(":bin{ ");
102: indent();
103: if (o != null) {
104: for (int i = 0; i < o.length; i++) {
105: print(toHex(o[i]));
106: }
107: }
108: outdent();
109: println(" }");
110: }
111:
112: protected void write(Integer o) {
113: print(":int { ");
114: indent();
115: if (o != null) {
116: print(o.toString());
117: }
118: outdent();
119: println(" }");
120: }
121:
122: protected void write(Object[] o) {
123: String key = null;
124: println(":array { ");
125: indent();
126: for (int i = 0; i < o.length; i++) {
127: if (o[i] instanceof String) {
128: printString((String) o[i]);
129: } else {
130: write(key, o[i]);
131: }
132: println(",");
133: }
134: outdent();
135: println(" }");
136: }
137:
138: protected void write(Object[][] o) {
139: //String key = null;
140: println(":table {");
141: indent();
142: for (int i = 0; i < o.length; i++) {
143:
144: if (o[i][1] instanceof String) {
145: print((String) o[i][0]);
146: print("{");
147: printString((String) o[i][1]);
148: println("}");
149: } else {
150: write((String) o[i][0], o[i][1]);
151: println("");
152: }
153:
154: }
155: outdent();
156: println(" }");
157: }
158:
159: ///////////////////////////////////////////////////
160: /// Only for compatibility with super class
161: /// not required otherwise
162: //////////////////////////////////////////////////
163:
164: /////// BEGIN //////////////////
165:
166: protected void write(String tag, String value) {
167: print(tag);
168: print(" { ");
169: printString(value);
170: println(" }");
171: }
172:
173: protected void write(String tag, String[] value) {
174: if (tag != null) {
175: print(tag);
176: println(" { ");
177: } else {
178: println("{");
179: }
180: indent();
181: for (int i = 0; i < value.length; i++) {
182: printString(value[i]);
183: println(",");
184: }
185: outdent();
186: println("}");
187: }
188:
189: protected void write2D(String tag, String[][] value) {
190: print(tag);
191: println(" { ");
192: indent();
193: for (int i = 0; i < value.length; i++) {
194: write(null, value[i]);
195: }
196: outdent();
197: println("}");
198: }
199:
200: protected void writeTagged(String tag, Object[][] value) {
201: print(tag);
202: println(" { ");
203: indent();
204: for (int i = 0; i < value.length; i++) {
205: write((String) value[i][0], value[i][1]);
206: }
207: outdent();
208: println("}");
209: }
210:
211: protected void writeTagged(String tag, String[][] value) {
212: print(tag);
213: println(" { ");
214: indent();
215: for (int i = 0; i < value.length; i++) {
216: write(value[i][0], value[i][1]);
217: }
218: outdent();
219: println("}");
220: }
221:
222: //////////////////// END //////////////////////////////////
223: protected void close() {
224: outdent();
225: println("}");
226: // super.closeFileHandle();
227: }
228:
229: protected String getStringJoiningCharacter() {
230: return "";
231: }
232:
233: }
|