**** Merged from MCS ****
[mono-project.git] / mcs / class / System.XML / System.Xml / XmlReader.cs
blobe07594ca3b3cd20d66225c33fe549d16c52d86a1
1 //
2 // XmlReader.cs
3 //
4 // Authors:
5 // Jason Diamond (jason@injektilo.org)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // Atsushi Enomoto (ginga@kit.hi-ho.ne.jp)
8 //
9 // (C) 2001, 2002 Jason Diamond http://injektilo.org/
10 // (c) 2002 Ximian, Inc. (http://www.ximian.com)
11 // (C) 2003 Atsushi Enomoto
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 //
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 //
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Collections;
35 using System.IO;
36 using System.Text;
37 using System.Xml.Schema; // only required for NET_2_0 (SchemaInfo)
38 using System.Xml.Serialization; // only required for NET_2_0 (SchemaInfo)
39 using Mono.Xml; // only required for NET_2_0
41 namespace System.Xml
43 #if NET_2_0
44 public abstract class XmlReader : IDisposable
45 #else
46 public abstract class XmlReader
47 #endif
49 private StringBuilder readStringBuffer;
50 #if NET_2_0
51 private XmlReaderSettings settings;
52 #endif
54 #region Constructor
56 protected XmlReader ()
60 #endregion
62 #region Properties
64 public abstract int AttributeCount { get; }
66 public abstract string BaseURI { get; }
68 public virtual bool CanResolveEntity
70 get { return false; }
73 public abstract int Depth { get; }
75 public abstract bool EOF { get; }
77 public virtual bool HasAttributes
79 get { return AttributeCount > 0; }
82 public abstract bool HasValue { get; }
84 #if NET_2_0
85 public virtual bool IsDefault {
86 get { return false; }
89 public virtual bool IsEmptyElement {
90 get { return false; }
93 public virtual string this [int i] {
94 get { return GetAttribute (i); }
97 public virtual string this [string name] {
98 get { return GetAttribute (name); }
101 public virtual string this [string name, string namespaceURI] {
102 get { return GetAttribute (name, namespaceURI); }
104 #else
105 public abstract bool IsDefault { get; }
107 public abstract bool IsEmptyElement { get; }
109 public abstract string this [int i] { get; }
111 public abstract string this [string name] { get; }
113 public abstract string this [string localName, string namespaceName] { get; }
114 #endif
116 public abstract string LocalName { get; }
118 public abstract string Name { get; }
120 public abstract string NamespaceURI { get; }
122 public abstract XmlNameTable NameTable { get; }
124 public abstract XmlNodeType NodeType { get; }
126 public abstract string Prefix { get; }
128 #if NET_2_0
129 public virtual char QuoteChar {
130 get { return '\"'; }
132 #else
133 public abstract char QuoteChar { get; }
134 #endif
136 public abstract ReadState ReadState { get; }
138 #if NET_2_0
139 public virtual IXmlSchemaInfo SchemaInfo {
140 get { return null; }
143 public virtual XmlReaderSettings Settings {
144 get { return settings; }
146 #endif
148 public abstract string Value { get; }
150 #if NET_2_0
151 public virtual string XmlLang {
152 get { return String.Empty; }
155 public virtual XmlSpace XmlSpace {
156 get { return XmlSpace.None; }
158 #else
159 public abstract string XmlLang { get; }
161 public abstract XmlSpace XmlSpace { get; }
162 #endif
164 #endregion
166 #region Methods
168 public abstract void Close ();
170 #if NET_2_0
171 public static XmlReader Create (Stream stream)
173 return Create (stream, null, null, new XmlUrlResolver (), null);
176 public static XmlReader Create (string url)
178 return Create (url, null);
181 public static XmlReader Create (TextReader reader)
183 return Create (reader, null, new XmlUrlResolver (), null);
186 public static XmlReader Create (string url, XmlReaderSettings settings)
188 return Create (url, null, new XmlUrlResolver (), settings);
191 public static XmlReader Create (XmlReader reader, XmlReaderSettings settings)
193 return Create (reader, new XmlUrlResolver (), settings);
196 [MonoTODO ("ConformanceLevel, IgnoreSchemaXXX etc.")]
197 public static XmlReader Create (XmlReader reader, XmlResolver resolver, XmlReaderSettings settings)
199 return CreateFilteredXmlReader (reader, resolver, settings);
202 [MonoTODO ("ConformanceLevel, IgnoreSchemaXXX etc.; Encoding")]
203 public static XmlReader Create (string url, Encoding encoding, XmlResolver resolver, XmlReaderSettings settings)
205 return CreateCustomizedTextReader (new XmlTextReader (url, settings != null ? settings.NameTable : null), resolver, settings);
208 [MonoTODO ("ConformanceLevel, IgnoreSchemaXXX etc.")]
209 public static XmlReader Create (TextReader reader, string baseUri, XmlResolver resolver, XmlReaderSettings settings)
211 return CreateCustomizedTextReader (new XmlTextReader (baseUri, reader, settings != null ? settings.NameTable : null), resolver, settings);
214 [MonoTODO ("ConformanceLevel, IgnoreSchemaXXX etc.")]
215 public static XmlReader Create (Stream stream, string baseUri, Encoding encoding, XmlResolver resolver, XmlReaderSettings settings)
217 XmlNameTable nameTable = settings != null ? settings.NameTable : null;
218 return CreateCustomizedTextReader (
219 encoding == null ?
220 new XmlTextReader (baseUri, stream, nameTable) :
221 new XmlTextReader (baseUri, new StreamReader (stream, encoding), nameTable),
222 resolver,
223 settings);
226 private static XmlReader CreateCustomizedTextReader (XmlTextReader reader, XmlResolver resolver, XmlReaderSettings settings)
228 reader.XmlResolver = resolver;
229 // Normalization is set true by default.
230 reader.Normalization = true;
232 if (settings == null)
233 settings = new XmlReaderSettings ();
235 if (settings.ProhibitDtd)
236 reader.ProhibitDtd = true;
238 if (!settings.CheckCharacters)
239 reader.CharacterChecking = false;
241 // I guess it might be changed in 2.0 RTM to set true
242 // as default, or just disappear. It goes against
243 // XmlTextReader's default usage and users will have
244 // to close input manually (that's annoying). Moreover,
245 // MS XmlTextReader consumes text input more than
246 // actually read and users can acquire those extra
247 // consumption by GetRemainder() that returns different
248 // TextReader.
249 reader.CloseInput = settings.CloseInput;
251 // I would like to support it in detail later;
252 // MSDN description looks source of confusion. We don't
253 // need examples, but precise list of how it works.
254 reader.Conformance = settings.ConformanceLevel;
256 reader.AdjustLineInfoOffset (settings.LineNumberOffset,
257 settings.LinePositionOffset);
259 // FIXME: maybe we had better create XmlParserContext.
260 if (settings.NameTable != null)
261 reader.SetNameTable (settings.NameTable);
263 return CreateFilteredXmlReader (reader, resolver, settings);
266 private static XmlReader CreateFilteredXmlReader (XmlReader reader, XmlResolver resolver, XmlReaderSettings settings)
268 reader = CreateValidatingXmlReader (reader, settings);
270 if (reader.Settings != null ||
271 settings.IgnoreComments ||
272 settings.IgnoreProcessingInstructions ||
273 settings.IgnoreWhitespace)
274 return new XmlFilterReader (reader, settings);
275 else {
276 reader.settings = settings;
277 return reader;
281 private static XmlReader CreateValidatingXmlReader (XmlReader reader, XmlReaderSettings settings)
283 XmlValidatingReader xvr = null;
284 if (settings.DtdValidate) {
285 xvr = new XmlValidatingReader (reader);
286 if (!settings.XsdValidate)
287 xvr.ValidationType = ValidationType.DTD;
288 // otherwise .Auto by default.
289 } else if (settings.XsdValidate) {
290 xvr = new XmlValidatingReader (reader);
291 xvr.ValidationType = ValidationType.Schema;
293 if (xvr != null)
294 xvr.SetSchemas (settings.Schemas);
296 if (settings.IgnoreIdentityConstraints)
297 throw new NotImplementedException ();
298 if (!settings.IgnoreInlineSchema)
299 throw new NotImplementedException ();
300 if (!settings.IgnoreSchemaLocation)
301 throw new NotImplementedException ();
302 if (!settings.IgnoreValidationWarnings)
303 throw new NotImplementedException ();
305 return xvr != null ? xvr : reader;
307 #endif
309 #if NET_2_0
310 public virtual void Dispose ()
312 if (ReadState != ReadState.Closed)
313 Close ();
315 #endif
317 public abstract string GetAttribute (int i);
319 public abstract string GetAttribute (string name);
321 public abstract string GetAttribute (
322 string localName,
323 string namespaceName);
325 public static bool IsName (string s)
327 return s != null && XmlChar.IsName (s);
330 public static bool IsNameToken (string s)
332 return s != null && XmlChar.IsNmToken (s);
335 public virtual bool IsStartElement ()
337 return (MoveToContent () == XmlNodeType.Element);
340 public virtual bool IsStartElement (string name)
342 if (!IsStartElement ())
343 return false;
345 return (Name == name);
348 public virtual bool IsStartElement (string localName, string namespaceName)
350 if (!IsStartElement ())
351 return false;
353 return (LocalName == localName && NamespaceURI == namespaceName);
356 public abstract string LookupNamespace (string prefix);
358 #if NET_2_0
359 public virtual string LookupNamespace (string prefix, bool atomizedNames)
360 #else
361 internal virtual string LookupNamespace (string prefix, bool atomizedNames)
362 #endif
364 return LookupNamespace (prefix);
367 public abstract void MoveToAttribute (int i);
369 public abstract bool MoveToAttribute (string name);
371 public abstract bool MoveToAttribute (
372 string localName,
373 string namespaceName);
375 private bool IsContent (XmlNodeType nodeType)
377 /* MS doc says:
378 * (non-white space text, CDATA, Element, EndElement, EntityReference, or EndEntity)
380 switch (nodeType) {
381 case XmlNodeType.Text:
382 return true;
383 case XmlNodeType.CDATA:
384 return true;
385 case XmlNodeType.Element:
386 return true;
387 case XmlNodeType.EndElement:
388 return true;
389 case XmlNodeType.EntityReference:
390 return true;
391 case XmlNodeType.EndEntity:
392 return true;
395 return false;
398 public virtual XmlNodeType MoveToContent ()
400 if (NodeType == XmlNodeType.Attribute)
401 MoveToElement ();
403 do {
404 if (IsContent (NodeType))
405 return NodeType;
406 Read ();
407 } while (!EOF);
408 return XmlNodeType.None;
411 public abstract bool MoveToElement ();
413 public abstract bool MoveToFirstAttribute ();
415 public abstract bool MoveToNextAttribute ();
417 public abstract bool Read ();
419 public abstract bool ReadAttributeValue ();
421 public virtual string ReadElementString ()
423 if (MoveToContent () != XmlNodeType.Element) {
424 string error = String.Format ("'{0}' is an invalid node type.",
425 NodeType.ToString ());
426 throw XmlError (error);
429 string result = String.Empty;
430 if (!IsEmptyElement) {
431 Read ();
432 result = ReadString ();
433 if (NodeType != XmlNodeType.EndElement) {
434 string error = String.Format ("'{0}' is an invalid node type.",
435 NodeType.ToString ());
436 throw XmlError (error);
440 Read ();
441 return result;
444 public virtual string ReadElementString (string name)
446 if (MoveToContent () != XmlNodeType.Element) {
447 string error = String.Format ("'{0}' is an invalid node type.",
448 NodeType.ToString ());
449 throw XmlError (error);
452 if (name != Name) {
453 string error = String.Format ("The {0} tag from namespace {1} is expected.",
454 Name, NamespaceURI);
455 throw XmlError (error);
458 string result = String.Empty;
459 if (!IsEmptyElement) {
460 Read ();
461 result = ReadString ();
462 if (NodeType != XmlNodeType.EndElement) {
463 string error = String.Format ("'{0}' is an invalid node type.",
464 NodeType.ToString ());
465 throw XmlError (error);
469 Read ();
470 return result;
473 public virtual string ReadElementString (string localName, string namespaceName)
475 if (MoveToContent () != XmlNodeType.Element) {
476 string error = String.Format ("'{0}' is an invalid node type.",
477 NodeType.ToString ());
478 throw XmlError (error);
481 if (localName != LocalName || NamespaceURI != namespaceName) {
482 string error = String.Format ("The {0} tag from namespace {1} is expected.",
483 LocalName, NamespaceURI);
484 throw XmlError (error);
487 string result = String.Empty;
488 if (!IsEmptyElement) {
489 Read ();
490 result = ReadString ();
491 if (NodeType != XmlNodeType.EndElement) {
492 string error = String.Format ("'{0}' is an invalid node type.",
493 NodeType.ToString ());
494 throw XmlError (error);
498 Read ();
499 return result;
502 public virtual void ReadEndElement ()
504 if (MoveToContent () != XmlNodeType.EndElement) {
505 string error = String.Format ("'{0}' is an invalid node type.",
506 NodeType.ToString ());
507 throw XmlError (error);
510 Read ();
513 #if NET_1_0
514 public abstract string ReadInnerXml ();
516 public abstract string ReadOuterXml ();
518 #else
519 public virtual string ReadInnerXml ()
521 return ReadInnerXmlInternal ();
524 public virtual string ReadOuterXml ()
526 return ReadOuterXmlInternal ();
528 #endif
530 internal string ReadInnerXmlInternal ()
532 if (ReadState != ReadState.Interactive || NodeType == XmlNodeType.EndElement)
533 return String.Empty;
535 StringWriter sw = new StringWriter ();
536 XmlTextWriter xtw = new XmlTextWriter (sw);
537 if (NodeType == XmlNodeType.Element) {
538 if (IsEmptyElement) {
539 Read ();
540 return String.Empty;
542 int startDepth = Depth;
543 Read ();
544 while (startDepth < Depth) {
545 if (ReadState != ReadState.Interactive)
546 throw XmlError ("Unexpected end of the XML reader.");
547 xtw.WriteNode (this, false);
549 // reader is now end element, then proceed once more.
550 Read ();
552 else
553 xtw.WriteNode (this, false);
555 return sw.ToString ();
558 internal string ReadOuterXmlInternal ()
560 if (ReadState != ReadState.Interactive || NodeType == XmlNodeType.EndElement)
561 return String.Empty;
563 StringWriter sw = new StringWriter ();
564 XmlTextWriter xtw = new XmlTextWriter (sw);
565 xtw.WriteNode (this, false);
566 return sw.ToString ();
569 public virtual void ReadStartElement ()
571 if (MoveToContent () != XmlNodeType.Element) {
572 string error = String.Format ("'{0}' is an invalid node type.",
573 NodeType.ToString ());
574 throw XmlError (error);
577 Read ();
580 public virtual void ReadStartElement (string name)
582 if (MoveToContent () != XmlNodeType.Element) {
583 string error = String.Format ("'{0}' is an invalid node type.",
584 NodeType.ToString ());
585 throw XmlError (error);
588 if (name != Name) {
589 string error = String.Format ("The {0} tag from namespace {1} is expected.",
590 Name, NamespaceURI);
591 throw XmlError (error);
594 Read ();
597 public virtual void ReadStartElement (string localName, string namespaceName)
599 if (MoveToContent () != XmlNodeType.Element) {
600 string error = String.Format ("'{0}' is an invalid node type.",
601 NodeType.ToString ());
602 throw XmlError (error);
605 if (localName != LocalName || NamespaceURI != namespaceName) {
606 string error = String.Format ("Expecting {0} tag from namespace {1}, got {2} and {3} instead",
607 localName, namespaceName,
608 LocalName, NamespaceURI);
609 throw XmlError (error);
612 Read ();
615 #if NET_1_0
616 public abstract string ReadString ();
617 #else
618 public virtual string ReadString ()
620 return ReadStringInternal ();
622 #endif
624 internal string ReadStringInternal ()
626 if (readStringBuffer == null)
627 readStringBuffer = new StringBuilder ();
628 readStringBuffer.Length = 0;
630 MoveToElement ();
632 switch (NodeType) {
633 default:
634 return String.Empty;
635 case XmlNodeType.Element:
636 if (IsEmptyElement)
637 return String.Empty;
638 do {
639 Read ();
640 switch (NodeType) {
641 case XmlNodeType.Text:
642 case XmlNodeType.CDATA:
643 case XmlNodeType.Whitespace:
644 case XmlNodeType.SignificantWhitespace:
645 readStringBuffer.Append (Value);
646 continue;
648 break;
649 } while (true);
650 break;
651 case XmlNodeType.Text:
652 case XmlNodeType.CDATA:
653 case XmlNodeType.Whitespace:
654 case XmlNodeType.SignificantWhitespace:
655 do {
656 switch (NodeType) {
657 case XmlNodeType.Text:
658 case XmlNodeType.CDATA:
659 case XmlNodeType.Whitespace:
660 case XmlNodeType.SignificantWhitespace:
661 readStringBuffer.Append (Value);
662 Read ();
663 continue;
665 break;
666 } while (true);
667 break;
669 string ret = readStringBuffer.ToString ();
670 readStringBuffer.Length = 0;
671 return ret;
674 #if NET_2_0
675 public virtual Type ValueType {
676 get { return typeof (string); }
679 [MonoTODO]
680 public virtual bool ReadToDescendant (string name)
682 if (NodeType != XmlNodeType.Element || IsEmptyElement)
683 return false;
684 int depth = Depth;
685 for (Read (); depth < Depth; Read ())
686 if (NodeType == XmlNodeType.Element && name == Name)
687 return true;
688 return false;
691 [MonoTODO]
692 public virtual bool ReadToDescendant (string localName, string namespaceURI)
694 if (NodeType != XmlNodeType.Element || IsEmptyElement)
695 return false;
696 int depth = Depth;
697 for (Read (); depth < Depth; Read ())
698 if (NodeType == XmlNodeType.Element && localName == LocalName && namespaceURI == NamespaceURI)
699 return true;
700 return false;
703 [MonoTODO]
704 public virtual bool ReadToNextSibling (string name)
706 if (NodeType != XmlNodeType.Element || IsEmptyElement)
707 return false;
708 int depth = Depth;
709 for (Skip (); depth < Depth; Skip ())
710 if (NodeType == XmlNodeType.Element && name == Name)
711 return true;
712 return false;
715 [MonoTODO]
716 public virtual bool ReadToNextSibling (string localName, string namespaceURI)
718 if (NodeType != XmlNodeType.Element || IsEmptyElement)
719 return false;
720 int depth = Depth;
721 for (Skip (); depth < Depth; Skip ())
722 if (NodeType == XmlNodeType.Element && localName == LocalName && namespaceURI == NamespaceURI)
723 return true;
724 return false;
727 [MonoTODO]
728 public virtual XmlReader ReadSubtree ()
730 return new SubtreeXmlReader (this);
733 [MonoTODO]
734 public virtual object ReadTypedValue ()
736 return ReadValueAs (ValueType);
739 [MonoTODO]
740 public virtual object ReadValueAs (Type type)
742 return ReadValueAs (type, this as IXmlNamespaceResolver);
745 [MonoTODO]
746 public virtual object ReadValueAs (Type type, IXmlNamespaceResolver resolver)
748 string text = ReadString ();
749 try {
750 if (type == typeof (XmlQualifiedName))
751 return XmlQualifiedName.Parse (text, resolver);
753 switch (Type.GetTypeCode (type)) {
754 case TypeCode.Boolean:
755 return ReadValueAsBoolean ();
756 case TypeCode.DateTime:
757 return ReadValueAsDateTime ();
758 case TypeCode.Decimal:
759 return ReadValueAsDecimal ();
760 case TypeCode.Double:
761 return ReadValueAsDouble ();
762 case TypeCode.Int32:
763 return ReadValueAsInt32 ();
764 case TypeCode.Int64:
765 return ReadValueAsInt64 ();
766 case TypeCode.Single:
767 return ReadValueAsSingle ();
768 case TypeCode.String:
769 return ReadValueAsString ();
771 } catch (Exception ex) {
772 return new FormatException (String.Format ("Current text value '{0}' is not acceptable for specified type '{1}'.", text, type));
774 throw new ArgumentException (String.Format ("Specified type '{0}' is not supported.", type));
777 [MonoTODO]
778 public virtual bool ReadValueAsBoolean ()
780 return XQueryConvert.StringToBoolean (ReadString ());
783 [MonoTODO]
784 public virtual DateTime ReadValueAsDateTime ()
786 return XQueryConvert.StringToDateTime (ReadString ());
789 [MonoTODO]
790 public virtual decimal ReadValueAsDecimal ()
792 return XQueryConvert.StringToDecimal (ReadString ());
795 [MonoTODO]
796 public virtual double ReadValueAsDouble ()
798 return XQueryConvert.StringToDouble (ReadString ());
801 [MonoTODO]
802 public virtual int ReadValueAsInt32 ()
804 return XQueryConvert.StringToInt (ReadString ());
807 [MonoTODO]
808 public virtual long ReadValueAsInt64 ()
810 return XQueryConvert.StringToInteger (ReadString ());
813 [MonoTODO]
814 public virtual ICollection ReadValueAsList ()
816 throw new NotImplementedException ();
819 [MonoTODO]
820 public virtual float ReadValueAsSingle ()
822 return XQueryConvert.StringToFloat (ReadString ());
825 [MonoTODO]
826 public virtual string ReadValueAsString ()
828 return ReadString ();
830 #endif
832 public abstract void ResolveEntity ();
834 public virtual void Skip ()
836 if (ReadState != ReadState.Interactive)
837 return;
839 MoveToElement ();
840 if (NodeType != XmlNodeType.Element || IsEmptyElement) {
841 Read ();
842 return;
845 int depth = Depth;
846 while (Read() && depth < Depth);
847 if (NodeType == XmlNodeType.EndElement)
848 Read ();
851 private XmlException XmlError (string message)
853 return new XmlException (this as IXmlLineInfo, BaseURI, message);
856 #endregion