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: public class JavaLocaleWriter extends LocaleWriter {
013: public JavaLocaleWriter(PrintStream out) {
014: super (out);
015: }
016:
017: public JavaLocaleWriter(PrintStream out, PrintStream err) {
018: super (out, err);
019: }
020:
021: public void open(Locale locale) {
022: println(HEADER);
023: indent(3);
024: }
025:
026: protected void write(String tag, Object o) {
027: if ("collations".equals(tag)) {
028: writeTagged(tag, (Object[][]) o);
029: } else if (!(o instanceof CollationItem[])) {
030: super .write(tag, o);
031: } else {
032: CollationItem[] items = (CollationItem[]) o;
033: print("{ ");
034: printString("collations");
035: println(", ");
036: for (int i = 0; i < items.length; i++) {
037: if (items[i] != null) {
038: printString(items[i].toString());
039: if (items[i].comment != null) {
040: print("+");
041: tabTo(30);
042: print("//");
043: println(items[i].comment);
044: } else {
045: println("+");
046: }
047: }
048: }
049: println("\"\"");
050: println(" },");
051: }
052: }
053:
054: public void write(String tag, String value) {
055: print("{ ");
056: printString(tag);
057: print(", ");
058: printString(value);
059: println(" },");
060: }
061:
062: public void write(String tag, String[] value) {
063: print("{ ");
064: if (tag != null) {
065: printString(tag);
066: println(",");
067: } else {
068: println("");
069: }
070: indent();
071: println("new String[] {");
072: indent();
073: for (int i = 0; i < value.length; i++) {
074: printString(value[i]);
075: println(",");
076: }
077: outdent();
078: println("}");
079: outdent();
080: println("},");
081: }
082:
083: public void write2D(String tag, String[][] value) {
084: print("{ ");
085: printString(tag);
086: println(",");
087: indent();
088: println("new String[][] {");
089: indent();
090: for (int i = 0; i < value.length; i++) {
091: println("{");
092: indent();
093: for (int j = 0; j < value[i].length; j++) {
094: printString(value[i][j]);
095: println(",");
096: }
097: outdent();
098: println("},");
099: }
100: outdent();
101: println("}");
102: outdent();
103: println("},");
104: }
105:
106: public void writeTagged(String tag, String[][] value) {
107: print("{ ");
108: printString(tag);
109: println(",");
110: indent();
111: println("new String[][] {");
112: indent();
113: for (int i = 0; i < value.length; i++) {
114: write(value[i][0], value[i][1]);
115: }
116: outdent();
117: println("}");
118: outdent();
119: println("},");
120: }
121:
122: public void writeTagged(String tag, Object[][] value) {
123: print("{ ");
124: printString(tag);
125: println(",");
126: indent();
127: println("new String[][] {");
128: indent();
129: for (int i = 0; i < value.length; i++) {
130: write((String) value[i][0], value[i][1]);
131: }
132: outdent();
133: println("}");
134: outdent();
135: println("},");
136: }
137:
138: public void close() {
139: outdent(3);
140: print(FOOTER);
141: println("");
142: }
143:
144: protected void appendEscapedChar(char c, StringBuffer buffer) {
145: if (c < '\u0020' || c == '"' || c == '\\') {
146: buffer.append('\\');
147: buffer.append(HEX_DIGIT[(c & 0700) >> 6]); // HEX_DIGIT works for octal
148: buffer.append(HEX_DIGIT[(c & 0070) >> 3]);
149: buffer.append(HEX_DIGIT[(c & 0007)]);
150: } else {
151: super .appendEscapedChar(c, buffer);
152: }
153: }
154:
155: protected String getStringJoiningCharacter() {
156: return "+";
157: }
158:
159: private static final String HEADER = "package java.text.resources;\n"
160: + "import java.util.ListResourceBundle;\n"
161: + "public class TestLocaleElements extends ListResourceBundle {\n"
162: + " public Object[][] getContents() {\n"
163: + " return new Object[][] {";
164:
165: private static final String FOOTER = " };\n" + " }\n"
166: + "}";
167: //{{DECLARE_CONTROLS
168: //}}
169: }
|