Skip to content

Commit 67becfc

Browse files
committed
car ReadAsync
1 parent 47c920c commit 67becfc

File tree

10 files changed

+81
-14
lines changed

10 files changed

+81
-14
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
<LatestTargetFramework>net8.0</LatestTargetFramework>
7171
<SamplesFrameworks>net8.0</SamplesFrameworks>
7272
<SamplesFrameworks Condition=" '$(OS)' == 'Windows_NT' ">$(SamplesFrameworks);net472</SamplesFrameworks>
73-
<DefineConstants Condition=" '$(TargetFramework)' != 'net35' And '$(TargetFramework)' != 'net40' And '$(TargetFramework)' != 'net46' And '$(TargetFramework)' != 'net472' ">$(DefineConstants);FEATURE_ASYNC_SAX_XML</DefineConstants>
73+
<DefineConstants Condition=" '$(TargetFramework)' != 'net35' And '$(TargetFramework)' != 'net40' And '$(TargetFramework)' != 'net46' And '$(TargetFramework)' != 'net472' ">$(DefineConstants);TASKS_SUPPORTED</DefineConstants>
7474
</PropertyGroup>
7575
</Otherwise>
7676
</Choose>

src/DocumentFormat.OpenXml.Framework/OpenXmlPartReader.cs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
using System.Diagnostics;
1111
using System.IO;
1212
using System.Linq;
13+
#if TASKS_SUPPORTED
14+
using System.Threading.Tasks;
15+
#endif
1316
using System.Xml;
1417

1518
namespace DocumentFormat.OpenXml
@@ -100,7 +103,7 @@ public OpenXmlPartReader(Stream partStream, IFeatureCollection features, OpenXml
100103

101104
_resolver = features.GetRequired<IOpenXmlNamespaceResolver>();
102105
_rootElements = features.GetRequired<IRootElementFeature>();
103-
_xmlReader = CreateReader(partStream, options.CloseStream, options.MaxCharactersInPart, ignoreWhitespace: options.IgnoreWhitespace, out _standalone, out _encoding);
106+
_xmlReader = CreateReader(partStream, options, out _standalone, out _encoding);
104107
}
105108

106109
/// <summary>
@@ -394,6 +397,17 @@ public override void Skip()
394397
/// <inheritdoc/>
395398
public override IXmlLineInfo GetLineInfo() => XmlLineInfo.Get(_xmlReader);
396399

400+
#region Async methods
401+
#if TASKS_SUPPORTED
402+
public override Task<bool> ReadAsync()
403+
{
404+
ThrowIfObjectDisposed();
405+
406+
407+
return _xmlReader.ReadAsync();
408+
}
409+
#endif
410+
#endregion
397411
#region private methods
398412

399413
/// <summary>
@@ -667,17 +681,20 @@ public override void Close()
667681
_xmlReader.Close();
668682
}
669683

670-
private XmlReader CreateReader(Stream partStream, bool closeInput, long maxCharactersInPart, bool ignoreWhitespace, out bool? standalone, out string? encoding)
684+
private XmlReader CreateReader(Stream partStream, OpenXmlPartReaderOptions options, out bool? standalone, out string? encoding)
671685
{
672686
var settings = new XmlReaderSettings
673687
{
674-
MaxCharactersInDocument = maxCharactersInPart,
675-
CloseInput = closeInput,
676-
IgnoreWhitespace = ignoreWhitespace,
688+
MaxCharactersInDocument = options.MaxCharactersInPart,
689+
CloseInput = options.CloseStream,
690+
IgnoreWhitespace = options.IgnoreWhitespace,
677691
#if NET35
678692
ProhibitDtd = true,
679693
#else
680694
DtdProcessing = DtdProcessing.Prohibit,
695+
#endif
696+
#if TASKS_SUPPORTED
697+
Async = options.Async,
681698
#endif
682699
};
683700

src/DocumentFormat.OpenXml.Framework/OpenXmlPartReaderOptions.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,26 @@ public struct OpenXmlPartReaderOptions
3131
/// </summary>
3232
public bool CloseStream { get; set; }
3333

34+
#if TASKS_SUPPORTED
35+
/// <summary>
36+
/// Gets or sets a value indicating whether the part reader should operate asynchronously.
37+
/// </summary>
38+
/// <remarks>
39+
/// When set to <c>true</c>, the reader will use asynchronous methods for reading XML data,
40+
/// allowing non-blocking operations. This property is only available when the build target
41+
/// supports asynchronous SAX XML processing.
42+
/// </remarks>
43+
public bool Async { get; set; }
44+
#endif
45+
3446
internal OpenXmlPartReaderOptions UpdateForPart(OpenXmlPart part) => new()
3547
{
3648
ReadMiscellaneousNodes = ReadMiscellaneousNodes,
3749
MaxCharactersInPart = MaxCharactersInPart != 0 ? MaxCharactersInPart : part.MaxCharactersInPart,
3850
IgnoreWhitespace = IgnoreWhitespace,
3951
CloseStream = true,
52+
#if TASKS_SUPPORTED
53+
Async = Async,
54+
#endif
4055
};
4156
}

src/DocumentFormat.OpenXml.Framework/OpenXmlPartWriter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using System.Collections.Generic;
77
using System.IO;
88
using System.Text;
9-
#if FEATURE_ASYNC_SAX_XML
9+
#if TASKS_SUPPORTED
1010
using DocumentFormat.OpenXml.Framework;
1111
using System.Threading.Tasks;
1212
#endif
@@ -82,7 +82,7 @@ public OpenXmlPartWriter(OpenXmlPart openXmlPart, OpenXmlPartWriterSettings sett
8282
{
8383
CloseOutput = true,
8484
Encoding = settings.Encoding,
85-
#if FEATURE_ASYNC_SAX_XML
85+
#if TASKS_SUPPORTED
8686
Async = settings.Async,
8787
#endif
8888
};
@@ -146,7 +146,7 @@ public OpenXmlPartWriter(Stream partStream, OpenXmlPartWriterSettings settings)
146146
{
147147
CloseOutput = settings.CloseOutput,
148148
Encoding = settings.Encoding,
149-
#if FEATURE_ASYNC_SAX_XML
149+
#if TASKS_SUPPORTED
150150
Async = settings.Async,
151151
#endif
152152
};
@@ -430,7 +430,7 @@ public override void Close()
430430
#endregion
431431

432432
// Async Methods
433-
#if FEATURE_ASYNC_SAX_XML
433+
#if TASKS_SUPPORTED
434434
/// <summary>
435435
/// Asynchronously writes the XML declaration with the version "1.0".
436436
/// </summary>

src/DocumentFormat.OpenXml.Framework/OpenXmlPartWriterSettings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace DocumentFormat.OpenXml;
1010
/// </summary>
1111
public class OpenXmlPartWriterSettings
1212
{
13-
#if FEATURE_ASYNC_SAX_XML
13+
#if TASKS_SUPPORTED
1414
/// <summary>
1515
/// Gets or sets a value indicating whether asynchronous <see cref="OpenXmlPartWriter" /> methods can be used.
1616
/// </summary>

src/DocumentFormat.OpenXml.Framework/OpenXmlReader.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
using System.Collections.Generic;
77
using System.Collections.ObjectModel;
88
using System.IO;
9+
#if TASKS_SUPPORTED
10+
using System.Threading.Tasks;
11+
#endif
912
using System.Xml;
1013

1114
namespace DocumentFormat.OpenXml
@@ -235,6 +238,27 @@ public virtual bool HasAttributes
235238
/// </summary>
236239
public abstract void Close();
237240

241+
#if TASKS_SUPPORTED
242+
#region Async methods
243+
244+
/// <summary>
245+
/// Asynchronously reads the next element in the Open XML document.
246+
/// </summary>
247+
/// <returns>
248+
/// A task that represents the asynchronous read operation. The task result is <c>true</c> if the next element
249+
/// was read successfully; <c>false</c> if there are no more elements to read.
250+
/// </returns>
251+
/// <remarks>
252+
/// This method is only available when the build target supports asynchronous SAX XML processing.
253+
/// It allows non-blocking operations for reading large Open XML documents.
254+
/// </remarks>
255+
public virtual Task<bool> ReadAsync()
256+
{
257+
return Task.FromResult(Read());
258+
}
259+
#endregion
260+
#endif
261+
238262
/// <summary>
239263
/// Thrown if the object is disposed.
240264
/// </summary>

src/DocumentFormat.OpenXml.Framework/OpenXmlWriter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using System.Collections.Generic;
77
using System.IO;
88
using System.Text;
9-
#if FEATURE_ASYNC_SAX_XML
9+
#if TASKS_SUPPORTED
1010
using System.Threading.Tasks;
1111
#endif
1212

@@ -131,7 +131,7 @@ protected OpenXmlWriter()
131131
/// </summary>
132132
public abstract void Close();
133133

134-
#if FEATURE_ASYNC_SAX_XML
134+
#if TASKS_SUPPORTED
135135
/// <summary>
136136
/// Asynchronously writes the XML declaration with the version "1.0".
137137
/// </summary>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
#nullable enable
2+
DocumentFormat.OpenXml.OpenXmlPartReaderOptions.Async.get -> bool
3+
DocumentFormat.OpenXml.OpenXmlPartReaderOptions.Async.set -> void
4+
override DocumentFormat.OpenXml.OpenXmlPartReader.ReadAsync() -> System.Threading.Tasks.Task<bool>!
5+
virtual DocumentFormat.OpenXml.OpenXmlReader.ReadAsync() -> System.Threading.Tasks.Task<bool>!

src/DocumentFormat.OpenXml.Framework/XmlConvertingReader.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
using DocumentFormat.OpenXml.Features;
55
using DocumentFormat.OpenXml.Framework;
66
using System;
7+
#if TASKS_SUPPORTED
8+
using System.Threading.Tasks;
9+
#endif
710
using System.Xml;
811

912
namespace DocumentFormat.OpenXml
@@ -53,6 +56,10 @@ protected override void Dispose(bool disposing)
5356
/// <inheritdoc/>
5457
public override bool Read() => BaseReader.Read();
5558

59+
#if TASKS_SUPPORTED
60+
public override Task<bool> ReadAsync() => BaseReader.ReadAsync();
61+
#endif
62+
5663
/// <inheritdoc/>
5764
public override string GetAttribute(int index) => BaseReader.GetAttribute(index);
5865

test/DocumentFormat.OpenXml.Tests/ofapiTest/OpenXmlWriterTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public void WriteStringExceptionTest7()
239239
}
240240
}
241241

242-
#if FEATURE_ASYNC_SAX_XML
242+
#if TASKS_SUPPORTED
243243
[Fact]
244244
public async Task WriteStartDocumentAsync_ShouldWriteStartDocument()
245245
{

0 commit comments

Comments
 (0)