Project

Profile

Help

Using TextWriterDestination in Saxon .Net

Added by Anonymous almost 18 years ago

Legacy ID: #3787719 Legacy Poster: Scott Douglas-Watson (sdowa)

When using a TextWriterDestination the underlying Stream is closed after the transform is run which AFAICT makes storing the result in to a memory stream pointless/impossible as you can't access the result after the stream is closed. Is this behaviour intended? Also the TextWriterDestination spews a lot of debug info to the console which the DomDestination doesn't along with the following exception: java.lang.NullPointerException: debug at net.sf.saxon.event.TracingFilter.startDocument (TracingFilter.java:15 1) at net.sf.saxon.event.ProxyReceiver.startDocument (ProxyReceiver.java:98 ) at net.sf.saxon.event.ComplexContentOutputter.startDocument (ComplexCont entOutputter.java:93) at net.sf.saxon.Controller.transformDocument (Controller.java:1553) at cli.Saxon.Api.XsltTransformer.Run (Unknown Source) at cli.Test.Main (Test.cs:23) Once again is this to be expected? Thanks, Scott Test case MemStrm.cs: using System; using Saxon.Api; using System.IO; using System.Xml; class MemStrm { static void Main(string[] args) { if (args.Length < 2) { Console.WriteLine("Usage: MemStrm <xsl file> <xml file> [destination file]"); Environment.Exit(0); } Processor p = new Processor(); DocumentBuilder builder = p.NewDocumentBuilder(); XsltTransformer trans = p.NewXsltCompiler().Compile(new XmlTextReader(File.OpenText(args[0]))).Load(); MemoryStream ms = new MemoryStream(); using (StreamReader strm = File.OpenText(args[1])) { trans.InitialContextNode = builder.Build(new XmlTextReader(strm)); TextWriterDestination twd = new TextWriterDestination(new XmlTextWriter(new StreamWriter(ms))); trans.Run(twd); } ms.Position = 0; if (args.Length < 3 || args[2].Equals("-")) { Console.WriteLine(new StreamReader(ms).ReadToEnd()); } else { using (StreamWriter sw = File.CreateText(args[2])) { sw.Write(new StreamReader(ms).ReadToEnd()); } } } }


Replies (1)

RE: Using TextWriterDestination in Saxon .Net - Added by Anonymous almost 18 years ago

Legacy ID: #3789393 Legacy Poster: Michael Kay (mhkay)

Are you using the latest version (8.7.3)? I thought the problem of unwanted trace code had been fixed. I'll investigate the issue of closing the stream. It's always tricky to know whether one should close a stream or not. My usual rule of thumb is that the responsibility for closing a stream lies with the component that created the stream.

    (1-1/1)

    Please register to reply