1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
|
using System.Threading.Tasks;
#if !FEATURE_NETCORE
using System.Xml.XPath;
#endif // !FEATURE_NETCORE
namespace System.Xml {
internal class XmlAsyncCheckWriter : XmlWriter {
private readonly XmlWriter coreWriter = null;
private Task lastTask = AsyncHelper.DoneTask;
internal XmlWriter CoreWriter {
get {
return coreWriter;
}
}
public XmlAsyncCheckWriter(XmlWriter writer) {
coreWriter = writer;
}
private void CheckAsync() {
if (!lastTask.IsCompleted) {
throw new InvalidOperationException(Res.GetString(Res.Xml_AsyncIsRunningException));
}
}
#region Sync Methods, Properties Check
public override XmlWriterSettings Settings {
get {
XmlWriterSettings settings = coreWriter.Settings;
if (null != settings) {
settings = settings.Clone();
}
else {
settings = new XmlWriterSettings();
}
settings.Async = true;
settings.ReadOnly = true;
return settings;
}
}
public override void WriteStartDocument() {
CheckAsync();
coreWriter.WriteStartDocument();
}
public override void WriteStartDocument(bool standalone) {
CheckAsync();
coreWriter.WriteStartDocument(standalone);
}
public override void WriteEndDocument() {
CheckAsync();
coreWriter.WriteEndDocument();
}
public override void WriteDocType(string name, string pubid, string sysid, string subset) {
CheckAsync();
coreWriter.WriteDocType(name, pubid, sysid, subset);
}
public override void WriteStartElement(string prefix, string localName, string ns) {
CheckAsync();
coreWriter.WriteStartElement(prefix, localName, ns);
}
public override void WriteEndElement() {
CheckAsync();
coreWriter.WriteEndElement();
}
public override void WriteFullEndElement() {
CheckAsync();
coreWriter.WriteFullEndElement();
}
public override void WriteStartAttribute(string prefix, string localName, string ns) {
CheckAsync();
coreWriter.WriteStartAttribute(prefix, localName, ns);
}
public override void WriteEndAttribute() {
CheckAsync();
coreWriter.WriteEndAttribute();
}
public override void WriteCData(string text) {
CheckAsync();
coreWriter.WriteCData(text);
}
public override void WriteComment(string text) {
CheckAsync();
coreWriter.WriteComment(text);
}
public override void WriteProcessingInstruction(string name, string text) {
CheckAsync();
coreWriter.WriteProcessingInstruction(name, text);
}
public override void WriteEntityRef(string name) {
CheckAsync();
coreWriter.WriteEntityRef(name);
}
public override void WriteCharEntity(char ch) {
CheckAsync();
coreWriter.WriteCharEntity(ch);
}
public override void WriteWhitespace(string ws) {
CheckAsync();
coreWriter.WriteWhitespace(ws);
}
public override void WriteString(string text) {
CheckAsync();
coreWriter.WriteString(text);
}
public override void WriteSurrogateCharEntity(char lowChar, char highChar) {
CheckAsync();
coreWriter.WriteSurrogateCharEntity(lowChar, highChar);
}
public override void WriteChars(char[] buffer, int index, int count) {
CheckAsync();
coreWriter.WriteChars(buffer, index, count);
}
public override void WriteRaw(char[] buffer, int index, int count) {
CheckAsync();
coreWriter.WriteRaw(buffer, index, count);
}
public override void WriteRaw(string data) {
CheckAsync();
coreWriter.WriteRaw(data);
}
public override void WriteBase64(byte[] buffer, int index, int count) {
CheckAsync();
coreWriter.WriteBase64(buffer, index, count);
}
public override void WriteBinHex(byte[] buffer, int index, int count) {
CheckAsync();
coreWriter.WriteBinHex(buffer, index, count);
}
public override WriteState WriteState {
get {
CheckAsync();
return coreWriter.WriteState;
}
}
public override void Close() {
CheckAsync();
coreWriter.Close();
}
public override void Flush() {
CheckAsync();
coreWriter.Flush();
}
public override string LookupPrefix(string ns) {
CheckAsync();
return coreWriter.LookupPrefix(ns);
}
public override XmlSpace XmlSpace {
get {
CheckAsync();
return coreWriter.XmlSpace;
}
}
public override string XmlLang {
get {
CheckAsync();
return coreWriter.XmlLang;
}
}
public override void WriteNmToken(string name) {
CheckAsync();
coreWriter.WriteNmToken(name);
}
public override void WriteName(string name) {
CheckAsync();
coreWriter.WriteName(name);
}
public override void WriteQualifiedName(string localName, string ns) {
CheckAsync();
coreWriter.WriteQualifiedName(localName, ns);
}
public override void WriteValue(object value) {
CheckAsync();
coreWriter.WriteValue(value);
}
public override void WriteValue(string value) {
CheckAsync();
coreWriter.WriteValue(value);
}
public override void WriteValue(bool value) {
CheckAsync();
coreWriter.WriteValue(value);
}
public override void WriteValue(DateTime value) {
CheckAsync();
coreWriter.WriteValue(value);
}
public override void WriteValue(DateTimeOffset value) {
CheckAsync();
coreWriter.WriteValue(value);
}
public override void WriteValue(double value) {
CheckAsync();
coreWriter.WriteValue(value);
}
public override void WriteValue(float value) {
CheckAsync();
coreWriter.WriteValue(value);
}
public override void WriteValue(decimal value) {
CheckAsync();
coreWriter.WriteValue(value);
}
public override void WriteValue(int value) {
CheckAsync();
coreWriter.WriteValue(value);
}
public override void WriteValue(long value) {
CheckAsync();
coreWriter.WriteValue(value);
}
public override void WriteAttributes(XmlReader reader, bool defattr) {
CheckAsync();
coreWriter.WriteAttributes(reader, defattr);
}
public override void WriteNode(XmlReader reader, bool defattr) {
CheckAsync();
coreWriter.WriteNode(reader, defattr);
}
#if !FEATURE_NETCORE
public override void WriteNode(XPathNavigator navigator, bool defattr) {
CheckAsync();
coreWriter.WriteNode(navigator, defattr);
}
#endif
protected override void Dispose(bool disposing) {
CheckAsync();
//since it is protected method, we can't call coreWriter.Dispose(disposing).
//Internal, it is always called to Dipose(true). So call coreWriter.Dispose() is OK.
coreWriter.Dispose();
}
#endregion
#region Async Methods
public override Task WriteStartDocumentAsync() {
CheckAsync();
var task = coreWriter.WriteStartDocumentAsync();
lastTask = task;
return task;
}
public override Task WriteStartDocumentAsync(bool standalone) {
CheckAsync();
var task = coreWriter.WriteStartDocumentAsync(standalone);
lastTask = task;
return task;
}
public override Task WriteEndDocumentAsync() {
CheckAsync();
var task = coreWriter.WriteEndDocumentAsync();
lastTask = task;
return task;
}
public override Task WriteDocTypeAsync(string name, string pubid, string sysid, string subset) {
CheckAsync();
var task = coreWriter.WriteDocTypeAsync(name, pubid, sysid, subset);
lastTask = task;
return task;
}
public override Task WriteStartElementAsync(string prefix, string localName, string ns) {
CheckAsync();
var task = coreWriter.WriteStartElementAsync(prefix, localName, ns);
lastTask = task;
return task;
}
public override Task WriteEndElementAsync() {
CheckAsync();
var task = coreWriter.WriteEndElementAsync();
lastTask = task;
return task;
}
public override Task WriteFullEndElementAsync() {
CheckAsync();
var task = coreWriter.WriteFullEndElementAsync();
lastTask = task;
return task;
}
protected internal override Task WriteStartAttributeAsync(string prefix, string localName, string ns) {
CheckAsync();
var task = coreWriter.WriteStartAttributeAsync(prefix, localName, ns);
lastTask = task;
return task;
}
protected internal override Task WriteEndAttributeAsync() {
CheckAsync();
var task = coreWriter.WriteEndAttributeAsync();
lastTask = task;
return task;
}
public override Task WriteCDataAsync(string text) {
CheckAsync();
var task = coreWriter.WriteCDataAsync(text);
lastTask = task;
return task;
}
public override Task WriteCommentAsync(string text) {
CheckAsync();
var task = coreWriter.WriteCommentAsync(text);
lastTask = task;
return task;
}
public override Task WriteProcessingInstructionAsync(string name, string text) {
CheckAsync();
var task = coreWriter.WriteProcessingInstructionAsync(name, text);
lastTask = task;
return task;
}
public override Task WriteEntityRefAsync(string name) {
CheckAsync();
var task = coreWriter.WriteEntityRefAsync(name);
lastTask = task;
return task;
}
public override Task WriteCharEntityAsync(char ch) {
CheckAsync();
var task = coreWriter.WriteCharEntityAsync(ch);
lastTask = task;
return task;
}
public override Task WriteWhitespaceAsync(string ws) {
CheckAsync();
var task = coreWriter.WriteWhitespaceAsync(ws);
lastTask = task;
return task;
}
public override Task WriteStringAsync(string text) {
CheckAsync();
var task = coreWriter.WriteStringAsync(text);
lastTask = task;
return task;
}
public override Task WriteSurrogateCharEntityAsync(char lowChar, char highChar) {
CheckAsync();
var task = coreWriter.WriteSurrogateCharEntityAsync(lowChar, highChar);
lastTask = task;
return task;
}
public override Task WriteCharsAsync(char[] buffer, int index, int count) {
CheckAsync();
var task = coreWriter.WriteCharsAsync(buffer, index, count);
lastTask = task;
return task;
}
public override Task WriteRawAsync(char[] buffer, int index, int count) {
CheckAsync();
var task = coreWriter.WriteRawAsync(buffer, index, count);
lastTask = task;
return task;
}
public override Task WriteRawAsync(string data) {
CheckAsync();
var task = coreWriter.WriteRawAsync(data);
lastTask = task;
return task;
}
public override Task WriteBase64Async(byte[] buffer, int index, int count) {
CheckAsync();
var task = coreWriter.WriteBase64Async(buffer, index, count);
lastTask = task;
return task;
}
public override Task WriteBinHexAsync(byte[] buffer, int index, int count) {
CheckAsync();
var task = coreWriter.WriteBinHexAsync(buffer, index, count);
lastTask = task;
return task;
}
public override Task FlushAsync() {
CheckAsync();
var task = coreWriter.FlushAsync();
lastTask = task;
return task;
}
public override Task WriteNmTokenAsync(string name) {
CheckAsync();
var task = coreWriter.WriteNmTokenAsync(name);
lastTask = task;
return task;
}
public override Task WriteNameAsync(string name) {
CheckAsync();
var task = coreWriter.WriteNameAsync(name);
lastTask = task;
return task;
}
public override Task WriteQualifiedNameAsync(string localName, string ns) {
CheckAsync();
var task = coreWriter.WriteQualifiedNameAsync(localName, ns);
lastTask = task;
return task;
}
public override Task WriteAttributesAsync(XmlReader reader, bool defattr) {
CheckAsync();
var task = coreWriter.WriteAttributesAsync(reader, defattr);
lastTask = task;
return task;
}
public override Task WriteNodeAsync(XmlReader reader, bool defattr) {
CheckAsync();
var task = coreWriter.WriteNodeAsync(reader, defattr);
lastTask = task;
return task;
}
#if !FEATURE_NETCORE
public override Task WriteNodeAsync(XPathNavigator navigator, bool defattr) {
CheckAsync();
var task = coreWriter.WriteNodeAsync(navigator, defattr);
lastTask = task;
return task;
}
#endif // !FEATURE_NETCORE
#endregion
}
}
|