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