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 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
|
//
// XmlDictionaryReader.cs
//
// Author:
// Atsushi Enomoto <atsushi@ximian.com>
//
// Copyright (C) 2005, 2007 Novell, Inc. http://www.novell.com
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.IO;
using System.Reflection;
using System.Text;
using System.Xml;
namespace System.Xml
{
public abstract partial class XmlDictionaryReader : XmlReader
{
protected XmlDictionaryReader ()
{
}
XmlDictionaryReaderQuotas quotas;
public virtual bool CanCanonicalize {
get { return false; }
}
public virtual XmlDictionaryReaderQuotas Quotas {
get {
if (quotas == null)
quotas = new XmlDictionaryReaderQuotas ();
return quotas;
}
}
public virtual void EndCanonicalization ()
{
throw new NotSupportedException ();
}
public virtual string GetAttribute (
XmlDictionaryString localName,
XmlDictionaryString namespaceUri)
{
if (localName == null)
throw new ArgumentNullException ("localName");
if (namespaceUri == null)
throw new ArgumentNullException ("namespaceUri");
return GetAttribute (localName.Value, namespaceUri.Value);
}
public virtual int IndexOfLocalName (
string [] localNames, string namespaceUri)
{
if (localNames == null)
throw new ArgumentNullException ("localNames");
if (namespaceUri == null)
throw new ArgumentNullException ("namespaceUri");
if (NamespaceURI != namespaceUri)
return -1;
for (int i = 0; i < localNames.Length; i++)
if (localNames [i] == LocalName)
return i;
return -1;
}
public virtual int IndexOfLocalName (
XmlDictionaryString [] localNames,
XmlDictionaryString namespaceUri)
{
if (localNames == null)
throw new ArgumentNullException ("localNames");
if (namespaceUri == null)
throw new ArgumentNullException ("namespaceUri");
if (NamespaceURI != namespaceUri.Value)
return -1;
XmlDictionaryString localName;
if (!TryGetLocalNameAsDictionaryString (out localName))
return -1;
IXmlDictionary dict = localName.Dictionary;
XmlDictionaryString iter;
for (int i = 0; i < localNames.Length; i++)
if (dict.TryLookup (localNames [i], out iter) && object.ReferenceEquals (iter, localName))
return i;
return -1;
}
public virtual bool IsArray (out Type type)
{
type = null;
return false;
}
public virtual bool IsLocalName (string localName)
{
return LocalName == localName;
}
public virtual bool IsLocalName (XmlDictionaryString localName)
{
if (localName == null)
throw new ArgumentNullException ("localName");
return LocalName == localName.Value;
}
public virtual bool IsNamespaceUri (string namespaceUri)
{
return NamespaceURI == namespaceUri;
}
public virtual bool IsNamespaceUri (XmlDictionaryString namespaceUri)
{
if (namespaceUri == null)
throw new ArgumentNullException ("namespaceUri");
return NamespaceURI == namespaceUri.Value;
}
public virtual bool IsStartArray (out Type type)
{
type = null;
return false;
}
public virtual bool IsStartElement (
XmlDictionaryString localName,
XmlDictionaryString namespaceUri)
{
if (localName == null)
throw new ArgumentNullException ("localName");
if (namespaceUri == null)
throw new ArgumentNullException ("namespaceUri");
return IsStartElement (localName.Value, namespaceUri.Value);
}
protected bool IsTextNode (XmlNodeType nodeType)
{
switch (nodeType) {
case XmlNodeType.Attribute: // wow, it isn't indeed.
case XmlNodeType.Text:
case XmlNodeType.CDATA:
case XmlNodeType.Whitespace:
case XmlNodeType.SignificantWhitespace:
return true;
default:
return false;
}
}
XmlException XmlError (string message)
{
IXmlLineInfo li = this as IXmlLineInfo;
if (li == null || !li.HasLineInfo ())
return new XmlException (message);
else
return new XmlException (String.Format ("{0} in {1} , at ({2},{3})", message, BaseURI, li.LineNumber, li.LinePosition));
}
public virtual void MoveToStartElement ()
{
MoveToContent ();
if (NodeType != XmlNodeType.Element)
throw XmlError (String.Format ("Element node is expected, but got {0} node.", NodeType));
}
public virtual void MoveToStartElement (string name)
{
if (name == null)
throw new ArgumentNullException ("name");
MoveToStartElement ();
if (Name != name)
throw XmlError (String.Format ("Element node '{0}' is expected, but got '{1}' element.", name, Name));
}
public virtual void MoveToStartElement (
string localName, string namespaceUri)
{
if (localName == null)
throw new ArgumentNullException ("localName");
if (namespaceUri == null)
throw new ArgumentNullException ("namespaceUri");
MoveToStartElement ();
if (LocalName != localName || NamespaceURI != namespaceUri)
throw XmlError (String.Format ("Element node '{0}' in namespace '{1}' is expected, but got '{2}' in namespace '{3}' element.", localName, namespaceUri, LocalName, NamespaceURI));
}
public virtual void MoveToStartElement (
XmlDictionaryString localName,
XmlDictionaryString namespaceUri)
{
if (localName == null)
throw new ArgumentNullException ("localName");
if (namespaceUri == null)
throw new ArgumentNullException ("namespaceUri");
MoveToStartElement (localName.Value, namespaceUri.Value);
}
public virtual void StartCanonicalization (
Stream stream, bool includeComments,
string [] inclusivePrefixes)
{
throw new NotSupportedException ();
}
public virtual bool TryGetArrayLength (out int count)
{
count = -1;
return false;
}
public virtual bool TryGetBase64ContentLength (out int count)
{
count = -1;
return false;
}
public virtual bool TryGetLocalNameAsDictionaryString (
out XmlDictionaryString localName)
{
localName = null;
return false;
}
public virtual bool TryGetNamespaceUriAsDictionaryString (
out XmlDictionaryString namespaceUri)
{
namespaceUri = null;
return false;
}
#region Content Reader Methods
public override object ReadContentAs (Type type, IXmlNamespaceResolver nsResolver)
{
return base.ReadContentAs (type, nsResolver);
}
public virtual byte [] ReadContentAsBase64 ()
{
int len;
if (!TryGetBase64ContentLength (out len))
return Convert.FromBase64String (ReadContentAsString ());
byte [] bytes = new byte [len];
ReadContentAsBase64 (bytes, 0, len);
return bytes;
}
MethodInfo xmlconv_from_bin_hex = typeof (XmlConvert).GetMethod ("FromBinHexString", BindingFlags.Static | BindingFlags.NonPublic, null, new Type [] {typeof (string)}, null);
byte [] FromBinHexString (string s)
{
return (byte []) xmlconv_from_bin_hex.Invoke (null, new object [] {s});
}
public virtual byte [] ReadContentAsBinHex ()
{
int len;
if (!TryGetArrayLength (out len))
return FromBinHexString (ReadContentAsString ());
return ReadContentAsBinHex (len);
}
protected byte [] ReadContentAsBinHex (int maxByteArrayContentLength)
{
byte [] bytes = new byte [maxByteArrayContentLength];
ReadContentAsBinHex (bytes, 0, maxByteArrayContentLength);
return bytes;
}
[MonoTODO]
public virtual int ReadContentAsChars (char [] chars, int offset, int count)
{
throw new NotImplementedException ();
}
public override decimal ReadContentAsDecimal ()
{
return base.ReadContentAsDecimal ();
}
public override float ReadContentAsFloat ()
{
return base.ReadContentAsFloat ();
}
public virtual Guid ReadContentAsGuid ()
{
return XmlConvert.ToGuid (ReadContentAsString ());
}
public virtual void ReadContentAsQualifiedName (out string localName, out string namespaceUri)
{
XmlQualifiedName qname = (XmlQualifiedName) ReadContentAs (typeof (XmlQualifiedName), this as IXmlNamespaceResolver);
localName = qname.Name;
namespaceUri = qname.Namespace;
}
public override string ReadContentAsString ()
{
return ReadContentAsString (Quotas.MaxStringContentLength);
}
[MonoTODO]
protected string ReadContentAsString (int maxStringContentLength)
{
return base.ReadContentAsString ();
}
[MonoTODO ("there is exactly no information on the web")]
public virtual string ReadContentAsString (string [] strings, out int index)
{
throw new NotImplementedException ();
}
[MonoTODO ("there is exactly no information on the web")]
public virtual string ReadContentAsString (XmlDictionaryString [] strings, out int index)
{
throw new NotImplementedException ();
}
public virtual TimeSpan ReadContentAsTimeSpan ()
{
return XmlConvert.ToTimeSpan (ReadContentAsString ());
}
public virtual UniqueId ReadContentAsUniqueId ()
{
return new UniqueId (ReadContentAsString ());
}
public virtual byte [] ReadElementContentAsBase64 ()
{
ReadStartElement ();
byte [] ret = ReadContentAsBase64 ();
ReadEndElement ();
return ret;
}
public virtual byte [] ReadElementContentAsBinHex ()
{
ReadStartElement ();
byte [] ret = ReadContentAsBinHex ();
ReadEndElement ();
return ret;
}
public virtual Guid ReadElementContentAsGuid ()
{
ReadStartElement ();
Guid ret = ReadContentAsGuid ();
ReadEndElement ();
return ret;
}
public virtual TimeSpan ReadElementContentAsTimeSpan ()
{
ReadStartElement ();
TimeSpan ret = ReadContentAsTimeSpan ();
ReadEndElement ();
return ret;
}
public virtual UniqueId ReadElementContentAsUniqueId ()
{
ReadStartElement ();
UniqueId ret = ReadContentAsUniqueId ();
ReadEndElement ();
return ret;
}
public override string ReadElementContentAsString ()
{
if (IsEmptyElement) {
Read ();
return String.Empty;
} else {
ReadStartElement ();
string s;
if (NodeType == XmlNodeType.EndElement)
s = String.Empty;
else
s = ReadContentAsString ();
ReadEndElement ();
return s;
}
}
public virtual void ReadFullStartElement ()
{
if (!IsStartElement ())
throw new XmlException ("Current node is not a start element");
ReadStartElement ();
}
public virtual void ReadFullStartElement (string name)
{
if (!IsStartElement (name))
throw new XmlException (String.Format ("Current node is not a start element '{0}'", name));
ReadStartElement (name);
}
public virtual void ReadFullStartElement (string localName, string namespaceUri)
{
if (!IsStartElement (localName, namespaceUri))
throw new XmlException (String.Format ("Current node is not a start element '{0}' in namesapce '{1}'", localName, namespaceUri));
ReadStartElement (localName, namespaceUri);
}
public virtual void ReadFullStartElement (XmlDictionaryString localName, XmlDictionaryString namespaceUri)
{
if (!IsStartElement (localName, namespaceUri))
throw new XmlException (String.Format ("Current node is not a start element '{0}' in namesapce '{1}'", localName, namespaceUri));
ReadStartElement (localName.Value, namespaceUri.Value);
}
public virtual void ReadStartElement (XmlDictionaryString localName, XmlDictionaryString namespaceUri)
{
if (localName == null)
throw new ArgumentNullException ("localName");
if (namespaceUri == null)
throw new ArgumentNullException ("namespaceUri");
ReadStartElement (localName.Value, namespaceUri.Value);
}
public override string ReadString ()
{
return ReadString (Quotas.MaxStringContentLength);
}
[MonoTODO]
protected string ReadString (int maxStringContentLength)
{
return base.ReadString ();
}
public virtual int ReadValueAsBase64 (byte [] bytes, int start, int length)
{
throw new NotSupportedException (); // as it is documented ...
}
public virtual bool TryGetValueAsDictionaryString (out XmlDictionaryString value)
{
throw new NotSupportedException (); // as documented
}
#endregion
#region Factory Methods
public static XmlDictionaryReader CreateBinaryReader (
byte [] buffer, XmlDictionaryReaderQuotas quotas)
{
return CreateBinaryReader (buffer, 0, buffer.Length, quotas);
}
public static XmlDictionaryReader CreateBinaryReader (
byte [] buffer, int offset, int count,
XmlDictionaryReaderQuotas quotas)
{
return CreateBinaryReader (buffer, offset, count, new XmlDictionary (), quotas);
}
public static XmlDictionaryReader CreateBinaryReader (
byte [] buffer, int offset, int count,
IXmlDictionary dictionary,
XmlDictionaryReaderQuotas quotas)
{
return CreateBinaryReader (buffer, offset, count,
dictionary, quotas,
new XmlBinaryReaderSession (), null);
}
public static XmlDictionaryReader CreateBinaryReader (
byte [] buffer, int offset, int count,
IXmlDictionary dictionary,
XmlDictionaryReaderQuotas quotas,
XmlBinaryReaderSession session)
{
return CreateBinaryReader (buffer, offset, count,
dictionary, quotas,
session, null);
}
public static XmlDictionaryReader CreateBinaryReader (
byte [] buffer, int offset, int count,
IXmlDictionary dictionary,
XmlDictionaryReaderQuotas quotas,
XmlBinaryReaderSession session,
OnXmlDictionaryReaderClose onClose)
{
return new XmlBinaryDictionaryReader (buffer,
offset, count,
dictionary, quotas, session, onClose);
}
public static XmlDictionaryReader CreateBinaryReader (
Stream stream, XmlDictionaryReaderQuotas quotas)
{
return CreateBinaryReader (stream, new XmlDictionary (), quotas);
}
public static XmlDictionaryReader CreateBinaryReader (
Stream stream, IXmlDictionary dictionary,
XmlDictionaryReaderQuotas quotas)
{
return CreateBinaryReader (stream, dictionary, quotas,
new XmlBinaryReaderSession (), null);
}
public static XmlDictionaryReader CreateBinaryReader (
Stream stream, IXmlDictionary dictionary,
XmlDictionaryReaderQuotas quotas,
XmlBinaryReaderSession session)
{
return CreateBinaryReader (stream, dictionary, quotas,
session, null);
}
public static XmlDictionaryReader CreateBinaryReader (
Stream stream, IXmlDictionary dictionary,
XmlDictionaryReaderQuotas quotas,
XmlBinaryReaderSession session,
OnXmlDictionaryReaderClose onClose)
{
return new XmlBinaryDictionaryReader (stream,
dictionary, quotas, session, onClose);
}
public static XmlDictionaryReader CreateDictionaryReader (
XmlReader reader)
{
return new XmlSimpleDictionaryReader (reader);
}
#if !NET_2_1
public static XmlDictionaryReader CreateMtomReader (
Stream stream, Encoding encoding,
XmlDictionaryReaderQuotas quotas)
{
return new XmlMtomDictionaryReader (stream, encoding, quotas);
}
public static XmlDictionaryReader CreateMtomReader (
Stream stream, Encoding [] encodings,
XmlDictionaryReaderQuotas quotas)
{
return CreateMtomReader (stream, encodings, null, quotas);
}
public static XmlDictionaryReader CreateMtomReader (
Stream stream, Encoding [] encodings, string contentType,
XmlDictionaryReaderQuotas quotas)
{
return CreateMtomReader (stream, encodings, contentType, quotas, int.MaxValue, null);
}
public static XmlDictionaryReader CreateMtomReader (
Stream stream, Encoding [] encodings, string contentType,
XmlDictionaryReaderQuotas quotas,
int maxBufferSize,
OnXmlDictionaryReaderClose onClose)
{
return new XmlMtomDictionaryReader (stream, encodings, contentType, quotas, maxBufferSize, onClose);
}
public static XmlDictionaryReader CreateMtomReader (
byte [] buffer, int offset, int count,
Encoding encoding, XmlDictionaryReaderQuotas quotas)
{
return CreateMtomReader (new MemoryStream (buffer, offset, count), encoding, quotas);
}
public static XmlDictionaryReader CreateMtomReader (
byte [] buffer, int offset, int count,
Encoding [] encodings, XmlDictionaryReaderQuotas quotas)
{
return CreateMtomReader (new MemoryStream (buffer, offset, count), encodings, quotas);
}
public static XmlDictionaryReader CreateMtomReader (
byte [] buffer, int offset, int count,
Encoding [] encodings, string contentType,
XmlDictionaryReaderQuotas quotas)
{
return CreateMtomReader (new MemoryStream (buffer, offset, count), encodings, contentType, quotas);
}
public static XmlDictionaryReader CreateMtomReader (
byte [] buffer, int offset, int count,
Encoding [] encodings, string contentType,
XmlDictionaryReaderQuotas quotas,
int maxBufferSize,
OnXmlDictionaryReaderClose onClose)
{
return CreateMtomReader (new MemoryStream (buffer, offset, count), encodings, contentType, quotas, maxBufferSize, onClose);
}
#endif
public static XmlDictionaryReader CreateTextReader (byte [] buffer, XmlDictionaryReaderQuotas quotas)
{
return CreateTextReader (buffer, 0, buffer.Length, quotas);
}
public static XmlDictionaryReader CreateTextReader (
byte [] buffer, int offset, int count,
XmlDictionaryReaderQuotas quotas)
{
return CreateTextReader (buffer, offset, count,
Encoding.UTF8, quotas, null);
}
public static XmlDictionaryReader CreateTextReader (
byte [] buffer, int offset, int count,
Encoding encoding,
XmlDictionaryReaderQuotas quotas,
OnXmlDictionaryReaderClose onClose)
{
return CreateTextReader (new MemoryStream (buffer, offset, count), encoding, quotas, onClose);
}
public static XmlDictionaryReader CreateTextReader (
Stream stream, XmlDictionaryReaderQuotas quotas)
{
return CreateTextReader (stream, Encoding.UTF8, quotas, null);
}
public static XmlDictionaryReader CreateTextReader (
Stream stream, Encoding encoding,
XmlDictionaryReaderQuotas quotas,
OnXmlDictionaryReaderClose onClose)
{
XmlReaderSettings s = new XmlReaderSettings ();
XmlNameTable nt = new NameTable ();
XmlParserContext c = new XmlParserContext (nt, new XmlNamespaceManager (nt), String.Empty, XmlSpace.None, encoding);
XmlDictionaryReader res = new XmlSimpleDictionaryReader (XmlReader.Create (stream, s, c), null, onClose);
res.quotas = quotas;
return res;
}
#endregion
}
}
|