2 // EntityResolvingXmlReader.cs - XmlReader that handles entity resolution
5 // Atsushi Enomoto (ginga@kit.hi-ho.ne.jp)
7 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections.Generic;
33 using System.Globalization;
35 using System.Security.Permissions;
37 using System.Xml.Schema;
42 [PermissionSet (SecurityAction.InheritanceDemand, Unrestricted = true)]
43 internal class EntityResolvingXmlReader : XmlReader,
45 IXmlNamespaceResolver,
47 IXmlLineInfo, IHasXmlParserContext
49 EntityResolvingXmlReader entity;
51 XmlParserContext context;
53 EntityHandling entity_handling;
54 bool entity_inside_attr;
58 public EntityResolvingXmlReader (XmlReader source, XmlParserContext context)
61 this.context = context;
64 EntityResolvingXmlReader (XmlReader entityContainer,
67 source = entityContainer;
68 this.entity_inside_attr = inside_attr;
73 private XmlReader Current {
74 get { return entity != null && entity.ReadState != ReadState.Initial ? (XmlReader) entity : source; }
79 public override string this [int i] {
80 get { return GetAttribute (i); }
83 public override string this [string name] {
84 get { return GetAttribute (name); }
87 public override string this [string localName, string namespaceName] {
88 get { return GetAttribute (localName, namespaceName); }
92 public override int AttributeCount {
93 get { return Current.AttributeCount; }
96 public override string BaseURI {
97 get { return Current.BaseURI; }
100 public override bool CanResolveEntity {
104 public override int Depth {
106 // On EndEntity, depth is the same as that
107 // of EntityReference.
108 if (entity != null && entity.ReadState == ReadState.Interactive)
109 return source.Depth + entity.Depth + 1;
115 public override bool EOF {
116 get { return source.EOF; }
120 public override bool HasValue {
121 get { return Current.HasValue; }
125 public override bool IsDefault {
126 get { return Current.IsDefault; }
129 public override bool IsEmptyElement {
130 get { return Current.IsEmptyElement; }
133 public override string LocalName {
134 get { return Current.LocalName; }
137 public override string Name {
138 get { return Current.Name; }
141 public override string NamespaceURI {
142 get { return Current.NamespaceURI; }
145 public override XmlNameTable NameTable {
146 get { return Current.NameTable; }
149 public override XmlNodeType NodeType {
151 if (entity != null) {
152 if (entity.ReadState == ReadState.Initial)
153 return source.NodeType;
154 return entity.EOF ? XmlNodeType.EndEntity : entity.NodeType;
156 return source.NodeType;
160 internal XmlParserContext ParserContext {
161 get { return context; }
164 XmlParserContext IHasXmlParserContext.ParserContext {
165 get { return context; }
168 public override string Prefix {
169 get { return Current.Prefix; }
172 public override char QuoteChar {
173 get { return Current.QuoteChar; }
176 public override ReadState ReadState {
177 get { return entity != null ? ReadState.Interactive : source.ReadState; }
180 public override string Value {
181 get { return Current.Value; }
184 public override string XmlLang {
185 get { return Current.XmlLang; }
188 public override XmlSpace XmlSpace {
189 get { return Current.XmlSpace; }
194 private void CopyProperties (EntityResolvingXmlReader other)
196 context = other.context;
197 resolver = other.resolver;
198 entity_handling = other.entity_handling;
203 public EntityHandling EntityHandling {
204 get { return entity_handling; }
207 entity.EntityHandling = value;
208 entity_handling = value;
212 public int LineNumber {
214 IXmlLineInfo li = Current as IXmlLineInfo;
215 return li == null ? 0 : li.LineNumber;
219 public int LinePosition {
221 IXmlLineInfo li = Current as IXmlLineInfo;
222 return li == null ? 0 : li.LinePosition;
226 public XmlResolver XmlResolver {
229 entity.XmlResolver = value;
240 public override void Close ()
247 public override string GetAttribute (int i)
249 return Current.GetAttribute (i);
252 // MS.NET 1.0 msdn says that this method returns String.Empty
253 // for absent attribute, but in fact it returns null.
254 // This description is corrected in MS.NET 1.1 msdn.
255 public override string GetAttribute (string name)
257 return Current.GetAttribute (name);
260 public override string GetAttribute (string localName, string namespaceURI)
262 return Current.GetAttribute (localName, namespaceURI);
266 public IDictionary<string, string> GetNamespacesInScope (XmlNamespaceScope scope)
268 return ((IXmlNamespaceResolver) Current).GetNamespacesInScope (scope);
271 IDictionary<string, string> IXmlNamespaceResolver.GetNamespacesInScope (XmlNamespaceScope scope)
273 return GetNamespacesInScope (scope);
276 string IXmlNamespaceResolver.LookupPrefix (string ns)
278 return ((IXmlNamespaceResolver) Current).LookupPrefix (ns);
282 public override string LookupNamespace (string prefix)
284 return Current.LookupNamespace (prefix);
287 public override void MoveToAttribute (int i)
289 if (entity != null && entity_inside_attr) {
293 Current.MoveToAttribute (i);
297 public override bool MoveToAttribute (string name)
299 if (entity != null && !entity_inside_attr)
300 return entity.MoveToAttribute (name);
301 if (!source.MoveToAttribute (name))
303 if (entity != null && entity_inside_attr) {
311 public override bool MoveToAttribute (string localName, string namespaceName)
313 if (entity != null && !entity_inside_attr)
314 return entity.MoveToAttribute (localName, namespaceName);
315 if (!source.MoveToAttribute (localName, namespaceName))
317 if (entity != null && entity_inside_attr) {
325 public override bool MoveToElement ()
327 if (entity != null && entity_inside_attr) {
331 if (!Current.MoveToElement ())
337 public override bool MoveToFirstAttribute ()
339 if (entity != null && !entity_inside_attr)
340 return entity.MoveToFirstAttribute ();
341 if (!source.MoveToFirstAttribute ())
343 if (entity != null && entity_inside_attr) {
351 public override bool MoveToNextAttribute ()
353 if (entity != null && !entity_inside_attr)
354 return entity.MoveToNextAttribute ();
355 if (!source.MoveToNextAttribute ())
357 if (entity != null && entity_inside_attr) {
365 public override bool Read ()
374 if (entity != null && (entity_inside_attr || entity.EOF)) {
378 if (entity != null) {
381 if (EntityHandling == EntityHandling.ExpandEntities) {
382 // EndEntity must be skipped
388 return true; // either success or EndEntity
393 if (EntityHandling == EntityHandling.ExpandEntities
394 && source.NodeType == XmlNodeType.EntityReference) {
403 public override bool ReadAttributeValue ()
405 if (entity != null && entity_inside_attr) {
412 return true; // either success or EndEntity
415 return Current.ReadAttributeValue ();
419 public override string ReadString ()
421 return base.ReadString ();
425 void ResolveEntity ()
434 void DoResolveEntity ()
437 entity.ResolveEntity ();
439 if (source.NodeType != XmlNodeType.EntityReference)
440 throw new InvalidOperationException ("The current node is not an Entity Reference");
441 if (ParserContext.Dtd == null)
442 throw new XmlException (this as IXmlLineInfo, this.BaseURI, String.Format ("Cannot resolve entity without DTD: '{0}'", source.Name));
443 XmlReader entReader = ParserContext.Dtd.GenerateEntityContentReader (
444 source.Name, ParserContext);
445 if (entReader == null)
446 throw new XmlException (this as IXmlLineInfo, this.BaseURI, String.Format ("Reference to undeclared entity '{0}'.", source.Name));
448 entity = new EntityResolvingXmlReader (
449 entReader, inside_attr);
450 entity.CopyProperties (this);
454 public override void Skip ()
459 public bool HasLineInfo ()
461 IXmlLineInfo li = Current as IXmlLineInfo;
462 return li == null ? false : li.HasLineInfo ();