You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by mp...@apache.org on 2003/12/22 17:43:11 UTC

cvs commit: cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/generation WoodyGenerator.java

mpo         2003/12/22 08:43:11

  Modified:    src/blocks/woody/java/org/apache/cocoon/woody/transformation
                        WidgetReplacingPipe.java
               src/blocks/woody/java/org/apache/cocoon/woody/generation
                        WoodyGenerator.java
  Added:       src/blocks/woody/java/org/apache/cocoon/woody/transformation
                        WoodyPipeLineConfig.java
  Log:
  Refactoring the common configuration settings for Generator and Transformer.
  Making sure the same flow-integration support is now available on the Generator.
  And hoping that future additions on either side will be beneficial to the other component as well.
  
  Revision  Changes    Path
  1.17      +6 -35     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.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- WidgetReplacingPipe.java	8 Dec 2003 21:32:26 -0000	1.16
  +++ WidgetReplacingPipe.java	22 Dec 2003 16:43:11 -0000	1.17
  @@ -55,13 +55,12 @@
   import java.util.Locale;
   
   import org.apache.avalon.excalibur.pool.Recyclable;
  +import org.apache.cocoon.i18n.I18nUtils;
   import org.apache.cocoon.woody.Constants;
   import org.apache.cocoon.woody.formmodel.Repeater;
   import org.apache.cocoon.woody.formmodel.Widget;
  -import org.apache.cocoon.woody.formmodel.Form;
   import org.apache.cocoon.xml.AbstractXMLPipe;
   import org.apache.cocoon.xml.SaxBuffer;
  -import org.apache.cocoon.i18n.I18nUtils;
   import org.apache.commons.jxpath.JXPathException;
   import org.xml.sax.Attributes;
   import org.xml.sax.SAXException;
  @@ -93,11 +92,6 @@
       private static final String FORM_TEMPLATE_EL = "form-template";
       private static final String STYLING_EL = "styling";
   
  -    /**
  -     * Default key under which the woody form is stored in the JXPath context.
  -     */
  -    public static final String WOODY_FORM = "woody-form";
  -
       protected Widget contextWidget;
   
       /**
  @@ -133,7 +127,7 @@
       protected boolean repeaterWidget;
   
       protected WoodyTemplateTransformer.InsertStylingContentHandler stylingHandler = new WoodyTemplateTransformer.InsertStylingContentHandler();
  -    protected WoodyTemplateTransformer pipeContext;
  +    protected WoodyPipeLineConfig pipeContext;
   
       /**
        * Have we encountered a <wi:style> element in a widget ?
  @@ -146,7 +140,7 @@
       protected String namespacePrefix;
       
   
  -    public void init(Widget newContextWidget, WoodyTemplateTransformer newPipeContext) {
  +    public void init(Widget newContextWidget, WoodyPipeLineConfig newPipeContext) {
           contextWidget = newContextWidget;
           inWidgetElement = false;
           elementNestingCounter = 0;
  @@ -214,34 +208,11 @@
                       AttributesImpl attrsCopy = new AttributesImpl(attributes);
                       attrsCopy.removeAttribute(attributes.getIndex(LOCATION));
                       attributes = attrsCopy;
  -
  -                    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 (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 \"" + pipeContext.getAttributeName() + "\"");
  -                    }
  -                } else { // and then see if we got a form from the flow
  -                    formJXPath = "/" + WoodyTemplateTransformer.WOODY_FORM;
  -                    Object form = null;
  -                    try {
  -                        form = pipeContext.getJXPathContext().getValue(formJXPath);
  -                    } catch (JXPathException e) { /* do nothing */ }
  -                    if (form != null) {
  -                        contextWidget = (Form)form;
  -                    } else {
  -                        throw new SAXException("No Woody form found.");
  -                    }
                   }
  +                contextWidget = pipeContext.findForm(formJXPath);
   
                   // ====> Determine the Locale
  +                //TODO pull this locale stuff also up in the Config object?
   
                   String localeAttr = attributes.getValue("locale");
                   if (localeAttr != null) { // first use value of locale attribute if any
  
  
  
  1.1                  cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/transformation/WoodyPipeLineConfig.java
  
  Index: WoodyPipeLineConfig.java
  ===================================================================
  /*
   * File WoodyPipeLineConfig.java 
   * created by mpo
   * on Dec 19, 2003 | 12:38:34 PM
   * 
   * (c) 2003 - Outerthought BVBA
   */
  package org.apache.cocoon.woody.transformation;
  
  import java.util.Locale;
  import java.util.Map;
  
  import org.apache.avalon.framework.parameters.Parameters;
  import org.apache.cocoon.components.flow.FlowHelper;
  import org.apache.cocoon.components.flow.WebContinuation;
  import org.apache.cocoon.environment.ObjectModelHelper;
  import org.apache.cocoon.environment.Request;
  import org.apache.cocoon.environment.Session;
  import org.apache.cocoon.i18n.I18nUtils;
  import org.apache.cocoon.woody.formmodel.Form;
  import org.apache.commons.jxpath.JXPathContext;
  import org.apache.commons.jxpath.JXPathException;
  import org.apache.commons.jxpath.Variables;
  import org.xml.sax.SAXException;
  
  /**
   * WoodyPipeLineConfig
   */
  public class WoodyPipeLineConfig {
  
      /**
       * Default key under which the woody form is stored in the JXPath context.
       */
      public static final String WOODY_FORM = "woody-form";
      
      /** 
       * Name of the request attribute under which the Woody form is stored (optional). */
      private final String attributeName;
      
      /**
       * Pointer to the current request object.     */
      private final Request request;
      
      /**
       * Initialized jxpathcontext to evaluate passed expressions with.     */
      private final JXPathContext jxpathContext;
      
      /** 
       * Containts locale specified as a parameter to the transformer, if any. */
      private final Locale localeParameter;
  
      /** 
       * The locale currently used by the transformer. */
      private Locale locale;
  
  
      
      private WoodyPipeLineConfig(String attName, Request req, JXPathContext jxpc, Locale localeParam) {
          this.attributeName = attName;
          this.request = req;
          this.jxpathContext =jxpc;
          this.localeParameter = localeParam;
      }
      
     
      public static WoodyPipeLineConfig createConfig(Map objectModel, Parameters parameters) {
          // create and set the jxpathContext...
          Object flowContext = FlowHelper.getContextObject(objectModel);
          WebContinuation wk = FlowHelper.getWebContinuation(objectModel);
          JXPathContext jxpc = JXPathContext.newContext(flowContext);
          Variables vars = jxpc.getVariables();
          vars.declareVariable("continuation", wk);
          Request request = ObjectModelHelper.getRequest(objectModel);
          vars.declareVariable("request", request);
          Session session = request.getSession(false);
          vars.declareVariable("session", session);
          vars.declareVariable("parameters", parameters);
          
          String attributeName = parameters.getParameter("attribute-name", null);
          
          Locale localeParameter = null;
          String localeStr = parameters.getParameter("locale", null);
          if (localeStr != null) {
              localeParameter = I18nUtils.parseLocale(localeStr);
          }
          
          return new WoodyPipeLineConfig(attributeName, request, jxpc, localeParameter);
      }
      
      
      /**
       * Overloads {@see #findForm(String)} by setting the jxpath-expression to null
       */
      public Form findForm() throws SAXException {
          return this.findForm(null);
      }
  
      /**
       * Finds the form from the current request-context based on the settings of 
       * this configuration object.  The fall-back search-procedure is as follows:
       * <ol><li>Use the provided jxpathEpression (if not null)</li>
       * <li>Use the setting of the 'attribute-name' parameter on the request</li>
       * <li>Obtainn the form from it's default location in the flow context</li>
       * </ol> 
       * 
       * @param jxpathExpression that should be pointing to the form
       * @return the found form if found
       * @throws SAXException in any of the folowing cases:
       * <ul><li>The provided jxpathExpression (if not null) is not pointing to 
       * a {@see Form} instance.</li>
       * <li>The request is not holding a {@see Form} instance under the key 
       * specified by 'attribute-name' (if specified)</li>
       * <li>Both jxpathExpresiion and 'attribute-name' were not specified AND
       * also the default location was not holding a valid {@see Form} instance.</li>
       * </ol> 
       */
      public Form findForm(String jxpathExpression) throws SAXException {
  
          if (jxpathExpression != null) {
              
              Object form = this.jxpathContext.getValue(jxpathExpression);
              if (form == null) {
                  throw new SAXException("No form found at location \"" + jxpathExpression + "\".");
              }
              if (!(form instanceof Form)) {
                  throw new SAXException("Object returned by expression \"" + jxpathExpression + "\" is not a Woody Form.");
              }
              return (Form)form;
          } else if (this.attributeName != null) { // then see if an attribute-name was specified
              Object form = this.request.getAttribute(this.attributeName);
              
              if (form == null) {
                  throw new SAXException("No form found in request attribute with name \"" + this.attributeName + "\"");
              }
              if (!(form instanceof Form)) {
                  throw new SAXException("Object found in request (attribute = '" + this.attributeName + "') is not a Woody Form.");
              }
              return (Form)form;
          } else { // and then see if we got a form from the flow
              jxpathExpression = "/" + WoodyPipeLineConfig.WOODY_FORM;
              Object form = null;
              try {
                  form = this.jxpathContext.getValue(jxpathExpression);
              } catch (JXPathException e) { /* do nothing */ }
              if (form != null) {
                  return (Form)form;
              } else {
                  throw new SAXException("No Woody form found.");
              }
          }
      }
  
      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       +17 -25    cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/generation/WoodyGenerator.java
  
  Index: WoodyGenerator.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/generation/WoodyGenerator.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- WoodyGenerator.java	27 Oct 2003 19:58:19 -0000	1.3
  +++ WoodyGenerator.java	22 Dec 2003 16:43:11 -0000	1.4
  @@ -50,21 +50,19 @@
   */
   package org.apache.cocoon.woody.generation;
   
  -import org.apache.cocoon.generation.AbstractGenerator;
  +import java.io.IOException;
  +import java.util.Locale;
  +import java.util.Map;
  +
  +import org.apache.avalon.framework.parameters.Parameters;
   import org.apache.cocoon.ProcessingException;
  -import org.apache.cocoon.woody.formmodel.Form;
  -import org.apache.cocoon.woody.Constants;
   import org.apache.cocoon.environment.SourceResolver;
  -import org.apache.cocoon.environment.Request;
  -import org.apache.cocoon.environment.ObjectModelHelper;
  -import org.apache.avalon.framework.parameters.Parameters;
  -import org.apache.avalon.framework.parameters.ParameterException;
  +import org.apache.cocoon.generation.AbstractGenerator;
  +import org.apache.cocoon.woody.Constants;
  +import org.apache.cocoon.woody.formmodel.Form;
  +import org.apache.cocoon.woody.transformation.WoodyPipeLineConfig;
   import org.xml.sax.SAXException;
   
  -import java.io.IOException;
  -import java.util.Map;
  -import java.util.Locale;
  -
   /**
    * A generator that streams an XML representation of a Woody {@link Form}. This will
    * recursively contain the XML for all widgets on the form. This can then be styled
  @@ -73,8 +71,11 @@
    * <p>An alternative approach that requires less (or even none) XSLT work is offered by
    * the {@link org.apache.cocoon.woody.transformation.WoodyTemplateTransformer WoodyTemplateTransformer}.
    *
  - * <p>The Form whose XML should be produced should reside in a request attribute, whose
  - * name should be provided to this generator as a sitemap parameter called "attribute-name".
  + * <p>The Form whose XML should be produced should reside either 
  + * <ol><li> In a request attribute, whose name should be provided to this 
  + * generator as a sitemap parameter called "attribute-name".</li>
  + * <li> Or else at its default-location in the flow context-object.</li>
  + * </ol>
    */
   public class WoodyGenerator extends AbstractGenerator {
       
  @@ -83,18 +84,9 @@
       public void setup(SourceResolver resolver, Map objectModel, String src, Parameters par)
               throws ProcessingException, SAXException, IOException {
           super.setup(resolver, objectModel, src, par);
  -        // get the form from a request attribute
  -        String formAttribute;
  -        try {
  -            formAttribute = parameters.getParameter("attribute-name");
  -        } catch (ParameterException e) {
  -            throw new ProcessingException("Missing 'attribute-name' parameter for WoodyGenerator.");
  -        }
  -        Request request = ObjectModelHelper.getRequest(objectModel);
  -        form = (Form)request.getAttribute(formAttribute);
  -        if (form == null) {
  -            throw new ProcessingException("WoodyGenerator cannot find a form in the request attribute named " + formAttribute);
  -        }
  +        
  +        WoodyPipeLineConfig config = WoodyPipeLineConfig.createConfig(objectModel, parameters);
  +        form = config.findForm();     
       }
   
       public void recycle() {
  
  
  

Re: cvs commit: cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/generation WoodyGenerator.java

Posted by Marc Portier <mp...@outerthought.org>.

Sylvain Wallez wrote:

> Vadim Gritsenko wrote:
> 
>> mpo@apache.org wrote:
>>
>>>  1.1                  
>>> cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/transformation/WoodyPipeLineConfig.java 
>>>
>>>  
>>>  Index: WoodyPipeLineConfig.java
>>>  ===================================================================
>>>  /*
>>>   * File WoodyPipeLineConfig.java   * created by mpo
>>>   * on Dec 19, 2003 | 12:38:34 PM
>>>   *   * (c) 2003 - Outerthought BVBA
>>>   */
>>>  package org.apache.cocoon.woody.transformation;
>>
>>
>>
>> Ahem-ahem?
> 
> 

blast,
sorry for that, fixing....

> ROTFL ;-)
> 
> BTW, it should be "WoodyPipelineConfig", and not "WoodyPipeLineConfig" 
> (lowercase "l").
> 
> What's this used for?
> 

fixing that too...
common configuration settings shared by WoodyGenerator and WoodyTransformer

The last one got lots of attention and thus a good integration with 
flowscript... this pushes those integration features to the generator as 
well by refactoring common things to a common place...

the hope is that new features for the one will be more naturally 
available or at least considered for the other.

regards,
-marc=
-- 
Marc Portier                            http://outerthought.org/
Outerthought - Open Source, Java & XML Competence Support Center
Read my weblog at                http://blogs.cocoondev.org/mpo/
mpo@outerthought.org                              mpo@apache.org


Re: cvs commit: cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/generation WoodyGenerator.java

Posted by Sylvain Wallez <sy...@apache.org>.
Vadim Gritsenko wrote:

> mpo@apache.org wrote:
>
>>  1.1                  
>> cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/transformation/WoodyPipeLineConfig.java 
>>
>>  
>>  Index: WoodyPipeLineConfig.java
>>  ===================================================================
>>  /*
>>   * File WoodyPipeLineConfig.java   * created by mpo
>>   * on Dec 19, 2003 | 12:38:34 PM
>>   *   * (c) 2003 - Outerthought BVBA
>>   */
>>  package org.apache.cocoon.woody.transformation;
>
>
> Ahem-ahem?


ROTFL ;-)

BTW, it should be "WoodyPipelineConfig", and not "WoodyPipeLineConfig" 
(lowercase "l").

What's this used for?

Sylvain

-- 
Sylvain Wallez                                  Anyware Technologies
http://www.apache.org/~sylvain           http://www.anyware-tech.com
{ XML, Java, Cocoon, OpenSource }*{ Training, Consulting, Projects }
Orixo, the opensource XML business alliance  -  http://www.orixo.com



Re: cvs commit: cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/generation WoodyGenerator.java

Posted by Vadim Gritsenko <va...@verizon.net>.
mpo@apache.org wrote:

>  1.1                  cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/transformation/WoodyPipeLineConfig.java
>  
>  Index: WoodyPipeLineConfig.java
>  ===================================================================
>  /*
>   * File WoodyPipeLineConfig.java 
>   * created by mpo
>   * on Dec 19, 2003 | 12:38:34 PM
>   * 
>   * (c) 2003 - Outerthought BVBA
>   */
>  package org.apache.cocoon.woody.transformation;
>  
>

Ahem-ahem?

Vadim