Project

Profile

Help

RE: Trouble When I use Console.SetOut and Console.SetError » Program.cs

Daniel Langdon, 2013-11-01 17:23

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;

namespace SaxonOutputBug
{
class Program
{
static void Main(string[] args)
{
// Cache the default Console TextWriter so we can use it again later.
TextWriter defaultWriter = Console.Out;

// Create a RecordingTextWriter and set it to be used for Console.Out and Console.Error
RecordingTextWriter writer = new RecordingTextWriter();
Console.SetOut(writer);
Console.SetError(writer);

// Perform some operations that will write to the Console, which has been set to the RecordingTextWriter.
performOperation();

// Now that everything is done, set Console.Out back to the default value and display the result:
Console.SetOut(defaultWriter);
Console.WriteLine("The RecordingTextWriter object recorded the following string:\n" + writer.Record.ToString());
}

private static void performOperation()
{
TextWriterByteArrayOutputStream os = new TextWriterByteArrayOutputStream(Console.Out);
java.io.PrintStream ps = new java.io.PrintStream(os);
java.lang.System.setErr(ps);
java.lang.System.setOut(ps);

// Will record to the RecordingTextWriter class
Console.WriteLine("I'm writing on the default Console. Next, I want to see what Saxon writes on the default Console.");

Saxon.Api.Processor processor = null;
Saxon.Api.XdmNode xsltInput = null;
Saxon.Api.XsltCompiler xsltCompiler = null;
Saxon.Api.XsltExecutable xsltExecutable = null;
Saxon.Api.XsltTransformer xsltTransformer = null;
Saxon.Api.Serializer dataSerializer = null;

// Next, create some Saxon classes and make them dump errors on the Console.
try
{
processor = new Saxon.Api.Processor();
XmlDocument invalidXsltDocument = new XmlDocument();
invalidXsltDocument.LoadXml(@"<xsl:stylesheet version=""1.0"" xmlns:xsl=""http://www.w3.org/1999/XSL/Transform""><xsl:template match=""/""><xsl:value-of select='document(""abc"")' /></xsl:template></xsl:stylesheet>");
xsltInput = processor.NewDocumentBuilder().Build(invalidXsltDocument);
xsltCompiler = processor.NewXsltCompiler();
xsltExecutable = xsltCompiler.Compile(xsltInput);
xsltTransformer = xsltExecutable.Load();
XmlDocument document = new XmlDocument();
document.LoadXml("<Hello><world/></Hello>");
Stream transformedStream = new MemoryStream();
xsltTransformer.InitialContextNode = processor.NewDocumentBuilder().Build(document);
dataSerializer = processor.NewSerializer(transformedStream);
xsltTransformer.Run(dataSerializer);
}
catch (Exception ex)
{
Console.Error.WriteLine("Oh no! There was an error!");
Console.Error.WriteLine(ex.ToString());
}
}
}

public class RecordingTextWriter : TextWriter
{
public StringBuilder Record { get; private set; }

public RecordingTextWriter()
{
Record = new StringBuilder();
}

// Override Write and WriteLine so that the values recorded go to the Record StringBuilder.
public override void Write(string value)
{
Record.Append(value);
}
public override void WriteLine(string value)
{
Record.AppendLine(value);
}
public override void WriteLine()
{
Record.AppendLine();
}

public override Encoding Encoding
{
get { throw new NotImplementedException(); }
}
}

public class TextWriterByteArrayOutputStream : java.io.ByteArrayOutputStream
{
TextWriter TextWriter { get; set; }

public TextWriterByteArrayOutputStream(TextWriter writer)
{
TextWriter = writer;
}

public override void write(byte[] b)
{
string s = Encoding.UTF8.GetString(b);
TextWriter.Write(s);
}

public override void write(byte[] b, int off, int len)
{
string s = Encoding.UTF8.GetString(b, off, len);
TextWriter.Write(s);
}

public override void write(int b)
{
TextWriter.Write(b);
}
}
}
    (1-1/1)