You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Sylvain Wallez <sy...@anyware-tech.com> on 2001/01/23 15:50:46 UTC

Re: toDOM and toSAX of XMLFragment


Ulrich Mayring a écrit :
> 
> Hello,
> 
> how are the implementations of the toDOM and toSAX methods supposed to
> look like? I see that they receive a Node or a ContentHandler
> respectively and return nothing - what are they supposed to do?
> 
> Ulrich
> 
> --
> Ulrich Mayring
> DENIC eG, Systementwicklung
> 

An XMLFragment is an object that can be represented as a XML document
fragment. The toDOM() method (used by C1) should append the XML
representation of the object as children of the given node, and the
toSAX() method (used by C2) should generate the XML representation as
SAX events.

A typical XMLFragment implementation could look like this :

  public class FragmentTest implements XMLFragment
  {
    public void toDOM(Node node)
    {
      Document doc = node.getOwnerDocument();
      Element e = doc.createElement("foo");
      e.appendChild(doc.createTextNode("bar"));
      node.appendChild(e);
    }

    public void toSAX(ContentHandler handler)
    {
      AttributesImpl attr = new AttributesImpl();
      handler.startElement("","foo","foo",attr);

      String content = "bar";
      handler.characters(content.toCharArray(), 0, content.length());

      handler.endElement("","foo","foo");
    }
  }

Both methods result in <foo>bar</foo> to be inserted in the resulting
document.

Hope this helps.
-- 
Sylvain Wallez
Anyware Technologies

Re: toDOM and toSAX of XMLFragment

Posted by Ulrich Mayring <ul...@denic.de>.
Echoes wrote:
> 
>  I think you're wrong here. The XMLFragment interface has to be
> implemented
> on your objects (aka beans). It's pretty easy if you use an XML
> marshalling
> framework like Castor.

So it's only for cases where I have an object? I have code like this:

<soap:call
	url="http://mysoap.server.com/soap/servlet/rpcrouter"
	id="DocumentHandler"
	method="handle"
	as="XML">

	<soap:param name="soap_debug" value="yes"/>
	<soap:param name="url"
value="http://mycocoon.host.com/faxes/myfax.xml"/>
	<soap:param name="action" value="faxDocument"/>
	<soap:param name="action" value="saveInArchive"/>
	<soap:param name="action" value="showOnScreen"/>

</soap:call>

This generates a response from the SOAP server telling me if it did what
I requested. This response is in XML. Other than the XSP page that
contains this code I have no object. So I would have to convert this
taglib into a Bean or a class and write my toDOM and toSAX methods
there?

Ulrich

-- 
Ulrich Mayring
DENIC eG, Systementwicklung

Re: toDOM and toSAX of XMLFragment

Posted by Echoes <ec...@free.fr>.
Ulrich Mayring wrote:
> 
> Torsten Curdt wrote:
> >
> > That is your part dude! It is an interface! Only you know about the XML
> > representation of the oject that implements XMLFragment!
> 
> ugghhh... you're saying I should hard-code according to the structure of
> the generated XML?? What if the structure changes? Dude, don't send me
> back to ASP or ColdFusion days ;-)
> 
> Ok, suppose I have XSP pages like these:
> 
> <xsp:page ...>
> 
> <xsp:logic>
>         String xmlstring = getXMLfromSomewhere();
>         // parse it, append to current node
> </xsp:logic>
> 
> </xsp:page>
> 
> According to how I understand the XMLFragment interface I now have to
> insert a toDOM and a toSAX method into each and every XSP page that I
> have.

 I think you're wrong here. The XMLFragment interface has to be
implemented
on your objects (aka beans). It's pretty easy if you use an XML
marshalling
framework like Castor.

  Eugène

RE: toDOM and toSAX of XMLFragment

Posted by Uli Mayring <ul...@denic.de>.
On Tue, 23 Jan 2001, Torsten Curdt wrote:

> Well, my toDOM() goes through the ResultSet and creates the Nodes from
> the rows and columns. No parsing :)  (esql was far away that time ;)

Ouch, that hurts! ;-)

> > Hey, if you tell me how to do that I might just change the SOAP taglib :)
> 
> Well, as I said it depends on how you get the data...
> SOAP over HTTP will need a parser :)

I think we should have a general plan. There's many situations where you
get data from external sources, obviously. Some, like databases, are
translated to XML by logicsheets (SQL processor etc.). But there will
always be cases, where it doesn't make sense to write a taglib, because
the thing is just not generic enough. This is often the case with custom
and/or legacy code that you just use, but that is too specific to your
company to be generally useful. So in these cases you just call your
proprietary Java method inside a xsp:logic block.

Then there's util:include and the xinclude processor. There's the fp
taglib and there are persistency classes that get data (Prowler) or even
schema-based data-gatherers (Schemox). So many ways to get XML, but no
standard way to include it in the current page.

I realize that we cannot generalize over all of that, but there should be
a standardized way of appending nodes/events to the current node/event.
This should be a method that can be called in xsp:logic blocks (or
better yet put into a taglib) and that will allow us to integrate not all,
but most kinds of data sources. It can't be that hard, the only problem is
to find a common input source - something we can cast all things Java to,
like java.lang.object :-)

Ulrich

-- 
Ulrich Mayring
DENIC eG, Softwareentwicklung


RE: toDOM and toSAX of XMLFragment

Posted by Torsten Curdt <tc...@dff.st>.
> > Why in a XSP page?!
> 
> Cause it's the only object I have :-)

Then there is no place for toDOM or toSAX...

> > For your SOAP taglib parsing is necessary for sure. But for e.g.
> > my database beans: they query the database within the same VM
> > why should I build a String from that and then let Cocoon parse it?!
> 
> And what exactly does your database return that you don't have to parse
> it?

Well, my toDOM() goes through the ResultSet and creates the Nodes from
the rows and columns. No parsing :)  (esql was far away that time ;)

> > > How does a SAX parser parse arbitrary XML files? It reads characters and
> > > generates events :)
> > 
> > But why parse if you don't really need to?
> 
> I'm all ears on how to get XML from somewhere and appending it to the
> current node without parsing it first ;-)

see above

> Hey, if you tell me how to do that I might just change the SOAP taglib :)

Well, as I said it depends on how you get the data...
SOAP over HTTP will need a parser :)
--
Torsten

RE: toDOM and toSAX of XMLFragment

Posted by Uli Mayring <ul...@denic.de>.
On Tue, 23 Jan 2001, Torsten Curdt wrote:

> > Instead of hard-coding toDOM and toSAX methods into each and every XSP
> > page, I just write one CocoonMethod for strings, one for Nodes, one for
> 
> Why in a XSP page?!

Cause it's the only object I have :-)

> For your SOAP taglib parsing is necessary for sure. But for e.g.
> my database beans: they query the database within the same VM
> why should I build a String from that and then let Cocoon parse it?!

And what exactly does your database return that you don't have to parse
it?

> > How does a SAX parser parse arbitrary XML files? It reads characters and
> > generates events :)
> 
> But why parse if you don't really need to?

I'm all ears on how to get XML from somewhere and appending it to the
current node without parsing it first ;-)

Hey, if you tell me how to do that I might just change the SOAP taglib :)

Ulrich

-- 
Ulrich Mayring
DENIC eG, Softwareentwicklung


RE: toDOM and toSAX of XMLFragment

Posted by Torsten Curdt <tc...@dff.st>.
 
> > That is your part dude! It is an interface! Only you know about the XML
> > representation of the oject that implements XMLFragment!
> 
> ugghhh... you're saying I should hard-code according to the structure of
> the generated XML?? What if the structure changes? Dude, don't send me
> back to ASP or ColdFusion days ;-)

No,no,no... didn't want to scare you ;)

> Ok, suppose I have XSP pages like these:
> 
> <xsp:page ...>
> 
> <xsp:logic>
> 	String xmlstring = getXMLfromSomewhere();
> 	// parse it, append to current node
> </xsp:logic>
> 
> </xsp:page>

Doesn't need to be fix! In our last project we
used JavaBeans to query a database. They had
a toDOM() methode to return the result.

> According to how I understand the XMLFragment interface I now have to
> insert a toDOM and a toSAX method into each and every XSP page that I
> have.
> 
> Instead why not write it like this:
> 
> <xsp:page ...>
> 
> <xsp:logic>
> 	String xmlstring = getXMLfromSomewhere();
> 	CocoonMethod(xmlstring);
> </xsp:logic>
> 
> </xsp:page>

I guess you could use the util:taglib for this. (Or at least this is
the place where something like that should be ;)
Should be quite easy to extent if not...

> Instead of hard-coding toDOM and toSAX methods into each and every XSP
> page, I just write one CocoonMethod for strings, one for Nodes, one for

Why in a XSP page?!

> SAX events and whatever else I have as input. This method could then
> determine if we're running cocoon1 or cocoon2 and initiate the
> appropriate action. And what do you know: the methods I write can also
> be used by others :)

But parsing will be much more expensive! Check the archives.
Last summer there was a thread on this topic.

I think it just depends if you have the information in your VM or not.

For your SOAP taglib parsing is necessary for sure. But for e.g.
my database beans: they query the database within the same VM
why should I build a String from that and then let Cocoon parse it?!

> > How would you do a generic toSAX method?! - There cannot be THE ONE!
> > All you could do is build a DOM tree and then turn it into SAX.
> > But that would be stupid!:)
> 
> How does a SAX parser parse arbitrary XML files? It reads characters and
> generates events :)

But why parse if you don't really need to?

Cheers
--
Torsten

Re: toDOM and toSAX of XMLFragment

Posted by Ulrich Mayring <ul...@denic.de>.
Torsten Curdt wrote:
> 
> That is your part dude! It is an interface! Only you know about the XML
> representation of the oject that implements XMLFragment!

ugghhh... you're saying I should hard-code according to the structure of
the generated XML?? What if the structure changes? Dude, don't send me
back to ASP or ColdFusion days ;-)

Ok, suppose I have XSP pages like these:

<xsp:page ...>

<xsp:logic>
	String xmlstring = getXMLfromSomewhere();
	// parse it, append to current node
</xsp:logic>

</xsp:page>

According to how I understand the XMLFragment interface I now have to
insert a toDOM and a toSAX method into each and every XSP page that I
have.

Instead why not write it like this:

<xsp:page ...>

<xsp:logic>
	String xmlstring = getXMLfromSomewhere();
	CocoonMethod(xmlstring);
</xsp:logic>

</xsp:page>

Instead of hard-coding toDOM and toSAX methods into each and every XSP
page, I just write one CocoonMethod for strings, one for Nodes, one for
SAX events and whatever else I have as input. This method could then
determine if we're running cocoon1 or cocoon2 and initiate the
appropriate action. And what do you know: the methods I write can also
be used by others :)

> How would you do a generic toSAX method?! - There cannot be THE ONE!
> All you could do is build a DOM tree and then turn it into SAX.
> But that would be stupid!:)

How does a SAX parser parse arbitrary XML files? It reads characters and
generates events :)

Ulrich

-- 
Ulrich Mayring
DENIC eG, Systementwicklung

RE: toDOM and toSAX of XMLFragment

Posted by Torsten Curdt <tc...@dff.st>.
> > An XMLFragment is an object that can be represented as a XML document
> > fragment. The toDOM() method (used by C1) should append the XML
> > representation of the object as children of the given node, and the
> > toSAX() method (used by C2) should generate the XML representation as
> > SAX events.
> 
> And why are these methods not implemented by cocoon? It doesn't make
> much sense for every user to implement his own version, why not have

That is your part dude! It is an interface! Only you know about the XML
representation of the oject that implements XMLFragment!

> This way it's not as useful, because I'll take my existing code and put
> it into a toDOM method and that's that. It still doesn't run with
> cocoon2 - if there were a generic toSAX method it would.

How would you do a generic toSAX method?! - There cannot be THE ONE!
All you could do is build a DOM tree and then turn it into SAX.
But that would be stupid!:)
--
Torsten


Re: toDOM and toSAX of XMLFragment

Posted by Ulrich Mayring <ul...@denic.de>.
Sylvain Wallez wrote:
> 
> An XMLFragment is an object that can be represented as a XML document
> fragment. The toDOM() method (used by C1) should append the XML
> representation of the object as children of the given node, and the
> toSAX() method (used by C2) should generate the XML representation as
> SAX events.

And why are these methods not implemented by cocoon? It doesn't make
much sense for every user to implement his own version, why not have
generic methods that take a string or a Node as input?

This way it's not as useful, because I'll take my existing code and put
it into a toDOM method and that's that. It still doesn't run with
cocoon2 - if there were a generic toSAX method it would.

Ulrich

-- 
Ulrich Mayring
DENIC eG, Systementwicklung