On Sun, 13 Feb 2005 22:02:02 +0100, echarp <[EMAIL PROTECTED]> wrote:
> 
>   The cforms binding can not handle this (easily)
> 
>   see:
>   http://marc.theaimsgroup.com/?l=xml-cocoon-users&m=110146268307395&w=2
>   http://marc.theaimsgroup.com/?l=xml-cocoon-users&m=108127256626966&w=2
> 
>   Personnaly I'm loading it in my flow like this:

>   // bind the document data to the form
>   form.load(document);

<snip/>

In case you find it useful, this is a custom binding I'm using (far
from robust but working so far). Compile it as use it like this:

  <fb:context path="/" >
    <fb:custom id="content" path="whatever" 
      class="com.orixo.cocoon.forms.binding.CustomXmlAsStringBinding"/>
  </fb:context>

*
 * Copyright (c)2004 Pronetics S.r.l. - All rights reserved
 */
package com.orixo.cocoon.forms.binding;

import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.cocoon.ProcessingException;
import org.apache.cocoon.forms.binding.AbstractCustomBinding;
import org.apache.cocoon.forms.binding.BindingException;
import org.apache.cocoon.forms.formmodel.Widget;
import org.apache.cocoon.xml.XMLUtils;
import org.apache.commons.jxpath.JXPathContext;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 * Simple custom binding for HTMLArea and XML data.
 *
 * @author <a href="mailto:[EMAIL PROTECTED]">Gianugo Rabellino </a>
 * @version $Id$
 */
public class CustomXmlAsStringBinding extends AbstractCustomBinding {

    /**
     * Serialize the node as String upon load.
     *
     * @see 
org.apache.cocoon.forms.binding.AbstractCustomBinding#doLoad(org.apache.cocoon.forms.formmodel.Widget,
     *      org.apache.commons.jxpath.JXPathContext)
     */
    protected void doLoad(Widget frmModel, JXPathContext context)
            throws BindingException {
        Node value = (Node) context.getContextPointer().getNode();
        try {
            frmModel.setValue(XMLUtils.serializeNode(value));
            // TODO Auto-generated method stub
        } catch (ProcessingException e) {
            throw new BindingException("Unable to serialize node", e);
        }
    }

    /**
     * Parse and import the XML String upon save.
     * 
     * @throws IOException
     * @throws SAXException
     * @see 
org.apache.cocoon.forms.binding.AbstractCustomBinding#doSave(org.apache.cocoon.forms.formmodel.Widget,
     *      org.apache.commons.jxpath.JXPathContext)
     */
    protected void doSave(Widget frmModel, JXPathContext context)
            throws BindingException, SAXException, IOException {
        DocumentBuilder builder = null;
        try {
            builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            throw new BindingException("Problem getting a parser", e);
        }
        Document dom = builder.parse(new InputSource(new StringReader(
                (String) frmModel.getValue())));

        Node result = ((Node) context.getContextPointer().getNode())
                .getOwnerDocument().importNode(dom.getDocumentElement(), true);

        context.getContextPointer().setValue(result);

    }

}

HTH,

-- 
Gianugo Rabellino
Pro-netics s.r.l. -  http://www.pro-netics.com
Orixo, the XML business alliance: http://www.orixo.com
(blogging at http://www.rabellino.it/blog/)

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to