You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Henrik Gustafsson <he...@telepo.com> on 2004/10/06 16:51:47 UTC

Forms with bindings in nested repeaters

I'm working with a bean model and have a binding to nested repeaters. The
inner binding depends on the current element of the outer binding. What I
would like to do is to use a variable in the path expression of the inner
binding that is set in the outer binding but I have had no luck finding
information how to do that yet. Any suggestions? The only information I
found was a guy on this mailing list with a similar problem but he got the
suggestion transforming his xml source, but I have a bean model and like
to keep working with that.

Since I desperatly needed to solve the problem and have read somewhere that
adding new bindings is very easy, I made my very own
<fb:variable  name="<variable name>" path="<jxpath>"/>

Inspired by the existing value binding I start using the
JXPathBindingBuilderBase
and JXPathBindingBase base classes to implement my binding but got quickly
into problems. The JXPathBindingBuilderBase.CommonAttributes is package
protected and I had to do an ugly hack putting my extension into the
org.apache.cocoon.forms.binding package. Is it a bad idea fiddling around
with the JXPathContext sent to the bindings or should it be possible to
build your own JXPathBindingBase based class? I need to get the
JXPathContext used by the other bindings to be able to set the xpath
variables.

With some few lines of code, attached to the end of this mail, i could start
writing bindings like this:

  <fb:repeater id="intervalls" parent-path="/" row-path="intervalls">
    <fb:identity>
      <fb:value id="intervall_name" path="name"/>
    </fb:identity>
    <fb:on-bind>
      <!-- The xpath variable $intervall is bound to the value of the 
current intervall name -->
      <fb:variable name="intervall" path="name"/>

      <fb:repeater id="destinations" parent-path="/tariff" 
row-path="destinations">
        <fb:identity>
          <fb:value id="phonenumber" path="phonenumber"/>
        </fb:identity>
        <fb:on-bind>
          <fb:value id="start_fee" 
path="rates[intervall/name=$intervall]/startfee"/>
          <fb:value id="minute_fee" 
path="rates[intervall/name=$intervall]/minutefee"/>
        </fb:on-bind>
      </fb:repeater>
    </fb:on-bind>
  </fb:repeater>

I would really like to put this in my own package or use something else if
it is already supported in the cforms binding.

/Henrik

--------------------------------
package org.apache.cocoon.forms.binding;

import org.apache.cocoon.forms.formmodel.Widget;
import org.apache.commons.jxpath.JXPathContext;

public class VariableJXPathBinding extends JXPathBindingBase {

    private final String name;
    private final String xpath;

    public VariableJXPathBinding( JXPathBindingBuilderBase.CommonAttributes
commonAtts, String name, String xpath ) {
        super( commonAtts );
        this.name = name;
        this.xpath = xpath;
    }

    public void doLoad(Widget frmModel, JXPathContext jxpc) {
        jxpc.getVariables().declareVariable( name, jxpc.getValue( xpath ) );
    }

    public void doSave(Widget frmModel, JXPathContext jxpc) {
        jxpc.getVariables().declareVariable( name, jxpc.getValue( xpath ) );
    }

    public String toString() {
        return "VariableJXPathBinding [name="+name+";xpath="+xpath+"]";
    }
}

--------------------------------
package org.apache.cocoon.forms.binding;

import org.apache.cocoon.forms.util.DomHelper;
import org.w3c.dom.Element;

public class VariableJXPathBindingBuilder extends JXPathBindingBuilderBase {

    public JXPathBindingBase buildBinding( Element bindingElm,
JXPathBindingManager.Assistant assistant) throws BindingException {
        try {
            CommonAttributes commonAtts =
JXPathBindingBuilderBase.getCommonAttributes( bindingElm );
            String name = DomHelper.getAttribute(bindingElm, "name");
            String xpath = DomHelper.getAttribute(bindingElm, "path");

            return new VariableJXPathBinding( commonAtts, name, xpath );
        } catch ( BindingException e ) {
            throw e;
        } catch ( Exception e ) {
            throw new BindingException("Error building context binding
defined at " + DomHelper.getLocation(bindingElm), e);
        }
    }
}
-------------------------------- 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org