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 public override bool HasValue {
130 get { return Current.HasValue; }
133 public override bool IsDefault {
134 get { return Current.IsDefault; }
137 public override bool IsEmptyElement {
138 get { return Current.IsEmptyElement; }
143 public override string this [int i] {
144 get { return GetAttribute (i); }
147 public override string this [string name] {
148 get { return GetAttribute (name); }
151 public override string this [string name, string namespaceURI] {
152 get { return GetAttribute (name, namespaceURI); }
156 public override string LocalName {
157 get { return Current.LocalName; }
160 public override string Name {
161 get { return Current.Name; }
164 public override string NamespaceURI {
165 get { return Current.NamespaceURI; }
168 public override XmlNameTable NameTable {
169 get { return Current.NameTable; }
172 public override XmlNodeType NodeType {
175 return entity.ReadState == ReadState.Initial ?
177 entity.EOF ? XmlNodeType.EndEntity :
180 return source.NodeType;
184 XmlParserContext IHasXmlParserContext.ParserContext {
185 get { return ((IHasXmlParserContext) Current).ParserContext; }
188 public override string Prefix {
189 get { return Current.Prefix; }
194 public override char QuoteChar {
199 public override ReadState ReadState {
200 get { return entity != null ? ReadState.Interactive : source.ReadState; }
204 public override IXmlSchemaInfo SchemaInfo {
205 get { return entity != null ? entity.SchemaInfo : source.SchemaInfo; }
209 public override string Value {
210 get { return Current.Value; }
213 public override string XmlLang {
214 get { return Current.XmlLang; }
217 public override XmlSpace XmlSpace {
218 get { return Current.XmlSpace; }
224 // If current entityReference is a child of an attribute,
225 // then MoveToAttribute simply means that we no more need this entity Current.
226 // Otherwise, this invokation means that
227 // it is expected to move to resolved (maybe) element's attribute.
229 // This rule applies to many methods like MoveTo*Attribute().
231 public override void Close ()
238 public override string GetAttribute (int attributeIndex)
240 return Current.GetAttribute (attributeIndex);
243 public override string GetAttribute (string name)
245 return Current.GetAttribute (name);
248 public override string GetAttribute (string name, string namespaceURI)
250 return Current.GetAttribute (name, namespaceURI);
254 IDictionary<string, string> IXmlNamespaceResolver.GetNamespacesInScope (XmlNamespaceScope scope)
256 return ((IXmlNamespaceResolver) Current).GetNamespacesInScope (scope);
260 public override string LookupNamespace (string prefix)
262 return Current.LookupNamespace (prefix);
266 string IXmlNamespaceResolver.LookupPrefix (string ns)
268 return ((IXmlNamespaceResolver) Current).LookupPrefix (ns);
272 public override void MoveToAttribute (int i)
274 if (entity != null && entityInsideAttribute) {
278 Current.MoveToAttribute (i);
279 insideAttribute = true;
282 public override bool MoveToAttribute (string name)
284 if (entity != null && !entityInsideAttribute)
285 return entity.MoveToAttribute (name);
286 if (!source.MoveToAttribute (name))
288 if (entity != null && entityInsideAttribute) {
292 insideAttribute = true;
296 public override bool MoveToAttribute (string localName, string namespaceURI)
298 if (entity != null && !entityInsideAttribute)
299 return entity.MoveToAttribute (localName, namespaceURI);
300 if (!source.MoveToAttribute (localName, namespaceURI))
302 if (entity != null && entityInsideAttribute) {
306 insideAttribute = true;
310 public override bool MoveToElement ()
312 if (entity != null && entityInsideAttribute)
314 if (!Current.MoveToElement ())
316 insideAttribute = false;
320 public override bool MoveToFirstAttribute ()
322 if (entity != null && !entityInsideAttribute)
323 return entity.MoveToFirstAttribute ();
324 if (!source.MoveToFirstAttribute ())
326 if (entity != null && entityInsideAttribute) {
330 insideAttribute = true;
334 public override bool MoveToNextAttribute ()
336 if (entity != null && !entityInsideAttribute)
337 return entity.MoveToNextAttribute ();
338 if (!source.MoveToNextAttribute ())
340 if (entity != null && entityInsideAttribute) {
344 insideAttribute = true;
348 public override bool Read ()
350 insideAttribute = false;
351 if (entity != null && (entityInsideAttribute || entity.EOF))
353 if (entity != null) {
355 return true; // either success or EndEntity
358 return source.Read ();
361 public override bool ReadAttributeValue ()
363 if (entity != null && entityInsideAttribute) {
368 return true; // either success or EndEntity
371 return Current.ReadAttributeValue ();
375 public override int ReadContentAsBase64 (
376 byte [] buffer, int offset, int length)
378 // return base.ReadContentAsBase64 (
379 // buffer, offset, length);
380 // FIXME: This is problematic wrt end of entity.
382 return entity.ReadContentAsBase64 (
383 buffer, offset, length);
385 return source.ReadContentAsBase64 (
386 buffer, offset, length);
389 public override int ReadContentAsBinHex (
390 byte [] buffer, int offset, int length)
392 // return base.ReadContentAsBinHex (
393 // buffer, offset, length);
394 // FIXME: This is problematic wrt end of entity.
396 return entity.ReadContentAsBinHex (
397 buffer, offset, length);
399 return source.ReadContentAsBinHex (
400 buffer, offset, length);
403 public override int ReadElementContentAsBase64 (
404 byte [] buffer, int offset, int length)
406 // return base.ReadElementContentAsBase64 (
407 // buffer, offset, length);
408 // FIXME: This is problematic wrt end of entity.
410 return entity.ReadElementContentAsBase64 (
411 buffer, offset, length);
413 return source.ReadElementContentAsBase64 (
414 buffer, offset, length);
417 public override int ReadElementContentAsBinHex (
418 byte [] buffer, int offset, int length)
420 // return base.ReadElementContentAsBinHex (
421 // buffer, offset, length);
422 // FIXME: This is problematic wrt end of entity.
424 return entity.ReadElementContentAsBinHex (
425 buffer, offset, length);
427 return source.ReadElementContentAsBinHex (
428 buffer, offset, length);
432 public override string ReadString ()
434 return base.ReadString ();
437 public override void ResolveEntity ()
440 entity.ResolveEntity ();
442 if (source.NodeType != XmlNodeType.EntityReference)
443 throw new InvalidOperationException ("The current node is not an Entity Reference");
444 entity = new XmlNodeReader (source, insideAttribute);
448 public override void Skip ()
450 if (entity != null && entityInsideAttribute)