[Generator-rt-devel] generator_runtime/src/java/generator/runtime/translation BuildTranslationFile
Brought to you by:
rickknowles
|
From: Rick K. <ric...@us...> - 2010-03-26 01:21:52
|
Update of /cvsroot/generator-rt/generator_runtime/src/java/generator/runtime/translation In directory sfp-cvsdas-2.v30.ch3.sourceforge.com:/tmp/cvs-serv28711/src/java/generator/runtime/translation Added Files: BuildTranslationFileFromXSLController.java Log Message: added <gen:translate> tag support --- NEW FILE: BuildTranslationFileFromXSLController.java --- /* * Generator Runtime Servlet Framework * Copyright (C) 2004 Rick Knowles * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * Version 2 as published by the Free Software Foundation. * * This program 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 General Public License Version 2 for more details. * * You should have received a copy of the GNU General Public License * Version 2 along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ package generator.runtime.translation; import generator.runtime.controller.Controller; import generator.runtime.log.Log; import generator.runtime.log.LogManager; import generator.runtime.utils.ParamUtils; import generator.runtime.xml.XMLUtils; import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class BuildTranslationFileFromXSLController extends Controller { private final Log log = LogManager.getLog(BuildTranslationFileFromXSLController.class); public String execute(Map attributes) { // Scan all xsl sheets under folder, and resolve unique translateables File parentFolder = getParentFolder(); Set translateablePhrases = new HashSet(); Map exceptionsAndFile = new HashMap(); parseChildXSLSheetsForTranslateables(parentFolder, translateablePhrases, exceptionsAndFile); addExtraTranslations(translateablePhrases); log.info("Loaded " + translateablePhrases.size() + " translations from XSL sheets in " + parentFolder.getPath()); // Load the supplied translations sheet File translationsFile = getTranslationsFile(attributes); Map translations = loadTranslations(translationsFile); // Write out the new translation sheet String out = formatTranslationSheet(translateablePhrases, translations, exceptionsAndFile); attributes.put("translationOutput", out); return "OK"; } protected void addExtraTranslations(Set translateablePhrases) {} protected File getParentFolder() { return new File(getProperties().stringProperty("application/@webroot", "."), "WEB-INF/xsl"); } protected File getTranslationsFile(Map attributes) { String language = ParamUtils.getParameter(attributes, "language", "japanese"); return new File(getProperties().stringProperty("application/@webroot", "."), "WEB-INF/translations/" + language + ".xml"); } protected void parseChildXSLSheetsForTranslateables(File parentFolder, Set translateablePhrases, Map exceptionsAndFile) { log.info("Scanning folder for translateable XSL sheets: " + parentFolder.getPath()); File children[] = parentFolder.listFiles(); if (children != null) { for (int n = 0; n < children.length; n++) { if (children[n].isDirectory()) { parseChildXSLSheetsForTranslateables(children[n], translateablePhrases, exceptionsAndFile); } else if (children[n].isFile() && children[n].getName().endsWith(".xsl")) { log.info("Scanning file for translateable XSL sheets: " + children[n].getName()); Document doc = XMLUtils.parseFileToXML(children[n]); scanForTranslateTemplateCall(children[n], doc.getDocumentElement(), translateablePhrases, exceptionsAndFile); } else { log.debug("Skipping non-XSL sheet: " + children[n].getName()); } } } } protected void scanForTranslateTemplateCall(File file, Node node, Set translateablePhrases, Map exceptionsAndFile) { NodeList children = node.getChildNodes(); for (int n = 0; n < children.getLength(); n++) { Node child = children.item(n); if (child.getNodeType() != Node.ELEMENT_NODE) { scanForTranslateTemplateCall(file, child, translateablePhrases, exceptionsAndFile); } else if (child.getNodeName().indexOf("call-template") == -1) { scanForTranslateTemplateCall(file, child, translateablePhrases, exceptionsAndFile); } else { String templateName = XMLUtils.getAttributeByName(child, "name"); if (!templateName.equals("translate")) { scanForTranslateTemplateCall(file, child, translateablePhrases, exceptionsAndFile); } else { // Get the <xsl:with-param name="template"/> element NodeList translateChildren = child.getChildNodes(); for (int k = 0; k < translateChildren.getLength(); k++) { Node translateChild = translateChildren.item(k); if ((translateChild.getNodeType() == Node.ELEMENT_NODE) && (translateChild.getNodeName().indexOf("with-param") != -1)) { String paramName = XMLUtils.getAttributeByName(translateChild, "name"); if (paramName.equals("template")) { String template = XMLUtils.xmlToString("none", translateChild, false, false, null); if (template.equals("<none/>")) { template = XMLUtils.xmlToString("none", child, true, false, null); template = template.substring(6); template = template.substring(0, template.length() - 7); exceptionsAndFile.put(template, file); } else { template = template.substring(6); template = template.substring(0, template.length() - 7); translateablePhrases.add(template); } } else { scanForTranslateTemplateCall(file, translateChild, translateablePhrases, exceptionsAndFile); } } else { scanForTranslateTemplateCall(file, translateChild, translateablePhrases, exceptionsAndFile); } } } } } } protected Map loadTranslations(File translationFile) { Map out = new HashMap(); Document doc = XMLUtils.parseFileToXML(translationFile); Node top = doc.getDocumentElement(); NodeList translations = top.getChildNodes(); for (int n = 0; n < translations.getLength(); n++) { Node child = translations.item(n); if ((child.getNodeType() == Node.ELEMENT_NODE) && (child.getNodeName().equalsIgnoreCase("translation"))) { String key = XMLUtils.getAttributeByName(child, "key"); String translation = XMLUtils.extractStringFromElement(child); out.put(key, translation); } } log.info("Loaded " + out.size() + " translations from " + translationFile.getPath()); return out; } private static final String CR_LF = System.getProperty("line.separator"); protected String formatTranslationSheet(Set translateablePhrases, Map translations, Map exceptionsAndFile) { String keys[] = (String[]) translateablePhrases.toArray(new String[translateablePhrases.size()]); Arrays.sort(keys); List notFound = new ArrayList(); List found = new ArrayList(); // Build case-insensitive lookup Map lcTranslationKeys = new HashMap(); for (Iterator i = translations.keySet().iterator(); i.hasNext(); ) { String key = (String) i.next(); lcTranslationKeys.put(key.toLowerCase(), key); } StringBuffer out = new StringBuffer(); out.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>").append(CR_LF); out.append("<translations>").append(CR_LF); out.append(" <!-- found in translation sheet and in XSL -->").append(CR_LF); for (int n = 0; n < keys.length; n++) { // For this xsl entry, get the translations file matched case-insensitive String translationKey = (String) lcTranslationKeys.get(keys[n].toLowerCase()); if (translationKey == null) { notFound.add(keys[n]); continue; } String translation = (String) translations.get(translationKey); if (translation != null) { out.append(" <translation key=\"").append(XMLUtils.attValue(keys[n])).append("\">"); out.append(XMLUtils.tagValue(translation)).append("</translation>").append(CR_LF); found.add(keys[n].toLowerCase()); } else { notFound.add(keys[n]); } } out.append(CR_LF); out.append(" <!-- found in translation sheet, but not found in XSL (dont delete, might be used in Java code) -->").append(CR_LF); String extraKeys[] = (String []) translations.keySet().toArray(new String[translations.size()]); Arrays.sort(extraKeys); for (int n = 0; n < extraKeys.length; n++) { if (found.contains(extraKeys[n].toLowerCase())) { continue; } String translation = (String) translations.remove(extraKeys[n]); if (translation != null) { out.append(" <translation key=\"").append(XMLUtils.attValue(extraKeys[n])).append("\">"); out.append(XMLUtils.tagValue(translation)).append("</translation>").append(CR_LF); } } out.append(CR_LF); out.append(" <!-- not found in translation sheet, but found in XSL (NEED TRANSLATIONS FOR THESE) -->").append(CR_LF); out.append(" <!--").append(CR_LF); String notFoundKeys[] = (String[]) notFound.toArray(new String[notFound.size()]); Arrays.sort(notFoundKeys); for (int n = 0; n < notFoundKeys.length; n++) { out.append(" <translation key=\"").append(XMLUtils.attValue(notFoundKeys[n])).append("\">"); out.append("</translation>").append(CR_LF); } out.append(" -->").append(CR_LF); out.append(" <!-- exceptions found in XSL (NEED TRANSLATIONS FOR THESE) -->").append(CR_LF); String exceptionKeys[] = (String[]) exceptionsAndFile.keySet().toArray(new String[exceptionsAndFile.size()]); Arrays.sort(exceptionKeys); for (int n = 0; n < exceptionKeys.length; n++) { File fromFile = (File) exceptionsAndFile.get(exceptionKeys[n]); out.append(" <!-- File: ").append(fromFile.getName()).append(CR_LF); out.append(" ").append(exceptionKeys[n]).append(CR_LF); out.append(" -->").append(CR_LF); } out.append("</translations>").append(CR_LF); return out.toString(); } } |