Fix: too harsh return value check.
[gigi.git] / util / org / cacert / gigi / util / FetchLocales.java
1 package org.cacert.gigi.util;
2
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.io.InputStreamReader;
7 import java.net.URL;
8 import java.net.URLConnection;
9 import java.util.Scanner;
10
11 import javax.xml.parsers.DocumentBuilder;
12 import javax.xml.parsers.DocumentBuilderFactory;
13 import javax.xml.parsers.ParserConfigurationException;
14 import javax.xml.transform.OutputKeys;
15 import javax.xml.transform.Transformer;
16 import javax.xml.transform.TransformerException;
17 import javax.xml.transform.TransformerFactory;
18 import javax.xml.transform.dom.DOMSource;
19 import javax.xml.transform.stream.StreamResult;
20
21 import org.w3c.dom.Document;
22 import org.w3c.dom.Element;
23 import org.w3c.dom.Node;
24
25 public class FetchLocales {
26
27     public static final String DOWNLOAD_SERVER = "translations.cacert.org";
28
29     public static final String PO_URL_TEMPLATE = "http://" + DOWNLOAD_SERVER + "/export/cacert/%/messages.po";
30
31     public static final String[] AUTO_LANGS = new String[] {
32             "en", "de", "nl", "pt_BR", "fr", "sv", "it", "es", "hu", "fi", "ja", "bg", "pt", "da", "pl", "zh_CN", "ru", "lv", "cs", "zh_TW", "el", "tr", "ar"
33     };
34
35     public static void main(String[] args) throws IOException, ParserConfigurationException, TransformerException {
36         System.out.println("downloading locales ...");
37         File locale = new File("locale");
38         if ( !locale.isDirectory() && !locale.mkdir()) {
39             throw new IOException("Could not create locales directory.");
40         }
41
42         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
43         DocumentBuilder db = dbf.newDocumentBuilder();
44         for (String lang : AUTO_LANGS) {
45             Document doc = db.newDocument();
46             doc.appendChild(doc.createElement("translations"));
47             URL fetch = new URL(PO_URL_TEMPLATE.replace("%", lang));
48             URLConnection uc = fetch.openConnection();
49             Scanner sc = new Scanner(new InputStreamReader(uc.getInputStream(), "UTF-8"));
50             String s = readLine(sc);
51             StringBuffer contents = new StringBuffer();
52             String id = "";
53             while (s != null) {
54                 if (s.startsWith("msgid")) {
55                     contents.delete(0, contents.length());
56                     s = readString(s, sc, contents);
57                     id = contents.toString();
58                     continue;
59                 } else if (s.startsWith("msgstr")) {
60                     contents.delete(0, contents.length());
61                     // System.out.println("msgstr");
62                     s = readString(s, sc, contents);
63                     String msg = contents.toString().replace("\\\"", "\"").replace("\\n", "\n");
64                     insertTranslation(doc, id, msg);
65                 } else if (s.startsWith("#")) {
66                     // System.out.println(s);
67                 } else if (s.equals("") || s.equals("\r")) {
68
69                 } else {
70                     System.out.println("unknown line: " + s);
71                 }
72                 s = readLine(sc);
73             }
74             TransformerFactory tFactory = TransformerFactory.newInstance();
75             Transformer transformer = tFactory.newTransformer();
76
77             DOMSource source = new DOMSource(doc);
78             FileOutputStream fos = new FileOutputStream(new File(locale, lang + ".xml"));
79             StreamResult result = new StreamResult(fos);
80             transformer.setOutputProperty(OutputKeys.INDENT, "yes");
81             transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
82             transformer.transform(source, result);
83             fos.close();
84         }
85         System.out.println("Done.");
86     }
87
88     private static String readLine(Scanner sc) {
89         String line = sc.findWithinHorizon("[^\n]*\n", 0);
90         if (line == null) {
91             return null;
92         }
93         return line.substring(0, line.length() - 1);
94     }
95
96     private static void insertTranslation(Document doc, String id, String msg) {
97         Node idN = doc.createTextNode(id);
98         Node textN = doc.createTextNode(msg);
99         Element tr = doc.createElement("translation");
100         Element e = doc.createElement("id");
101         e.appendChild(idN);
102         tr.appendChild(e);
103         e = doc.createElement("msg");
104         e.appendChild(textN);
105         tr.appendChild(e);
106         doc.getDocumentElement().appendChild(tr);
107     }
108
109     private static String readString(String head, Scanner sc, StringBuffer contents) throws IOException {
110         head = head.split(" ", 2)[1];
111         contents.append(head.substring(1, head.length() - 1));
112         String s;
113         while ((s = readLine(sc)) != null) {
114             if ( !s.startsWith("\"")) {
115                 break;
116             }
117             contents.append(s.substring(1, s.length() - 1));
118         }
119         return s;
120     }
121
122 }