cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Walter Gamba <wal...@e-gramma.net>
Subject Re: how to call a servlet from a generator, passing parameters with POST! (2)
Date Mon, 18 Mar 2002 18:43:20 GMT
Hi to all..
This is a repost of a  previously unansweered mail :) if somebody could 
help I'll be eternally grateful :)

Summary:
I've written a Generator that calls a servlet object directly without 
passing again via HTTP/tomcat (i.e. it istantiate the servlet object if not 
already created, then calls a "service-like" method that returns an XML 
(wrapped ina DOM's Document). Unfortunately i have:
1) problems with namespace.. hope to solve them or to be more precise next 
time (XSL is not applied when I have a namespace prefixed to node names... 
weird because if I write a staitc XML file that has exactly the same output 
returned by the servlet, it works..)

2) serious problem with ContentHandler:  Generator.ContentHandler is NULL 
one invokation, not null the following, NULL... not null, null not null 
alternatively... why?? why???

Thanx a lot

Walter

---start repost---

Thanx 4 all the help ..

unfortunately I can't install Tomcat4 so I'll stick with this soultion

>
>>Hello!
>>
>>SUMMARY: How to use a servlet as a generator in Cocoon, but allowing 
>>forms data to be passed via POST method (And NOT GET!!)
>>----------------------------------------------
>>
>>
>>I can obtain correct XML output, provided I am happy  with the raw XML 
>>and i don't translate it .. (and a side question could be.. how can I 
>>make the transformation from DOM to SAX efficiently, using some 
>>xml.apache.org library..  without serializing the DOM on a Stream and 
>>parsing it back?? THis brute force approach is the one I use now... but 
>>this is another question.. i suppose.. )
>
>
>Cocoon provides helpers to deal with DOM2SAX and SAX2DOM : 
>org.apache.cocoon.xml.dom.DOMStreamer in your case. Look at Javadocs if 
>this matches your needs.

Il'' see it in a while when I've resolved every other issue.. .. see below..


>Concerning your NPE, I already had the same : my mistake was to have sent 
>two contentHandler.startDocument().
>And I think you may do so here according to your code : the first is 
>explicitly written, and the second one may be implicit within the 
>parser.parse().

Thanx! That was it!!!!!!!!!!.. now i can have the output without errors. 
(erll.. sometimes).. It is still not parseed correctly by the XSL but I'll 
manage.. it seem to be a problem with namespace only..  But I have another 
nasty problem:

every other invokation of the servlet I get an exception because the 
super.contentHandler of my generator is NULL. If i relaod it is not null, 
if I reload again it is null (i've checked it by logging..) ..
Any idea?? I've found a vague refernce to this, stating that
"The content handler is NULL when trying to process
the SAX Events through the AbstractDOMTransformer."
but I can't see why it works every other invokation. THe generator is 
recreated each time, I gather?

Thanks

Walter






>>THe Problem arises when I add a Trasformer:
>>
>>         <map:match pattern="prova/test.html">
>>                 <map:generate  type="testgen" >
>>                         <map:parameter name="servlet-class" 
>> value="TestServlet"/>
>>                 </map:generate>
>>                 <map:transform src="stylesheets/prova/TestServlet.xsl"/>
>>                 <map:serialize type="html"/>
>>         </map:match>
>>
>>I've made the xsl such that it works fine with our testServlet and the 
>>requestGenerator:
>>...
>>       <map:generate  type="request" />
>>      <map:transform src="stylesheets/prova/TestServlet.xsl"/>
>>
>>With the request generator it works fine and I get neatly formatted HTML, 
>>with our servlet, that apparently generates the same kind of XMLm it 
>>breaks down..
>>
>>Or, to be correct, it does not match any tags. The XSL is read because in 
>>the browser the basic HTML tags and title of the page (as specified inthe 
>>top level XSL "/" template-mathcing rule) are displayed, but any further 
>>XML tag is ignored, and moreover cocoon complains:
>>
>>FATAL_E (2002-03-13) 09:54.57:446   [core.xslt-processor] 
>>(/cocoon/prova/test.html) Thread-20/TraxErrorHandler: Error in 
>>TraxTransformer: javax.xml.transform.TransformerException: 
>>java.lang.NullPointerException
>>javax.xml.transform.TransformerException: java.lang.NullPointerException
>>         at 
>> org.apache.xalan.transformer.TransformerImpl.transformNode(TransformerImpl.java:1230)

>>
>>         at 
>> org.apache.xalan.transformer.TransformerImpl.run(TransformerImpl.java:3139)
>>         at java.lang.Thread.run(Thread.java:484)
>>---------
>>java.lang.NullPointerException
>>         at 
>> org.apache.xalan.transformer.TransformerImpl.applyTemplateToNode(TransformerImpl.java:1887)

>>
>>         at 
>> org.apache.xalan.transformer.TransformerImpl.transformNode(TransformerImpl.java:1175)

>>
>>         at 
>> org.apache.xalan.transformer.TransformerImpl.run(TransformerImpl.java:3139)
>>         at java.lang.Thread.run(Thread.java:484)
>>
>>
>>THat's it. I enclose the bit of code of the Generator that translated a 
>>DOM to SAX events.. perhaps there is something here that works with raw 
>>XML but breaks down when SAX events are sent down the pipeline..
>>
>>Any help would be appreciated!
>>Thanx
>>
>>Walter Gamba
>>
>>-----------------------
>>public synchronized void outputXML(String NSURI, Document doc)
>>{
>>         try{
>>                 super.contentHandler.startDocument();
>>                 //super.contentHandler.startPrefixMapping("",NSURI);
>>                 transformDOM2SAX(super.contentHandler,doc);
>>                 //super.contentHandler.endPrefixMapping("");
>>                 super.contentHandler.endDocument();
>>         }catch(SAXException s){
>>                 log.error("Errore SAX!:"+s);
>>         }
>>}
>>
>>private static /* synchronized */ void transformDOM2SAX(ContentHandler 
>>cHandler, Document doc) {
>>  //brute force way
>>try {
>>         //1) open stream
>>         CharArrayWriter writer = new CharArrayWriter();
>>
>>         printDocument(writer, doc);
>>
>>         //2) create SAX parserthat can read from stream
>>         SAXParserFactory spf = SAXParserFactory.newInstance();
>>         SAXParser parser = spf.newSAXParser();
>>
>>         //3) build a reader and InputSource
>>         Reader reader = new CharArrayReader(writer.toCharArray());
>>         InputSource source = new InputSource(reader);
>>
>>         log.debug("TestGenerator::transformDOM2SAX -> parse");
>>         ServletDefaultHandler sdh=new ServletDefaultHandler(cHandler);
>>         try{
>>           parser.parse(source, sdh);
>>         }
>>         catch(Exception npe){
>>                 log.error("Errore in PARSE:"+npe,npe);
>>         }
>>         //writer.close();
>>         log.debug("TestGenerator::transformDOM2SAX -> end parse");
>>
>>}
>>catch (IOException ioe) {
>>         log.error("Errore in creazione reader/writer oppure in source:" 
>> + ioe, ioe);
>>}
>>catch (ParserConfigurationException pfe) {
>>         log.error("Errore parseconfig!:" + pfe, pfe);
>>}
>>catch (SAXException sax) {
>>         log.error("Errore sax!:" + sax, sax);
>>}
>>
>>
>>Walter Gamba
>>walter@e-gramma.it
>>E-Gramma srl
>>via Palazzo di Citta' 8 - 10122 TORINO
>>tel 011-5136583 fax 011-5136582
>>www.e-gramma.it
>>
>>
>>---------------------------------------------------------------------
>>Please check that your question has not already been answered in the
>>FAQ before posting. <http://xml.apache.org/cocoon/faqs.html>
>>
>>To unsubscribe, e-mail: <cocoon-users-unsubscribe@xml.apache.org>
>>For additional commands, e-mail: <cocoon-users-help@xml.apache.org>
>
>
>
>---------------------------------------------------------------------
>Please check that your question has not already been answered in the
>FAQ before posting. <http://xml.apache.org/cocoon/faqs.html>
>
>To unsubscribe, e-mail: <cocoon-users-unsubscribe@xml.apache.org>
>For additional commands, e-mail: <cocoon-users-help@xml.apache.org>

Walter Gamba
walter@e-gramma.it
E-Gramma srl
via Palazzo di Citta' 8 - 10122 TORINO
tel 011-5136583 fax 011-5136582
www.e-gramma.it


---------------------------------------------------------------------
Please check that your question has not already been answered in the
FAQ before posting. <http://xml.apache.org/cocoon/faqs.html>

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


Mime
View raw message