bruno 2003/07/26 07:05:05
Modified: src/blocks/woody/java/org/apache/cocoon/woody/acting HandleFormSubmitAction.java src/blocks/woody/java/org/apache/cocoon/woody/flow/javascript woody.js src/blocks/woody/java/org/apache/cocoon/woody/transformation WidgetReplacingPipe.java WoodyTemplateTransformer.java src/blocks/woody/samples/flow woody_flow_example.js Log: removed hardcoded locales Revision Changes Path 1.7 +7 -1 cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/acting/HandleFormSubmitAction.java Index: HandleFormSubmitAction.java =================================================================== RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/acting/HandleFormSubmitAction.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- HandleFormSubmitAction.java 16 Jul 2003 13:59:12 -0000 1.6 +++ HandleFormSubmitAction.java 26 Jul 2003 14:05:05 -0000 1.7 @@ -60,6 +60,7 @@ import org.apache.cocoon.woody.FormContext; import org.apache.cocoon.woody.FormHandler; import org.apache.cocoon.woody.formmodel.Form; +import org.apache.cocoon.i18n.I18nUtils; import org.apache.excalibur.source.Source; import java.util.Map; @@ -86,6 +87,11 @@ String formAttribute = parameters.getParameter("attribute-name"); String formHandlerClassName = parameters.getParameter("formhandler", null); + Locale locale = Locale.getDefault(); + String localeStr = parameters.getParameter("locale", null); + if (localeStr != null) + locale = I18nUtils.parseLocale(localeStr, locale); + Source source = resolver.resolveURI(formSource); try { Form form = formManager.createForm(source); @@ -101,7 +107,7 @@ form.setFormHandler(formHandler); } - FormContext formContext = new FormContext(request, Locale.US); + FormContext formContext = new FormContext(request, locale); boolean finished = form.process(formContext); request.setAttribute(formAttribute, form); 1.7 +10 -6 cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/flow/javascript/woody.js Index: woody.js =================================================================== RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/flow/javascript/woody.js,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- woody.js 25 Jul 2003 09:05:35 -0000 1.6 +++ woody.js 26 Jul 2003 14:05:05 -0000 1.7 @@ -104,12 +104,16 @@ return this.formWidget; } -Form.prototype.show = function(uri, validator) { +Form.prototype.show = function(uri, validator, locale) { var lastWebCont = this.lastWebContinuation; // create a continuation, the invocation of which will resend // the page: this will be used to implement automated "back" // navigation var wk = this.start(lastWebCont); + + if (locale == null) + locale = java.util.Locale.getDefault(); + while (true) { if (cocoon.request == null) { // this continuation has been invalidated @@ -117,7 +121,7 @@ handleInvalidContinuation(); Woody.suicide(); } - var thisWebCont = this._show(uri, wk); + var thisWebCont = this._show(uri, locale, wk); // _show creates a continuation, the invocation of which // will return right here: it is used to implement // automated "next" navigation @@ -126,9 +130,9 @@ handleInvalidContinuation(); suicide(); } + var formContext = - new Packages.org.apache.cocoon.woody.FormContext(this.woody.request, - java.util.Locale.US); + new Packages.org.apache.cocoon.woody.FormContext(this.woody.request, locale); var finished = this.form.process(formContext); var evt = formContext.getActionEvent(); if (evt != null) { @@ -145,10 +149,10 @@ } } -Form.prototype._show = function(uri, lastWebCont, timeToLive) { +Form.prototype._show = function(uri, locale, lastWebCont, timeToLive) { var k = new Continuation(); var wk = this.woody.makeWebContinuation(k, lastWebCont, timeToLive); - var bizData = { "woody-form": this.form }; + var bizData = { "woody-form": this.form, "locale" : locale }; this.lastWebContinuation = wk; this.woody.forwardTo(uri, bizData, wk); Woody.suicide(); 1.6 +34 -16 cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/transformation/WidgetReplacingPipe.java Index: WidgetReplacingPipe.java =================================================================== RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/transformation/WidgetReplacingPipe.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- WidgetReplacingPipe.java 25 Jul 2003 11:23:10 -0000 1.5 +++ WidgetReplacingPipe.java 26 Jul 2003 14:05:05 -0000 1.6 @@ -62,7 +62,7 @@ import org.apache.cocoon.woody.formmodel.Widget; import org.apache.cocoon.woody.formmodel.Form; import org.apache.cocoon.xml.AbstractXMLPipe; -import org.apache.commons.jxpath.JXPathContext; +import org.apache.cocoon.i18n.I18nUtils; import org.apache.commons.jxpath.JXPathException; import org.xml.sax.Attributes; import org.xml.sax.SAXException; @@ -105,14 +105,15 @@ protected Widget widget; /** Boolean indicating wether the current widget requires special repeater-treatement. */ protected boolean repeaterWidget; + protected WoodyTemplateTransformer.InsertStylingContentHandler stylingHandler = new WoodyTemplateTransformer.InsertStylingContentHandler(); - protected JXPathContext jxpathContext; + protected WoodyTemplateTransformer pipeContext; - public void init(Widget contextWidget, JXPathContext jxpathContext) { + public void init(Widget contextWidget, WoodyTemplateTransformer pipeContext) { this.contextWidget = contextWidget; inWidgetElement = false; elementNestingCounter = 0; - this.jxpathContext = jxpathContext; + this.pipeContext = pipeContext; } public void startElement(String namespaceURI, String localName, String qName, Attributes attributes) @@ -159,10 +160,7 @@ throw new SAXException("Detected nested wt:form-template elements, this is not allowed."); contentHandler.startPrefixMapping(Constants.WI_PREFIX, Constants.WI_NS); - // Note: since the wt:form-template element is the top-level element in which - // all other widget elements must be nested, we can safely assume here that this - // instance of the WidgetReplacingPipe == WoodyTemplateTransformer - WoodyTemplateTransformer wtt = (WoodyTemplateTransformer)this; + // ====> Retrieve the form // first look for the form using the location attribute, if any String formJXPath = attributes.getValue("location"); @@ -172,21 +170,21 @@ attrsCopy.removeAttribute(attributes.getIndex("location")); attributes = attrsCopy; - Object form = jxpathContext.getValue(formJXPath); + Object form = pipeContext.getJXPathContext().getValue(formJXPath); if (form == null) throw new SAXException("No form found at location \"" + formJXPath + "\"."); if (!(form instanceof Form)) throw new SAXException("Object returned by expression \"" + formJXPath + "\" is not a Woody Form."); contextWidget = (Form)form; - } else if (wtt.attributeName != null) { // then see if an attribute-name was specified - contextWidget = (Form)wtt.request.getAttribute(wtt.attributeName); + } else if (pipeContext.getAttributeName() != null) { // then see if an attribute-name was specified + contextWidget = (Form)pipeContext.getRequest().getAttribute(pipeContext.getAttributeName()); if (contextWidget == null) - throw new SAXException("No form found in request attribute with name \"" + wtt.attributeName); + throw new SAXException("No form found in request attribute with name \"" + pipeContext.getAttributeName()); } else { // and then see if we got a form from the flow formJXPath = "/" + WoodyTemplateTransformer.WOODY_FORM; Object form = null; try { - form = jxpathContext.getValue(formJXPath); + form = pipeContext.getJXPathContext().getValue(formJXPath); } catch (JXPathException e) {} if (form != null) contextWidget = (Form)form; @@ -194,6 +192,26 @@ throw new SAXException("No Woody form found."); } + // ====> Determine the Locale + + String localeAttr = attributes.getValue("locale"); + if (localeAttr != null) { // first use value of locale attribute if any + localeAttr = translateText(localeAttr); + pipeContext.setLocale(I18nUtils.parseLocale(localeAttr)); + } else if (pipeContext.getLocaleParameter() != null) { // then use locale specified as transformer parameter, if any + pipeContext.setLocale(pipeContext.getLocaleParameter()); + } else { // use locale specified in bizdata supplied for form + String localeJXPath = "/locale"; + Object locale = null; + try { + locale = pipeContext.getJXPathContext().getValue(localeJXPath); + } catch (JXPathException e) {} + if (locale != null) + pipeContext.setLocale((Locale)locale); + else // final solution: use US locale + pipeContext.setLocale(Locale.getDefault()); + } + String[] namesToTranslate = {"action"}; Attributes transAtts = translateAttributes(attributes, namesToTranslate); contentHandler.startElement(Constants.WI_NS , FORM_TEMPLATE_EL, Constants.WI_PREFIX_COLON + FORM_TEMPLATE_EL, transAtts); @@ -271,7 +289,7 @@ } private String evaluateExpression(String expression) { - return jxpathContext.getValue(expression).toString(); + return pipeContext.getJXPathContext().getValue(expression).toString(); } protected Widget getWidget(Attributes attributes) throws SAXException { @@ -298,7 +316,7 @@ Object saxFragment = xmlCompiler.getSAXFragment(); for (int i = 0; i < rowCount; i++) { Repeater.RepeaterRow row = repeater.getRow(i); - rowPipe.init(row, jxpathContext); + rowPipe.init(row, pipeContext); rowPipe.setContentHandler(contentHandler); rowPipe.setLexicalHandler(lexicalHandler); interpreter.setConsumer(rowPipe); @@ -312,7 +330,7 @@ stylingHandler.setContentHandler(contentHandler); stylingHandler.setLexicalHandler(lexicalHandler); contentHandler.startPrefixMapping(Constants.WI_PREFIX, Constants.WI_NS); - widget.generateSaxFragment(stylingHandler, Locale.US /* TODO take locale from transformer parameter */); + widget.generateSaxFragment(stylingHandler, pipeContext.getLocale()); contentHandler.endPrefixMapping(Constants.WI_PREFIX); } 1.4 +46 -3 cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/transformation/WoodyTemplateTransformer.java Index: WoodyTemplateTransformer.java =================================================================== RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/transformation/WoodyTemplateTransformer.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- WoodyTemplateTransformer.java 25 Jul 2003 09:07:46 -0000 1.3 +++ WoodyTemplateTransformer.java 26 Jul 2003 14:05:05 -0000 1.4 @@ -52,9 +52,11 @@ import java.io.IOException; import java.util.Map; +import java.util.Locale; import org.apache.avalon.framework.parameters.Parameters; import org.apache.cocoon.ProcessingException; +import org.apache.cocoon.i18n.I18nUtils; import org.apache.cocoon.components.flow.FlowHelper; import org.apache.cocoon.components.flow.WebContinuation; import org.apache.cocoon.environment.ObjectModelHelper; @@ -72,8 +74,13 @@ public class WoodyTemplateTransformer extends WidgetReplacingPipe implements Transformer { /** Name of the request attribute under which the Woody form is stored (optional). */ - protected String attributeName; - protected Request request; + private String attributeName; + private Request request; + private JXPathContext jxpathContext; + /** Containts locale specified as a parameter to the transformer, if any. */ + private Locale localeParameter; + /** The locale currently used by the transformer. */ + private Locale locale; public void setup(SourceResolver resolver, Map objectModel, String src, Parameters parameters) throws ProcessingException, SAXException, IOException { @@ -94,6 +101,42 @@ this.attributeName = parameters.getParameter("attribute-name", null); this.request = request; - init(null, jxpathContext); + String localeStr = parameters.getParameter("locale", null); + if (localeStr != null) + localeParameter = I18nUtils.parseLocale(localeStr); + + init(null, this); + } + + public JXPathContext getJXPathContext() { + return jxpathContext; + } + + public String getAttributeName() { + return attributeName; + } + + public Request getRequest() { + return request; + } + + public JXPathContext getJxpathContext() { + return jxpathContext; + } + + public void setJxpathContext(JXPathContext jxpathContext) { + this.jxpathContext = jxpathContext; + } + + public Locale getLocale() { + return locale; + } + + public void setLocale(Locale locale) { + this.locale = locale; + } + + public Locale getLocaleParameter() { + return localeParameter; } } 1.4 +33 -22 cocoon-2.1/src/blocks/woody/samples/flow/woody_flow_example.js Index: woody_flow_example.js =================================================================== RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/samples/flow/woody_flow_example.js,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- woody_flow_example.js 22 Jul 2003 20:58:41 -0000 1.3 +++ woody_flow_example.js 26 Jul 2003 14:05:05 -0000 1.4 @@ -1,6 +1,7 @@ cocoon.load("resource://org/apache/cocoon/woody/flow/javascript/woody.js"); function form1(form) { + var locale = determineLocale(); var model = form.getModel(); model.email = "[EMAIL PROTECTED]"; model.somebool = true; @@ -15,31 +16,41 @@ model.contacts[2].firstname = "Chris"; model.drinks = ["Jupiler", "Coca Cola"]; - form.show("form1-display-pipeline", function(form) { - print("submitId="+form.getSubmitId()); - switch(form.getSubmitId()) { - case "remove-selected-contacts": - { - for (var i = model.contacts.length-1; i >= 0; i--) { - if (model.contacts[i].select) { - model.contacts.remove(i); - } - } - } - break; - case "add-contact": - { - model.contacts.length++; - } - break; - default: - return true; - } - return false; - }); + form.show("form1-display-pipeline", formHandler, locale); print("visa="+model.visa); cocoon.sendPage("form1-success-pipeline"); form.finish(); +} + +function formHandler(form) { + print("submitId="+form.getSubmitId()); + switch(form.getSubmitId()) { + case "remove-selected-contacts": + { + for (var i = model.contacts.length-1; i >= 0; i--) { + if (model.contacts[i].select) { + model.contacts.remove(i); + } + } + } + break; + case "add-contact": + { + model.contacts.length++; + } + break; + default: + return true; + } + return false; +} + +function determineLocale() { + var localeParam = cocoon.request.get("locale"); + if (localeParam != null && localeParam.length > 0) { + return Packages.org.apache.cocoon.i18n.I18nUtils.parseLocale(localeParam); + } + return null; }