2 // System.Xml.XmlNodeReader2.cs - splitted XmlNodeReader that manages entities.
5 // Duncan Mak (duncan@ximian.com)
6 // Atsushi Enomoto (ginga@kit.hi-ho.ne.jp)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Collections.Generic;
38 using System.Xml.Schema;
45 public class XmlNodeReader : XmlReader, IHasXmlParserContext, IXmlNamespaceResolver
47 public class XmlNodeReader : XmlReader, IHasXmlParserContext
51 XmlNodeReaderImpl source;
52 bool entityInsideAttribute;
57 public XmlNodeReader (XmlNode node)
59 source = new XmlNodeReaderImpl (node);
62 private XmlNodeReader (XmlNodeReaderImpl entityContainer, bool insideAttribute)
64 source = new XmlNodeReaderImpl (entityContainer);
65 this.entityInsideAttribute = insideAttribute;
72 private XmlReader Current {
73 get { return entity != null && entity.ReadState != ReadState.Initial ? entity : source; }
76 public override int AttributeCount {
77 get { return Current.AttributeCount; }
80 public override string BaseURI {
81 get { return Current.BaseURI; }
85 public override bool CanReadBinaryContent {
90 public override bool CanReadValueChunk {
95 internal override bool CanReadBinaryContent {
100 internal override bool CanReadValueChunk {
106 public override bool CanResolveEntity {
110 public override int Depth {
112 // On EndEntity, depth is the same as that
113 // of EntityReference.
114 if (entity != null && entity.ReadState == ReadState.Interactive)
115 return source.Depth + entity.Depth + 1;
121 public override bool EOF {
122 get { return source.EOF; }
125 public override bool HasAttributes {
126 get { return Current.HasAttributes; }
129 #if !NET_2_1 || MONOTOUCH
130 public override bool HasValue {
131 get { return Current.HasValue; }
135 public override bool IsDefault {
136 get { return Current.IsDefault; }
139 public override bool IsEmptyElement {
140 get { return Current.IsEmptyElement; }
145 public override string this [int i] {
146 get { return GetAttribute (i); }
149 public override string this [string name] {
150 get { return GetAttribute (name); }
153 public override string this [string name, string namespaceURI] {
154 get { return GetAttribute (name, namespaceURI); }
158 public override string LocalName {
159 get { return Current.LocalName; }
162 public override string Name {
163 get { return Current.Name; }
166 public override string NamespaceURI {
167 get { return Current.NamespaceURI; }
170 public override XmlNameTable NameTable {
171 get { return Current.NameTable; }
174 public override XmlNodeType NodeType {
177 return entity.ReadState == ReadState.Initial ?
179 entity.EOF ? XmlNodeType.EndEntity :
182 return source.NodeType;
186 XmlParserContext IHasXmlParserContext.ParserContext {
187 get { return ((IHasXmlParserContext) Current).ParserContext; }
190 public override string Prefix {
191 get { return Current.Prefix; }
196 public override char QuoteChar {
201 public override ReadState ReadState {
202 get { return entity != null ? ReadState.Interactive : source.ReadState; }
206 public override IXmlSchemaInfo SchemaInfo {
207 get { return entity != null ? entity.SchemaInfo : source.SchemaInfo; }
211 public override string Value {
212 get { return Current.Value; }
215 public override string XmlLang {
216 get { return Current.XmlLang; }
219 public override XmlSpace XmlSpace {
220 get { return Current.XmlSpace; }
226 // If current entityReference is a child of an attribute,
227 // then MoveToAttribute simply means that we no more need this entity Current.
228 // Otherwise, this invokation means that
229 // it is expected to move to resolved (maybe) element's attribute.
231 // This rule applies to many methods like MoveTo*Attribute().
233 public override void Close ()
240 public override string GetAttribute (int attributeIndex)
242 return Current.GetAttribute (attributeIndex);
245 public override string GetAttribute (string name)
247 return Current.GetAttribute (name);
250 public override string GetAttribute (string name, string namespaceURI)
252 return Current.GetAttribute (name, namespaceURI);
256 IDictionary<string, string> IXmlNamespaceResolver.GetNamespacesInScope (XmlNamespaceScope scope)
258 return ((IXmlNamespaceResolver) Current).GetNamespacesInScope (scope);
262 public override string LookupNamespace (string prefix)
264 return Current.LookupNamespace (prefix);
268 string IXmlNamespaceResolver.LookupPrefix (string ns)
270 return ((IXmlNamespaceResolver) Current).LookupPrefix (ns);
274 public override void MoveToAttribute (int i)
276 if (entity != null && entityInsideAttribute) {
280 Current.MoveToAttribute (i);
281 insideAttribute = true;
284 public override bool MoveToAttribute (string name)
286 if (entity != null && !entityInsideAttribute)
287 return entity.MoveToAttribute (name);
288 if (!source.MoveToAttribute (name))
290 if (entity != null && entityInsideAttribute) {
294 insideAttribute = true;
298 public override bool MoveToAttribute (string localName, string namespaceURI)
300 if (entity != null && !entityInsideAttribute)
301 return entity.MoveToAttribute (localName, namespaceURI);
302 if (!source.MoveToAttribute (localName, namespaceURI))
304 if (entity != null && entityInsideAttribute) {
308 insideAttribute = true;
312 public override bool MoveToElement ()
314 if (entity != null && entityInsideAttribute)
316 if (!Current.MoveToElement ())
318 insideAttribute = false;
322 public override bool MoveToFirstAttribute ()
324 if (entity != null && !entityInsideAttribute)
325 return entity.MoveToFirstAttribute ();
326 if (!source.MoveToFirstAttribute ())
328 if (entity != null && entityInsideAttribute) {
332 insideAttribute = true;
336 public override bool MoveToNextAttribute ()
338 if (entity != null && !entityInsideAttribute)
339 return entity.MoveToNextAttribute ();
340 if (!source.MoveToNextAttribute ())
342 if (entity != null && entityInsideAttribute) {
346 insideAttribute = true;
350 public override bool Read ()
352 insideAttribute = false;
353 if (entity != null && (entityInsideAttribute || entity.EOF))
355 if (entity != null) {
357 return true; // either success or EndEntity
360 return source.Read ();
363 public override bool ReadAttributeValue ()
365 if (entity != null && entityInsideAttribute) {
370 return true; // either success or EndEntity
373 return Current.ReadAttributeValue ();
377 public override int ReadContentAsBase64 (
378 byte [] buffer, int offset, int length)
380 // return base.ReadContentAsBase64 (
381 // buffer, offset, length);
382 // FIXME: This is problematic wrt end of entity.
384 return entity.ReadContentAsBase64 (
385 buffer, offset, length);
387 return source.ReadContentAsBase64 (
388 buffer, offset, length);
391 public override int ReadContentAsBinHex (
392 byte [] buffer, int offset, int length)
394 // return base.ReadContentAsBinHex (
395 // buffer, offset, length);
396 // FIXME: This is problematic wrt end of entity.
398 return entity.ReadContentAsBinHex (
399 buffer, offset, length);
401 return source.ReadContentAsBinHex (
402 buffer, offset, length);
405 public override int ReadElementContentAsBase64 (
406 byte [] buffer, int offset, int length)
408 // return base.ReadElementContentAsBase64 (
409 // buffer, offset, length);
410 // FIXME: This is problematic wrt end of entity.
412 return entity.ReadElementContentAsBase64 (
413 buffer, offset, length);
415 return source.ReadElementContentAsBase64 (
416 buffer, offset, length);
419 public override int ReadElementContentAsBinHex (
420 byte [] buffer, int offset, int length)
422 // return base.ReadElementContentAsBinHex (
423 // buffer, offset, length);
424 // FIXME: This is problematic wrt end of entity.
426 return entity.ReadElementContentAsBinHex (
427 buffer, offset, length);
429 return source.ReadElementContentAsBinHex (
430 buffer, offset, length);
434 public override string ReadString ()
436 return base.ReadString ();
439 #if !NET_2_1 || MONOTOUCH
440 public override void ResolveEntity ()
443 entity.ResolveEntity ();
445 if (source.NodeType != XmlNodeType.EntityReference)
446 throw new InvalidOperationException ("The current node is not an Entity Reference");
447 entity = new XmlNodeReader (source, insideAttribute);
452 public override void Skip ()
454 if (entity != null && entityInsideAttribute)