Project

Profile

Help

Bug #3401 » UnclosedFilesExample.txt

Harry Davidson, 2017-08-20 11:18

 
public class XsltSimpleMultipleInclude : Example
{

public override String testName
{
get { return "XsltSimpleMultipleInclude"; }
}

public override void run(Uri samplesDir)
{
// Create a Processor instance.
Processor processor = new Processor();
processor.XmlResolver = new TrackingResolver();

////////////////////////////// ADDING THIS CAUSES AN ISSUE IN THE CONTEXT ///////////////////////////
processor.Implementation.setTraceListener(new SaxonBug.MyTraceListener());
/////////////////////////////////////////////////////////////////////////////////////////////////////

// Load the source document
XdmNode input = processor.NewDocumentBuilder().Build(new Uri(samplesDir, "data/books.xml"));

// Create a transformer for the stylesheet.
XsltTransformer transformer = processor.NewXsltCompiler().Compile(new Uri(samplesDir, "multipleInclude/a.xsl")).Load();

// Set the root node of the source document to be the initial context node
transformer.InitialContextNode = input;

// Create a serializer, with output to the standard output stream
Serializer serializer = new Serializer();
serializer.SetOutputWriter(Console.Out);

// Transform the source XML and serialize the result document
transformer.Run(serializer);

TrackedStream.CheckAllStreamsClosed();
}
}

class TrackingResolver : System.Xml.XmlUrlResolver
{

public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn)
{
object obj = base.GetEntity(absoluteUri, role, ofObjectToReturn);
Debug.Assert(obj is Stream, "Only expecting a stream, this is scratch code");
return new TrackedStream(obj as Stream, absoluteUri);
}
}

class TrackedStream : Stream
{
private Stream _innerStream = null;
private Uri _absoluteUri;
static System.Collections.Generic.List<TrackedStream> allStreams = new System.Collections.Generic.List<TrackedStream>();

public static void CheckAllStreamsClosed()
{
foreach(TrackedStream s in allStreams)
{
if (s.CanRead)
Console.WriteLine($"The stream should have been closed {s.GetHashCode()} = {s._absoluteUri}");
}
}

public TrackedStream(Stream innerStream, Uri absoluteUri)
{
allStreams.Add(this);
_innerStream = innerStream;
_absoluteUri = absoluteUri;
Console.WriteLine($"Tracking Stream Created {_innerStream.GetHashCode()} = {_absoluteUri}");
}
public override void Close()
{
Console.WriteLine($"Tracking Stream Closed {_innerStream.GetHashCode()} = {_absoluteUri}");
_innerStream.Close();
}

protected override void Dispose(bool disposing)
{
Console.WriteLine($"Tracking Stream Disposed {_innerStream.GetHashCode()} = {_absoluteUri}");
_innerStream.Dispose();
}

public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state) { return _innerStream.BeginRead(buffer, offset, count, callback, state); }
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state) { return _innerStream.BeginWrite(buffer, offset, count, callback, state); }
public override bool CanRead { get { return _innerStream.CanRead; } }
public override bool CanSeek { get { return _innerStream.CanSeek; } }
public override bool CanTimeout { get { return _innerStream.CanTimeout; } }
public override bool CanWrite { get { return _innerStream.CanWrite; } }
public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken) { return _innerStream.CopyToAsync(destination, bufferSize, cancellationToken); }
public override ObjRef CreateObjRef(Type requestedType) { return _innerStream.CreateObjRef(requestedType); }
public override int EndRead(IAsyncResult asyncResult) { return _innerStream.EndRead(asyncResult); }
public override void EndWrite(IAsyncResult asyncResult) { _innerStream.EndWrite(asyncResult); }
public override bool Equals(object obj) { return _innerStream.Equals(obj); }
public override void Flush() { _innerStream.Flush(); }
public override Task FlushAsync(CancellationToken cancellationToken) { return _innerStream.FlushAsync(cancellationToken); }
public override int GetHashCode() { return _innerStream.GetHashCode(); }
public override object InitializeLifetimeService() { return _innerStream.InitializeLifetimeService(); }
public override long Length { get { return _innerStream.Length; } }
public override long Position { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public override int Read(byte[] buffer, int offset, int count) { return _innerStream.Read(buffer, offset, count); }
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) { return _innerStream.ReadAsync(buffer, offset, count, cancellationToken); }
public override int ReadByte() { return _innerStream.ReadByte(); }
public override int ReadTimeout { get => _innerStream.ReadTimeout; set => _innerStream.ReadTimeout = value; }
public override long Seek(long offset, SeekOrigin origin) { return _innerStream.Seek(offset, origin); }
public override void SetLength(long value) { _innerStream.SetLength(value); }
public override string ToString() { return _innerStream.ToString(); }
public override void Write(byte[] buffer, int offset, int count) { _innerStream.Write(buffer, offset, count); }
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) { return _innerStream.WriteAsync(buffer, offset, count, cancellationToken); }
public override void WriteByte(byte value) { _innerStream.WriteByte(value); }
public override int WriteTimeout { get => _innerStream.WriteTimeout; set => _innerStream.WriteTimeout = value; }
}








































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































(2-2/4)