Project

Profile

Help

Bug #3580

Problem with Serializer and method SerializeXdmValue closing underlying TextWriter

Added by Martin Honnen almost 4 years ago. Updated over 3 years ago.

Status:
Closed
Priority:
Normal
Category:
.NET API
Sprint/Milestone:
-
Start date:
2017-12-25
Due date:
% Done:

100%

Estimated time:
Legacy ID:
Applies to branch:
9.8
Fix Committed on Branch:
9.8, trunk
Fixed in Maintenance Release:
Platforms:

Description

The documentation of the NewSerializer(textWriter) method https://www.saxonica.com/html/documentation/dotnetdoc/Saxon/Api/Processor.html#NewSerializer(System.IO.TextWriter) clearly says

Create a Serializer initialized to write to a given Writer. Closing the writer after use is the responsibility of the caller.

so I would expect to be able to create a Serializer over a TextWriter and call methods like SerializeXdmValue repeatedly, however when trying to do that with Saxon 9.8.0.7 HE on .NET I get an error System.ObjectDisposedException that writing to a closed TextWriter is not possible.

Here is a simple test case:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Saxon.Api;
using System.IO;

namespace SaxonTextWriterProblem
{
    class Program
    {
        static void Main(string[] args)
        {
            Test1();
        }

        static void Test1()
        {
            string xquery = @"1, 'string', true(), document{<root><foo>bar</foo></root>}, ['a','b','c'], map { 'foo' : 'bar' }";
            Test(xquery);
        }

        static void Test(string xquery)
        {
            Processor processor = new Processor();
            XQueryCompiler compiler = processor.NewXQueryCompiler();

            XQueryExecutable executable = compiler.Compile(xquery);

            XQueryEvaluator evaluator = executable.Load();

            StringWriter sw = new StringWriter();

            Serializer serializer = processor.NewSerializer(sw);
            serializer.SetOutputProperty(new QName("method"), "adaptive");

            foreach (XdmItem item in evaluator)
            {
                serializer.SerializeXdmValue(item);
            }

            Console.WriteLine(sw.ToString());
            Console.WriteLine();

            sw.Close();
            sw.Dispose();
        }

The stack trace is

Unbehandelte Ausnahme: System.ObjectDisposedException: In einen geschlossenen TextWriter kann nicht geschrieben werden.
   bei System.IO.__Error.WriterClosed()
   bei System.IO.StringWriter.Write(String value)
   bei net.sf.saxon.dotnet.DotNetWriter.write(String str)
   bei java.io.Writer.append(CharSequence csq)
   bei net.sf.saxon.serialize.AdaptiveEmitter.emit(CharSequence )
   bei net.sf.saxon.serialize.AdaptiveEmitter.serializeItem(Item )
   bei net.sf.saxon.serialize.AdaptiveEmitter.write(Item item)
   bei net.sf.saxon.event.SequenceWriter.append(Item item, Location locationId, Int32 copyNamespaces)
   bei net.sf.saxon.query.QueryResult.serializeSequence(SequenceIterator iterator, Configuration config, Result result, Properties outputProperties)
   bei Saxon.Api.Serializer.SerializeXdmValue(XdmValue value)
   bei SaxonTextWriterProblem.Program.Test(String xquery) in c:\users\marti\source\repos\SaxonTextWriterProblem\SaxonTextWriterProblem\Program.cs:Zeile 40.
   bei SaxonTextWriterProblem.Program.Test1() in c:\users\marti\source\repos\SaxonTextWriterProblem\SaxonTextWriterProblem\Program.cs:Zeile 21.
   bei SaxonTextWriterProblem.Program.Main(String[] args) in c:\users\marti\source\repos\SaxonTextWriterProblem\SaxonTextWriterProblem\Program.cs:Zeile 15.

So it seems either the documentation is misleading or the implementation unintentionally closes the @TextWriter@.

History

#1 Updated by Michael Kay almost 4 years ago

  • Assignee set to O'Neil Delpratt

#2 Updated by Michael Kay almost 4 years ago

It looks to me as if this statement in the documentation is correct when the Serializer is used as a Destination, but not when the serializeXdmValue() method is used.

#3 Updated by O'Neil Delpratt over 3 years ago

  • Status changed from New to Resolved
  • % Done changed from 0 to 100
  • Fix Committed on Branch 9.8, trunk added

We have decided to update the documentation to reflect the current situation: Added text in the SerializeXdmValue: "A call on this method will close the writer or output stream internally".

#4 Updated by O'Neil Delpratt over 3 years ago

  • Status changed from Resolved to Closed
  • Fixed in Maintenance Release 9.8.0.8 added

Bug fix applied in the Saxon 9.8.0.8 maintenance release.

Please register to edit this issue

Also available in: Atom PDF