2 // DTDValidatingReader.cs
5 // Atsushi Enomoto (ginga@kit.hi-ho.ne.jp)
7 // (C)2003 Atsushi Enomoto
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections;
36 using System.Xml.Schema;
39 using XmlTextReaderImpl = Mono.Xml2.XmlTextReader;
41 using XmlTextReaderImpl = System.Xml.XmlTextReader;
48 internal class DTDValidatingReader : XmlReader, IXmlLineInfo, IHasXmlParserContext, IHasXmlSchemaInfo, IXmlNamespaceResolver
50 internal class DTDValidatingReader : XmlReader, IXmlLineInfo, IHasXmlParserContext, IHasXmlSchemaInfo
53 public DTDValidatingReader (XmlReader reader)
58 internal DTDValidatingReader (XmlReader reader,
59 XmlValidatingReader validatingReader)
61 entityReaderStack = new Stack ();
62 entityReaderDepthStack = new Stack ();
64 this.sourceTextReader = reader as XmlTextReader;
65 elementStack = new Stack ();
66 automataStack = new Stack ();
67 attributes = new ArrayList ();
68 attributeValues = new Hashtable ();
69 attributeLocalNames = new Hashtable ();
70 attributeNamespaces = new Hashtable ();
71 attributePrefixes = new Hashtable ();
72 nsmgr = new XmlNamespaceManager (reader.NameTable);
73 this.validatingReader = validatingReader;
74 valueBuilder = new StringBuilder ();
75 idList = new ArrayList ();
76 missingIDReferences = new ArrayList ();
77 XmlTextReader xtReader = reader as XmlTextReader;
78 if (xtReader != null) {
79 resolver = xtReader.Resolver;
82 resolver = new XmlUrlResolver ();
85 Stack entityReaderStack;
86 Stack entityReaderDepthStack;
88 XmlTextReader sourceTextReader;
89 XmlTextReader nextEntityReader;
93 string currentElement;
94 string currentAttribute;
95 string currentTextValue;
96 string constructingTextValue;
97 bool shouldResetCurrentTextValue;
98 bool consumedAttribute;
100 DTDAutomata currentAutomata;
101 DTDAutomata previousAutomata;
103 ArrayList attributes;
104 Hashtable attributeValues;
105 Hashtable attributeLocalNames;
106 Hashtable attributeNamespaces;
107 Hashtable attributePrefixes;
108 XmlNamespaceManager nsmgr;
109 StringBuilder valueBuilder;
111 ArrayList missingIDReferences;
112 XmlResolver resolver;
113 EntityHandling currentEntityHandling;
114 bool isSignificantWhitespace;
117 bool dontResetTextType;
119 // This field is used to get properties and to raise events.
120 XmlValidatingReader validatingReader;
122 public DTDObjectModel DTD {
126 public EntityHandling EntityHandling {
127 get { return currentEntityHandling; }
128 set { currentEntityHandling = value; }
131 public override void Close ()
136 // We had already done attribute validation, so can ignore name.
137 public override string GetAttribute (int i)
139 if (currentTextValue != null)
140 throw new IndexOutOfRangeException ("Specified index is out of range: " + i);
143 return reader.GetAttribute (i);
145 if (attributes.Count <= i)
146 throw new IndexOutOfRangeException ("Specified index is out of range: " + i);
148 string attrName = (string) attributes [i];
149 return FilterNormalization (attrName, (string) attributeValues [attrName]);
152 public override string GetAttribute (string name)
154 if (currentTextValue != null)
158 return reader.GetAttribute (name);
160 return FilterNormalization (name, (string) attributeValues [name]);
163 public override string GetAttribute (string name, string ns)
165 if (currentTextValue != null)
169 return reader.GetAttribute (name, ns);
171 return reader.GetAttribute ((string) attributeLocalNames [name], ns);
175 IDictionary IXmlNamespaceResolver.GetNamespacesInScope (XmlNamespaceScope scope)
177 IXmlNamespaceResolver res = reader as IXmlNamespaceResolver;
178 return res != null ? res.GetNamespacesInScope (scope) : new Hashtable ();
182 bool IXmlLineInfo.HasLineInfo ()
184 IXmlLineInfo ixli = reader as IXmlLineInfo;
186 return ixli.HasLineInfo ();
191 public override string LookupNamespace (string prefix)
193 return nsmgr.LookupNamespace (NameTable.Get (prefix));
197 string IXmlNamespaceResolver.LookupPrefix (string ns)
199 IXmlNamespaceResolver res = reader as IXmlNamespaceResolver;
200 return res != null ? res.LookupPrefix (ns) : null;
204 public override void MoveToAttribute (int i)
206 if (currentTextValue != null)
207 throw new IndexOutOfRangeException ("The index is out of range.");
210 reader.MoveToAttribute (i);
211 currentAttribute = reader.Name;
212 consumedAttribute = false;
216 if (currentElement == null)
217 throw new IndexOutOfRangeException ("The index is out of range.");
219 if (attributes.Count > i) {
220 if (reader.AttributeCount > i)
221 reader.MoveToAttribute (i);
222 currentAttribute = (string) attributes [i];
223 consumedAttribute = false;
226 throw new IndexOutOfRangeException ("The index is out of range.");
229 public override bool MoveToAttribute (string name)
231 if (currentTextValue != null)
235 bool b = reader.MoveToAttribute (name);
237 currentAttribute = reader.Name;
238 consumedAttribute = false;
243 if (currentElement == null)
246 int idx = attributes.IndexOf (name);
248 currentAttribute = name;
249 consumedAttribute = false;
255 public override bool MoveToAttribute (string name, string ns)
257 if (currentTextValue != null)
261 bool b = reader.MoveToAttribute (name, ns);
263 currentAttribute = reader.Name;
264 consumedAttribute = false;
269 if (reader.MoveToAttribute (name, ns)) {
270 currentAttribute = reader.Name;
271 consumedAttribute = false;
275 for (int i = 0; i < attributes.Count; i++) {
276 string iter = (string) attributes [i];
277 if ((string) attributeLocalNames [iter] == name)
278 return MoveToAttribute (iter);
283 public override bool MoveToElement ()
285 if (currentTextValue != null)
288 bool b = reader.MoveToElement ();
289 if (!b && !IsDefault)
291 currentAttribute = null;
292 consumedAttribute = false;
296 public override bool MoveToFirstAttribute ()
298 if (currentTextValue != null)
302 bool b = reader.MoveToFirstAttribute ();
304 currentAttribute = reader.Name;
305 consumedAttribute = false;
310 if (attributes.Count == 0)
312 currentAttribute = (string) attributes [0];
313 reader.MoveToAttribute (currentAttribute);
314 consumedAttribute = false;
318 public override bool MoveToNextAttribute ()
320 if (currentTextValue != null)
324 bool b = reader.MoveToNextAttribute ();
326 currentAttribute = reader.Name;
327 consumedAttribute = false;
332 if (currentAttribute == null)
333 return MoveToFirstAttribute ();
335 int idx = attributes.IndexOf (currentAttribute);
336 if (idx + 1 < attributes.Count) {
337 currentAttribute = (string) attributes [idx + 1];
338 reader.MoveToAttribute (currentAttribute);
339 consumedAttribute = false;
346 private void OnValidationEvent (object o, ValidationEventArgs e)
348 this.HandleError (e.Exception, e.Severity);
352 public override bool Read ()
354 if (currentTextValue != null)
355 shouldResetCurrentTextValue = true;
359 currentElement = null;
360 currentAttribute = null;
361 consumedAttribute = false;
363 attributeLocalNames.Clear ();
364 attributeValues.Clear ();
365 attributeNamespaces.Clear ();
366 attributePrefixes.Clear ();
367 isWhitespace = false;
368 isSignificantWhitespace = false;
370 dontResetTextType = false;
372 bool b = ReadContent () || currentTextValue != null;
373 if (!b && this.missingIDReferences.Count > 0) {
374 this.HandleError ("Missing ID reference was found: " +
375 String.Join (",", missingIDReferences.ToArray (typeof (string)) as string []),
376 XmlSeverityType.Error);
377 // Don't output the same errors so many times.
378 this.missingIDReferences.Clear ();
380 if (validatingReader != null)
381 EntityHandling = validatingReader.EntityHandling;
385 private bool ReadContent ()
387 if (nextEntityReader != null) {
388 if (DTD == null || DTD.EntityDecls [reader.Name] == null)
389 throw NotWFError (String.Format ("Entity '{0}' was not declared.", reader.Name));
390 entityReaderStack.Push (reader);
391 entityReaderDepthStack.Push (Depth);
392 reader = sourceTextReader = nextEntityReader;
393 nextEntityReader = null;
394 return ReadContent ();
395 } else if (reader.EOF && entityReaderStack.Count > 0) {
397 reader = entityReaderStack.Pop () as XmlReader;
398 entityReaderDepthStack.Pop ();
399 sourceTextReader = reader as XmlTextReader;
400 return ReadContent ();
403 bool b = !reader.EOF;
404 if (shouldResetCurrentTextValue) {
405 currentTextValue = null;
406 shouldResetCurrentTextValue = false;
411 if (!insideContent && reader.NodeType == XmlNodeType.Element) {
412 insideContent = true;
414 currentAutomata = null;
416 currentAutomata = dtd.RootAutomata;
420 if (entityReaderStack.Count > 0) {
421 if (validatingReader.EntityHandling == EntityHandling.ExpandEntities)
422 return ReadContent ();
424 return true; // EndEntity
427 if (elementStack.Count != 0)
428 throw new InvalidOperationException ("Unexpected end of XmlReader.");
432 DTDElementDeclaration elem = null;
434 switch (reader.NodeType) {
435 case XmlNodeType.XmlDeclaration:
436 if (GetAttribute ("standalone") == "yes")
438 ValidateAttributes (null, false);
441 case XmlNodeType.DocumentType:
442 // XmlTextReader xmlTextReader = reader as XmlTextReader;
443 IHasXmlParserContext ctx = reader as IHasXmlParserContext;
445 dtd = ctx.ParserContext.Dtd;
447 XmlTextReaderImpl xmlTextReader = new XmlTextReaderImpl ("", XmlNodeType.Document, null);
448 xmlTextReader.XmlResolver = resolver;
449 xmlTextReader.GenerateDTDObjectModel (reader.Name,
450 reader ["PUBLIC"], reader ["SYSTEM"], reader.Value);
451 dtd = xmlTextReader.DTD;
454 // Validity Constraints Check.
455 if (DTD.Errors.Length > 0)
456 for (int i = 0; i < DTD.Errors.Length; i++)
457 HandleError (DTD.Errors [i].Message, XmlSeverityType.Error);
459 // NData target exists.
460 foreach (DTDEntityDeclaration ent in dtd.EntityDecls.Values)
461 if (ent.NotationName != null && dtd.NotationDecls [ent.NotationName] == null)
462 this.HandleError ("Target notation was not found for NData in entity declaration " + ent.Name + ".",
463 XmlSeverityType.Error);
464 // NOTATION exists for attribute default values
465 foreach (DTDAttListDeclaration attListIter in dtd.AttListDecls.Values)
466 foreach (DTDAttributeDefinition def in attListIter.Definitions)
467 if (def.Datatype.TokenizedType == XmlTokenizedType.NOTATION) {
468 foreach (string notation in def.EnumeratedNotations)
469 if (dtd.NotationDecls [notation] == null)
470 this.HandleError ("Target notation was not found for NOTATION typed attribute default " + def.Name + ".",
471 XmlSeverityType.Error);
476 case XmlNodeType.Element:
477 if (constructingTextValue != null) {
478 currentTextValue = constructingTextValue;
479 constructingTextValue = null;
481 ValidateWhitespaceNode ();
484 elementStack.Push (reader.Name);
486 // If no schema specification, then skip validation.
487 if (currentAutomata == null) {
488 ValidateAttributes (null, false);
489 if (reader.IsEmptyElement)
490 goto case XmlNodeType.EndElement;
494 previousAutomata = currentAutomata;
495 currentAutomata = currentAutomata.TryStartElement (reader.Name);
496 if (currentAutomata == DTD.Invalid) {
497 HandleError (String.Format ("Invalid start element found: {0}", reader.Name),
498 XmlSeverityType.Error);
499 currentAutomata = previousAutomata;
501 elem = DTD.ElementDecls [reader.Name];
503 HandleError (String.Format ("Element {0} is not declared.", reader.Name),
504 XmlSeverityType.Error);
505 currentAutomata = previousAutomata;
508 currentElement = Name;
509 automataStack.Push (currentAutomata);
510 if (elem != null) // i.e. not invalid
511 currentAutomata = elem.ContentModel.GetAutomata ();
513 DTDAttListDeclaration attList = dtd.AttListDecls [currentElement];
514 if (attList != null) {
516 ValidateAttributes (attList, true);
517 currentAttribute = null;
519 if (reader.HasAttributes) {
520 HandleError (String.Format (
521 "Attributes are found on element {0} while it has no attribute definitions.", currentElement),
522 XmlSeverityType.Error);
524 // SetupValidityIgnorantAttributes ();
525 ValidateAttributes (null, false);
527 // If it is empty element then directly check end element.
528 if (reader.IsEmptyElement)
529 goto case XmlNodeType.EndElement;
532 case XmlNodeType.EndElement:
533 if (constructingTextValue != null) {
534 currentTextValue = constructingTextValue;
535 constructingTextValue = null;
541 // If no schema specification, then skip validation.
542 if (currentAutomata == null)
545 elem = DTD.ElementDecls [reader.Name];
547 HandleError (String.Format ("Element {0} is not declared.", reader.Name),
548 XmlSeverityType.Error);
551 previousAutomata = currentAutomata;
552 // Don't let currentAutomata
553 DTDAutomata tmpAutomata = currentAutomata.TryEndElement ();
554 if (tmpAutomata == DTD.Invalid) {
555 HandleError (String.Format ("Invalid end element found: {0}", reader.Name),
556 XmlSeverityType.Error);
557 currentAutomata = previousAutomata;
560 currentAutomata = automataStack.Pop () as DTDAutomata;
563 case XmlNodeType.CDATA:
564 isSignificantWhitespace = isWhitespace = false;
569 if (currentTextValue != null) {
570 currentTextValue = constructingTextValue;
571 constructingTextValue = null;
575 case XmlNodeType.SignificantWhitespace:
577 isSignificantWhitespace = true;
578 isWhitespace = false;
579 dontResetTextType = true;
580 goto case XmlNodeType.DocumentFragment;
581 case XmlNodeType.Text:
582 isWhitespace = isSignificantWhitespace = false;
584 goto case XmlNodeType.DocumentFragment;
585 case XmlNodeType.DocumentFragment:
586 // it should not happen, but in case if
587 // XmlReader really returns it, just ignore.
588 if (reader.NodeType == XmlNodeType.DocumentFragment)
593 if (entityReaderStack.Count > 0 && validatingReader.EntityHandling == EntityHandling.ExpandEntities) {
594 constructingTextValue += reader.Value;
595 return ReadContent ();
598 case XmlNodeType.Whitespace:
599 if (!isText && !isSignificantWhitespace)
601 goto case XmlNodeType.DocumentFragment;
602 case XmlNodeType.EntityReference:
603 if (validatingReader.EntityHandling == EntityHandling.ExpandEntities) {
605 return ReadContent ();
610 ValidateWhitespaceNode ();
611 currentTextValue = constructingTextValue;
612 constructingTextValue = null;
617 private void ValidateText ()
619 if (currentAutomata == null)
622 DTDElementDeclaration elem = null;
623 if (elementStack.Count > 0)
624 elem = dtd.ElementDecls [elementStack.Peek () as string];
625 // Here element should have been already validated, so
626 // if no matching declaration is found, simply ignore.
627 if (elem != null && !elem.IsMixedContent && !elem.IsAny && !isWhitespace) {
628 HandleError (String.Format ("Current element {0} does not allow character data content.", elementStack.Peek () as string),
629 XmlSeverityType.Error);
630 currentAutomata = previousAutomata;
634 private void ValidateWhitespaceNode ()
636 // VC Standalone Document Declaration (2.9)
637 if (this.isStandalone && DTD != null && elementStack.Count > 0) {
638 DTDElementDeclaration elem = DTD.ElementDecls [elementStack.Peek () as string];
639 if (elem != null && !elem.IsInternalSubset && !elem.IsMixedContent && !elem.IsAny && !elem.IsEmpty)
640 HandleError ("In standalone document, whitespace cannot appear in an element whose declaration explicitly contains child content model, not Mixed content.", XmlSeverityType.Error);
644 private XmlException NotWFError (string message)
646 return new XmlException (this as IXmlLineInfo, BaseURI, message);
649 private void HandleError (string message, XmlSeverityType severity)
651 if (validatingReader != null &&
652 validatingReader.ValidationType == ValidationType.None)
655 IXmlLineInfo info = this as IXmlLineInfo;
656 bool hasLine = info.HasLineInfo ();
657 XmlSchemaException ex = new XmlSchemaException (
659 hasLine ? info.LineNumber : 0,
660 hasLine ? info.LinePosition : 0,
664 HandleError (ex, severity);
667 private void HandleError (XmlSchemaException ex, XmlSeverityType severity)
669 if (validatingReader != null &&
670 validatingReader.ValidationType == ValidationType.None)
673 if (validatingReader != null)
674 this.validatingReader.OnValidationEvent (this,
675 new ValidationEventArgs (ex, ex.Message, severity));
676 else if (severity == XmlSeverityType.Error)
680 Stack attributeValueEntityStack = new Stack ();
682 private void ValidateAttributes (DTDAttListDeclaration decl, bool validate)
684 DtdValidateAttributes (decl, validate);
686 foreach (string attr in attributes)
687 if (attr == "xmlns" ||
688 String.CompareOrdinal (attr, 0, "xmlns:", 0, 6) == 0)
690 attr == "xmlns" ? String.Empty : (string) attributeLocalNames [attr],
691 (string) attributeValues [attr]);
694 foreach (string attr in attributes) {
695 string prefix = attr == "xmlns" ? "xmlns" : attributePrefixes [attr] as string;
696 if (prefix == String.Empty)
697 attributeNamespaces.Add (attr, String.Empty);
699 attributeNamespaces.Add (attr, LookupNamespace (prefix));
703 private void DtdValidateAttributes (DTDAttListDeclaration decl, bool validate)
705 while (reader.MoveToNextAttribute ()) {
706 string attrName = reader.Name;
707 this.currentAttribute = attrName;
708 attributes.Add (attrName);
709 attributeLocalNames.Add (attrName, reader.LocalName);
710 attributePrefixes.Add (attrName, reader.Prefix);
711 XmlReader targetReader = reader;
712 string attrValue = null;
713 // It always resolves entity references on attributes (documented as such).
714 // if (currentEntityHandling == EntityHandling.ExpandCharEntities)
715 // attrValue = reader.Value;
718 while (attributeValueEntityStack.Count >= 0) {
719 if (!targetReader.ReadAttributeValue ()) {
720 if (attributeValueEntityStack.Count > 0) {
721 targetReader = attributeValueEntityStack.Pop () as XmlReader;
726 switch (targetReader.NodeType) {
727 case XmlNodeType.EntityReference:
728 DTDEntityDeclaration edecl = DTD.EntityDecls [targetReader.Name];
730 HandleError (String.Format ("Referenced entity {0} is not declared.", targetReader.Name),
731 XmlSeverityType.Error);
733 XmlTextReader etr = new XmlTextReader (edecl.EntityValue, XmlNodeType.Attribute, ParserContext);
734 attributeValueEntityStack.Push (targetReader);
739 case XmlNodeType.EndEntity:
742 if (attrValue != null) {
743 valueBuilder.Append (attrValue);
747 if (valueBuilder.Length != 0)
748 valueBuilder.Append (targetReader.Value);
750 attrValue = targetReader.Value;
756 if (attrValue == null) {
757 attrValue = valueBuilder.ToString ();
758 valueBuilder.Length = 0;
761 reader.MoveToElement ();
762 reader.MoveToAttribute (attrName);
763 attributeValues.Add (attrName, attrValue);
770 DTDAttributeDefinition def = decl [reader.Name];
772 HandleError (String.Format ("Attribute {0} is not declared.", reader.Name),
773 XmlSeverityType.Error);
777 // check enumeration constraint
778 if (def.EnumeratedAttributeDeclaration.Count > 0)
779 if (!def.EnumeratedAttributeDeclaration.Contains (
780 FilterNormalization (reader.Name, attrValue)))
781 HandleError (String.Format ("Attribute enumeration constraint error in attribute {0}, value {1}.",
782 reader.Name, attrValue), XmlSeverityType.Error);
783 if (def.EnumeratedNotations.Count > 0)
784 if (!def.EnumeratedNotations.Contains (
785 FilterNormalization (reader.Name, attrValue)))
786 HandleError (String.Format ("Attribute notation enumeration constraint error in attribute {0}, value {1}.",
787 reader.Name, attrValue), XmlSeverityType.Error);
789 // check type constraint
790 string normalized = null;
791 if (def.Datatype != null)
792 normalized = FilterNormalization (def.Name, attrValue);
794 normalized = attrValue;
795 DTDEntityDeclaration ent;
797 // Common process to get list value
798 string [] list = null;
799 switch (def.Datatype.TokenizedType) {
800 case XmlTokenizedType.IDREFS:
801 case XmlTokenizedType.ENTITIES:
802 case XmlTokenizedType.NMTOKENS:
804 list = def.Datatype.ParseValue (normalized, NameTable, null) as string [];
805 } catch (Exception) {
806 HandleError ("Attribute value is invalid against its data type.", XmlSeverityType.Error);
807 list = new string [0];
812 def.Datatype.ParseValue (normalized, NameTable, null);
813 } catch (Exception ex) {
814 HandleError (String.Format ("Attribute value is invalid against its data type '{0}'. {1}", def.Datatype, ex.Message), XmlSeverityType.Error);
819 switch (def.Datatype.TokenizedType) {
820 case XmlTokenizedType.ID:
821 if (this.idList.Contains (normalized)) {
822 HandleError (String.Format ("Node with ID {0} was already appeared.", attrValue),
823 XmlSeverityType.Error);
825 if (missingIDReferences.Contains (normalized))
826 missingIDReferences.Remove (normalized);
827 idList.Add (normalized);
830 case XmlTokenizedType.IDREF:
831 if (!idList.Contains (normalized))
832 missingIDReferences.Add (normalized);
834 case XmlTokenizedType.IDREFS:
835 for (int i = 0; i < list.Length; i++) {
836 string idref = list [i];
837 if (!idList.Contains (idref))
838 missingIDReferences.Add (idref);
841 case XmlTokenizedType.ENTITY:
842 ent = dtd.EntityDecls [normalized];
844 HandleError ("Reference to undeclared entity was found in attribute: " + reader.Name + ".", XmlSeverityType.Error);
845 else if (ent.NotationName == null)
846 HandleError ("The entity specified by entity type value must be an unparsed entity. The entity definition has no NDATA in attribute: " + reader.Name + ".", XmlSeverityType.Error);
848 case XmlTokenizedType.ENTITIES:
849 for (int i = 0; i < list.Length; i++) {
850 string entref = list [i];
851 ent = dtd.EntityDecls [FilterNormalization (reader.Name, entref)];
853 HandleError ("Reference to undeclared entity was found in attribute: " + reader.Name + ".", XmlSeverityType.Error);
854 else if (ent.NotationName == null)
855 HandleError ("The entity specified by ENTITIES type value must be an unparsed entity. The entity definition has no NDATA in attribute: " + reader.Name + ".", XmlSeverityType.Error);
858 // case XmlTokenizedType.NMTOKEN: nothing to do
859 // case XmlTokenizedType.NMTOKENS: nothing to do
862 if (isStandalone && !def.IsInternalSubset &&
863 attrValue != normalized)
864 HandleError ("In standalone document, attribute value characters must not be checked against external definition.", XmlSeverityType.Error);
866 if (def.OccurenceType ==
867 DTDAttributeOccurenceType.Fixed &&
868 attrValue != def.DefaultValue)
869 HandleError (String.Format ("Fixed attribute {0} in element {1} has invalid value {2}.",
870 def.Name, decl.Name, attrValue),
871 XmlSeverityType.Error);
875 VerifyDeclaredAttributes (decl);
880 private void VerifyDeclaredAttributes (DTDAttListDeclaration decl)
882 // Check if all required attributes exist, and/or
883 // if there is default values, then add them.
884 for (int i = 0; i < decl.Definitions.Count; i++) {
885 DTDAttributeDefinition def = (DTDAttributeDefinition) decl.Definitions [i];
886 if (attributes.Contains (def.Name))
889 if (def.OccurenceType == DTDAttributeOccurenceType.Required) {
890 HandleError (String.Format ("Required attribute {0} in element {1} not found .",
891 def.Name, decl.Name),
892 XmlSeverityType.Error);
896 else if (def.DefaultValue == null)
899 if (this.isStandalone && !def.IsInternalSubset)
900 HandleError ("In standalone document, external default value definition must not be applied.", XmlSeverityType.Error);
902 switch (validatingReader.ValidationType) {
903 case ValidationType.Auto:
904 if (validatingReader.Schemas.Count == 0)
905 goto case ValidationType.DTD;
907 case ValidationType.DTD:
908 case ValidationType.None:
909 // Other than them, ignore DTD defaults.
910 attributes.Add (def.Name);
911 int colonAt = def.Name.IndexOf (':');
912 attributeLocalNames.Add (def.Name,
913 colonAt < 0 ? def.Name :
914 def.Name.Substring (colonAt + 1));
915 string prefix = colonAt < 0 ?
917 def.Name.Substring (0, colonAt);
918 attributePrefixes.Add (def.Name, prefix);
919 attributeValues.Add (def.Name, def.DefaultValue);
925 public override bool ReadAttributeValue ()
927 if (consumedAttribute)
929 if (NodeType == XmlNodeType.Attribute &&
930 currentEntityHandling == EntityHandling.ExpandEntities) {
931 consumedAttribute = true;
934 else if (IsDefault) {
935 consumedAttribute = true;
939 return reader.ReadAttributeValue ();
943 public override string ReadInnerXml ()
945 // MS.NET 1.0 has a serious bug here. It skips validation.
946 return reader.ReadInnerXml ();
949 public override string ReadOuterXml ()
951 // MS.NET 1.0 has a serious bug here. It skips validation.
952 return reader.ReadOuterXml ();
956 public override string ReadString ()
958 // It seems to be the same as ReadInnerXml().
959 return base.ReadStringInternal ();
962 public override void ResolveEntity ()
964 if (resolver == null)
967 // "reader." is required since NodeType must not be entityref by nature.
968 if (reader.NodeType != XmlNodeType.EntityReference)
969 throw new InvalidOperationException ("The current node is not an Entity Reference");
970 DTDEntityDeclaration entity = DTD != null ? DTD.EntityDecls [reader.Name] as DTDEntityDeclaration : null;
972 XmlNodeType xmlReaderNodeType =
973 (currentAttribute != null) ? XmlNodeType.Attribute : XmlNodeType.Element;
977 throw NotWFError (String.Format ("Reference to undeclared entity '{0}'.", reader.Name));
980 // MS.NET 1.x ignores undeclared entity reference here..
981 if (entity != null && entity.SystemId != null) {
982 Uri baseUri = entity.BaseURI == String.Empty ? null : new Uri (entity.BaseURI);
983 Stream stream = resolver.GetEntity (resolver.ResolveUri (baseUri, entity.SystemId), null, typeof (Stream)) as Stream;
984 nextEntityReader = new XmlTextReader (stream, xmlReaderNodeType, ParserContext);
986 string replacementText =
987 (entity != null) ? entity.EntityValue : String.Empty;
988 nextEntityReader = new XmlTextReader (replacementText, xmlReaderNodeType, ParserContext);
990 nextEntityReader.XmlResolver = resolver;
993 public override int AttributeCount {
995 if (currentTextValue != null)
998 if (dtd == null || !insideContent)
999 return reader.AttributeCount;
1001 return attributes.Count;
1005 public override string BaseURI {
1007 return reader.BaseURI;
1011 public override bool CanResolveEntity {
1012 get { return true; }
1015 public override int Depth {
1017 int baseNum = reader.Depth;
1018 if (entityReaderDepthStack.Count > 0) {
1019 baseNum += (int) entityReaderDepthStack.Peek ();
1020 if (NodeType != XmlNodeType.EndEntity)
1023 if (currentTextValue != null && reader.NodeType == XmlNodeType.EndElement)
1026 return IsDefault ? baseNum + 1 : baseNum;
1030 public override bool EOF {
1031 get { return reader.EOF && entityReaderStack.Count == 0; }
1034 public override bool HasValue {
1036 return IsDefault ? true :
1037 currentTextValue != null ? true :
1041 public override bool IsDefault {
1043 if (currentTextValue != null)
1045 if (currentAttribute == null)
1047 return reader.GetAttribute (currentAttribute) == null;
1051 public override bool IsEmptyElement {
1053 if (currentTextValue != null)
1055 return reader.IsEmptyElement;
1059 public override string this [int i] {
1060 get { return GetAttribute (i); }
1063 public override string this [string name] {
1064 get { return GetAttribute (name); }
1067 public override string this [string name, string ns] {
1068 get { return GetAttribute (name, ns); }
1071 public int LineNumber {
1073 IXmlLineInfo info = reader as IXmlLineInfo;
1074 return (info != null) ? info.LineNumber : 0;
1078 public int LinePosition {
1080 IXmlLineInfo info = reader as IXmlLineInfo;
1081 return (info != null) ? info.LinePosition : 0;
1085 public override string LocalName {
1087 if (currentTextValue != null || consumedAttribute)
1088 return String.Empty;
1089 if (entityReaderStack.Count > 0 && reader.EOF)
1090 return ((XmlReader) entityReaderStack.Peek ()).LocalName; // name of EndEntity
1091 else if (NodeType == XmlNodeType.Attribute)
1092 return (string) attributeLocalNames [currentAttribute];
1094 return reader.LocalName;
1098 public override string Name {
1100 if (currentTextValue != null || consumedAttribute)
1101 return String.Empty;
1102 if (entityReaderStack.Count > 0 && reader.EOF)
1103 return ((XmlReader) entityReaderStack.Peek ()).Name; // name of EndEntity
1104 else if (NodeType == XmlNodeType.Attribute)
1105 return currentAttribute;
1111 public override string NamespaceURI {
1113 if (currentTextValue != null || consumedAttribute)
1114 return String.Empty;
1115 else if (NodeType == XmlNodeType.Attribute)
1116 return (string) attributeNamespaces [currentAttribute];
1117 return nsmgr.LookupNamespace (Prefix);
1121 public override XmlNameTable NameTable {
1122 get { return reader.NameTable; }
1125 public override XmlNodeType NodeType {
1127 if (currentTextValue != null)
1128 return isSignificantWhitespace ? XmlNodeType.SignificantWhitespace :
1129 isWhitespace ? XmlNodeType.Whitespace :
1132 if (entityReaderStack.Count > 0 && reader.EOF)
1133 return XmlNodeType.EndEntity;
1135 // If consumedAttribute is true, then entities must be resolved.
1136 return consumedAttribute ? XmlNodeType.Text :
1137 IsDefault ? XmlNodeType.Attribute :
1142 public XmlParserContext ParserContext {
1143 get { return XmlSchemaUtil.GetParserContext (reader); }
1146 public override string Prefix {
1148 if (currentTextValue != null || consumedAttribute)
1149 return String.Empty;
1150 else if (NodeType == XmlNodeType.Attribute)
1151 return (string) attributePrefixes [currentAttribute];
1153 return reader.Prefix;
1157 public override char QuoteChar {
1159 // If it is not actually on an attribute, then it returns
1160 // undefined value or '"'.
1161 return reader.QuoteChar;
1165 public override ReadState ReadState {
1167 if (reader.ReadState == ReadState.EndOfFile && currentTextValue != null)
1168 return ReadState.Interactive;
1169 return reader.ReadState;
1173 public object SchemaType {
1175 if (currentElement == null)
1177 DTDAttListDeclaration decl =
1178 DTD.AttListDecls [currentElement];
1179 DTDAttributeDefinition def =
1180 decl != null ? decl [currentAttribute] : null;
1181 return def != null ? def.Datatype : null;
1185 char [] whitespaceChars = new char [] {' '};
1186 private string FilterNormalization (string attrName, string rawValue)
1188 if (DTD == null || NodeType != XmlNodeType.Attribute ||
1189 sourceTextReader == null ||
1190 !sourceTextReader.Normalization)
1193 DTDAttributeDefinition def =
1194 dtd.AttListDecls [currentElement].Get (attrName);
1195 valueBuilder.Append (rawValue);
1196 valueBuilder.Replace ('\r', ' ');
1197 valueBuilder.Replace ('\n', ' ');
1198 valueBuilder.Replace ('\t', ' ');
1200 if (def.Datatype.TokenizedType == XmlTokenizedType.CDATA)
1201 return valueBuilder.ToString ();
1203 for (int i=0; i < valueBuilder.Length; i++) {
1204 if (valueBuilder [i] != ' ')
1206 while (++i < valueBuilder.Length && valueBuilder [i] == ' ')
1207 valueBuilder.Remove (i, 1);
1209 return valueBuilder.ToString ().Trim (whitespaceChars);
1211 valueBuilder.Length = 0;
1215 public override string Value {
1217 if (currentTextValue != null)
1218 return currentTextValue;
1219 // As to this property, MS.NET seems ignorant of EntityHandling...
1220 else if (NodeType == XmlNodeType.Attribute
1221 // It also covers default attribute text.
1222 || consumedAttribute)
1223 return FilterNormalization (Name, (string) attributeValues [currentAttribute]);
1225 return FilterNormalization (Name, reader.Value);
1229 public override string XmlLang {
1231 string val = this ["xml:lang"];
1232 return val != null ? val : reader.XmlLang;
1236 public XmlResolver XmlResolver {
1242 public override XmlSpace XmlSpace {
1244 string val = this ["xml:space"];
1247 return XmlSpace.Preserve;
1249 return XmlSpace.Default;
1251 return reader.XmlSpace;