You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cocoon.apache.org by Daniel Fagerstrom <da...@nada.kth.se> on 2004/06/08 18:11:41 UTC
[cforms] Simple XML binding
I have written an adapter: org.apache.cocoon.forms.util.XMLAdapter
between a form object and a simple XML format that can be used without
needing to wriite a binding definition.
In the applications where we have used cforms we mostly have connected
the form objects to XML through the binding framework. While the binding
framework has a lot of flexibility in how to bind the form to DOM, we
usually have used a more or less 1-1 coupling between DOM and the form.
So the flexibilty of the binding framework feels in our applications
like overkill and writing the bindings unnecessary.
--- o0o ---
The XML format is such that there is one XML element for each widget and
the element get the widget id as name. Exceptions from this is that the
elements in a repeater gets the name "item" and a attribute "position"
with the position of the repeater child, instead of just a number (which
is not allowed as element name). Childs of a MultiValueField are also
embeded within an "item" element. If the Form widget does not have an id
it get the name "unknown". I added the possiblity to give an id to the
Form widget.
An AggregateField can both be interpreted as one value and as several
widgets. This ambiguity is resolved by chosing to emit the single value
rather than the fields as XML. For population of the form both forms are
however allowed.
XMLAdapter implements XMLConsumer and XMLizable, so it can be used both
as a SAX source and destination. I added a utility function to forms.js:
getXML(), that returns the widget wrapped in an XMLAdapter. A typical
flowscript can look like:
function form2simpleXML(form) {
// get the documentURI parameter from the sitemap which contains the
// location of the file to be edited
var documentURI = cocoon.parameters["documentURI"];
// get the XML adapter
var xmlAdapter = form.getXML();
// parse the document to a widget tree
var pipeUtil =
cocoon.createObject(Packages.org.apache.cocoon.components.flow.util.PipelineUtil);
pipeUtil.processToSAX(documentURI, null, xmlAdapter);
// show the form to the user until it is validated successfully
form.showForm("form2-display-pipeline");
// show the xml generated from the form
cocoon.sendPage("form2simpleXML-success-pipeline", xmlAdapter);
}
I also checked in a sample, (a variant of the binding sample), that show
how to use the adapter.
WDYT?
/Daniel
Re: [cforms] Simple XML binding
Posted by Marc Portier <mp...@outerthought.org>.
Daniel Fagerstrom wrote:
> I have written an adapter: org.apache.cocoon.forms.util.XMLAdapter
> between a form object and a simple XML format that can be used without
> needing to wriite a binding definition.
>
<snip/>
>
> I also checked in a sample, (a variant of the binding sample), that show
> how to use the adapter.
>
> WDYT?
>
cool stuff!
-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: [cforms] Simple XML binding
Posted by Reinhard Poetz <re...@apache.org>.
Daniel Fagerstrom wrote:
> I have added loadXML(uri) and saveXML(uri) to the api, now the example
> look like:
>
> // get the documentURI parameter from the sitemap which contains the
> // location of the file to be edited
> var documentURI = cocoon.parameters["documentURI"];
>
> // populate the form
> form.loadXML(documentURI);
>
> // show the form to the user until it is validated successfully
> form.showForm("form2-display-pipeline");
>
> // save the content of the form to a file
> form.saveXML(makeTargetURI(documentURI));
>
> // show the xml generated from the form
> cocoon.sendPage("form2simpleXML-success-pipeline", form.getXML());
>
> Here we also save the content to a file (or any modifiable source).
> The getXML() method is still part of the api as it gives the
> possiblity to use the content handler and xmlizable interfaces, and
> also to set properties for the adapter, (this far only the choise of
> locale for string conversion).
>
> /Daniel
>
Very nice!
--
Reinhard
Re: [cforms] Simple XML binding
Posted by Daniel Fagerstrom <da...@nada.kth.se>.
Reinhard Poetz wrote:
> Daniel Fagerstrom wrote:
>
>> I have written an adapter: org.apache.cocoon.forms.util.XMLAdapter
>> between a form object and a simple XML format that can be used without
>> needing to wriite a binding definition.
>>
>
> Thank you very much! I think this is a very valuable addition to Cocoon
> Forms and is the answer for many form applications which have straight
> binding.
>
> One question - why do you make this explicit:
>
> // get the documentURI parameter from the sitemap which contains the
> // location of the file to be edited
> var documentURI = cocoon.parameters["documentURI"];
>
> // get the XML adapter
> var xmlAdapter = form.getXML();
>
> // parse the document to a widget tree
> var pipeUtil =
>
> cocoon.createObject(Packages.org.apache.cocoon.components.flow.util.PipelineUtil);
>
> pipeUtil.processToSAX(documentURI, null, xmlAdapter);
>
>
> and don't hide it in the form.getXML() function?
>
> e.g.
> var xmlAdapter = form.getXML( cocoon.parameters["documentURI"] );
>
Agree :)
I have added loadXML(uri) and saveXML(uri) to the api, now the example
look like:
// get the documentURI parameter from the sitemap which contains the
// location of the file to be edited
var documentURI = cocoon.parameters["documentURI"];
// populate the form
form.loadXML(documentURI);
// show the form to the user until it is validated successfully
form.showForm("form2-display-pipeline");
// save the content of the form to a file
form.saveXML(makeTargetURI(documentURI));
// show the xml generated from the form
cocoon.sendPage("form2simpleXML-success-pipeline", form.getXML());
Here we also save the content to a file (or any modifiable source). The
getXML() method is still part of the api as it gives the possiblity to
use the content handler and xmlizable interfaces, and also to set
properties for the adapter, (this far only the choise of locale for
string conversion).
/Daniel
Re: [cforms] Simple XML binding
Posted by Reinhard Poetz <re...@apache.org>.
Daniel Fagerstrom wrote:
> I have written an adapter: org.apache.cocoon.forms.util.XMLAdapter
> between a form object and a simple XML format that can be used without
> needing to wriite a binding definition.
>
Thank you very much! I think this is a very valuable addition to Cocoon
Forms and is the answer for many form applications which have straight
binding.
One question - why do you make this explicit:
// get the documentURI parameter from the sitemap which contains the
// location of the file to be edited
var documentURI = cocoon.parameters["documentURI"];
// get the XML adapter
var xmlAdapter = form.getXML();
// parse the document to a widget tree
var pipeUtil =
cocoon.createObject(Packages.org.apache.cocoon.components.flow.util.PipelineUtil);
pipeUtil.processToSAX(documentURI, null, xmlAdapter);
and don't hide it in the form.getXML() function?
e.g.
var xmlAdapter = form.getXML( cocoon.parameters["documentURI"] );
--
Reinhard
Re: [cforms] Simple XML binding
Posted by Daniel Fagerstrom <da...@nada.kth.se>.
Bruno Dumon wrote:
> On Wed, 2004-06-09 at 10:36, Daniel Fagerstrom wrote:
<snip/>
>> In such case, that is not what I want to achieve, I think that the
>>XML format should be locale independent (at least as default),
>
> I think so too
>
>> how do
>>one implement that?
>
> simply use Locale.US?
Seem like a better idea, I changed the code to use Locale.US as default.
/Daniel
Re: [cforms] Simple XML binding
Posted by Bruno Dumon <br...@outerthought.org>.
On Wed, 2004-06-09 at 10:36, Daniel Fagerstrom wrote:
> Bruno Dumon wrote:
>
> > On Tue, 2004-06-08 at 18:11, Daniel Fagerstrom wrote:
> >
> >>I have written an adapter: org.apache.cocoon.forms.util.XMLAdapter
> >>between a form object and a simple XML format that can be used without
> >>needing to wriite a binding definition.
> >
> > <snip/>
> >
> >>WDYT?
> >
> >
> > Very useful addition. I've been thinking about such a thing myself, but
> > never got to it. This effectively provides zero-effort load/store of the
> > form content. Thanks!
>
> Thanks,
>
> I have a question about locale handling in cforms that maybe you (or
> someone else) could answer. In the conversion between the text in XML
> and the widgets I use the locale that is set globaly within the form.
> Does this mean that the XML format will depend on what locale the client
> has?
yes
> In such case, that is not what I want to achieve, I think that the
> XML format should be locale independent (at least as default),
I think so too
> how do
> one implement that?
simply use Locale.US?
--
Bruno Dumon http://outerthought.org/
Outerthought - Open Source, Java & XML Competence Support Center
bruno@outerthought.org bruno@apache.org
Re: [cforms] Simple XML binding
Posted by Daniel Fagerstrom <da...@nada.kth.se>.
Bruno Dumon wrote:
> On Tue, 2004-06-08 at 18:11, Daniel Fagerstrom wrote:
>
>>I have written an adapter: org.apache.cocoon.forms.util.XMLAdapter
>>between a form object and a simple XML format that can be used without
>>needing to wriite a binding definition.
>
> <snip/>
>
>>WDYT?
>
>
> Very useful addition. I've been thinking about such a thing myself, but
> never got to it. This effectively provides zero-effort load/store of the
> form content. Thanks!
Thanks,
I have a question about locale handling in cforms that maybe you (or
someone else) could answer. In the conversion between the text in XML
and the widgets I use the locale that is set globaly within the form.
Does this mean that the XML format will depend on what locale the client
has? In such case, that is not what I want to achieve, I think that the
XML format should be locale independent (at least as default), how do
one implement that?
/Daniel
Re: [cforms] Simple XML binding
Posted by Bruno Dumon <br...@outerthought.org>.
On Tue, 2004-06-08 at 18:11, Daniel Fagerstrom wrote:
> I have written an adapter: org.apache.cocoon.forms.util.XMLAdapter
> between a form object and a simple XML format that can be used without
> needing to wriite a binding definition.
<snip/>
>
> WDYT?
Very useful addition. I've been thinking about such a thing myself, but
never got to it. This effectively provides zero-effort load/store of the
form content. Thanks!
--
Bruno Dumon http://outerthought.org/
Outerthought - Open Source, Java & XML Competence Support Center
bruno@outerthought.org bruno@apache.org
Re: [cforms] Simple XML binding
Posted by Andreas Hochsteger <e9...@student.tuwien.ac.at>.
Daniel Fagerstrom wrote:
> Andreas Hochsteger wrote:
>
>> Hi Daniel,
>>
>> this sounds really interesting!
>>
>> Would this functionality allow you to bind XML content to a textarea
>> input element too?
>> What I mean is, that this way you could populate certain parts of an
>> XML document directly with the XHTML-Output of the HTMLArea control
>> (or plain textarea if you like).
>>
>> Thanks,
>> Andreas.
>
>
> There is no such functionality this far, but it sound like a useful
> extension. I think it would be best to implement it by creating an XML
> data type for CForms. The XML data type is reponsible for converting
> between text (in the input element e.g.) and an appropriate Java data
> type, (DOM i guess). The XMLAdapter could then be extended so that it
> can recognize the XML data type, and populate and serialize it.
>
> WDYT?
Yes, I thought about an XML data type for CForms too some time ago
(http://marc.theaimsgroup.com/?l=xml-cocoon-dev&m=107644540003885&w=2).
Unfortunately I didn't find the time to take a closer look at it.
Your solution sounds good to me.
>
> /Daniel
Andreas.
Re: [cforms] Simple XML binding
Posted by Daniel Fagerstrom <da...@nada.kth.se>.
Andreas Hochsteger wrote:
> Hi Daniel,
>
> this sounds really interesting!
>
> Would this functionality allow you to bind XML content to a textarea
> input element too?
> What I mean is, that this way you could populate certain parts of an XML
> document directly with the XHTML-Output of the HTMLArea control (or
> plain textarea if you like).
>
> Thanks,
> Andreas.
There is no such functionality this far, but it sound like a useful
extension. I think it would be best to implement it by creating an XML
data type for CForms. The XML data type is reponsible for converting
between text (in the input element e.g.) and an appropriate Java data
type, (DOM i guess). The XMLAdapter could then be extended so that it
can recognize the XML data type, and populate and serialize it.
WDYT?
/Daniel
Re: [cforms] Simple XML binding
Posted by Andreas Hochsteger <e9...@student.tuwien.ac.at>.
Hi Daniel,
this sounds really interesting!
Would this functionality allow you to bind XML content to a textarea
input element too?
What I mean is, that this way you could populate certain parts of an XML
document directly with the XHTML-Output of the HTMLArea control (or
plain textarea if you like).
Thanks,
Andreas.
Daniel Fagerstrom wrote:
> I have written an adapter: org.apache.cocoon.forms.util.XMLAdapter
> between a form object and a simple XML format that can be used without
> needing to wriite a binding definition.
>
> In the applications where we have used cforms we mostly have connected
> the form objects to XML through the binding framework. While the binding
> framework has a lot of flexibility in how to bind the form to DOM, we
> usually have used a more or less 1-1 coupling between DOM and the form.
> So the flexibilty of the binding framework feels in our applications
> like overkill and writing the bindings unnecessary.
>
> --- o0o ---
>
> The XML format is such that there is one XML element for each widget and
> the element get the widget id as name. Exceptions from this is that the
> elements in a repeater gets the name "item" and a attribute "position"
> with the position of the repeater child, instead of just a number (which
> is not allowed as element name). Childs of a MultiValueField are also
> embeded within an "item" element. If the Form widget does not have an id
> it get the name "unknown". I added the possiblity to give an id to the
> Form widget.
>
> An AggregateField can both be interpreted as one value and as several
> widgets. This ambiguity is resolved by chosing to emit the single value
> rather than the fields as XML. For population of the form both forms are
> however allowed.
>
> XMLAdapter implements XMLConsumer and XMLizable, so it can be used both
> as a SAX source and destination. I added a utility function to forms.js:
> getXML(), that returns the widget wrapped in an XMLAdapter. A typical
> flowscript can look like:
>
> function form2simpleXML(form) {
> // get the documentURI parameter from the sitemap which contains the
> // location of the file to be edited
> var documentURI = cocoon.parameters["documentURI"];
>
> // get the XML adapter
> var xmlAdapter = form.getXML();
>
> // parse the document to a widget tree
> var pipeUtil =
>
> cocoon.createObject(Packages.org.apache.cocoon.components.flow.util.PipelineUtil);
>
> pipeUtil.processToSAX(documentURI, null, xmlAdapter);
>
> // show the form to the user until it is validated successfully
> form.showForm("form2-display-pipeline");
>
> // show the xml generated from the form
> cocoon.sendPage("form2simpleXML-success-pipeline", xmlAdapter);
> }
>
> I also checked in a sample, (a variant of the binding sample), that show
> how to use the adapter.
>
> WDYT?
>
> /Daniel
>
>