package gnu.gettext;
import java.lang.reflect.*;
import java.util.*;
import java.io.*;
public class DumpResource {
private Writer out;
private void dumpString (String str) throws IOException {
int n = str.length();
out.write('"');
for (int i = 0; i < n; i++) {
char c = str.charAt(i);
if (c == 0x0008) {
out.write('\\'); out.write('b');
} else if (c == 0x000c) {
out.write('\\'); out.write('f');
} else if (c == 0x000a) {
out.write('\\'); out.write('n');
} else if (c == 0x000d) {
out.write('\\'); out.write('r');
} else if (c == 0x0009) {
out.write('\\'); out.write('t');
} else if (c == '\\' || c == '"') {
out.write('\\'); out.write(c);
} else
out.write(c);
}
out.write('"');
}
private void dumpMessage (String msgid, String msgid_plural, Object msgstr) throws IOException {
out.write("msgid "); dumpString(msgid); out.write('\n');
if (msgid_plural != null) {
out.write("msgid_plural "); dumpString(msgid_plural); out.write('\n');
for (int i = 0; i < ((String[])msgstr).length; i++) {
out.write("msgstr[" + i + "] ");
dumpString(((String[])msgstr)[i]);
out.write('\n');
}
} else {
out.write("msgstr "); dumpString((String)msgstr); out.write('\n');
}
out.write('\n');
}
private ResourceBundle catalog;
private Method lookupMethod;
private Field pluralField;
private Object lookup (String key) {
Object value = null;
if (lookupMethod != null) {
try {
value = lookupMethod.invoke(catalog, new Object[] { key });
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.getTargetException().printStackTrace();
}
} else {
try {
value = catalog.getObject(key);
} catch (MissingResourceException e) {
}
}
return value;
}
private void dump () throws IOException {
lookupMethod = null;
try {
lookupMethod = catalog.getClass().getMethod("lookup", new Class[] { java.lang.String.class });
} catch (NoSuchMethodException e) {
} catch (SecurityException e) {
}
pluralField = null;
try {
pluralField = catalog.getClass().getField("plural");
} catch (NoSuchFieldException e) {
} catch (SecurityException e) {
}
{
Object header_entry = null;
Enumeration keys = catalog.getKeys();
while (keys.hasMoreElements())
if ("".equals(keys.nextElement())) {
header_entry = lookup("");
break;
}
if (header_entry == null)
header_entry = "Content-Type: text/plain; charset=UTF-8\n";
dumpMessage("",null,header_entry);
}
{
Enumeration keys = catalog.getKeys();
Object plural = null;
if (pluralField != null)
try {
plural = pluralField.get(catalog);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
if (plural instanceof String[]) {
int i = 0;
while (keys.hasMoreElements()) {
String key = (String)keys.nextElement();
Object value = lookup(key);
String key_plural = (value instanceof String[] ? ((String[])plural)[i++] : null);
if (!"".equals(key))
dumpMessage(key,key_plural,value);
}
if (i != ((String[])plural).length)
throw new RuntimeException("wrong plural field length");
} else if (plural instanceof Hashtable) {
while (keys.hasMoreElements()) {
String key = (String)keys.nextElement();
if (!"".equals(key)) {
Object value = lookup(key);
String key_plural = (value instanceof String[] ? (String)((Hashtable)plural).get(key) : null);
dumpMessage(key,key_plural,value);
}
}
} else if (plural == null) {
while (keys.hasMoreElements()) {
String key = (String)keys.nextElement();
if (!"".equals(key))
dumpMessage(key,null,lookup(key));
}
} else
throw new RuntimeException("wrong plural field value");
}
}
public DumpResource (String resource_name, String locale_name) {
String language;
String country;
String variant;
language = locale_name;
{
int i = language.indexOf('_');
if (i >= 0) {
country = language.substring(i+1);
language = language.substring(0,i);
} else
country = "";
}
{
int j = country.indexOf('_');
if (j >= 0) {
variant = country.substring(j+1);
country = country.substring(0,j);
} else
variant = "";
}
Locale locale = new Locale(language,country,variant);
ResourceBundle catalog = ResourceBundle.getBundle(resource_name,locale);
try {
Writer w1 = new OutputStreamWriter(System.out,"UTF8");
Writer w2 = new BufferedWriter(w1);
this.out = w2;
this.catalog = catalog;
dump();
w2.close();
w1.close();
System.out.flush();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
public static void main (String[] args) {
new DumpResource(args[0], args.length > 1 ? args[1] : "");
System.exit(0);
}
}