You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Jonas Lundberg <my...@gmail.com> on 2006/01/05 12:15:06 UTC

How to return xml from a flowscript?

I am quite new to flowscript, so this is a basic quesiton...
However, I have not been able to get any of the examples I found on
Google to work.

I have the following flowscript:
function cleanWordHtml() {

var base = Packages.java.lang.String(cocoon.parameters["base"]);
var page = Packages.java.lang.String(cocoon.parameters["page"]);
var output = new Packages.java.io.ByteArrayOutputStream();

cocoon.processPipelineTo("filesystem.cleanwhtm/"+base+"/"+page,null,output);

var cleanxml = output.toString();

cocoon.sendPage("shapecms_upload.diskutera", {cleanxml: cleanxml});
}

It reads a Cocoon pipeline, which converts a Word HTML file to XML.
Then it sends this as a string in a parameter, to a pipeline that
processes it further.

What I *would like* to do, is to send XML in the parameter, instead of a string.
Is this possible? Does anyone have a solution for me?

Best regards
Hans

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


Re: How to return xml from a flowscript?

Posted by Jonas Lundberg <my...@gmail.com>.
Yes, de-serializing with util:eval works in some cases. However, it
does not work for documents that contain < or > in the text contents,
because that will then be de-serialized as well. This will cause an
error.
For instance, the XHTML:
<p> &lt;!-- Add here other XML:DB compliant databases' drivers --&gt; </p>

will be converted to (when it is all converted to a string.):
&lt;p&gt; &lt;!-- Add here other XML:DB compliant databases' drivers
--&gt;&lt;/p&gt;

Then, if I de-serialize that, I will get:
<p> <-- Add here other XML:DB compliant databases' drivers --> </p>

And that is is not good at all. So the problem remains....

Regards
Hans


On 1/5/06, Huib Verweij <hu...@streamtec.nl> wrote:
> Jonas Lundberg wrote:
> > The xquery stores the XML file in eXist.
> > Thus, if I try to insert the xml file using an Xinclude in discuss.xq,
> > and then use an xinclude transformer after generation,
> > then it will be put there *after* the query is executed. Which is not
> > useful in this case, since the XML is needed during execution.
> >
> Hi,
>
> I'm curious about this too. I've so far serialised the XML in
> javascript, stored it in the session (this can be improved I think, it's
> 'not great') and passed the serialised version as a parameter to the
> xquery. In the xquery you can use util:eval() to de-serialise the XML
> (possibly skipping the XML declaration though). It's not great, but it
> works. Serialising your XML isn't 'cool', but when you use XML-RPC (like
> I do) it'll be serialised anyway, doesn't it?
>
> Regards,
>
> Huib.
>
> > Regards
> > Hans
> >
> > On 1/5/06, Geert Josten <Ge...@daidalos.nl> wrote:
> >
> >>> Thanks for the replies.
> >>> The problem is that I then have use the xml in a pipeline that starts
> >>> with an xquery generator (this is the pipeline I call from the
> >>> flowscript):
> >>>      <map:generate src="xq/discuss.xq" type="xquery">
> >>>      <map:parameter name="cleanedxml" value="{flow-attribute:cleanxml}"/>
> >>>
> >> How is the xquery using the xml in the parameter? Is it an idea to have another pipe that
> >> *generates* the discuss.xq which has your cleanedxml fragment included somewhere?
> >>
> >> Cheers,
> >> Geert
> >>
> >> ---------------------------------------------------------------------
> >> 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
>
>

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


Re: How to return xml from a flowscript?

Posted by Huib Verweij <hu...@streamtec.nl>.
Jonas Lundberg wrote:
> The xquery stores the XML file in eXist.
> Thus, if I try to insert the xml file using an Xinclude in discuss.xq,
> and then use an xinclude transformer after generation,
> then it will be put there *after* the query is executed. Which is not
> useful in this case, since the XML is needed during execution.
>   
Hi,

I'm curious about this too. I've so far serialised the XML in 
javascript, stored it in the session (this can be improved I think, it's 
'not great') and passed the serialised version as a parameter to the 
xquery. In the xquery you can use util:eval() to de-serialise the XML 
(possibly skipping the XML declaration though). It's not great, but it 
works. Serialising your XML isn't 'cool', but when you use XML-RPC (like 
I do) it'll be serialised anyway, doesn't it?

Regards,

Huib.

> Regards
> Hans
>
> On 1/5/06, Geert Josten <Ge...@daidalos.nl> wrote:
>   
>>> Thanks for the replies.
>>> The problem is that I then have use the xml in a pipeline that starts
>>> with an xquery generator (this is the pipeline I call from the
>>> flowscript):
>>>      <map:generate src="xq/discuss.xq" type="xquery">
>>>      <map:parameter name="cleanedxml" value="{flow-attribute:cleanxml}"/>
>>>       
>> How is the xquery using the xml in the parameter? Is it an idea to have another pipe that
>> *generates* the discuss.xq which has your cleanedxml fragment included somewhere?
>>
>> Cheers,
>> Geert
>>
>> ---------------------------------------------------------------------
>> 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: How to return xml from a flowscript?

Posted by Jonas Lundberg <my...@gmail.com>.
Thanks,
yes that would be a good way to store the document. (I currently do
that using XSP)

However, I should add that the xQuery is the core of a content
management system, so it also does version management, among other
things, (while storing the file). Therefore, storing a temporary file,
does not seem optimal. I would like to avoid that.



Regards
Hans
http://shapecms.sourceforge.net/

On 1/5/06, Zbigniew Bomert OP <zb...@dominikanie.pl> wrote:
> Jonas Lundberg wrote:
> > The xquery stores the XML file in eXist. (...)
>
> You can store xml in eXist from flowscript, without using xquery, as
> described in:
>
> http://wiki.apache.org/cocoon/CformsExistAndFlow
>
> Zbigniew Bomert
>
> ---------------------------------------------------------------------
> 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: How to return xml from a flowscript?

Posted by Zbigniew Bomert OP <zb...@dominikanie.pl>.
Jonas Lundberg wrote:
> The xquery stores the XML file in eXist. (...)

You can store xml in eXist from flowscript, without using xquery, as 
described in:

http://wiki.apache.org/cocoon/CformsExistAndFlow

Zbigniew Bomert

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


Re: How to return xml from a flowscript?

Posted by Suzan Foster <su...@nerocmediaware.nl>.
Does the xquery do any processing on the xml, or does it just use 
eXist's update extensions to store it as-is? If you need to do the 
latter you can use an XMLDBSource to write the file directly into eXist 
from your flowscript.

Jonas Lundberg wrote:

>The xquery stores the XML file in eXist.
>Thus, if I try to insert the xml file using an Xinclude in discuss.xq,
>and then use an xinclude transformer after generation,
>then it will be put there *after* the query is executed. Which is not
>useful in this case, since the XML is needed during execution.
>
>Regards
>Hans
>
>On 1/5/06, Geert Josten <Ge...@daidalos.nl> wrote:
>  
>
>>>Thanks for the replies.
>>>The problem is that I then have use the xml in a pipeline that starts
>>>with an xquery generator (this is the pipeline I call from the
>>>flowscript):
>>>     <map:generate src="xq/discuss.xq" type="xquery">
>>>     <map:parameter name="cleanedxml" value="{flow-attribute:cleanxml}"/>
>>>      
>>>
>>How is the xquery using the xml in the parameter? Is it an idea to have another pipe that
>>*generates* the discuss.xq which has your cleanedxml fragment included somewhere?
>>
>>Cheers,
>>Geert
>>
>>---------------------------------------------------------------------
>>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
>
>  
>
-- 

Met vriendelijke groet,

Suzan Foster
Software Engineer

*NEROC'MEDIAWARE *
De Run 1131
5503 LB Veldhoven
T +31 (0)40 258 66 66
F +31 (0)40 258 66 77

E suzan.foster@nerocmediaware.nl <ma...@nerocmediaware.nl>

================================================
De informatie opgenomen in dit bericht kan vertrouwelijk zijn en
is uitsluitend bestemd voor de geadresseerde. Indien u dit bericht
onterecht ontvangt, wordt u verzocht de inhoud niet te gebruiken en
de afzender direct te informeren door het bericht te retourneren.
================================================
The information contained in this message may be confidential
and is intended to be exclusively for the addressee. Should you
receive this message unintentionally, please do not use the contents
herein and notify the sender immediately by return e-mail.

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


Re: How to return xml from a flowscript?

Posted by Jonas Lundberg <my...@gmail.com>.
That sounds like a very attractive solution.
How is that done? Do you have an example to share?

Regards
Hans

On 1/6/06, Suzan Foster <su...@nerocmediaware.nl> wrote:
> I think you are missing the option of running the XQuery directly from
> the flowscript using your xml as context document. This is what we are
> doing with X-Hive's XQuery implementation.
>
> Jonas Lundberg wrote:
>
> >Right. It is not possible to pass the XML to an qxuery pipeline
> >(starting with an xquery generator), then (unless it is passed as an
> >escaped string). Thanks for the explanations.
> >
> >That leaves two options, since the xquery generator does not support
> >the Cocoon protocol.
> >a) store from flowscript.
> >b) store from xsp (as I do now).
> >(and then do the content management of the stored data (before or
> >after storage) in the xquery)
> >To me, there does not seem to be any specific advantages or
> >disadvantages of either solution, if I only store uploaded files in
> >that way. Does that seem right?
> >
> >Use of HTMLarea, or similar widget, should get the same problem. Is
> >that correct?
> >(the problem being that all '<' and '>' in the text are escaped
> >together with html tags, causing problems when un-escaping the data to
> >get xml)
> >
> >
> --
>
> Met vriendelijke groet,
>
> Suzan Foster
> Software Engineer
>
> *NEROC'MEDIAWARE *
> De Run 1131
> 5503 LB Veldhoven
> T +31 (0)40 258 66 66
> F +31 (0)40 258 66 77
>
> E suzan.foster@nerocmediaware.nl <ma...@nerocmediaware.nl>
>
> ================================================
> De informatie opgenomen in dit bericht kan vertrouwelijk zijn en
> is uitsluitend bestemd voor de geadresseerde. Indien u dit bericht
> onterecht ontvangt, wordt u verzocht de inhoud niet te gebruiken en
> de afzender direct te informeren door het bericht te retourneren.
> ================================================
> The information contained in this message may be confidential
> and is intended to be exclusively for the addressee. Should you
> receive this message unintentionally, please do not use the contents
> herein and notify the sender immediately by return e-mail.
>
> ---------------------------------------------------------------------
> 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: How to return xml from a flowscript?

Posted by Suzan Foster <su...@nerocmediaware.nl>.
I think you are missing the option of running the XQuery directly from 
the flowscript using your xml as context document. This is what we are 
doing with X-Hive's XQuery implementation.

Jonas Lundberg wrote:

>Right. It is not possible to pass the XML to an qxuery pipeline
>(starting with an xquery generator), then (unless it is passed as an
>escaped string). Thanks for the explanations.
>
>That leaves two options, since the xquery generator does not support
>the Cocoon protocol.
>a) store from flowscript.
>b) store from xsp (as I do now).
>(and then do the content management of the stored data (before or
>after storage) in the xquery)
>To me, there does not seem to be any specific advantages or
>disadvantages of either solution, if I only store uploaded files in
>that way. Does that seem right?
>
>Use of HTMLarea, or similar widget, should get the same problem. Is
>that correct?
>(the problem being that all '<' and '>' in the text are escaped
>together with html tags, causing problems when un-escaping the data to
>get xml)
>  
>
-- 

Met vriendelijke groet,

Suzan Foster
Software Engineer

*NEROC'MEDIAWARE *
De Run 1131
5503 LB Veldhoven
T +31 (0)40 258 66 66
F +31 (0)40 258 66 77

E suzan.foster@nerocmediaware.nl <ma...@nerocmediaware.nl>

================================================
De informatie opgenomen in dit bericht kan vertrouwelijk zijn en
is uitsluitend bestemd voor de geadresseerde. Indien u dit bericht
onterecht ontvangt, wordt u verzocht de inhoud niet te gebruiken en
de afzender direct te informeren door het bericht te retourneren.
================================================
The information contained in this message may be confidential
and is intended to be exclusively for the addressee. Should you
receive this message unintentionally, please do not use the contents
herein and notify the sender immediately by return e-mail.

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


Re: How to return xml from a flowscript?

Posted by Jonas Lundberg <my...@gmail.com>.
Right. It is not possible to pass the XML to an qxuery pipeline
(starting with an xquery generator), then (unless it is passed as an
escaped string). Thanks for the explanations.

That leaves two options, since the xquery generator does not support
the Cocoon protocol.
a) store from flowscript.
b) store from xsp (as I do now).
(and then do the content management of the stored data (before or
after storage) in the xquery)
To me, there does not seem to be any specific advantages or
disadvantages of either solution, if I only store uploaded files in
that way. Does that seem right?

Use of HTMLarea, or similar widget, should get the same problem. Is
that correct?
(the problem being that all '<' and '>' in the text are escaped
together with html tags, causing problems when un-escaping the data to
get xml)

Regards
Hans


On 1/5/06, Geert Josten <Ge...@daidalos.nl> wrote:
> > So, what you are saying now is that you cannot pass an xml document as
> > a parameter in Cocoon, at all?
>
> What do you mean with xml document and what with parameter?
>
> The parameters you pass from FlowScript can contain any object type, but I'm pretty sure that
> <map:parameter> does not allow that. That is probably why you are inserting the XML itself as value
> of the parameter, with all the complications you already mentioned.
>
> I really think it is wise to reconsider you approach. You don't want to treat XML as an escaped
> string...
>
> I liked the suggestions of uploading the xml to eXist from FlowScript directly...
>
> Regards,
> Geert
>
> ---------------------------------------------------------------------
> 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: How to return xml from a flowscript?

Posted by Geert Josten <Ge...@daidalos.nl>.
> So, what you are saying now is that you cannot pass an xml document as
> a parameter in Cocoon, at all?

What do you mean with xml document and what with parameter?

The parameters you pass from FlowScript can contain any object type, but I'm pretty sure that 
<map:parameter> does not allow that. That is probably why you are inserting the XML itself as value 
of the parameter, with all the complications you already mentioned.

I really think it is wise to reconsider you approach. You don't want to treat XML as an escaped 
string...

I liked the suggestions of uploading the xml to eXist from FlowScript directly...

Regards,
Geert

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


Re: How to return xml from a flowscript?

Posted by Jonas Lundberg <my...@gmail.com>.
So, what you are saying now is that you cannot pass an xml document as
a parameter in Cocoon, at all?
If one can pass xml documents as parameters, then...
How would I parse the output stream, and pass along the DOM object?

Regards
Hans


On 1/5/06, Geert Josten <Ge...@daidalos.nl> wrote:
> You can parse the output stream in Flowscript, just like you would in Java or Javascript and put a
> DOM object in the parameter you are passing to the pipeline, but I really don't know how you should
> access it from your xquery generator. You could try to use the eval function as well?
>
> Regards,
> Geert
>
> Jonas Lundberg wrote:
>
> > That might work, but it is not a good solution for me, although it
> > might work in other cases. My xqueries are very complex.
> > But this can't end here, can it? Is there really no way of returning
> > XML in a parameter from a flowscript in Cocoon?
> >
> > Regards,
> > Hans
>
> ---------------------------------------------------------------------
> 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: How to return xml from a flowscript?

Posted by Geert Josten <Ge...@daidalos.nl>.
You can parse the output stream in Flowscript, just like you would in Java or Javascript and put a 
DOM object in the parameter you are passing to the pipeline, but I really don't know how you should 
access it from your xquery generator. You could try to use the eval function as well?

Regards,
Geert

Jonas Lundberg wrote:

> That might work, but it is not a good solution for me, although it
> might work in other cases. My xqueries are very complex.
> But this can't end here, can it? Is there really no way of returning
> XML in a parameter from a flowscript in Cocoon?
> 
> Regards,
> Hans

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


Re: How to return xml from a flowscript?

Posted by Jonas Lundberg <my...@gmail.com>.
That seems promising. However, the "Module.NAMESPACE_URI,
Module.PREFIX" part gives me 4 compilation errors, starting with:
    [javac] E:\Program\eXist1203\src\com\nps\xquery\request\DocFunction.java:30:
 cannot find symbol
    [javac] symbol  : variable Module

I have not written any extension moduels myself, so I have no idea
about how to fix this.... what could be wrong here?

Regards
Hans

On 1/6/06, Suzan Foster <su...@nerocmediaware.nl> wrote:
> The problem however with using variables is that eXist can only operate
> on it's own persistent DOM implementation within the query. You need to
> create a temporary document to pass to the XQuery. I hit this problem
> when writing an extension function for processing the request as XML
> (sent by an XForm) and wrote the following implementation:
>
> package com.nps.xquery.request;
>
> import org.exist.dom.QName;
> import org.exist.http.servlets.RequestWrapper;
> import org.exist.xquery.functions.request.RequestModule;
> import org.exist.xquery.XQueryContext;
> import org.exist.xquery.BasicFunction;
> import org.exist.xquery.Cardinality;
> import org.exist.xquery.FunctionSignature;
> import org.exist.xquery.XPathException;
> import org.exist.xquery.Variable;
> import org.exist.xquery.value.Sequence;
> import org.exist.xquery.value.SequenceType;
> import org.exist.xquery.value.Type;
> import org.exist.xquery.value.JavaObjectValue;
> import org.exist.dom.NodeProxy;
> import org.exist.dom.DocumentImpl;
> import org.exist.dom.NodeImpl;
> import org.exist.memtree.MemTreeBuilder;
> import org.exist.memtree.DocumentBuilderReceiver;
> import javax.xml.parsers.SAXParserFactory;
>
> import org.xml.sax.InputSource;
> import org.xml.sax.XMLReader;
>
> public class DocFunction extends BasicFunction {
>
>     public final static FunctionSignature signature =
>         new FunctionSignature(
>             new QName("doc", Module.NAMESPACE_URI, Module.PREFIX),
>             "",
>             null,
>             new SequenceType(Type.NODE, Cardinality.ZERO_OR_ONE)
>         );
>
>     public DocFunction(XQueryContext context) {
>         super(context, signature);
>     }
>
>     public Sequence eval(Sequence[] arg0, Sequence arg1) throws
> XPathException {
>         RequestModule requestModule =
>             (RequestModule) context.getModule(RequestModule.NAMESPACE_URI);
>
>         // request object is read from global variable $request
>         Variable var =
> requestModule.resolveVariable(RequestModule.REQUEST_VAR);
>         if(var == null)
>             throw new XPathException("No request object found in the
> current XQuery context.");
>         if (var.getValue().getItemType() != Type.JAVA_OBJECT)
>             throw new XPathException("Variable $request is not bound to
> a Java object.");
>         JavaObjectValue value = (JavaObjectValue) var.getValue().itemAt(0);
>         if (value.getObject() instanceof RequestWrapper) {
>             RequestWrapper request = (RequestWrapper)value.getObject();
>             try {
>                 MemTreeBuilder builder = context.getDocumentBuilder();
>                 DocumentBuilderReceiver receiver = new
> DocumentBuilderReceiver(builder);
>                 SAXParserFactory factory = SAXParserFactory.newInstance();
>                 factory.setNamespaceAware(true);
>                 XMLReader reader = factory.newSAXParser().getXMLReader();
>                 reader.setContentHandler(receiver);
>                 reader.parse(new InputSource(request.getInputStream()));
>                 DocumentImpl doc =
> context.storeTemporaryDoc(builder.getDocument());
>                 NodeImpl node =
> (NodeImpl)doc.getDocumentElement().getFirstChild();
>                 return new NodeProxy(doc, node.getGID(),
> node.getInternalAddress());
>             }
>             catch (Exception e) {
>                 throw new XPathException(getASTNode(), e.getMessage(), e);
>             }
>         } else
>             throw new XPathException("Variable $request is not bound to
> a Request object.");
>     }
> }
>
> I think it should be possible to use such a temporary document as
> context for the XQuery using XQueryService.query(XMLResource,  String).
>
> Jean-Baptiste Quenot wrote:
>
> >* Jonas Lundberg:
> >
> >
> >
> >>That might work, but it is  not a good solution for me, although
> >>it might work in other cases. My xqueries are very complex.  But
> >>this can't end here, can it? Is there really no way of returning
> >>XML in a parameter from a flowscript in Cocoon?
> >>
> >>
> >
> >Sitemap parameters  are Strings, not  Objects.  You cannot  pass a
> >DOM.  However you can execute an XQuery without the
> >XQueryGenerator, Java snippet follows:
> >
> >        public static Node executeXQuery(Collection collection, Source inputSource, Map variables, Logger logger) throws XMLDBException {
> >                XQueryService service = (XQueryService) collection.getService(
> >                                "XQueryService", "1.0");
> >                service.setProperty(Serializer.GENERATE_DOC_EVENTS, "false");
> >                if (variables != null) {
> >                        if (logger != null) logger.debug("Executing " + inputSource.getURI() + " with variables " + variables);
> >                        Iterator it = variables.entrySet().iterator();
> >                        while (it.hasNext()) {
> >                                Map.Entry entry = (Map.Entry)it.next();
> >                                service.declareVariable((String)entry.getKey(), entry.getValue());
> >                        }
> >                } else {
> >                        if (logger != null) logger.debug("Executing " + inputSource.getURI());
> >                }
> >                ResourceSet result = service.execute(new CocoonSource(inputSource, true));
> >                if (result == null) {
> >                        if (logger != null) logger.error("null result after executing XQuery!");
> >                        return null; // a DOM Node cannot be null so null can be used safely for signaling errors
> >                }
> >                if (result.getSize() == 0) {
> >                        if (logger != null) logger.error("empty XML result after executing XQuery!");
> >                        return null; // a DOM Node cannot be null so null can be used safely for signaling errors
> >                }
> >                XMLResource resource = (XMLResource) result.getResource(0);
> >                return resource.getContentAsDOM();
> >        }
> >
> >But I think the best solution in order to save directly an XML
> >document in the XML database is to use the XMLDBSource with
> >saveDocumentToSource("xmldb:exist://localhost:8080/exist/xmlrpc/db/file.xml", document),
> >FlowScript snippet follows:
> >
> >/*
> > * Save XML document to source.
> > * Parameters :
> > *   outputMode : the desired output mode : 'html', 'xml', or 'text'
> > *                Defaults to "xml"
> > */
> >function saveDocumentToSource(source, document, outputMode ) {
> >        var tf = Packages.javax.xml.transform.TransformerFactory.newInstance();
> >        var outputStream = null;
> >
> >        // If no mode is specified, we use XML
> >        if(! outputMode) {
> >                outputMode = 'xml';
> >        }
> >
> >        try {
> >                if (source instanceof Packages.org.apache.excalibur.source.ModifiableSource
> >                        && tf.getFeature(Packages.javax.xml.transform.sax.SAXTransformerFactory.FEATURE)) {
> >            outputStream = source.getOutputStream();
> >                var transformer = tf.newTransformer();
> >            transformer.setOutputProperty(Packages.javax.xml.transform.OutputKeys.INDENT, "yes");
> >            transformer.setOutputProperty(Packages.javax.xml.transform.OutputKeys.METHOD, outputMode);
> >                transformer.transform(
> >                     new Packages.javax.xml.transform.dom.DOMSource(document),
> >                         new Packages.javax.xml.transform.stream.StreamResult(outputStream));
> >                } else {
> >                        throw new Packages.org.apache.cocoon.ProcessingException("Cannot write to source " + uri);
> >                }
> >        } finally {
> >        if (outputStream != null) {
> >            outputStream.flush();
> >            outputStream.close();
> >        }
> >        }
> >}
> >
> >Best regards,
> >
> >
>
>
> --
>
> Met vriendelijke groet,
>
> Suzan Foster
> Software Engineer
>
> *NEROC'MEDIAWARE *
> De Run 1131
> 5503 LB Veldhoven
> T +31 (0)40 258 66 66
> F +31 (0)40 258 66 77
>
> E suzan.foster@nerocmediaware.nl <ma...@nerocmediaware.nl>
>
> ================================================
> De informatie opgenomen in dit bericht kan vertrouwelijk zijn en
> is uitsluitend bestemd voor de geadresseerde. Indien u dit bericht
> onterecht ontvangt, wordt u verzocht de inhoud niet te gebruiken en
> de afzender direct te informeren door het bericht te retourneren.
> ================================================
> The information contained in this message may be confidential
> and is intended to be exclusively for the addressee. Should you
> receive this message unintentionally, please do not use the contents
> herein and notify the sender immediately by return e-mail.
>
> ---------------------------------------------------------------------
> 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: How to return xml from a flowscript?

Posted by Suzan Foster <su...@nerocmediaware.nl>.
The problem however with using variables is that eXist can only operate 
on it's own persistent DOM implementation within the query. You need to 
create a temporary document to pass to the XQuery. I hit this problem 
when writing an extension function for processing the request as XML 
(sent by an XForm) and wrote the following implementation:

package com.nps.xquery.request;

import org.exist.dom.QName;
import org.exist.http.servlets.RequestWrapper;
import org.exist.xquery.functions.request.RequestModule;
import org.exist.xquery.XQueryContext;
import org.exist.xquery.BasicFunction;
import org.exist.xquery.Cardinality;
import org.exist.xquery.FunctionSignature;
import org.exist.xquery.XPathException;
import org.exist.xquery.Variable;
import org.exist.xquery.value.Sequence;
import org.exist.xquery.value.SequenceType;
import org.exist.xquery.value.Type;
import org.exist.xquery.value.JavaObjectValue;
import org.exist.dom.NodeProxy;
import org.exist.dom.DocumentImpl;
import org.exist.dom.NodeImpl;
import org.exist.memtree.MemTreeBuilder;
import org.exist.memtree.DocumentBuilderReceiver;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

public class DocFunction extends BasicFunction {

    public final static FunctionSignature signature =
        new FunctionSignature(
            new QName("doc", Module.NAMESPACE_URI, Module.PREFIX),
            "",
            null,
            new SequenceType(Type.NODE, Cardinality.ZERO_OR_ONE)
        );
   
    public DocFunction(XQueryContext context) {
        super(context, signature);
    }

    public Sequence eval(Sequence[] arg0, Sequence arg1) throws 
XPathException {
        RequestModule requestModule =
            (RequestModule) context.getModule(RequestModule.NAMESPACE_URI);

        // request object is read from global variable $request
        Variable var = 
requestModule.resolveVariable(RequestModule.REQUEST_VAR);
        if(var == null)
            throw new XPathException("No request object found in the 
current XQuery context.");
        if (var.getValue().getItemType() != Type.JAVA_OBJECT)
            throw new XPathException("Variable $request is not bound to 
a Java object.");
        JavaObjectValue value = (JavaObjectValue) var.getValue().itemAt(0);
        if (value.getObject() instanceof RequestWrapper) {
            RequestWrapper request = (RequestWrapper)value.getObject();
            try {
                MemTreeBuilder builder = context.getDocumentBuilder();
                DocumentBuilderReceiver receiver = new 
DocumentBuilderReceiver(builder);
                SAXParserFactory factory = SAXParserFactory.newInstance();
                factory.setNamespaceAware(true);
                XMLReader reader = factory.newSAXParser().getXMLReader();
                reader.setContentHandler(receiver);
                reader.parse(new InputSource(request.getInputStream()));
                DocumentImpl doc = 
context.storeTemporaryDoc(builder.getDocument());
                NodeImpl node = 
(NodeImpl)doc.getDocumentElement().getFirstChild();
                return new NodeProxy(doc, node.getGID(), 
node.getInternalAddress());
            }
            catch (Exception e) {
                throw new XPathException(getASTNode(), e.getMessage(), e);
            }
        } else
            throw new XPathException("Variable $request is not bound to 
a Request object.");       
    }
}

I think it should be possible to use such a temporary document as 
context for the XQuery using XQueryService.query(XMLResource,  String).

Jean-Baptiste Quenot wrote:

>* Jonas Lundberg:
>
>  
>
>>That might work, but it is  not a good solution for me, although
>>it might work in other cases. My xqueries are very complex.  But
>>this can't end here, can it? Is there really no way of returning
>>XML in a parameter from a flowscript in Cocoon?
>>    
>>
>
>Sitemap parameters  are Strings, not  Objects.  You cannot  pass a
>DOM.  However you can execute an XQuery without the
>XQueryGenerator, Java snippet follows:
>
>        public static Node executeXQuery(Collection collection, Source inputSource, Map variables, Logger logger) throws XMLDBException {
>                XQueryService service = (XQueryService) collection.getService(
>                                "XQueryService", "1.0");
>                service.setProperty(Serializer.GENERATE_DOC_EVENTS, "false");
>                if (variables != null) {
>                        if (logger != null) logger.debug("Executing " + inputSource.getURI() + " with variables " + variables);
>                        Iterator it = variables.entrySet().iterator();
>                        while (it.hasNext()) {
>                                Map.Entry entry = (Map.Entry)it.next();
>                                service.declareVariable((String)entry.getKey(), entry.getValue());
>                        }
>                } else {
>                        if (logger != null) logger.debug("Executing " + inputSource.getURI());
>                }
>                ResourceSet result = service.execute(new CocoonSource(inputSource, true));
>                if (result == null) {
>                        if (logger != null) logger.error("null result after executing XQuery!");
>                        return null; // a DOM Node cannot be null so null can be used safely for signaling errors
>                }
>                if (result.getSize() == 0) {
>                        if (logger != null) logger.error("empty XML result after executing XQuery!");
>                        return null; // a DOM Node cannot be null so null can be used safely for signaling errors
>                }
>                XMLResource resource = (XMLResource) result.getResource(0);
>                return resource.getContentAsDOM();
>        }
>
>But I think the best solution in order to save directly an XML
>document in the XML database is to use the XMLDBSource with
>saveDocumentToSource("xmldb:exist://localhost:8080/exist/xmlrpc/db/file.xml", document),
>FlowScript snippet follows:
>
>/*
> * Save XML document to source.
> * Parameters :
> *   outputMode : the desired output mode : 'html', 'xml', or 'text'
> *                Defaults to "xml"
> */
>function saveDocumentToSource(source, document, outputMode ) {
>        var tf = Packages.javax.xml.transform.TransformerFactory.newInstance();
>        var outputStream = null;
>
>        // If no mode is specified, we use XML
>        if(! outputMode) {
>                outputMode = 'xml';
>        }
>
>        try {
>                if (source instanceof Packages.org.apache.excalibur.source.ModifiableSource
>                        && tf.getFeature(Packages.javax.xml.transform.sax.SAXTransformerFactory.FEATURE)) {
>            outputStream = source.getOutputStream();
>                var transformer = tf.newTransformer();
>            transformer.setOutputProperty(Packages.javax.xml.transform.OutputKeys.INDENT, "yes");
>            transformer.setOutputProperty(Packages.javax.xml.transform.OutputKeys.METHOD, outputMode);
>                transformer.transform(
>                     new Packages.javax.xml.transform.dom.DOMSource(document),
>                         new Packages.javax.xml.transform.stream.StreamResult(outputStream));
>                } else {
>                        throw new Packages.org.apache.cocoon.ProcessingException("Cannot write to source " + uri);
>                }
>        } finally {
>        if (outputStream != null) {
>            outputStream.flush();
>            outputStream.close();
>        }
>        }
>}
>
>Best regards,
>  
>


-- 

Met vriendelijke groet,

Suzan Foster
Software Engineer

*NEROC'MEDIAWARE *
De Run 1131
5503 LB Veldhoven
T +31 (0)40 258 66 66
F +31 (0)40 258 66 77

E suzan.foster@nerocmediaware.nl <ma...@nerocmediaware.nl>

================================================
De informatie opgenomen in dit bericht kan vertrouwelijk zijn en
is uitsluitend bestemd voor de geadresseerde. Indien u dit bericht
onterecht ontvangt, wordt u verzocht de inhoud niet te gebruiken en
de afzender direct te informeren door het bericht te retourneren.
================================================
The information contained in this message may be confidential
and is intended to be exclusively for the addressee. Should you
receive this message unintentionally, please do not use the contents
herein and notify the sender immediately by return e-mail.

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


Re: How to return xml from a flowscript?

Posted by Jean-Baptiste Quenot <jb...@anyware-tech.com>.
* Jonas Lundberg:

> That might work, but it is  not a good solution for me, although
> it might work in other cases. My xqueries are very complex.  But
> this can't end here, can it? Is there really no way of returning
> XML in a parameter from a flowscript in Cocoon?

Sitemap parameters  are Strings, not  Objects.  You cannot  pass a
DOM.  However you can execute an XQuery without the
XQueryGenerator, Java snippet follows:

        public static Node executeXQuery(Collection collection, Source inputSource, Map variables, Logger logger) throws XMLDBException {
                XQueryService service = (XQueryService) collection.getService(
                                "XQueryService", "1.0");
                service.setProperty(Serializer.GENERATE_DOC_EVENTS, "false");
                if (variables != null) {
                        if (logger != null) logger.debug("Executing " + inputSource.getURI() + " with variables " + variables);
                        Iterator it = variables.entrySet().iterator();
                        while (it.hasNext()) {
                                Map.Entry entry = (Map.Entry)it.next();
                                service.declareVariable((String)entry.getKey(), entry.getValue());
                        }
                } else {
                        if (logger != null) logger.debug("Executing " + inputSource.getURI());
                }
                ResourceSet result = service.execute(new CocoonSource(inputSource, true));
                if (result == null) {
                        if (logger != null) logger.error("null result after executing XQuery!");
                        return null; // a DOM Node cannot be null so null can be used safely for signaling errors
                }
                if (result.getSize() == 0) {
                        if (logger != null) logger.error("empty XML result after executing XQuery!");
                        return null; // a DOM Node cannot be null so null can be used safely for signaling errors
                }
                XMLResource resource = (XMLResource) result.getResource(0);
                return resource.getContentAsDOM();
        }

But I think the best solution in order to save directly an XML
document in the XML database is to use the XMLDBSource with
saveDocumentToSource("xmldb:exist://localhost:8080/exist/xmlrpc/db/file.xml", document),
FlowScript snippet follows:

/*
 * Save XML document to source.
 * Parameters :
 *   outputMode : the desired output mode : 'html', 'xml', or 'text'
 *                Defaults to "xml"
 */
function saveDocumentToSource(source, document, outputMode ) {
        var tf = Packages.javax.xml.transform.TransformerFactory.newInstance();
        var outputStream = null;

        // If no mode is specified, we use XML
        if(! outputMode) {
                outputMode = 'xml';
        }

        try {
                if (source instanceof Packages.org.apache.excalibur.source.ModifiableSource
                        && tf.getFeature(Packages.javax.xml.transform.sax.SAXTransformerFactory.FEATURE)) {
            outputStream = source.getOutputStream();
                var transformer = tf.newTransformer();
            transformer.setOutputProperty(Packages.javax.xml.transform.OutputKeys.INDENT, "yes");
            transformer.setOutputProperty(Packages.javax.xml.transform.OutputKeys.METHOD, outputMode);
                transformer.transform(
                     new Packages.javax.xml.transform.dom.DOMSource(document),
                         new Packages.javax.xml.transform.stream.StreamResult(outputStream));
                } else {
                        throw new Packages.org.apache.cocoon.ProcessingException("Cannot write to source " + uri);
                }
        } finally {
        if (outputStream != null) {
            outputStream.flush();
            outputStream.close();
        }
        }
}

Best regards,
-- 
Jean-Baptiste Quenot
Systèmes d'Information
ANYWARE TECHNOLOGIES
Tel : +33 (0)5 61 00 52 90
Fax : +33 (0)5 61 00 51 46
http://www.anyware-tech.com/

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


Re: How to return xml from a flowscript?

Posted by Jonas Lundberg <my...@gmail.com>.
That might work, but it is not a good solution for me, although it
might work in other cases. My xqueries are very complex.
But this can't end here, can it? Is there really no way of returning
XML in a parameter from a flowscript in Cocoon?

Regards,
Hans


On 1/5/06, Geert Josten <Ge...@daidalos.nl> wrote:
> Err, perhaps switch to the XML syntax of the XQuery language? That should be available and I would
> be surprised if it is not supported.
>
> I believe there are converters that can translate one to the other..
>
> Regards,
> Geert
>
> Jonas Lundberg wrote:
>
> > That would work, if I could do it...
> > But xqueries are not written in XML, so how can I then include the XML
> > in a good place in my xquery?
> >
> > :-(
> > Hans
>
> ---------------------------------------------------------------------
> 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: How to return xml from a flowscript?

Posted by Geert Josten <Ge...@daidalos.nl>.
Err, perhaps switch to the XML syntax of the XQuery language? That should be available and I would 
be surprised if it is not supported.

I believe there are converters that can translate one to the other..

Regards,
Geert

Jonas Lundberg wrote:

> That would work, if I could do it...
> But xqueries are not written in XML, so how can I then include the XML
> in a good place in my xquery?
> 
> :-(
> Hans

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


Re: How to return xml from a flowscript?

Posted by Jonas Lundberg <my...@gmail.com>.
That would work, if I could do it...
But xqueries are not written in XML, so how can I then include the XML
in a good place in my xquery?

:-(
Hans

On 1/5/06, Geert Josten <Ge...@daidalos.nl> wrote:
> No, this is not what I meant. You can replace:
>
>         src="xq/discuss.xq"
>
> by:
>
>         src="cocoon:/insert-whtm-in-query/discuss.xq/path-to-whtm"
>
> And create an new pipeline that includes the cleaned whtm into your xquery instruction.
>
> :-)
>
> Jonas Lundberg wrote:
>
> > The xquery stores the XML file in eXist.
> > Thus, if I try to insert the xml file using an Xinclude in discuss.xq,
> > and then use an xinclude transformer after generation,
> > then it will be put there *after* the query is executed. Which is not
> > useful in this case, since the XML is needed during execution.
> >
> > Regards
> > Hans
>
> ---------------------------------------------------------------------
> 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: How to return xml from a flowscript?

Posted by Geert Josten <Ge...@daidalos.nl>.
No, this is not what I meant. You can replace:

	src="xq/discuss.xq"

by:

	src="cocoon:/insert-whtm-in-query/discuss.xq/path-to-whtm"

And create an new pipeline that includes the cleaned whtm into your xquery instruction.

:-)

Jonas Lundberg wrote:

> The xquery stores the XML file in eXist.
> Thus, if I try to insert the xml file using an Xinclude in discuss.xq,
> and then use an xinclude transformer after generation,
> then it will be put there *after* the query is executed. Which is not
> useful in this case, since the XML is needed during execution.
> 
> Regards
> Hans

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


Re: How to return xml from a flowscript?

Posted by Jonas Lundberg <my...@gmail.com>.
The xquery stores the XML file in eXist.
Thus, if I try to insert the xml file using an Xinclude in discuss.xq,
and then use an xinclude transformer after generation,
then it will be put there *after* the query is executed. Which is not
useful in this case, since the XML is needed during execution.

Regards
Hans

On 1/5/06, Geert Josten <Ge...@daidalos.nl> wrote:
> > Thanks for the replies.
> > The problem is that I then have use the xml in a pipeline that starts
> > with an xquery generator (this is the pipeline I call from the
> > flowscript):
> >      <map:generate src="xq/discuss.xq" type="xquery">
> >      <map:parameter name="cleanedxml" value="{flow-attribute:cleanxml}"/>
>
> How is the xquery using the xml in the parameter? Is it an idea to have another pipe that
> *generates* the discuss.xq which has your cleanedxml fragment included somewhere?
>
> Cheers,
> Geert
>
> ---------------------------------------------------------------------
> 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: How to return xml from a flowscript?

Posted by Geert Josten <Ge...@daidalos.nl>.
> Thanks for the replies.
> The problem is that I then have use the xml in a pipeline that starts
> with an xquery generator (this is the pipeline I call from the
> flowscript):
>      <map:generate src="xq/discuss.xq" type="xquery">
>      <map:parameter name="cleanedxml" value="{flow-attribute:cleanxml}"/>

How is the xquery using the xml in the parameter? Is it an idea to have another pipe that 
*generates* the discuss.xq which has your cleanedxml fragment included somewhere?

Cheers,
Geert

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


Re: How to return xml from a flowscript?

Posted by Jonas Lundberg <my...@gmail.com>.
Thanks for the replies.
The problem is that I then have use the xml in a pipeline that starts
with an xquery generator (this is the pipeline I call from the
flowscript):
     <map:generate src="xq/discuss.xq" type="xquery">
     <map:parameter name="cleanedxml" value="{flow-attribute:cleanxml}"/>

Therefore, I have to send the xml as a parameter. I could not figure
out how to do that with the stream generator either. Maybe that is
easy?

The process starts with a matcher that calls the flowscript:
                        <map:match pattern="store_filesystem_file">
                                <map:call function="cleanWordHtml">
                                       <map:parameter name="base"
value="{request-param:base}"/>
                                       <map:parameter name="page"
value="{request-param:url}"/>

                                </map:call>
                        </map:match>

(I would *also* like to read wordml, but I'll deal with that later)

Regards
Hans

On 1/5/06, Geert Josten <Ge...@daidalos.nl> wrote:
> > It reads a Cocoon pipeline, which converts a Word HTML file to XML.
> > Then it sends this as a string in a parameter, to a pipeline that
> > processes it further.
>
> Why not read WordML? :)
>
> > What I *would like* to do, is to send XML in the parameter, instead of a string.
> > Is this possible? Does anyone have a solution for me?
>
> Isn't it easier to change your upload pipe to read an internal pipe directly?
>
> In short something like:
>
> <pattern="cleanwhtm/**">
>         read and clean html using base and page parameters
>
> <pattern="upload/**">
>         <generate src="cocoon:/{1}">
>         <!-- set upload header flags and serialize -->
>
> flowscript:
>         sendPage("upload/cleanwhtm" + base + page);
>
>
> HTH,
> Geert
>
> ---------------------------------------------------------------------
> 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: How to return xml from a flowscript?

Posted by Geert Josten <Ge...@daidalos.nl>.
> It reads a Cocoon pipeline, which converts a Word HTML file to XML.
> Then it sends this as a string in a parameter, to a pipeline that
> processes it further.

Why not read WordML? :)

> What I *would like* to do, is to send XML in the parameter, instead of a string.
> Is this possible? Does anyone have a solution for me?

Isn't it easier to change your upload pipe to read an internal pipe directly?

In short something like:

<pattern="cleanwhtm/**">
	read and clean html using base and page parameters

<pattern="upload/**">
	<generate src="cocoon:/{1}">
	<!-- set upload header flags and serialize -->

flowscript:
	sendPage("upload/cleanwhtm" + base + page);


HTH,
Geert

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