星期五, 二月 20, 2004

pipe PrintWriter data to StreamSouce for XSLT transformation

é??é??:
????????¨XSLT???Printwrite???é??????????°?????¤??????é??(pipe)

Java Technology Forums

Hello All!

For 2 days I'm trying to pipe PrintWriter data to a StreamSource for XSLT transformation.

I got a PrintWriter variable named 'out' that, according to a incoming parameter, is a HttpResponse Writer or a PrintWriter to a PipedWriter the code looks like this:


public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {PrintWriter out = null;boolean transform = false;java.io.PipedWriter xmlOutput = null;java.io.PipedReader transformInput = null;StreamSource original = new StreamSource(); if (req.getParameter("format") != null && req.getParameter("format").equals("text/html")) { transform = true; // pipe output of printwriter to streamsource input xmlOutput = new PipedWriter(); transformInput = new PipedReader(xmlOutput); out = new PrintWriter(xmlOutput); original.setReader(transformInput);} else { out = res.getWriter();}try { out.println(""); .... // here xml document data is appended out.println(""); } catch (Exception e) { e.printStackTrace();} if (transform) { System.out.println("transform starting"); // set the result of the transformation to the response StreamResult result = new StreamResult(res.getWriter()); StreamSource stylesheet = new StreamSource("z:/temp/transform.xsl"); System.out.println("document streams initialized"); try { TransformerFactory factory = TransformerFactory.newInstance(); Transformer formatter = factory.newTransformer(stylesheet); System.out.println("formatter created"); formatter.transform(original, result); System.out.println("transform ready"); } catch (Exception e) { e.printStackTrace(); } } // end transform}



When I run this example (a servlet) there is no response at all when I set the parameter format=text/html, neither a stacktrace is printed.

To the console every System.out except for 'transform ready'.

Can someone tell me what I need to do to make it work, or what is wrong with the code above??

Daniel Portier...





!!!Re: How to pipe PrintWriter data to StreamSouce for XSLT transformation?
Author: flairn
In Reply To: How to pipe PrintWriter data to StreamSouce for XSLT transformation? May 24, 2002 8:46 AM

Reply 1 of 5


I'd like to know how to do this also.

Or, otherwise, I'd like to find out how can I convert "outxml" below - containing the XML document - into a StreamSource object to pass to another servlet for "transformation? ( I do not want to create file.)



ServletOutputStream outxml = null;
resp.setContentType("text/xml");
outxml = resp.getOutputStream();
try
{
requ.marshal(outxml);
}
catch (Exception ex)
{
System.out.println("Exception encountered2: " + ex.getMessage());
ex.printStackTrace();
}
finally
{
outxml.close();
}




Re: !!!Re: How to pipe PrintWriter data to StreamSouce for XSLT transformation?
Author: DrClap
In Reply To: !!!Re: How to pipe PrintWriter data to StreamSouce for XSLT transformation? May 24, 2002 10:23 AM

Reply 2 of 5


Only one question: Why?


Re: !!!Re: How to pipe PrintWriter data to StreamSouce for XSLT transformation?
Author: flairn
In Reply To: Re: !!!Re: How to pipe PrintWriter data to StreamSouce for XSLT transformation? May 24, 2002 12:40 PM

Reply 3 of 5


I want to pass the XML in string form to another servlet to process (transform, etc).

This routine will occur 100's of times per day - i.e., invoked by user entry in a web application.

I would prefer to pass a session object (for example., a String containing the XML) to the other servlet, rather than create a temp file for the other servlet to read (- where I have to be concerned with deleting the file after use, etc).

Thanks for any help!


Re: !!!Re: How to pipe PrintWriter data to StreamSouce for XSLT transformation?
Author: DrClap
In Reply To: Re: !!!Re: How to pipe PrintWriter data to StreamSouce for XSLT transformation? May 24, 2002 1:39 PM

Reply 4 of 5


> I would prefer to pass a session object (for example.,
> a String containing the XML) to the other servlet,
> rather than create a temp file for the other servlet
> to read (- where I have to be concerned with deleting
> the file after use, etc).
>
> Thanks for any help!

Well of course that would be preferable. So why are you writing it to the servlet response and then trying to claw it back? Have your "requ.marshal" call output the XML to e.g. a StringWriter, then just extract the String from that and use it.


Re: !!!Re: How to pipe PrintWriter data to StreamSouce for XSLT transformation?
Author: flairn
In Reply To: Re: !!!Re: How to pipe PrintWriter data to StreamSouce for XSLT transformation? May 24, 2002 2:45 PM

Reply 5 of 5


Thanks for setting me straight Doc!

Sometimes us newbies get so wound up we cant think outside of the "box", as it were

;-)