You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Mark Lundquist <ml...@comcast.net> on 2004/08/04 20:14:05 UTC
generate XML from flowscript
Hi,
I need a cocoon pipeline to serve up an XML stream obtained from a
back-end object. I'd like to avoid writing a generator if possible.
How would I write this using just flow + my POJO?
Thanks a lot,
Mark
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org
cocoon.session (was Re: generate XML from flowscript)
Posted by Mark Lundquist <ml...@wrinkledog.com>.
On Aug 4, 2004, at 8:22 PM, Tony Edwards wrote:
> Mark,
> Thanks for the pointer regarding the session object. I'll check it out.
cocoon.session
that's all you need to know, baby!
:-)
~ml
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org
Re: generate XML from flowscript
Posted by Mark Lundquist <ml...@wrinkledog.com>.
On Aug 5, 2004, at 12:11 AM, Bruno Dumon wrote:
> On Thu, 2004-08-05 at 05:55, Mark Lundquist wrote:
>> It would be slightly more elegant
>
> "slightly" is an understatement IMHO, putting data in the session which
> shouldn't be there is just plain ugly.
Yes, it is butt-ugly. But lately I am trying to correct my
idealist/perfectionist tendencies and become more pragmatic. Before, I
would have said "this is a travesty, whatever it takes to right this
wrong, it must be done!" :-)... so maybe I am overcompensating...
>
>> to be able to passthe source data in directly to sendPage() instead
>> of stashing it inthe session, but that hardly seems to justify the
>> trouble of writing awhole damn generator
>
> What you need is the module source combined with the flow-attribute
> input module.
>
> The module source is a source which gets its data from an input module.
>
> The usage is something like:
>
> <map:generate src="module:flow-attribute:mything"/>
>
> where 'mything' is the key used in the object passed to sendPage, ie
> sendPage("...", {"mykey": someObject});
>
> The type of someObject should be either an InputStream or a String. If
> you have your data retrieved in a byte array you can easily wrap it in
> a
> ByteArrayInputStream.
>
> OTOH, if you have your data already as a DOM-tree or an object
> implementing XMLizable, you can use the XModuleSource (still in
> scratchpad).
That's beautiful. I was just looking for the flow-attribute module
yesterday and couldn't find it, but I can see from my cocoon.xconf that
this is because it's moved into the core, duh!!!
In this case I'm actually starting with a string, so doing this with
the module source I can rip out my java code that builds an InputStream
and then parses it. I love getting rid of code.
Thanks a lot.
You know, I just have to say this every so often, and I haven't said it
for a while, so now it is time again: Cocoon just kicks total butt!
There, that should do for a while. I know I'm preaching to the choir,
but it's so true.
Thanks again,
~ml
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org
Re: generate XML from flowscript
Posted by Bruno Dumon <br...@outerthought.org>.
On Thu, 2004-08-05 at 05:55, Mark Lundquist wrote:
> On Aug 4, 2004, at 8:22 PM, Tony Edwards wrote:
>
> Mark,
> Thanks for the pointer regarding the session object. I'll
> check it out.
> To get my document into the pipeline, I do the following:
>
> Once I've stored the xml document in session I user the
> sessiongenerator in my pipeline to grab it and transform it:
>
> <map:match
> pattern="internal/tree-menu">
> <map:generate type="session-attr">
> <map:parameter name="attr-name"
> value="hrcyDoc"/>
> </map:generate>
> <map:tansform
> src="xml2tree.xsl"/>
> <map:serialize type="html" />
> </map:match>
>
> The session-attr generator refers to the
> SessionAttributeGenerator,declared thus:
> <map:generator
> name="session-attr"logger="sitemap.generator.session-attr"src="org.apache.cocoon.generation.SessionAttributeGenerator"/>
>
> It just grabs a session attribute object which in this case is
> the xmldocument we've cobbled together from the flowscript.
>
> Hope this helps!
>
> That does help. It helps a lot! Thanks, Tony...
>
> It's now on my to-do list to spiff up the userdoc
> forSessionAttributeGenerator :-)... there's not a whole lot there
> rightnow :-/.
>
> It would be slightly more elegant
"slightly" is an understatement IMHO, putting data in the session which
shouldn't be there is just plain ugly.
> to be able to passthe source data in directly to sendPage() instead
> of stashing it inthe session, but that hardly seems to justify the
> trouble of writing awhole damn generator
What you need is the module source combined with the flow-attribute
input module.
The module source is a source which gets its data from an input module.
The usage is something like:
<map:generate src="module:flow-attribute:mything"/>
where 'mything' is the key used in the object passed to sendPage, ie
sendPage("...", {"mykey": someObject});
The type of someObject should be either an InputStream or a String. If
you have your data retrieved in a byte array you can easily wrap it in a
ByteArrayInputStream.
OTOH, if you have your data already as a DOM-tree or an object
implementing XMLizable, you can use the XModuleSource (still in
scratchpad).
--
Bruno Dumon http://outerthought.org/
Outerthought - Open Source, Java & XML Competence Support Center
bruno@outerthought.org bruno@apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org
Re: generate XML from flowscript
Posted by Mark Lundquist <ml...@wrinkledog.com>.
On Aug 4, 2004, at 8:22 PM, Tony Edwards wrote:
> Mark,
> Thanks for the pointer regarding the session object. I'll check it out.
> To get my document into the pipeline, I do the following:
>
> Once I've stored the xml document in session I user the session
> generator in my pipeline to grab it and transform it:
>
> <map:match pattern="internal/tree-menu">
> <map:generate type="session-attr">
> <map:parameter name="attr-name" value="hrcyDoc"/>
> </map:generate>
> <map:tansform src="xml2tree.xsl"/>
> <map:serialize type="html" />
> </map:match>
>
> The session-attr generator refers to the SessionAttributeGenerator,
> declared thus:
> <map:generator name="session-attr"
> logger="sitemap.generator.session-attr"
> src="org.apache.cocoon.generation.SessionAttributeGenerator"/>
>
> It just grabs a session attribute object which in this case is the xml
> document we've cobbled together from the flowscript.
>
> Hope this helps!
That does help. It helps a lot! Thanks, Tony...
It's now on my to-do list to spiff up the userdoc for
SessionAttributeGenerator :-)... there's not a whole lot there right
now :-/.
It would be slightly more elegant to be able to pass the source data in
directly to sendPage() instead of stashing it in the session, but that
hardly seems to justify the trouble of writing a whole damn generator!
Anyway, it was quite instructive to look at the implementation of
SessionAttributeGenerator... I learned a little more about how Cocoon
works, and also see the better way if I were to write an "XMLGenerator"
component — viz., let XMLUtils.valueOf() do all the work for me :-)
So, thx-a-lot!
~ml
Re: generate XML from flowscript
Posted by Tony Edwards <te...@civica.com.au>.
Mark,
Thanks for the pointer regarding the session object. I'll check it out.
To get my document into the pipeline, I do the following:
Once I've stored the xml document in session I user the session
generator in my pipeline to grab it and transform it:
<map:match pattern="internal/tree-menu">
<map:generate type="session-attr">
<map:parameter name="attr-name" value="hrcyDoc"/>
</map:generate>
<map:tansform src="xml2tree.xsl"/>
<map:serialize type="html" />
</map:match>
The session-attr generator refers to the SessionAttributeGenerator,
declared thus:
<map:generator name="session-attr"
logger="sitemap.generator.session-attr"
src="org.apache.cocoon.generation.SessionAttributeGenerator"/>
It just grabs a session attribute object which in this case is the xml
document we've cobbled together from the flowscript.
Hope this helps!
Tony
Mark Lundquist wrote:
>On Aug 4, 2004, at 6:18 PM, Tony Edwards wrote:
>
>
>
>>Hi Mark,
>>I'm using flow to create and modify an xml document which I then send
>>
>>
>
>
>
>>through a pipeline for display.
>>I've forgotten where I pinched the code from but here's an example:
>>
>>
>
>Thanks for the reply! See below...
>
>
>
>>importClass(org.apache.xpath.XPathAPI);
>>importClass(javax.xml.parsers.DocumentBuilderFactory);
>>importClass(org.w3c.dom.Node);
>>importClass(org.w3c.dom.Element);
>>importClass(org.w3c.dom.NodeList);
>>
>>function newDocument(root, attributeName, attributeVal) {
>> var result =
>>
>>
>>
>DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()
>
>
>>;
>>
>> // Create a processing instruction targeted for xml.
>> var node = result.createProcessingInstruction("xml",
>>"version='1.0'");
>> result.appendChild(node);
>> node = null;
>>
>> // Create a comment for the document.
>> node = result.createComment(
>> "sample xml file created using XML DOM object.");
>> result.appendChild(node);
>> node=null;
>> result.appendChild(result.createElement(root));
>> print("Add attributes to root..." + attributeName);
>> if (attributeVal != null){
>> try {
>> print("creating attribute for root: " + attributeName);
>>
>>
>
>
>
>> var root = result.getFirstChild();
>> root.setAttribute(attributeName, attributeVal);
>> } catch (error) {
>> cocoon.log.error("Could not add attribute node to root: " +
>>
>>
>
>
>
>>error);
>> }
>> }
>>
>> return result;
>>}
>>To load an existing document I use this:
>>// Load a document from an URI, absolute or relative to the current
>>sitemap
>>function loadDocument(uri) {
>> var resolver =
>>cocoon.getComponent("org.apache.excalibur.source.SourceResolver");
>> var source = resolver.resolveURI(uri);
>> try {
>> document =
>>Packages.org.apache.cocoon.components.source.SourceUtil.toDOM(source);
>> } finally {
>> resolver.release(source);
>> cocoon.releaseComponent(resolver);
>> }
>> return document;
>>}
>>
>>
>
>OK, that's all routine XML and Cocoon stuff...
>
>
>
>
>>I then add the document to the session:
>> var sessionManager =
>>
>>
>>
>cocoon.getComponent("org.apache.cocoon.webapps.session.SessionManager")
>
>
>>;
>> var session = sessionManager.getSession(true);
>>
>>
>
>FYI, you already have a global "session" object in flowscript, so you
>don't need that bit... see
>http://cocoon.apache.org/2.1/userdocs/flow/api.html#Session+Object
>
>
>
>> session.setAttribute("hrcyDoc", hrcyDoc);
>> cocoon.sendPage("tree-menu");
>>the last step (sendPage("tree-menu")) renders the document as a tree
>>using xslt.
>>
>>
>
>OK, so what I need to know is, how does your "tree-menu" pipeline gets
>the DOM tree out of the session? You've stored it there, but how do
>you access it?
>
>I was kinda surprised to find nothing in Cocoon to do this, and even
>Google came up empty-handed. I've been thinking of writing a generator
>
>that would take the DOM tree (and/or an InputStream and/or String) in
>the context object, just like the "bizData" to JXTemplateGenerator, and
>
>then just plumb that in to its xmlConsumer. So then I could call that
>pipeline from flowscript using sendPage()... WDYT?
>
>~ml
>
>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
>For additional commands, e-mail: users-help@cocoon.apache.org
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org
Re: generate XML from flowscript
Posted by Mark Lundquist <ml...@wrinkledog.com>.
On Aug 5, 2004, at 11:16 PM, Ugo Cei wrote:
> Il giorno 06/ago/04, alle 00:43, Mark Lundquist ha scritto:
>
>>> <jx:out value="#{dom/*}"/>
>>
>> Not sure this is what I want... doesn't <jx:out> yield the
>> string-value of the node(s), a la <xsl:value-of>?
>
> Hmmm, not sure about that. It will surely work if you just put
> #{dom/*} as the value of an element in your template. If "dom"
> implements org.w3c.dom.Document, its content will be serialized as a
> child of the element.
I will give it a try!
thx, ~ml
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org
Re: generate XML from flowscript
Posted by Ugo Cei <ug...@apache.org>.
Il giorno 06/ago/04, alle 00:43, Mark Lundquist ha scritto:
>> <jx:out value="#{dom/*}"/>
>
> Not sure this is what I want... doesn't <jx:out> yield the
> string-value of the node(s), a la <xsl:value-of>?
Hmmm, not sure about that. It will surely work if you just put #{dom/*}
as the value of an element in your template. If "dom" implements
org.w3c.dom.Document, its content will be serialized as a child of the
element.
Ugo
--
Ugo Cei - http://beblogging.com/
Re: generate XML from flowscript
Posted by Mark Lundquist <ml...@wrinkledog.com>.
On Aug 5, 2004, at 2:50 AM, Ugo Cei wrote:
>
> JXTemplate can serialize DOM objects for you:
>
> cocoon.sendPage("view", { "dom", document });
>
> ...
>
> <jx:out value="#{dom/*}"/>
Not sure this is what I want... doesn't <jx:out> yield the string-value
of the node(s), a la <xsl:value-of>?
~ml
Re: generate XML from flowscript
Posted by Ugo Cei <ug...@apache.org>.
Il giorno 05/ago/04, alle 03:39, Mark Lundquist ha scritto:
> I was kinda surprised to find nothing in Cocoon to do this, and even
> Google came up empty-handed. I've been thinking of writing a
> generator that would take the DOM tree (and/or an InputStream and/or
> String) in the context object, just like the "bizData" to
> JXTemplateGenerator, and then just plumb that in to its xmlConsumer.
> So then I could call that pipeline from flowscript using sendPage()...
> WDYT?
JXTemplate can serialize DOM objects for you:
cocoon.sendPage("view", { "dom", document });
...
<jx:out value="#{dom/*}"/>
Should do the trick.
Ugo
--
Ugo Cei - http://beblogging.com/
Re: generate XML from flowscript
Posted by Mark Lundquist <ml...@wrinkledog.com>.
On Aug 4, 2004, at 6:18 PM, Tony Edwards wrote:
> Hi Mark,
> I'm using flow to create and modify an xml document which I then send
> through a pipeline for display.
> I've forgotten where I pinched the code from but here's an example:
Thanks for the reply! See below...
>
> importClass(org.apache.xpath.XPathAPI);
> importClass(javax.xml.parsers.DocumentBuilderFactory);
> importClass(org.w3c.dom.Node);
> importClass(org.w3c.dom.Element);
> importClass(org.w3c.dom.NodeList);
>
> function newDocument(root, attributeName, attributeVal) {
> var result =
> DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()
> ;
>
> // Create a processing instruction targeted for xml.
> var node = result.createProcessingInstruction("xml",
> "version='1.0'");
> result.appendChild(node);
> node = null;
>
> // Create a comment for the document.
> node = result.createComment(
> "sample xml file created using XML DOM object.");
> result.appendChild(node);
> node=null;
> result.appendChild(result.createElement(root));
> print("Add attributes to root..." + attributeName);
> if (attributeVal != null){
> try {
> print("creating attribute for root: " + attributeName);
> var root = result.getFirstChild();
> root.setAttribute(attributeName, attributeVal);
> } catch (error) {
> cocoon.log.error("Could not add attribute node to root: " +
> error);
> }
> }
>
> return result;
> }
> To load an existing document I use this:
> // Load a document from an URI, absolute or relative to the current
> sitemap
> function loadDocument(uri) {
> var resolver =
> cocoon.getComponent("org.apache.excalibur.source.SourceResolver");
> var source = resolver.resolveURI(uri);
> try {
> document =
> Packages.org.apache.cocoon.components.source.SourceUtil.toDOM(source);
> } finally {
> resolver.release(source);
> cocoon.releaseComponent(resolver);
> }
> return document;
> }
OK, that's all routine XML and Cocoon stuff...
>
> I then add the document to the session:
> var sessionManager =
> cocoon.getComponent("org.apache.cocoon.webapps.session.SessionManager")
> ;
> var session = sessionManager.getSession(true);
FYI, you already have a global "session" object in flowscript, so you
don't need that bit... see
http://cocoon.apache.org/2.1/userdocs/flow/api.html#Session+Object
> session.setAttribute("hrcyDoc", hrcyDoc);
> cocoon.sendPage("tree-menu");
> the last step (sendPage("tree-menu")) renders the document as a tree
> using xslt.
OK, so what I need to know is, how does your "tree-menu" pipeline gets
the DOM tree out of the session? You've stored it there, but how do
you access it?
I was kinda surprised to find nothing in Cocoon to do this, and even
Google came up empty-handed. I've been thinking of writing a generator
that would take the DOM tree (and/or an InputStream and/or String) in
the context object, just like the "bizData" to JXTemplateGenerator, and
then just plumb that in to its xmlConsumer. So then I could call that
pipeline from flowscript using sendPage()... WDYT?
~ml
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org
Re: generate XML from flowscript
Posted by Tony Edwards <te...@civica.com.au>.
Hi guys,
I sent Eric my function off list but I'll add the contents and serving
suggestion to the Wiki.
Stand by....
Tony
gounis@osmosis.gr wrote:
>its an interesting point
>
>can you create a wiki page?
>
>
>-- stavros
>
>On Wed, 25 Aug 2004, JACOB, ERIC wrote:
>
>
>
>>Hi Tony,
>>
>>
>>
>>>I can't attest as to its efficiency but it seems to go OK so far.
>>>
>>>
>I've
>
>
>>>got a whole bunch of other functions to add, move, copy nodes and
>>>attribute etc.
>>>If you need any of this, let me know and I'll send it to you.
>>>
>>>
>>I would be interested by your "flowscript" functions. I need to load
>>
>>
>an xml
>
>
>>document, retrieve a node (via XPath?), update its value and then save
>>
>>
>the
>
>
>>document.
>>
>>Many thanks,
>>
>>Eric
>>
>>
>>
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
>>For additional commands, e-mail: users-help@cocoon.apache.org
>>
>>
>>
>>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
>For additional commands, e-mail: users-help@cocoon.apache.org
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org
RE: generate XML from flowscript
Posted by go...@osmosis.gr.
its an interesting point
can you create a wiki page?
-- stavros
On Wed, 25 Aug 2004, JACOB, ERIC wrote:
> Hi Tony,
>
> > I can't attest as to its efficiency but it seems to go OK so far. I've
> > got a whole bunch of other functions to add, move, copy nodes and
> > attribute etc.
> > If you need any of this, let me know and I'll send it to you.
>
> I would be interested by your "flowscript" functions. I need to load an xml
> document, retrieve a node (via XPath?), update its value and then save the
> document.
>
> Many thanks,
>
> Eric
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
> For additional commands, e-mail: users-help@cocoon.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org
RE: generate XML from flowscript
Posted by "JACOB, ERIC" <er...@bell.ca>.
Hi Tony,
> I can't attest as to its efficiency but it seems to go OK so far. I've
> got a whole bunch of other functions to add, move, copy nodes and
> attribute etc.
> If you need any of this, let me know and I'll send it to you.
I would be interested by your "flowscript" functions. I need to load an xml
document, retrieve a node (via XPath?), update its value and then save the
document.
Many thanks,
Eric
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org
Re: generate XML from flowscript
Posted by Tony Edwards <te...@civica.com.au>.
Hi Mark,
I'm using flow to create and modify an xml document which I then send
through a pipeline for display.
I've forgotten where I pinched the code from but here's an example:
importClass(org.apache.xpath.XPathAPI);
importClass(javax.xml.parsers.DocumentBuilderFactory);
importClass(org.w3c.dom.Node);
importClass(org.w3c.dom.Element);
importClass(org.w3c.dom.NodeList);
function newDocument(root, attributeName, attributeVal) {
var result =
DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
// Create a processing instruction targeted for xml.
var node = result.createProcessingInstruction("xml", "version='1.0'");
result.appendChild(node);
node = null;
// Create a comment for the document.
node = result.createComment(
"sample xml file created using XML DOM object.");
result.appendChild(node);
node=null;
result.appendChild(result.createElement(root));
print("Add attributes to root..." + attributeName);
if (attributeVal != null){
try {
print("creating attribute for root: " + attributeName);
var root = result.getFirstChild();
root.setAttribute(attributeName, attributeVal);
} catch (error) {
cocoon.log.error("Could not add attribute node to root: " +
error);
}
}
return result;
}
To load an existing document I use this:
// Load a document from an URI, absolute or relative to the current sitemap
function loadDocument(uri) {
var resolver =
cocoon.getComponent("org.apache.excalibur.source.SourceResolver");
var source = resolver.resolveURI(uri);
try {
document =
Packages.org.apache.cocoon.components.source.SourceUtil.toDOM(source);
} finally {
resolver.release(source);
cocoon.releaseComponent(resolver);
}
return document;
}
I then add the document to the session:
var sessionManager =
cocoon.getComponent("org.apache.cocoon.webapps.session.SessionManager");
var session = sessionManager.getSession(true);
session.setAttribute("hrcyDoc", hrcyDoc);
cocoon.sendPage("tree-menu");
the last step (sendPage("tree-menu")) renders the document as a tree
using xslt.
I can't attest as to its efficiency but it seems to go OK so far. I've
got a whole bunch of other functions to add, move, copy nodes and
attribute etc.
If you need any of this, let me know and I'll send it to you.
Regards,
Tony
Mark Lundquist wrote:
>Hi,
>
>I need a cocoon pipeline to serve up an XML stream obtained from a
>back-end object. I'd like to avoid writing a generator if possible.
>How would I write this using just flow + my POJO?
>
>Thanks a lot,
>Mark
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
>For additional commands, e-mail: users-help@cocoon.apache.org
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org