001: /*
002: * GenerateLocalizedPropertiesFiles.java
003: *
004: * Created on January 9, 2006, 9:53 AM
005: *
006: * To change this template, choose Tools | Options and locate the template under
007: * the Source Creation and Management node. Right-click the template and choose
008: * Open. You can then make changes to the template in the Source Editor.
009: */
010:
011: package org.netbeans.modules.jmx.i18n;
012:
013: import java.io.File;
014: import java.io.FileInputStream;
015: import java.io.FileOutputStream;
016: import java.text.ParseException;
017: import java.util.Map.Entry;
018: import java.util.Properties;
019: import java.util.Set;
020: import java.util.StringTokenizer;
021:
022: /**
023: *
024: * @author jfdenise
025: */
026: public class GenerateLocalizedPropertiesFiles {
027:
028: /** Creates a new instance of Main */
029: public GenerateLocalizedPropertiesFiles() {
030: }
031:
032: /**
033: * @param args the command line arguments
034: */
035: public static void main(String[] args) throws Exception {
036:
037: String dirs = System.getProperty("jmx.i18n.properties.dirs");// NOI18N
038: StringTokenizer tokenizer = new StringTokenizer(dirs, ",");// NOI18N
039: String prefix = System.getProperty("jmx.i18n.prefix");// NOI18N
040: String locale = System.getProperty("jmx.i18n.locale");// NOI18N
041: while (tokenizer.hasMoreElements()) {
042: generateFile(locale, prefix, tokenizer.nextToken());
043: }
044: }
045:
046: private static void generateFile(String locale, String prefix,
047: String dir) throws Exception {
048: File file = new File("." + File.separator + "src"
049: + // NOI18N
050: File.separator + dir + File.separator
051: + "Bundle.properties");// NOI18N
052: System.out.println("Reading file : " + file.getAbsolutePath());// NOI18N
053: String newDir = "." + File.separator + "build" + // NOI18N
054: File.separator + "locale" + File.separator + dir;// NOI18N
055: String newFileName = newDir + File.separator + "Bundle_"
056: + locale + // NOI18N
057: ".properties";// NOI18N
058: File dirs = new File(newDir);
059: dirs.mkdirs();
060: File f = new File(newFileName);
061: f.createNewFile();
062: FileOutputStream output = new FileOutputStream(f);
063:
064: //Load properties file
065: Properties properties = new Properties();
066: properties.load(new FileInputStream(file));
067: Properties newProperties = new Properties();
068: Set<Entry<Object, Object>> entries = properties.entrySet();
069: for (Entry entry : entries) {
070: String key = (String) entry.getKey();
071:
072: String value = (String) entry.getValue();
073: if (!key.endsWith("Template")) {// NOI18N
074: //We are not translating Numbers
075: try {
076: Integer.valueOf(value);
077: } catch (Exception e) {
078: if (!("localhost".equals(value)))// NOI18N
079: value = parseEscapes(prefix) + entry.getValue();
080: }
081: }
082: newProperties.setProperty(key, value);
083: }
084: System.out.println("Localized file : " + newFileName);// NOI18N
085: newProperties.store(output, "Localized in " + locale);// NOI18N
086: output.close();
087: }
088:
089: public static String parseEscapes(CharSequence text)
090: throws ParseException {
091: // For each character...
092: int length = text.length();
093: StringBuffer sb = new StringBuffer(length);
094: for (int iChar = 0; iChar < length;) {
095: char ch = text.charAt(iChar++);
096:
097: // Handle escapes.
098: if (ch == '\\') {
099: if (iChar >= length) {
100: throw new ParseException(
101: "Orphaned escape character.", iChar);// NOI18N
102: }
103: ch = text.charAt(iChar++);
104: if (ch == 'u') {
105: // A Unicode escape sequence.
106: if (iChar > length - 4) {
107: throw new ParseException(
108: "Malformed Unicode escape.", iChar);// NOI18N
109: }
110: int value = 0;
111: for (int iDigit = 4; iDigit > 0; iDigit--) {
112: int digitValue = Character.digit(text
113: .charAt(iChar++), 16);
114: if (digitValue < 0) {
115: throw new ParseException(
116: "Malformed Unicode escape.", iChar);// NOI18N
117: }
118: value = (value << 4) + digitValue;
119: }
120: sb.append((char) value);
121: } else {
122: // A standard escape sequence.
123: switch (ch) {
124: case 't':
125: ch = '\t';
126: break;// NOI18N
127: case 'r':
128: ch = '\r';
129: break;// NOI18N
130: case 'n':
131: ch = '\n';
132: break;// NOI18N
133: case 'f':
134: ch = '\f';
135: break;// NOI18N
136: }
137: sb.append(ch);
138: }
139:
140: continue;
141: }
142:
143: // An unescaped character.
144: sb.append(ch);
145: }
146:
147: return sb.toString();
148: }
149: }
|