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 nsdecls = new Hashtable ();
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;
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 if (nsdecls.Count > 0 && nsdecls [prefix] != null)
194 return (string) nsdecls [prefix];
195 return reader.LookupNamespace (prefix);
199 string IXmlNamespaceResolver.LookupPrefix (string ns)
201 IXmlNamespaceResolver res = reader as IXmlNamespaceResolver;
202 return res != null ? res.LookupPrefix (ns) : null;
206 public override void MoveToAttribute (int i)
208 if (currentTextValue != null)
209 throw new IndexOutOfRangeException ("The index is out of range.");
212 reader.MoveToAttribute (i);
213 currentAttribute = reader.Name;
214 consumedAttribute = false;
218 if (currentElement == null)
219 throw new IndexOutOfRangeException ("The index is out of range.");
221 if (attributes.Count > i) {
222 if (reader.AttributeCount > i)
223 reader.MoveToAttribute (i);
224 currentAttribute = (string) attributes [i];
225 consumedAttribute = false;
228 throw new IndexOutOfRangeException ("The index is out of range.");
231 public override bool MoveToAttribute (string name)
233 if (currentTextValue != null)
237 bool b = reader.MoveToAttribute (name);
239 currentAttribute = reader.Name;
240 consumedAttribute = false;
245 if (currentElement == null)
248 int idx = attributes.IndexOf (name);
250 currentAttribute = name;
251 consumedAttribute = false;
257 public override bool MoveToAttribute (string name, string ns)
259 if (currentTextValue != null)
263 bool b = reader.MoveToAttribute (name, ns);
265 currentAttribute = reader.Name;
266 consumedAttribute = false;
271 if (reader.MoveToAttribute (name, ns)) {
272 currentAttribute = reader.Name;
273 consumedAttribute = false;
277 for (int i = 0; i < attributes.Count; i++) {
278 string iter = (string) attributes [i];
279 if ((string) attributeLocalNames [iter] == name)
280 return MoveToAttribute (iter);
285 public override bool MoveToElement ()
287 if (currentTextValue != null)
290 bool b = reader.MoveToElement ();
291 if (!b && !IsDefault)
293 currentAttribute = null;
294 consumedAttribute = false;
298 public override bool MoveToFirstAttribute ()
300 if (currentTextValue != null)
304 bool b = reader.MoveToFirstAttribute ();
306 currentAttribute = reader.Name;
307 consumedAttribute = false;
312 if (attributes.Count == 0)
314 currentAttribute = (string) attributes [0];
315 reader.MoveToAttribute (currentAttribute);
316 consumedAttribute = false;
320 public override bool MoveToNextAttribute ()
322 if (currentTextValue != null)
326 bool b = reader.MoveToNextAttribute ();
328 currentAttribute = reader.Name;
329 consumedAttribute = false;
334 if (currentAttribute == null)
335 return MoveToFirstAttribute ();
337 int idx = attributes.IndexOf (currentAttribute);
338 if (idx + 1 < attributes.Count) {
339 currentAttribute = (string) attributes [idx + 1];
340 reader.MoveToAttribute (currentAttribute);
341 consumedAttribute = false;
348 private void OnValidationEvent (object o, ValidationEventArgs e)
350 this.HandleError (e.Exception, e.Severity);
354 public override bool Read ()
356 if (currentTextValue != null)
357 shouldResetCurrentTextValue = true;
361 currentElement = null;
362 currentAttribute = null;
363 consumedAttribute = false;
365 attributeLocalNames.Clear ();
366 attributeValues.Clear ();
367 attributeNamespaces.Clear ();
368 attributePrefixes.Clear ();
370 isWhitespace = false;
371 isSignificantWhitespace = false;
373 dontResetTextType = false;
375 bool b = ReadContent () || currentTextValue != null;
376 if (!b && this.missingIDReferences.Count > 0) {
377 this.HandleError ("Missing ID reference was found: " +
378 String.Join (",", missingIDReferences.ToArray (typeof (string)) as string []),
379 XmlSeverityType.Error);
380 // Don't output the same errors so many times.
381 this.missingIDReferences.Clear ();
383 if (validatingReader != null)
384 EntityHandling = validatingReader.EntityHandling;
388 private bool ReadContent ()
390 if (nextEntityReader != null) {
391 if (DTD == null || DTD.EntityDecls [reader.Name] == null)
392 throw NotWFError (String.Format ("Entity '{0}' was not declared.", reader.Name));
393 entityReaderStack.Push (reader);
394 entityReaderDepthStack.Push (Depth);
395 reader = sourceTextReader = nextEntityReader;
396 nextEntityReader = null;
397 return ReadContent ();
398 } else if (reader.EOF && entityReaderStack.Count > 0) {
399 reader = entityReaderStack.Pop () as XmlReader;
400 entityReaderDepthStack.Pop ();
401 sourceTextReader = reader as XmlTextReader;
402 return ReadContent ();
405 bool b = !reader.EOF;
406 if (shouldResetCurrentTextValue) {
407 currentTextValue = null;
408 shouldResetCurrentTextValue = false;
413 if (!insideContent && reader.NodeType == XmlNodeType.Element) {
414 insideContent = true;
416 currentAutomata = null;
418 currentAutomata = dtd.RootAutomata;
422 if (entityReaderStack.Count > 0) {
423 if (validatingReader.EntityHandling == EntityHandling.ExpandEntities)
424 return ReadContent ();
426 return true; // EndEntity
429 if (elementStack.Count != 0)
430 throw new InvalidOperationException ("Unexpected end of XmlReader.");
434 DTDElementDeclaration elem = null;
436 switch (reader.NodeType) {
437 case XmlNodeType.XmlDeclaration:
438 if (GetAttribute ("standalone") == "yes")
440 ValidateAttributes (null, false);
443 case XmlNodeType.DocumentType:
444 // XmlTextReader xmlTextReader = reader as XmlTextReader;
445 IHasXmlParserContext ctx = reader as IHasXmlParserContext;
447 dtd = ctx.ParserContext.Dtd;
449 XmlTextReaderImpl xmlTextReader = new XmlTextReaderImpl ("", XmlNodeType.Document, null);
450 xmlTextReader.XmlResolver = resolver;
451 xmlTextReader.GenerateDTDObjectModel (reader.Name,
452 reader ["PUBLIC"], reader ["SYSTEM"], reader.Value);
453 dtd = xmlTextReader.DTD;
456 // Validity Constraints Check.
457 if (DTD.Errors.Length > 0)
458 for (int i = 0; i < DTD.Errors.Length; i++)
459 HandleError (DTD.Errors [i].Message, XmlSeverityType.Error);
461 // NData target exists.
462 foreach (DTDEntityDeclaration ent in dtd.EntityDecls.Values)
463 if (ent.NotationName != null && dtd.NotationDecls [ent.NotationName] == null)
464 this.HandleError ("Target notation was not found for NData in entity declaration " + ent.Name + ".",
465 XmlSeverityType.Error);
466 // NOTATION exists for attribute default values
467 foreach (DTDAttListDeclaration attListIter in dtd.AttListDecls.Values)
468 foreach (DTDAttributeDefinition def in attListIter.Definitions)
469 if (def.Datatype.TokenizedType == XmlTokenizedType.NOTATION) {
470 foreach (string notation in def.EnumeratedNotations)
471 if (dtd.NotationDecls [notation] == null)
472 this.HandleError ("Target notation was not found for NOTATION typed attribute default " + def.Name + ".",
473 XmlSeverityType.Error);
478 case XmlNodeType.Element:
479 if (constructingTextValue != null) {
480 currentTextValue = constructingTextValue;
481 constructingTextValue = null;
483 ValidateWhitespaceNode ();
486 elementStack.Push (reader.Name);
488 // If no schema specification, then skip validation.
489 if (currentAutomata == null) {
490 ValidateAttributes (null, false);
491 if (reader.IsEmptyElement)
492 goto case XmlNodeType.EndElement;
496 previousAutomata = currentAutomata;
497 currentAutomata = currentAutomata.TryStartElement (reader.Name);
498 if (currentAutomata == DTD.Invalid) {
499 HandleError (String.Format ("Invalid start element found: {0}", reader.Name),
500 XmlSeverityType.Error);
501 currentAutomata = previousAutomata;
503 elem = DTD.ElementDecls [reader.Name];
505 HandleError (String.Format ("Element {0} is not declared.", reader.Name),
506 XmlSeverityType.Error);
507 currentAutomata = previousAutomata;
510 currentElement = Name;
511 automataStack.Push (currentAutomata);
512 if (elem != null) // i.e. not invalid
513 currentAutomata = elem.ContentModel.GetAutomata ();
515 DTDAttListDeclaration attList = dtd.AttListDecls [currentElement];
516 if (attList != null) {
518 ValidateAttributes (attList, true);
519 currentAttribute = null;
521 if (reader.HasAttributes) {
522 HandleError (String.Format (
523 "Attributes are found on element {0} while it has no attribute definitions.", currentElement),
524 XmlSeverityType.Error);
526 // SetupValidityIgnorantAttributes ();
527 ValidateAttributes (null, false);
529 foreach (string attr in attributes)
530 if (attr == "xmlns" ||
531 String.CompareOrdinal (attr, 0, "xmlns", 0, 5) == 0)
533 attr == "xmlns" ? String.Empty : attributeLocalNames [attr],
534 attributeValues [attr]);
535 // If it is empty element then directly check end element.
536 if (reader.IsEmptyElement)
537 goto case XmlNodeType.EndElement;
540 case XmlNodeType.EndElement:
541 if (constructingTextValue != null) {
542 currentTextValue = constructingTextValue;
543 constructingTextValue = null;
548 // If no schema specification, then skip validation.
549 if (currentAutomata == null)
552 elem = DTD.ElementDecls [reader.Name];
554 HandleError (String.Format ("Element {0} is not declared.", reader.Name),
555 XmlSeverityType.Error);
558 previousAutomata = currentAutomata;
559 // Don't let currentAutomata
560 DTDAutomata tmpAutomata = currentAutomata.TryEndElement ();
561 if (tmpAutomata == DTD.Invalid) {
562 HandleError (String.Format ("Invalid end element found: {0}", reader.Name),
563 XmlSeverityType.Error);
564 currentAutomata = previousAutomata;
567 currentAutomata = automataStack.Pop () as DTDAutomata;
570 case XmlNodeType.CDATA:
571 if (currentTextValue != null) {
572 currentTextValue = constructingTextValue;
573 constructingTextValue = null;
577 case XmlNodeType.SignificantWhitespace:
579 isSignificantWhitespace = true;
580 dontResetTextType = true;
581 goto case XmlNodeType.Text;
582 case XmlNodeType.Text:
584 if (!dontResetTextType) {
585 isWhitespace = isSignificantWhitespace = false;
587 // If no schema specification, then skip validation.
588 if (currentAutomata == null)
591 if (elementStack.Count > 0)
592 elem = dtd.ElementDecls [elementStack.Peek () as string];
593 // Here element should have been already validated, so
594 // if no matching declaration is found, simply ignore.
595 if (elem != null && !elem.IsMixedContent && !elem.IsAny) {
596 HandleError (String.Format ("Current element {0} does not allow character data content.", elementStack.Peek () as string),
597 XmlSeverityType.Error);
598 currentAutomata = previousAutomata;
600 if (entityReaderStack.Count > 0 && validatingReader.EntityHandling == EntityHandling.ExpandEntities) {
601 constructingTextValue += reader.Value;
602 return ReadContent ();
605 case XmlNodeType.Whitespace:
606 if (!isText && !isSignificantWhitespace)
608 goto case XmlNodeType.Text;
609 case XmlNodeType.EntityReference:
610 if (validatingReader.EntityHandling == EntityHandling.ExpandEntities) {
612 return ReadContent ();
617 ValidateWhitespaceNode ();
618 currentTextValue = constructingTextValue;
619 constructingTextValue = null;
624 private void ValidateWhitespaceNode ()
626 // VC Standalone Document Declaration (2.9)
627 if (this.isStandalone && DTD != null && elementStack.Count > 0) {
628 DTDElementDeclaration elem = DTD.ElementDecls [elementStack.Peek () as string];
629 if (elem != null && !elem.IsInternalSubset && !elem.IsMixedContent && !elem.IsAny && !elem.IsEmpty)
630 HandleError ("In standalone document, whitespace cannot appear in an element whose declaration explicitly contains child content model, not Mixed content.", XmlSeverityType.Error);
634 private XmlException NotWFError (string message)
636 return new XmlException (this as IXmlLineInfo, BaseURI, message);
639 private void HandleError (string message, XmlSeverityType severity)
641 if (validatingReader != null &&
642 validatingReader.ValidationType == ValidationType.None)
645 IXmlLineInfo info = this as IXmlLineInfo;
646 bool hasLine = info.HasLineInfo ();
647 XmlSchemaException ex = new XmlSchemaException (
649 hasLine ? info.LineNumber : 0,
650 hasLine ? info.LinePosition : 0,
654 HandleError (ex, severity);
657 private void HandleError (XmlSchemaException ex, XmlSeverityType severity)
659 if (validatingReader != null &&
660 validatingReader.ValidationType == ValidationType.None)
663 if (validatingReader != null)
664 this.validatingReader.OnValidationEvent (this,
665 new ValidationEventArgs (ex, ex.Message, severity));
666 else if (severity == XmlSeverityType.Error)
670 Stack attributeValueEntityStack = new Stack ();
672 private void ValidateAttributes (DTDAttListDeclaration decl, bool validate)
674 while (reader.MoveToNextAttribute ()) {
675 string attrName = reader.Name;
676 this.currentAttribute = attrName;
677 attributes.Add (attrName);
678 attributeLocalNames.Add (attrName, reader.LocalName);
679 attributeNamespaces.Add (attrName, reader.NamespaceURI);
680 attributePrefixes.Add (attrName, reader.Prefix);
681 XmlReader targetReader = reader;
682 string attrValue = null;
683 // It always resolves entity references on attributes (documented as such).
684 // if (currentEntityHandling == EntityHandling.ExpandCharEntities)
685 // attrValue = reader.Value;
688 while (attributeValueEntityStack.Count >= 0) {
689 if (!targetReader.ReadAttributeValue ()) {
690 if (attributeValueEntityStack.Count > 0) {
691 targetReader = attributeValueEntityStack.Pop () as XmlReader;
696 switch (targetReader.NodeType) {
697 case XmlNodeType.EntityReference:
698 DTDEntityDeclaration edecl = DTD.EntityDecls [targetReader.Name];
700 HandleError (String.Format ("Referenced entity {0} is not declared.", targetReader.Name),
701 XmlSeverityType.Error);
703 XmlTextReader etr = new XmlTextReader (edecl.EntityValue, XmlNodeType.Attribute, ParserContext);
704 attributeValueEntityStack.Push (targetReader);
709 case XmlNodeType.EndEntity:
712 if (attrValue != null) {
713 valueBuilder.Append (attrValue);
717 if (valueBuilder.Length != 0)
718 valueBuilder.Append (targetReader.Value);
720 attrValue = targetReader.Value;
726 if (attrValue == null) {
727 attrValue = valueBuilder.ToString ();
728 valueBuilder.Length = 0;
731 reader.MoveToElement ();
732 reader.MoveToAttribute (attrName);
733 attributeValues.Add (attrName, attrValue);
740 DTDAttributeDefinition def = decl [reader.Name];
742 HandleError (String.Format ("Attribute {0} is not declared.", reader.Name),
743 XmlSeverityType.Error);
747 // check enumeration constraint
748 if (def.EnumeratedAttributeDeclaration.Count > 0)
749 if (!def.EnumeratedAttributeDeclaration.Contains (
750 FilterNormalization (reader.Name, attrValue)))
751 HandleError (String.Format ("Attribute enumeration constraint error in attribute {0}, value {1}.",
752 reader.Name, attrValue), XmlSeverityType.Error);
753 if (def.EnumeratedNotations.Count > 0)
754 if (!def.EnumeratedNotations.Contains (
755 FilterNormalization (reader.Name, attrValue)))
756 HandleError (String.Format ("Attribute notation enumeration constraint error in attribute {0}, value {1}.",
757 reader.Name, attrValue), XmlSeverityType.Error);
759 // check type constraint
760 string normalized = null;
761 if (def.Datatype != null)
762 normalized = FilterNormalization (def.Name, attrValue);
764 normalized = attrValue;
765 DTDEntityDeclaration ent;
767 // Common process to get list value
768 string [] list = null;
769 switch (def.Datatype.TokenizedType) {
770 case XmlTokenizedType.IDREFS:
771 case XmlTokenizedType.ENTITIES:
772 case XmlTokenizedType.NMTOKENS:
774 list = def.Datatype.ParseValue (normalized, NameTable, null) as string [];
775 } catch (Exception) {
776 HandleError ("Attribute value is invalid against its data type.", XmlSeverityType.Error);
777 list = new string [0];
782 def.Datatype.ParseValue (normalized, NameTable, null);
783 } catch (Exception ex) {
784 HandleError (String.Format ("Attribute value is invalid against its data type '{0}'. {1}", def.Datatype, ex.Message), XmlSeverityType.Error);
789 switch (def.Datatype.TokenizedType) {
790 case XmlTokenizedType.ID:
791 if (this.idList.Contains (normalized)) {
792 HandleError (String.Format ("Node with ID {0} was already appeared.", attrValue),
793 XmlSeverityType.Error);
795 if (missingIDReferences.Contains (normalized))
796 missingIDReferences.Remove (normalized);
797 idList.Add (normalized);
800 case XmlTokenizedType.IDREF:
801 if (!idList.Contains (normalized))
802 missingIDReferences.Add (normalized);
804 case XmlTokenizedType.IDREFS:
805 for (int i = 0; i < list.Length; i++) {
806 string idref = list [i];
807 if (!idList.Contains (idref))
808 missingIDReferences.Add (idref);
811 case XmlTokenizedType.ENTITY:
812 ent = dtd.EntityDecls [normalized];
814 HandleError ("Reference to undeclared entity was found in attribute: " + reader.Name + ".", XmlSeverityType.Error);
815 else if (ent.NotationName == null)
816 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);
818 case XmlTokenizedType.ENTITIES:
819 for (int i = 0; i < list.Length; i++) {
820 string entref = list [i];
821 ent = dtd.EntityDecls [FilterNormalization (reader.Name, entref)];
823 HandleError ("Reference to undeclared entity was found in attribute: " + reader.Name + ".", XmlSeverityType.Error);
824 else if (ent.NotationName == null)
825 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);
828 // case XmlTokenizedType.NMTOKEN: nothing to do
829 // case XmlTokenizedType.NMTOKENS: nothing to do
832 if (isStandalone && !def.IsInternalSubset &&
833 attrValue != normalized)
834 HandleError ("In standalone document, attribute value characters must not be checked against external definition.", XmlSeverityType.Error);
836 if (def.OccurenceType ==
837 DTDAttributeOccurenceType.Fixed &&
838 attrValue != def.DefaultValue)
839 HandleError (String.Format ("Fixed attribute {0} in element {1} has invalid value {2}.",
840 def.Name, decl.Name, attrValue),
841 XmlSeverityType.Error);
845 VerifyDeclaredAttributes (decl);
850 private void VerifyDeclaredAttributes (DTDAttListDeclaration decl)
852 // Check if all required attributes exist, and/or
853 // if there is default values, then add them.
854 for (int i = 0; i < decl.Definitions.Count; i++) {
855 DTDAttributeDefinition def = (DTDAttributeDefinition) decl.Definitions [i];
856 if (attributes.Contains (def.Name))
859 if (def.OccurenceType == DTDAttributeOccurenceType.Required) {
860 HandleError (String.Format ("Required attribute {0} in element {1} not found .",
861 def.Name, decl.Name),
862 XmlSeverityType.Error);
866 else if (def.DefaultValue == null)
869 if (this.isStandalone && !def.IsInternalSubset)
870 HandleError ("In standalone document, external default value definition must not be applied.", XmlSeverityType.Error);
872 switch (validatingReader.ValidationType) {
873 case ValidationType.Auto:
874 if (validatingReader.Schemas.Count == 0)
875 goto case ValidationType.DTD;
877 case ValidationType.DTD:
878 case ValidationType.None:
879 // Other than them, ignore DTD defaults.
880 attributes.Add (def.Name);
881 int colonAt = def.Name.IndexOf (':');
882 attributeLocalNames.Add (def.Name,
883 colonAt < 0 ? def.Name :
884 def.Name.Substring (colonAt + 1));
885 string prefix = colonAt < 0 ?
887 def.Name.Substring (0, colonAt);
888 attributePrefixes.Add (def.Name, prefix);
889 attributeNamespaces.Add (def.Name, reader.LookupNamespace (prefix));
890 attributeValues.Add (def.Name, def.DefaultValue);
896 public override bool ReadAttributeValue ()
898 if (consumedAttribute)
900 if (NodeType == XmlNodeType.Attribute &&
901 currentEntityHandling == EntityHandling.ExpandEntities) {
902 consumedAttribute = true;
905 else if (IsDefault) {
906 consumedAttribute = true;
910 return reader.ReadAttributeValue ();
914 public override string ReadInnerXml ()
916 // MS.NET 1.0 has a serious bug here. It skips validation.
917 return reader.ReadInnerXml ();
920 public override string ReadOuterXml ()
922 // MS.NET 1.0 has a serious bug here. It skips validation.
923 return reader.ReadOuterXml ();
927 public override string ReadString ()
929 // It seems to be the same as ReadInnerXml().
930 return base.ReadStringInternal ();
933 public override void ResolveEntity ()
935 if (resolver == null)
938 // "reader." is required since NodeType must not be entityref by nature.
939 if (reader.NodeType != XmlNodeType.EntityReference)
940 throw new InvalidOperationException ("The current node is not an Entity Reference");
941 DTDEntityDeclaration entity = DTD != null ? DTD.EntityDecls [reader.Name] as DTDEntityDeclaration : null;
943 XmlNodeType xmlReaderNodeType =
944 (currentAttribute != null) ? XmlNodeType.Attribute : XmlNodeType.Element;
948 throw NotWFError (String.Format ("Reference to undeclared entity '{0}'.", reader.Name));
951 // MS.NET 1.x ignores undeclared entity reference here..
952 if (entity != null && entity.SystemId != null) {
953 Uri baseUri = entity.BaseURI == null ? null : new Uri (entity.BaseURI);
954 Stream stream = resolver.GetEntity (resolver.ResolveUri (baseUri, entity.SystemId), null, typeof (Stream)) as Stream;
955 nextEntityReader = new XmlTextReader (stream, xmlReaderNodeType, ParserContext);
957 string replacementText =
958 (entity != null) ? entity.EntityValue : String.Empty;
959 nextEntityReader = new XmlTextReader (replacementText, xmlReaderNodeType, ParserContext);
961 nextEntityReader.XmlResolver = resolver;
964 public override int AttributeCount {
966 if (currentTextValue != null)
969 if (dtd == null || !insideContent)
970 return reader.AttributeCount;
972 return attributes.Count;
976 public override string BaseURI {
978 return reader.BaseURI;
982 public override bool CanResolveEntity {
986 public override int Depth {
988 int baseNum = reader.Depth;
989 if (entityReaderDepthStack.Count > 0) {
990 baseNum += (int) entityReaderDepthStack.Peek ();
991 if (NodeType != XmlNodeType.EndEntity)
994 if (currentTextValue != null && reader.NodeType == XmlNodeType.EndElement)
997 return IsDefault ? baseNum + 1 : baseNum;
1001 public override bool EOF {
1002 get { return reader.EOF && entityReaderStack.Count == 0; }
1005 public override bool HasValue {
1007 return IsDefault ? true :
1008 currentTextValue != null ? true :
1012 public override bool IsDefault {
1014 if (currentTextValue != null)
1016 if (currentAttribute == null)
1018 return reader.GetAttribute (currentAttribute) == null;
1022 public override bool IsEmptyElement {
1024 if (currentTextValue != null)
1026 return reader.IsEmptyElement;
1030 public override string this [int i] {
1031 get { return GetAttribute (i); }
1034 public override string this [string name] {
1035 get { return GetAttribute (name); }
1038 public override string this [string name, string ns] {
1039 get { return GetAttribute (name, ns); }
1042 public int LineNumber {
1044 IXmlLineInfo info = reader as IXmlLineInfo;
1045 return (info != null) ? info.LineNumber : 0;
1049 public int LinePosition {
1051 IXmlLineInfo info = reader as IXmlLineInfo;
1052 return (info != null) ? info.LinePosition : 0;
1056 public override string LocalName {
1058 if (currentTextValue != null || consumedAttribute)
1059 return String.Empty;
1060 if (entityReaderStack.Count > 0 && reader.EOF)
1061 return ((XmlReader) entityReaderStack.Peek ()).LocalName; // name of EndEntity
1062 else if (NodeType == XmlNodeType.Attribute)
1063 return (string) attributeLocalNames [currentAttribute];
1065 return String.Empty;
1067 return reader.LocalName;
1071 public override string Name {
1073 if (currentTextValue != null || consumedAttribute)
1074 return String.Empty;
1075 if (entityReaderStack.Count > 0 && reader.EOF)
1076 return ((XmlReader) entityReaderStack.Peek ()).Name; // name of EndEntity
1077 else if (NodeType == XmlNodeType.Attribute)
1078 return currentAttribute;
1080 return String.Empty;
1086 public override string NamespaceURI {
1088 if (currentTextValue != null || consumedAttribute)
1089 return String.Empty;
1090 else if (NodeType == XmlNodeType.Attribute)
1091 return (string) attributeNamespaces [currentAttribute];
1093 return String.Empty;
1094 else if (nsdecls.Count > 0 && nsdecls [Prefix] != null)
1095 return (string) nsdecls [Prefix];
1097 return reader.NamespaceURI;
1101 public override XmlNameTable NameTable {
1102 get { return reader.NameTable; }
1105 public override XmlNodeType NodeType {
1107 if (currentTextValue != null)
1108 return isSignificantWhitespace ? XmlNodeType.SignificantWhitespace :
1109 isWhitespace ? XmlNodeType.Whitespace :
1112 if (entityReaderStack.Count > 0 && reader.EOF)
1113 return XmlNodeType.EndEntity;
1115 // If consumedAttribute is true, then entities must be resolved.
1116 return consumedAttribute ? XmlNodeType.Text :
1117 IsDefault ? XmlNodeType.Attribute :
1122 public XmlParserContext ParserContext {
1123 get { return XmlSchemaUtil.GetParserContext (reader); }
1126 public override string Prefix {
1128 if (currentTextValue != null || consumedAttribute)
1129 return String.Empty;
1130 else if (NodeType == XmlNodeType.Attribute)
1131 return (string) attributePrefixes [currentAttribute];
1133 return String.Empty;
1135 return reader.Prefix;
1139 public override char QuoteChar {
1141 // If it is not actually on an attribute, then it returns
1142 // undefined value or '"'.
1143 return reader.QuoteChar;
1147 public override ReadState ReadState {
1149 if (reader.ReadState == ReadState.EndOfFile && currentTextValue != null)
1150 return ReadState.Interactive;
1151 return reader.ReadState;
1155 public object SchemaType {
1157 if (currentElement == null)
1159 DTDAttListDeclaration decl =
1160 DTD.AttListDecls [currentElement];
1161 DTDAttributeDefinition def =
1162 decl != null ? decl [currentAttribute] : null;
1163 return def != null ? def.Datatype : null;
1167 char [] whitespaceChars = new char [] {' '};
1168 private string FilterNormalization (string attrName, string rawValue)
1170 if (DTD == null || NodeType != XmlNodeType.Attribute ||
1171 sourceTextReader == null ||
1172 !sourceTextReader.Normalization)
1175 DTDAttributeDefinition def =
1176 dtd.AttListDecls [currentElement].Get (attrName);
1177 valueBuilder.Append (rawValue);
1178 valueBuilder.Replace ('\r', ' ');
1179 valueBuilder.Replace ('\n', ' ');
1180 valueBuilder.Replace ('\t', ' ');
1182 if (def.Datatype.TokenizedType == XmlTokenizedType.CDATA)
1183 return valueBuilder.ToString ();
1185 for (int i=0; i < valueBuilder.Length; i++) {
1186 if (valueBuilder [i] != ' ')
1188 while (++i < valueBuilder.Length && valueBuilder [i] == ' ')
1189 valueBuilder.Remove (i, 1);
1191 return valueBuilder.ToString ().Trim (whitespaceChars);
1193 valueBuilder.Length = 0;
1197 public override string Value {
1199 if (currentTextValue != null)
1200 return currentTextValue;
1201 // This check also covers value node of default attributes.
1203 DTDAttributeDefinition def =
1204 dtd.AttListDecls [currentElement] [currentAttribute] as DTDAttributeDefinition;
1205 return sourceTextReader != null && sourceTextReader.Normalization ?
1206 def.NormalizedDefaultValue : def.DefaultValue;
1208 // As to this property, MS.NET seems ignorant of EntityHandling...
1209 else if (NodeType == XmlNodeType.Attribute)// &&
1210 return FilterNormalization (Name, (string) attributeValues [currentAttribute]);
1211 else if (consumedAttribute)
1212 return FilterNormalization (Name, (string) attributeValues [this.currentAttribute]);
1214 return FilterNormalization (Name, reader.Value);
1218 public override string XmlLang {
1220 string val = this ["xml:lang"];
1221 return val != null ? val : reader.XmlLang;
1225 public XmlResolver XmlResolver {
1231 public override XmlSpace XmlSpace {
1233 string val = this ["xml:space"];
1236 return XmlSpace.Preserve;
1238 return XmlSpace.Default;
1240 return reader.XmlSpace;