/* * Moonlight|3D * Copyright (C) 2007 The Moonlight|3D team * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * Created on Oct 4, 2007 */ package eu.moonlight3d.ant; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Result; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactoryConfigurationError; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.apache.tools.ant.Task; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.w3c.dom.Text; import org.xml.sax.SAXException; public class UpdateTranslationTask extends Task { private String translation; private String dir; private HashMap> translations; private HashMap> translationFinished; public void setDir(String dir) { this.dir=dir; } public String getDir() { return dir; } public void setTranslation(String translation) { this.translation=translation; } public String getTranslation() { return translation; } private void readTranslation() { // parse Qt's ts file format here Document translationDoc; File translationFile=new File(translation); if(!translationFile.exists()) { return; } try { translationDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(translationFile.toURI().toString()); Element root = translationDoc.getDocumentElement(); if(!"ts".equals(root.getNodeName().toLowerCase())) { return; } NodeList contexts = root.getElementsByTagName("context"); for(int i=0;i contextMap=translations.get(contextName); HashMap finishedMap=translationFinished.get(contextName); if(contextMap==null) { contextMap=new HashMap(); finishedMap=new HashMap(); translations.put(contextName,contextMap); translationFinished.put(contextName,finishedMap); } NodeList messages = context.getElementsByTagName("message"); for(int j=0;j" + translationString); if(translationString!=null) { contextMap.put(sourceString, translationString); } else { contextMap.put(sourceString, sourceString); } if(translation.getAttribute("type").equals("unfinished")) { finishedMap.put(source.getChildNodes().item(0).getNodeValue(),false); } else { finishedMap.put(source.getChildNodes().item(0).getNodeValue(),true); } } } } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private void scanSourceFile(File file) { System.out.println(file.getAbsolutePath()); try { FileInputStream fileInput=new FileInputStream(file); BufferedReader input=new BufferedReader(new InputStreamReader(fileInput)); String line; String context=file.getName().substring(0,(int) (file.getName().length()-5)); ArrayList sourceStrings=new ArrayList(); while((line=input.readLine())!=null) { if(line.trim().startsWith("package")) { context=line.substring(7,line.indexOf(";")).trim() + "." + context; } int index=line.indexOf("L10N.translate"); if(index!=-1) { // TODO extract string to translate from reminder of line. String reminder=line.substring(index); int start=reminder.indexOf('"'); if(start==-1) { continue; } int stop=reminder.substring(start+1).indexOf('"'); if(stop==-1) { continue; } String source=reminder.substring(start+1,start+stop+1); System.out.println("Message: " + source); sourceStrings.add(source); } } HashMap oldTranslations=null; HashMap newTranslations=new HashMap(); HashMap oldFinished=null; HashMap newFinished=new HashMap(); if(translations.get(context)!=null) { oldTranslations=translations.get(context); oldFinished=translationFinished.get(context); } for(String source : sourceStrings) { if(oldTranslations!=null && oldTranslations.get(source)!=null) { newTranslations.put(source, oldTranslations.get(source)); newFinished.put(source, oldFinished.get(source)); } else { newTranslations.put(source, source); newFinished.put(source, false); } } if(newTranslations.keySet().size()>0) { translations.put(context, newTranslations); translationFinished.put(context, newFinished); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private void scanJavaSourceCode(File directory) { for(File file : directory.listFiles()) { if(file.isDirectory()) { scanJavaSourceCode(file); } else if(file.isFile()) { if(file.getName().toLowerCase().endsWith(".java")) { scanSourceFile(file); } } } } private void writeTranslation() { try { Document translationDoc=DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); Element root=translationDoc.createElement("TS"); root.setAttribute("version", "1.1"); for(String context : translations.keySet()) { Element contextElement=translationDoc.createElement("context"); Element nameElement=translationDoc.createElement("name"); Text nameText=translationDoc.createTextNode(context); nameElement.appendChild(nameText); contextElement.appendChild(nameElement); HashMap contextTranslations=translations.get(context); HashMap contextFinished=translationFinished.get(context); for(String message : contextTranslations.keySet()) { Element messageElement=translationDoc.createElement("message"); Element sourceElement=translationDoc.createElement("source"); Text sourceText=translationDoc.createTextNode(message); sourceElement.appendChild(sourceText); Element translationElement=translationDoc.createElement("translation"); if(!contextFinished.get(message)) { translationElement.setAttribute("type", "unfinished"); } Text translationText=translationDoc.createTextNode(contextTranslations.get(message)); translationElement.appendChild(translationText); messageElement.appendChild(sourceElement); messageElement.appendChild(translationElement); contextElement.appendChild(messageElement); } root.appendChild(contextElement); } translationDoc.appendChild(root); Source domSource = new DOMSource(translationDoc); Result streamResult = new StreamResult(new File(translation)); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty("indent","yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(domSource, streamResult); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void execute() { File directory=new File(dir); translations=new HashMap>(); translationFinished=new HashMap>(); readTranslation(); if(directory.exists()) { scanJavaSourceCode(directory); } writeTranslation(); } public static void main(String[] args) { UpdateTranslationTask task=new UpdateTranslationTask(); task.setDir("/home/gregor/progs/Moonlight/workspace/mlframework/src/"); task.setTranslation("test.ts"); task.execute(); } }