KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > tools > code > PropertiesToUTF8


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.tools.code;
6
7 import java.io.File JavaDoc;
8 import java.io.FileInputStream JavaDoc;
9 import java.io.FileOutputStream JavaDoc;
10 import java.io.InputStreamReader JavaDoc;
11 import java.io.OutputStreamWriter JavaDoc;
12 import java.io.RandomAccessFile JavaDoc;
13
14 import org.h2.server.web.PageParser;
15 import org.h2.util.IOUtils;
16 import org.h2.util.StringUtils;
17
18 public class PropertiesToUTF8 {
19     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
20         File JavaDoc[] list = new File JavaDoc("bin/org/h2/web/res").listFiles();
21         for(int i=0; i<list.length; i++) {
22             File JavaDoc f = list[i];
23             if(!f.getName().endsWith(".properties")) {
24                 continue;
25             }
26             FileInputStream JavaDoc in = new FileInputStream JavaDoc(f);
27             InputStreamReader JavaDoc r = new InputStreamReader JavaDoc(in, "UTF-8");
28             String JavaDoc s = IOUtils.readStringAndClose(r, -1);
29             in.close();
30             String JavaDoc name = f.getName();
31             if(name.startsWith("utf8")) {
32                 s = PageParser.escapeHtml(s, false);
33                 RandomAccessFile JavaDoc out = new RandomAccessFile JavaDoc(name.substring(4), "rw");
34                 out.write(s.getBytes());
35                 out.close();
36             } else {
37                 new CheckTextFiles().checkOrFixFile(f, false, false);
38                 s = unescapeHtml(s);
39                 s = StringUtils.javaDecode(s);
40                 FileOutputStream JavaDoc out = new FileOutputStream JavaDoc("utf8" + f.getName());
41                 OutputStreamWriter JavaDoc w = new OutputStreamWriter JavaDoc(out, "UTF-8");
42                 w.write(s);
43                 w.close();
44                 out.close();
45             }
46         }
47     }
48
49     private static String JavaDoc unescapeHtml(String JavaDoc s) {
50         String JavaDoc codes = "&lt; < &amp; & &gt; > &Auml; \u00c4 &Ouml; \u00d6 &Uuml; \u00dc &auml; \u00e4 &ouml; \u00f6 &uuml; \u00fc &ntilde; \u00f1 &oacute; \u00f3 &Iacute; \u00cd &ccedil; \u00e7 &eagrave; \u00e8 &ecirc; \u00ea &Uacute; \u00da &aacute; \u00e1 &uacute; \u00fa &eacute; \u00e9 &egrave; \u00e8 &icirc; \u00ee";
51         String JavaDoc[] list = StringUtils.arraySplit(codes, ' ', false);
52         for(int i=0; i<list.length; i+=2) {
53             s = StringUtils.replaceAll(s, list[i], list[i+1]);
54         }
55         if(s.indexOf("&") >= 0) {
56             throw new Error JavaDoc("??? " + s);
57         }
58         return s;
59     }
60 }
61
Popular Tags