001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (license2)
004: * Initial Developer: H2 Group
005: */
006: package org.h2.tools.i18n;
007:
008: import java.io.BufferedWriter;
009: import java.io.ByteArrayInputStream;
010: import java.io.ByteArrayOutputStream;
011: import java.io.File;
012: import java.io.FileInputStream;
013: import java.io.FileOutputStream;
014: import java.io.FileWriter;
015: import java.io.IOException;
016: import java.io.InputStreamReader;
017: import java.io.LineNumberReader;
018: import java.io.OutputStreamWriter;
019: import java.io.PrintWriter;
020: import java.io.RandomAccessFile;
021: import java.util.Enumeration;
022: import java.util.Properties;
023:
024: import org.h2.tools.code.CheckTextFiles;
025: import org.h2.tools.indexer.HtmlConverter;
026: import org.h2.util.FileUtils;
027: import org.h2.util.IOUtils;
028: import org.h2.util.SortedProperties;
029: import org.h2.util.StringUtils;
030:
031: /**
032: * This class converts a file stored in the UTF-8 encoding format to
033: * a properties file and vice versa.
034: */
035: public class PropertiesToUTF8 {
036:
037: public static void main(String[] args) throws Exception {
038: convert("bin/org/h2/res", ".");
039: convert("bin/org/h2/server/web/res", ".");
040: }
041:
042: static void propertiesToTextUTF8(String source, String target)
043: throws Exception {
044: if (!new File(source).exists()) {
045: return;
046: }
047: Properties prop = FileUtils.loadProperties(source);
048: FileOutputStream out = new FileOutputStream(target);
049: PrintWriter writer = new PrintWriter(new OutputStreamWriter(
050: out, "UTF-8"));
051: // keys is sorted
052: for (Enumeration en = prop.keys(); en.hasMoreElements();) {
053: String key = (String) en.nextElement();
054: String value = prop.getProperty(key, null);
055: writer.println("@" + key);
056: writer.println(value);
057: writer.println();
058: }
059: writer.close();
060: }
061:
062: static void textUTF8ToProperties(String source, String target)
063: throws Exception {
064: if (!new File(source).exists()) {
065: return;
066: }
067: LineNumberReader reader = new LineNumberReader(
068: new InputStreamReader(new FileInputStream(source),
069: "UTF-8"));
070: Properties prop = new SortedProperties();
071: StringBuffer buff = new StringBuffer();
072: String key = null;
073: while (true) {
074: String line = reader.readLine();
075: if (line == null) {
076: break;
077: }
078: line = line.trim();
079: if (line.length() == 0) {
080: continue;
081: }
082: if (line.startsWith("@")) {
083: if (key != null) {
084: prop.setProperty(key, buff.toString());
085: buff.setLength(0);
086: }
087: key = line.substring(1);
088: } else {
089: if (buff.length() > 0) {
090: buff.append(System.getProperty("line.separator"));
091: }
092: buff.append(line);
093: }
094: }
095: if (key != null) {
096: prop.setProperty(key, buff.toString());
097: }
098: storeProperties(prop, target);
099: }
100:
101: private static void convert(String source, String target)
102: throws Exception {
103: File[] list = new File(source).listFiles();
104: for (int i = 0; list != null && i < list.length; i++) {
105: File f = list[i];
106: if (!f.getName().endsWith(".properties")) {
107: continue;
108: }
109: FileInputStream in = new FileInputStream(f);
110: InputStreamReader r = new InputStreamReader(in, "UTF-8");
111: String s = IOUtils.readStringAndClose(r, -1);
112: in.close();
113: String name = f.getName();
114: String utf8, html;
115: if (name.startsWith("utf8")) {
116: utf8 = HtmlConverter.convertHtmlToString(s);
117: html = HtmlConverter.convertStringToHtml(utf8);
118: RandomAccessFile out = new RandomAccessFile("_"
119: + name.substring(4), "rw");
120: out.write(html.getBytes());
121: out.setLength(out.getFilePointer());
122: out.close();
123: } else {
124: new CheckTextFiles().checkOrFixFile(f, false, false);
125: html = s;
126: utf8 = HtmlConverter.convertHtmlToString(html);
127: // s = unescapeHtml(s);
128: utf8 = StringUtils.javaDecode(utf8);
129: FileOutputStream out = new FileOutputStream("_utf8"
130: + f.getName());
131: OutputStreamWriter w = new OutputStreamWriter(out,
132: "UTF-8");
133: w.write(utf8);
134: w.close();
135: out.close();
136: }
137: String java = StringUtils.javaEncode(utf8);
138: java = StringUtils.replaceAll(java, "\\r", "\r");
139: java = StringUtils.replaceAll(java, "\\n", "\n");
140: RandomAccessFile out = new RandomAccessFile(
141: "_java." + name, "rw");
142: out.write(java.getBytes());
143: out.setLength(out.getFilePointer());
144: out.close();
145: }
146: }
147:
148: static void storeProperties(Properties p, String fileName)
149: throws IOException {
150: ByteArrayOutputStream out = new ByteArrayOutputStream();
151: p.store(out, null);
152: ByteArrayInputStream in = new ByteArrayInputStream(out
153: .toByteArray());
154: InputStreamReader reader = new InputStreamReader(in,
155: "ISO8859-1");
156: LineNumberReader r = new LineNumberReader(reader);
157: FileWriter w = new FileWriter(fileName);
158: PrintWriter writer = new PrintWriter(new BufferedWriter(w));
159: while (true) {
160: String line = r.readLine();
161: if (line == null) {
162: break;
163: }
164: if (!line.startsWith("#")) {
165: writer.println(line);
166: }
167: }
168: writer.close();
169: }
170:
171: }
|