[Generator-rt-devel] generator_runtime/src/java/generator/runtime/jsp TranslateTag.java, NONE, 1.1
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/jsp In directory sfp-cvsdas-2.v30.ch3.sourceforge.com:/tmp/cvs-serv28711/src/java/generator/runtime/jsp Modified Files: ForEachTag.java Added Files: TranslateTag.java Log Message: added <gen:translate> tag support --- NEW FILE: TranslateTag.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.jsp; 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; import javax.servlet.jsp.JspTagException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.BodyContent; 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; public void setParameters(Object[] parameters) { this.parameters = parameters; } public void setParameterList(Collection parameters) { this.parameters = new ArrayList(parameters).toArray(); } public int doAfterBody() throws JspTagException { try { BodyContent bodyContent = super.getBodyContent(); String bodyString = bodyContent.getString(); 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; } } } Index: ForEachTag.java =================================================================== RCS file: /cvsroot/generator-rt/generator_runtime/src/java/generator/runtime/jsp/ForEachTag.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** ForEachTag.java 23 Dec 2006 06:41:31 -0000 1.3 --- ForEachTag.java 26 Mar 2010 01:21:44 -0000 1.4 *************** *** 26,65 **** import java.util.Iterator; ! import javax.servlet.jsp.JspException; /** * Special tag to extend forEach to allow SearchResult object contents to be iterated over easily */ ! public class ForEachTag extends org.apache.taglibs.standard.tag.el.core.ForEachTag { ! ! public int doStartTag() throws JspException { ! int retCode = super.doStartTag(); ! if ((retCode == EVAL_BODY_INCLUDE) || (retCode == EVAL_BODY_AGAIN)) { ! setExtraLoopVariables(); ! } ! return retCode; ! } ! ! public int doAfterBody() throws JspException { ! int retCode = super.doAfterBody(); ! if ((retCode == EVAL_BODY_INCLUDE) || (retCode == EVAL_BODY_AGAIN)) { ! setExtraLoopVariables(); ! } ! return retCode; ! } ! protected void setExtraLoopVariables() throws JspException { ! Object item = getCurrent(); ! if ((item != null) && (item instanceof SearchResult)) { ! SearchResult sr = (SearchResult) item; DBConnectionWrapper wrapper = DBConnectionWrapperAllocationFilter.getConnectionFromRequest( this.pageContext.getRequest()); DBPeerFactory dbPeerFactory = (DBPeerFactory) pageContext.getServletContext().getAttribute( DBPeerFactoryContextInitializer.ATTR_PEER_FACTORY); ! for (Iterator i = sr.keySet().iterator(); i.hasNext(); ) { ! String key = (String) i.next(); ! this.pageContext.setAttribute(key, sr.get(wrapper, dbPeerFactory, key)); ! } ! } ! } } --- 26,52 ---- import java.util.Iterator; ! import javax.servlet.jsp.JspTagException; /** * Special tag to extend forEach to allow SearchResult object contents to be iterated over easily */ ! public class ForEachTag extends org.apache.taglibs.standard.tag.rt.core.ForEachTag { ! // private final Log log = new Log(ForEachTag.class); ! protected Object next() throws JspTagException { ! Object item = super.next(); ! if ((item != null) && (item instanceof SearchResult)) { ! SearchResult sr = (SearchResult) item; DBConnectionWrapper wrapper = DBConnectionWrapperAllocationFilter.getConnectionFromRequest( this.pageContext.getRequest()); DBPeerFactory dbPeerFactory = (DBPeerFactory) pageContext.getServletContext().getAttribute( DBPeerFactoryContextInitializer.ATTR_PEER_FACTORY); ! for (Iterator i = sr.keySet().iterator(); i.hasNext(); ) { ! String key = (String) i.next(); ! Object value = sr.get(wrapper, dbPeerFactory, key); ! this.pageContext.setAttribute(key, value); ! } ! } ! return item; ! } } |