You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Jean-Claude Vogel <vo...@gmail.com> on 2007/11/16 11:53:25 UTC

Custom DOM transformer

Hello guys,

I would like to write my own dom transformer. So I simply wrote the
following in order to get firstly an identity transformer :

public class TestTransformer extends AbstractDOMTransformer {
        private Request request;

        public void setup(SourceResolver resolver, Map objectModel,
                          String src, Parameters par)
                          throws ProcessingException, SAXException,
IOException {
            super.setup(resolver, objectModel, src, par);
        }

        public Document transform(Document document) {
            //  When I log here I see that "document" contains what I want
it to contain
            return document;
        }
}


To test I send an HTTP post request with an XML in its body, but I my HTTP
request response body is desesperatly NULL.

I tried to log what happens in the transform method and I can see in my XML
body sent by HTTP. The post response is ever empty.

What's wrong in my code please ?

Re: Custom DOM transformer

Posted by Jean-Claude Vogel <vo...@gmail.com>.
Hello,

Thanks a lot for your answers which helped me to understand my problem. It
was finally not a Cocoon usage problem like I believed too long, I had a
"releaseConnection" on my PostMethod before logging the resulting body on
the client side. So the http response body was ever null.

In fact, the simple following sitemap code works fine (to get the post body
and send it again) :
        <map:pipeline>
            <map:match pattern="tentative">
                <map:generate type="stream">
                    <map:parameter name="defaultContentType"
value="text/xml" />
                </map:generate>
                <map:serialize type="xml"/>
            </map:match>
        </map:pipeline>

Thank again,
Jean-Claude

2007/11/19, solprovider@apache.org <so...@apache.org>:
>
> You have proven the problem is not in the custom DOMTransformer.
>
> Warrell suggests using the Request Generator.
>
> I generate form data using the Server Pages Generator with an XSP. I
> wrote this when I was very new to Cocoon.  It works, but I would be
> very interested in a debate about the best algorithm for reading POST
> data.
>
> http://svn.apache.org/repos/asf/lenya/branches/revolution/1.3.x/src/webapp/lenya/modules/form/post.xsp
> Instructions:
> http://svn.apache.org/repos/asf/lenya/branches/revolution/1.3.x/src/webapp/lenya/modules/form/module.xml
>
> If you need more assistance, start a new thread with a more
> appropriate subject to interest non-Java-writing ML readers scared  by
> the current title.
>
> solprovider
>
> On 11/19/07, Jean-Claude Vogel <vo...@gmail.com> wrote:
> > Thank you, effectively it was just a mistake during my drag and drop to
> > write my email.
> >
> > The true code is :
> >         <map:pipeline>
> >             <map:match pattern="tentative">
> >                 <map:generate type="stream">
> >                     <map:parameter name="defaultContentType"
> > value="text/xml"/>
> >                 </map:generate>
> >                 <map:transform type="testTransformer" />
> >                 <map:serialize type="xml"/>
> >             </map:match>
> >         </map:pipeline>
> >
> > My sitemap contains other pipelines which I don't show here, that's why
> I
> > have a matcher.
> >
> > I tried to remove the transformer to keep just :
> >         <map:pipeline>
> >             <map:match pattern="tentative">
> >                 <map:generate type="stream">
> >                     <map:parameter name="defaultContentType"
> > value="text/xml"/>
> >                 </map:generate>
> >                 <map:serialize type="xml"/>
> >             </map:match>
> >         </map:pipeline>
> >
> > Effectively, I have the same problem : no XML stream in the http
> response.
> > Like said before, I am sure that the body of my incoming HTTP request
> > contains an XML stream (when I let the "testTransformer" in the pipe I
> can
> > log the incoming XML stream).
> >
> > I am not able to understand where is my mistake and why the HTTP
> response
> > body stays empty.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
> For additional commands, e-mail: users-help@cocoon.apache.org
>
>

Re: Custom DOM transformer

Posted by so...@apache.org.
You have proven the problem is not in the custom DOMTransformer.

Warrell suggests using the Request Generator.

I generate form data using the Server Pages Generator with an XSP. I
wrote this when I was very new to Cocoon.  It works, but I would be
very interested in a debate about the best algorithm for reading POST
data.
http://svn.apache.org/repos/asf/lenya/branches/revolution/1.3.x/src/webapp/lenya/modules/form/post.xsp
Instructions: http://svn.apache.org/repos/asf/lenya/branches/revolution/1.3.x/src/webapp/lenya/modules/form/module.xml

If you need more assistance, start a new thread with a more
appropriate subject to interest non-Java-writing ML readers scared  by
the current title.

solprovider

On 11/19/07, Jean-Claude Vogel <vo...@gmail.com> wrote:
> Thank you, effectively it was just a mistake during my drag and drop to
> write my email.
>
> The true code is :
>         <map:pipeline>
>             <map:match pattern="tentative">
>                 <map:generate type="stream">
>                     <map:parameter name="defaultContentType"
> value="text/xml"/>
>                 </map:generate>
>                 <map:transform type="testTransformer" />
>                 <map:serialize type="xml"/>
>             </map:match>
>         </map:pipeline>
>
> My sitemap contains other pipelines which I don't show here, that's why I
> have a matcher.
>
> I tried to remove the transformer to keep just :
>         <map:pipeline>
>             <map:match pattern="tentative">
>                 <map:generate type="stream">
>                     <map:parameter name="defaultContentType"
> value="text/xml"/>
>                 </map:generate>
>                 <map:serialize type="xml"/>
>             </map:match>
>         </map:pipeline>
>
> Effectively, I have the same problem : no XML stream in the http response.
> Like said before, I am sure that the body of my incoming HTTP request
> contains an XML stream (when I let the "testTransformer" in the pipe I can
> log the incoming XML stream).
>
> I am not able to understand where is my mistake and why the HTTP response
> body stays empty.

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


Re: Custom DOM transformer

Posted by Jean-Claude Vogel <vo...@gmail.com>.
Thank you, effectively it was just a mistake during my drag and drop to
write my email.

The true code is :
        <map:pipeline>
            <map:match pattern="tentative">
                <map:generate type="stream">
                    <map:parameter name="defaultContentType"
value="text/xml"/>
                </map:generate>
                <map:transform type="testTransformer" />
                <map:serialize type="xml"/>
            </map:match>
        </map:pipeline>

My sitemap contains other pipelines which I don't show here, that's why I
have a matcher.

I tried to remove the transformer to keep just :
        <map:pipeline>
            <map:match pattern="tentative">
                <map:generate type="stream">
                    <map:parameter name="defaultContentType"
value="text/xml"/>
                </map:generate>
                <map:serialize type="xml"/>
            </map:match>
        </map:pipeline>

Effectively, I have the same problem : no XML stream in the http response.
Like said before, I am sure that the body of my incoming HTTP request
contains an XML stream (when I let the "testTransformer" in the pipe I can
log the incoming XML stream).

I am not able to understand where is my mistake and why the HTTP response
body stays empty.

2007/11/16, solprovider@apache.org < solprovider@apache.org>:
>
> The two examples are functionally equivalent.   Both pipelines would
> error without the pattern="tentative" match: the former because no
> pipeline match is found and the latter because no Serializer is
> called.  The map:match element is irrelevant because the match must be
> assumed and no numbered parameters are being set.
>
> This would be fine as the "concerned extract":
>         <map:pipeline>
>             <map:generate type="stream">
>                 <map:parameter name="defaultContentType"
> value="text/xml"/>
>             </map:generate>
>             <map:transform type="testTransformer" />
>             <map:serialize type="xml"/>
>         </map:pipeline>
>
> We should assume the match is for code outside the current concern and
> remained because the OP forgot to remove it as irrelevant.  The
> "mistake" was providing too much code.
>
> solprovider
>
>
> On 11/16/07, warrell harries < warrell.harries@googlemail.com> wrote:
> > Surely some mistake (probably a typo) but your pipeline should be :-
> > <map:pipeline>
> > <map:match pattern="tentative">
> >    <map:generate type="stream">
> >        <map:parameter name="defaultContentType" value="text/xml"/>
> >      </map:generate>
> >     <map:transform type="testTransformer" />
> >     <map:serialize type="xml"/>
> >  </map:match>
> > </map:pipeline>
> >
> > On 16/11/2007, Jean-Claude Vogel <vogel.jeanclaude@gmail.com > wrote:
> > > Here is the concerned extract of my sitemap :
> > >         <map:pipeline>
> > >             <map:generate type="stream">
> > >                 <map:parameter name="defaultContentType"
> > value="text/xml"/>
> > >             </map:generate>
> > >             <map:match pattern="tentative">
> > >                 <map:transform type="testTransformer" />
> > >                 <map:serialize type="xml"/>
> > >             </map:match>
> > >         </map:pipeline>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
> For additional commands, e-mail: users-help@cocoon.apache.org
>
>

Re: Custom DOM transformer

Posted by so...@apache.org.
The two examples are functionally equivalent.   Both pipelines would
error without the pattern="tentative" match: the former because no
pipeline match is found and the latter because no Serializer is
called.  The map:match element is irrelevant because the match must be
assumed and no numbered parameters are being set.

This would be fine as the "concerned extract":
        <map:pipeline>
            <map:generate type="stream">
                <map:parameter name="defaultContentType" value="text/xml"/>
            </map:generate>
            <map:transform type="testTransformer" />
            <map:serialize type="xml"/>
        </map:pipeline>

We should assume the match is for code outside the current concern and
remained because the OP forgot to remove it as irrelevant.  The
"mistake" was providing too much code.

solprovider


On 11/16/07, warrell harries <wa...@googlemail.com> wrote:
> Surely some mistake (probably a typo) but your pipeline should be :-
> <map:pipeline>
> <map:match pattern="tentative">
>    <map:generate type="stream">
>        <map:parameter name="defaultContentType" value="text/xml"/>
>      </map:generate>
>     <map:transform type="testTransformer" />
>     <map:serialize type="xml"/>
>  </map:match>
> </map:pipeline>
>
> On 16/11/2007, Jean-Claude Vogel <vogel.jeanclaude@gmail.com > wrote:
> > Here is the concerned extract of my sitemap :
> >         <map:pipeline>
> >             <map:generate type="stream">
> >                 <map:parameter name="defaultContentType"
> value="text/xml"/>
> >             </map:generate>
> >             <map:match pattern="tentative">
> >                 <map:transform type="testTransformer" />
> >                 <map:serialize type="xml"/>
> >             </map:match>
> >         </map:pipeline>

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


Re: Custom DOM transformer

Posted by warrell harries <wa...@googlemail.com>.
Surely some mistake (probably a typo) but your pipeline should be :-

<map:pipeline>

<map:match pattern="tentative">
   <map:generate type="stream">
      <map:parameter name="defaultContentType" value="text/xml"/>
    </map:generate>
    <map:transform type="testTransformer" />
    <map:serialize type="xml"/>
 </map:match>

</map:pipeline>

On 16/11/2007, Jean-Claude Vogel <vo...@gmail.com> wrote:
>
> Here is the concerned extract of my sitemap :
>         <map:pipeline>
>             <map:generate type="stream">
>                 <map:parameter name="defaultContentType"
> value="text/xml"/>
>             </map:generate>
>             <map:match pattern="tentative">
>                 <map:transform type="testTransformer" />
>                 <map:serialize type="xml"/>
>             </map:match>
>         </map:pipeline>
>
>
>

Re: Custom DOM transformer

Posted by so...@apache.org.
Remove the <map:transform type="testTransformer" /> line to verify the
problem is in the transformer.  Make certain everything worked without
the transformer, then add the line and see if the problem recurs.

solprovider

On 11/16/07, Jean-Claude Vogel <vo...@gmail.com> wrote:
> Here is the concerned extract of my sitemap :
>         <map:pipeline>
>             <map:generate type="stream">
>                 <map:parameter name="defaultContentType" value="text/xml"/>
>             </map:generate>
>             <map:match pattern="tentative">
>                 <map:transform type="testTransformer" />
>                 <map:serialize type="xml"/>
>             </map:match>
>         </map:pipeline>
>
> Here, a part of the applicationContext.xml (Spring declaration of my
> transformer) :
>         <bean name="testService"
>         class="
> org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
>         <property name="transactionManager">
>             <ref bean="maTransactionManager" />
>         </property>
>         <property name="target">
>             <bean class="xxxx.TestService">
>                 <property name="maxml2ObjectsService">
>                     <ref bean="maxml2ObjectsService" />
>                 </property>
>                 <property name="serviceProgramsService">
>                     <ref bean="serviceProgramsService" />
>                 </property>
>             </bean>
>         </property>
>         <property name="transactionAttributes">
>             <props>
>                 <prop
> key="test">PROPAGATION_REQUIRED</prop>
>             </props>
>         </property>
>     </bean>
>
>     <bean id="testTransformer"
>
> name="org.apache.cocoon.transformation.Transformer/testTransformer"
>         scope="prototype"
>         class="xxxx.TestTransformer">
>         <property name="testService">
>             <ref bean="testService"/>
>         </property>
>     </bean>
>
> What I had in my transformer to log the XML stream (wich is effectively
> received and logged by the transformer) :
>         public Document transform(Document document) {
>
>             try {
>                 DOMSource domSource = new DOMSource(document);
>                 StringWriter writer = new StringWriter();
>                 StreamResult result = new StreamResult(writer);
>                 TransformerFactory tf = TransformerFactory.newInstance();
>                 Transformer transformer = tf.newTransformer();
>                 transformer.transform(domSource, result);
>                 log.debug("TRANSFORMATION:" + writer.toString ());
>             }
>             catch (TransformerConfigurationException e) {
>                 e.printStackTrace();
>             }
>             catch (TransformerException e) {
>                 e.printStackTrace ();
>             }
>
>             return document;
>         }
>
> I use HttpClient to send the post request containing the XML stream and to
> get the XML stream received (unhappily NULL) :
>         HttpClient client = new HttpClient();
>
>         StringBuffer newUrl = new StringBuffer();
>         newUrl.append(url);
>
>         PostMethod method = new PostMethod(newUrl.toString());
>         method.setRequestBody(new FileInputStream(xmlFile));
>         String xml = post.getResponseBodyAsString();
>         System.out.println("Exported virtual XML : " + maxml); // Here the
> response is NULL
>
>  Thank for your help
>
> 2007/11/16, warrell harries <wa...@googlemail.com>:
> > Hi Jean-Claude,
> >
> > What generator are you using in your pipeline match? Can you show us the
> sitemap? You would need to use the Request generator or another one that
> 'loads' the pipeline with the contents of the HTTP Request.
> >
> > Cheers
> >
> >
> >
> > On 16/11/2007, Jean-Claude Vogel < vogel.jeanclaude@gmail.com> wrote:
> > > Hello guys,
> > >
> > > I would like to write my own dom transformer. So I simply wrote the
> following in order to get firstly an identity transformer :
> > >
> > > public class TestTransformer extends AbstractDOMTransformer {
> > >         private Request request;
> > >
> > >         public void setup(SourceResolver resolver, Map objectModel,
> > >                           String src, Parameters par)
> > >                           throws ProcessingException, SAXException,
> IOException {
> > >             super.setup(resolver, objectModel, src, par);
> > >         }
> > >
> > >         public Document transform(Document document) {
> > >             //  When I log here I see that "document" contains what I
> want it to contain
> > >             return document;
> > >         }
> > > }
> > >
> > >
> > > To test I send an HTTP post request with an XML in its body, but I my
> HTTP request response body is desesperatly NULL.
> > >
> > > I tried to log what happens in the transform method and I can see in my
> XML body sent by HTTP. The post response is ever empty.
> > >
> > > What's wrong in my code please ?

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


Re: Custom DOM transformer

Posted by Jean-Claude Vogel <vo...@gmail.com>.
Here is the concerned extract of my sitemap :
        <map:pipeline>
            <map:generate type="stream">
                <map:parameter name="defaultContentType" value="text/xml"/>
            </map:generate>
            <map:match pattern="tentative">
                <map:transform type="testTransformer" />
                <map:serialize type="xml"/>
            </map:match>
        </map:pipeline>

Here, a part of the applicationContext.xml (Spring declaration of my
transformer) :
        <bean name="testService"
        class="
org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="transactionManager">
            <ref bean="maTransactionManager" />
        </property>
        <property name="target">
            <bean class="xxxx.TestService">
                <property name="maxml2ObjectsService">
                    <ref bean="maxml2ObjectsService" />
                </property>
                <property name="serviceProgramsService">
                    <ref bean="serviceProgramsService" />
                </property>
            </bean>
        </property>
        <property name="transactionAttributes">
            <props>
                <prop key="test">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
    </bean>

    <bean id="testTransformer"
        name="org.apache.cocoon.transformation.Transformer/testTransformer"
        scope="prototype"
        class="xxxx.TestTransformer">
        <property name="testService">
            <ref bean="testService"/>
        </property>
    </bean>

What I had in my transformer to log the XML stream (wich is effectively
received and logged by the transformer) :
        public Document transform(Document document) {

            try {
                DOMSource domSource = new DOMSource(document);
                StringWriter writer = new StringWriter();
                StreamResult result = new StreamResult(writer);
                TransformerFactory tf = TransformerFactory.newInstance();
                Transformer transformer = tf.newTransformer();
                transformer.transform(domSource, result);
                log.debug("TRANSFORMATION:" + writer.toString());
            }
            catch (TransformerConfigurationException e) {
                e.printStackTrace();
            }
            catch (TransformerException e) {
                e.printStackTrace();
            }

            return document;
        }

I use HttpClient to send the post request containing the XML stream and to
get the XML stream received (unhappily NULL) :
        HttpClient client = new HttpClient();

        StringBuffer newUrl = new StringBuffer();
        newUrl.append(url);

        PostMethod method = new PostMethod(newUrl.toString());
        method.setRequestBody(new FileInputStream(xmlFile));
        String xml = post.getResponseBodyAsString();
        System.out.println("Exported virtual XML : " + maxml); // Here the
response is NULL

Thank for your help

2007/11/16, warrell harries <wa...@googlemail.com>:
>
> Hi Jean-Claude,
>
> What generator are you using in your pipeline match? Can you show us the
> sitemap? You would need to use the Request generator or another one that
> 'loads' the pipeline with the contents of the HTTP Request.
>
> Cheers
>
> On 16/11/2007, Jean-Claude Vogel <vo...@gmail.com> wrote:
> >
> > Hello guys,
> >
> > I would like to write my own dom transformer. So I simply wrote the
> > following in order to get firstly an identity transformer :
> >
> > public class TestTransformer extends AbstractDOMTransformer {
> >         private Request request;
> >
> >         public void setup(SourceResolver resolver, Map objectModel,
> >                           String src, Parameters par)
> >                           throws ProcessingException, SAXException,
> > IOException {
> >             super.setup(resolver, objectModel, src, par);
> >         }
> >
> >         public Document transform(Document document) {
> >             //  When I log here I see that "document" contains what I
> > want it to contain
> >             return document;
> >         }
> > }
> >
> >
> > To test I send an HTTP post request with an XML in its body, but I my
> > HTTP request response body is desesperatly NULL.
> >
> > I tried to log what happens in the transform method and I can see in my
> > XML body sent by HTTP. The post response is ever empty.
> >
> > What's wrong in my code please ?
> >
>
>

Re: Custom DOM transformer

Posted by warrell harries <wa...@googlemail.com>.
Hi Jean-Claude,

What generator are you using in your pipeline match? Can you show us the
sitemap? You would need to use the Request generator or another one that
'loads' the pipeline with the contents of the HTTP Request.

Cheers

On 16/11/2007, Jean-Claude Vogel <vo...@gmail.com> wrote:
>
> Hello guys,
>
> I would like to write my own dom transformer. So I simply wrote the
> following in order to get firstly an identity transformer :
>
> public class TestTransformer extends AbstractDOMTransformer {
>         private Request request;
>
>         public void setup(SourceResolver resolver, Map objectModel,
>                           String src, Parameters par)
>                           throws ProcessingException, SAXException,
> IOException {
>             super.setup(resolver, objectModel, src, par);
>         }
>
>         public Document transform(Document document) {
>             //  When I log here I see that "document" contains what I want
> it to contain
>             return document;
>         }
> }
>
>
> To test I send an HTTP post request with an XML in its body, but I my HTTP
> request response body is desesperatly NULL.
>
> I tried to log what happens in the transform method and I can see in my
> XML body sent by HTTP. The post response is ever empty.
>
> What's wrong in my code please ?
>