[Generator-rt-devel] generator_runtime/src/java/generator/runtime/jsp TranslateTag.java, 1.2, 1.3
Brought to you by:
rickknowles
|
From: Rick K. <ric...@us...> - 2010-04-05 02:31:50
|
Update of /cvsroot/generator-rt/generator_runtime/src/java/generator/runtime/jsp In directory sfp-cvsdas-2.v30.ch3.sourceforge.com:/tmp/cvs-serv11801/src/java/generator/runtime/jsp Modified Files: TranslateTag.java Log Message: A bunch of translation tag changes and addons to the velocity mail rendering. A fix to the DBConnectionPool keep alive process also included. Index: TranslateTag.java =================================================================== RCS file: /cvsroot/generator-rt/generator_runtime/src/java/generator/runtime/jsp/TranslateTag.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** TranslateTag.java 30 Mar 2010 02:46:36 -0000 1.2 --- TranslateTag.java 5 Apr 2010 02:31:41 -0000 1.3 *************** *** 19,42 **** import generator.runtime.exception.ApplicationException; - import generator.runtime.log.Log; - import generator.runtime.log.LogManager; import generator.runtime.router.AttributesMap; - import generator.runtime.router.RouterConstants; - import generator.runtime.servlet.ContextInitializer; import generator.runtime.session.HttpSessionWrapper; - import generator.runtime.session.ServerSideApplicationState; - import generator.runtime.session.ServerSideUserState; import generator.runtime.session.ServletContextWrapper; ! import generator.runtime.utils.ApplicationProperties; ! import generator.runtime.utils.ParamUtils; ! import generator.runtime.utils.StringUtils; ! import generator.runtime.xml.XMLUtils; - import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; - import java.util.Hashtable; - import java.util.Map; import javax.servlet.http.HttpServletRequest; --- 19,30 ---- import generator.runtime.exception.ApplicationException; import generator.runtime.router.AttributesMap; import generator.runtime.session.HttpSessionWrapper; import generator.runtime.session.ServletContextWrapper; ! import generator.runtime.translation.TranslationBean; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import javax.servlet.http.HttpServletRequest; *************** *** 46,58 **** import javax.servlet.jsp.tagext.BodyTagSupport; - import org.w3c.dom.Document; - import org.w3c.dom.Node; - import org.w3c.dom.NodeList; - /** * Special tag to translate the contents using the translations cache for the selected language */ public class TranslateTag extends BodyTagSupport { - private final Log log = LogManager.getLog(TranslateTag.class); private Object[] parameters = new Object[5]; --- 34,41 ---- *************** *** 92,162 **** JspWriter out = bodyContent.getEnclosingWriter(); ! String language = getLanguage((HttpServletRequest) pageContext.getRequest()); ! Map translations = getTranslations(language, new ServletContextWrapper(pageContext.getServletContext())); ! out.print(translate(bodyString, this.parameters, translations)); bodyContent.clear(); // empty buffer return SKIP_BODY; } catch (IOException e) { throw new ApplicationException("Error during translate tag", e); - } // end of catch - - } - protected String getLanguage(HttpServletRequest req) { - ServerSideUserState ssus = new HttpSessionWrapper(req); - String sessionDefault = (String) ParamUtils.nvl(ssus.getAttribute( - RouterConstants.PREFERRED_LANGUAGE_PARAM), "english"); - return ParamUtils.getParameter(new AttributesMap(req), RouterConstants.PREFERRED_LANGUAGE_PARAM, sessionDefault); - } - - protected String translate(String template, Object params[], Map translations) { - String translated = null; - if (translations != null) { - translated = (String) translations.get(template); - } - if (translated == null) { - translated = template; - } - if (params != null) { - String tokens[][] = new String[params.length][2]; - for (int n = 0; n < params.length; n++) { - tokens[n][0] = "[#" + (n + 1) + "]"; - tokens[n][1] = ParamUtils.nvl(params[n], "").toString(); - } - translated = StringUtils.stringReplace(translated, tokens); - } - return translated; - } - - protected Map getTranslations(String language, ServerSideApplicationState ssas) { - String contextKey = "translationMap." + language; - synchronized (ssas.getSemaphore(contextKey)) { - Map translations = (Map) ssas.getAttribute(contextKey); - if (translations == null) { - ApplicationProperties props = (ApplicationProperties) ssas.getAttribute(ContextInitializer.ATTR_PROPS); - String translationSheet = props.stringProperty("translationSheet/@" + language, - "${application/@webroot}/WEB-INF/translations/" + language + ".xml"); - File file = new File(translationSheet); - if (!file.isFile()) { - log.warning("Translation file not found: " + translationSheet); - return null; - } - Document doc = XMLUtils.parseFileToXML(file); - NodeList items = doc.getDocumentElement().getChildNodes(); - translations = new Hashtable(); - for (int n = 0; n < items.getLength(); n++) { - Node node = items.item(n); - if (node.getNodeType() == Node.ELEMENT_NODE) { - String key = XMLUtils.getAttributeByName(node, "key"); - String text = XMLUtils.extractStringFromElement(node); - if ((key != null) && (text != null)) { - translations.put(key, text); - } - } - } - log.info("Loaded " + translations.size() + " translations for " + language + " from " + translationSheet); - ssas.setAttribute(contextKey, translations); - } - return translations; } } --- 75,87 ---- JspWriter out = bodyContent.getEnclosingWriter(); ! HttpServletRequest req = (HttpServletRequest) pageContext.getRequest(); ! String language = TranslationBean.getLanguage(new HttpSessionWrapper(req), new AttributesMap(req)); ! TranslationBean translation = new TranslationBean(new ServletContextWrapper(pageContext.getServletContext()), language); ! out.print(translation.translate(bodyString, this.parameters)); bodyContent.clear(); // empty buffer return SKIP_BODY; } catch (IOException e) { throw new ApplicationException("Error during translate tag", e); } } |