[jdom-interest] newbie jdom -> xalan problem

Ken Rune Helland kenh at csc.no
Thu Apr 5 09:11:54 PDT 2001


You have to write to the pipe and read from the pipe in to different threads
because the write to the pipetoutputstream will block when
a seartain number of bytes have been written to it waiting for some tread to
read them from the pipedinputstream.

try:

   try{

       PipedInputStream in = new PipedInputStream();
       PipedOutputStream pout  = new PipedOutputStream( in );
       StreamSource source = new StreamSource( in );
       StreamResult outsource = new StreamResult (out);

       // starting a new thread with an "on the spot" Runnable impl.
       new Thread(new Runnable(){
          run(){

             XMLOutputter xmlOutputter = new XMLOutputter();
             xmlOutputter.output( xmldoc, pout  );

             pout.close();
          }
       }).start();

       processor.transform( source, outsource );
    }
    catch (Exception e) {
       e.printStackTrace();
    }

Warning, this is from the top of my head and i have not
had time to test this.


KenR


At 03:49 PM 4/5/2001 +0100, Alan Eustace wrote:
>I'd be very grateful if someone can shed any light on this;
>I'm processing xml in a servlet, using piped streams as per the jdom faq
>of about a month ago.
>The parser hangs at the  "xmlOutputter.output( doc, pout );" line. I had
>a similar problem before, which was to do
>with not closing the piped output stream: adding the close() call solved
>that, but I'm baffled by this problem. No exception is being thrown.
>
>Many thanks,
>Alan Eustace.
>
>import java.io.*;
>import javax.servlet.*;
>import javax.servlet.http.*;
>import java.util.*;
>import org.jdom.Document;
>import org.jdom.Element;
>import org.jdom.input.*;
>import org.jdom.output.*;
>import org.jdom.input.SAXBuilder;
>import org.jdom.output.XMLOutputter;
>
>// Xalan-J 2
>import javax.xml.transform.Result;
>import javax.xml.transform.Source;
>import javax.xml.transform.Templates;
>import javax.xml.transform.Transformer;
>import javax.xml.transform.TransformerFactory;
>import javax.xml.transform.stream.StreamSource;
>import javax.xml.transform.stream.StreamResult;
>
>
>
>public class JDOMXalan extends HttpServlet{
>
>
>   PrintWriter out;
>   Element msg;
>   Document xmldoc;
>   Transformer processor;
>
>
>   public void init(ServletConfig config)throws ServletException{
>               super.init(config);
>
>             try{
>
>               File xslt = new File("foo.xsl");
>               SAXBuilder builder = new SAXBuilder();
>               xmldoc = builder.build(new File("foo.xml"));
>               Element root = xmldoc.getRootElement();
>               msg = root.getChild("message");
>               TransformerFactory transformerFactory =
>TransformerFactory.newInstance();
>               Templates stylesheet = transformerFactory.newTemplates(new
>StreamSource( xslt ) );
>               processor = stylesheet.newTransformer();
>
>             }
>             catch (Exception j){
>                   j.printStackTrace();
>             }
>   }
>
>
>   public void doGet(HttpServletRequest req, HttpServletResponse
>res)throws
>   ServletException, IOException{
>         out = res.getWriter();
>         res.setContentType("text/html");
>
>       try{
>
>       PipedInputStream in = new PipedInputStream();
>       PipedOutputStream pout  = new PipedOutputStream( in );
>       StreamSource source = new StreamSource( in );
>       StreamResult outsource = new StreamResult (out);
>
>       XMLOutputter xmlOutputter = new XMLOutputter();
>        //***parser hangs here***
>       xmlOutputter.output( xmldoc, pout  );
>
>       pout.close();
>       processor.transform( source, outsource );
>       }
>       catch (Exception e) {
>          e.printStackTrace();
>       }
>   }
>}
>
>_______________________________________________
>To control your jdom-interest membership:
>http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com




More information about the jdom-interest mailing list