1 //------------------------------------------------------------------------------
2 // <copyright file="BaseValidator.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 // <owner current="true" primary="true">Microsoft</owner>
6 //------------------------------------------------------------------------------
8 namespace System
.Xml
.Schema
{
11 using System
.Diagnostics
;
14 using System
.Collections
;
16 #pragma warning disable 618
18 internal class BaseValidator
{
19 XmlSchemaCollection schemaCollection
;
20 IValidationEventHandling eventHandling
;
21 XmlNameTable nameTable
;
22 SchemaNames schemaNames
;
23 PositionInfo positionInfo
;
24 XmlResolver xmlResolver
;
27 protected SchemaInfo schemaInfo
;
28 protected XmlValidatingReaderImpl reader
;
29 protected XmlQualifiedName elementName
;
30 protected ValidationState context
;
31 protected StringBuilder textValue
;
32 protected string textString
;
33 protected bool hasSibling
;
34 protected bool checkDatatype
;
36 public BaseValidator(BaseValidator other
) {
37 reader
= other
.reader
;
38 schemaCollection
= other
.schemaCollection
;
39 eventHandling
= other
.eventHandling
;
40 nameTable
= other
.nameTable
;
41 schemaNames
= other
.schemaNames
;
42 positionInfo
= other
.positionInfo
;
43 xmlResolver
= other
.xmlResolver
;
44 baseUri
= other
.baseUri
;
45 elementName
= other
.elementName
;
48 public BaseValidator(XmlValidatingReaderImpl reader
, XmlSchemaCollection schemaCollection
, IValidationEventHandling eventHandling
) {
49 Debug
.Assert(schemaCollection
== null || schemaCollection
.NameTable
== reader
.NameTable
);
51 this.schemaCollection
= schemaCollection
;
52 this.eventHandling
= eventHandling
;
53 nameTable
= reader
.NameTable
;
54 positionInfo
= PositionInfo
.GetPositionInfo(reader
);
55 elementName
= new XmlQualifiedName();
58 public XmlValidatingReaderImpl Reader
{
59 get { return reader; }
62 public XmlSchemaCollection SchemaCollection
{
63 get { return schemaCollection; }
66 public XmlNameTable NameTable
{
67 get { return nameTable; }
70 public SchemaNames SchemaNames
{
72 if (schemaNames
!= null) {
75 if (schemaCollection
!= null) {
76 schemaNames
= schemaCollection
.GetSchemaNames(nameTable
);
79 schemaNames
= new SchemaNames(nameTable
);
85 public PositionInfo PositionInfo
{
86 get { return positionInfo; }
89 public XmlResolver XmlResolver
{
90 get { return xmlResolver; }
91 set { xmlResolver = value; }
95 get { return baseUri; }
96 set { baseUri = value; }
99 public ValidationEventHandler EventHandler
{
100 get { return (ValidationEventHandler)eventHandling.EventHandler; }
103 public SchemaInfo SchemaInfo
{
112 public IDtdInfo DtdInfo
{
117 SchemaInfo tmpSchemaInfo
= value as SchemaInfo
;
118 if (tmpSchemaInfo
== null) {
119 throw new XmlException(Res
.Xml_InternalError
, string.Empty
);
121 this.schemaInfo
= tmpSchemaInfo
;
125 public virtual bool PreserveWhitespace
{
131 public virtual void Validate() {
134 public virtual void CompleteValidation() {
137 public virtual object FindId(string name
) {
141 public void ValidateText() {
142 if (context
.NeedValidateChildren
) {
143 if (context
.IsNill
) {
144 SendValidationEvent(Res
.Sch_ContentInNill
, XmlSchemaValidator
.QNameString(context
.LocalName
, context
.Namespace
));
147 ContentValidator contentValidator
= context
.ElementDecl
.ContentValidator
;
148 XmlSchemaContentType contentType
= contentValidator
.ContentType
;
149 if (contentType
== XmlSchemaContentType
.ElementOnly
) {
150 ArrayList names
= contentValidator
.ExpectedElements(context
, false);
152 SendValidationEvent(Res
.Sch_InvalidTextInElement
, XmlSchemaValidator
.BuildElementName(context
.LocalName
, context
.Namespace
));
155 Debug
.Assert(names
.Count
> 0);
156 SendValidationEvent(Res
.Sch_InvalidTextInElementExpecting
, new string[] { XmlSchemaValidator.BuildElementName(context.LocalName, context.Namespace), XmlSchemaValidator.PrintExpectedElements(names, false) }
);
159 else if (contentType
== XmlSchemaContentType
.Empty
) {
160 SendValidationEvent(Res
.Sch_InvalidTextInEmpty
, string.Empty
);
163 SaveTextValue(reader
.Value
);
168 public void ValidateWhitespace() {
169 if (context
.NeedValidateChildren
) {
170 XmlSchemaContentType contentType
= context
.ElementDecl
.ContentValidator
.ContentType
;
171 if (context
.IsNill
) {
172 SendValidationEvent(Res
.Sch_ContentInNill
, XmlSchemaValidator
.QNameString(context
.LocalName
, context
.Namespace
));
174 if (contentType
== XmlSchemaContentType
.Empty
) {
175 SendValidationEvent(Res
.Sch_InvalidWhitespaceInEmpty
, string.Empty
);
178 SaveTextValue(reader
.Value
);
183 private void SaveTextValue(string value) {
184 if (textString
.Length
== 0) {
189 textValue
.Append(textString
);
192 textValue
.Append(value);
196 protected void SendValidationEvent(string code
) {
197 SendValidationEvent(code
, string.Empty
);
200 protected void SendValidationEvent(string code
, string[] args
) {
201 SendValidationEvent(new XmlSchemaException(code
, args
, reader
.BaseURI
, positionInfo
.LineNumber
, positionInfo
.LinePosition
));
204 protected void SendValidationEvent(string code
, string arg
) {
205 SendValidationEvent(new XmlSchemaException(code
, arg
, reader
.BaseURI
, positionInfo
.LineNumber
, positionInfo
.LinePosition
));
208 protected void SendValidationEvent(string code
, string arg1
, string arg2
) {
209 SendValidationEvent(new XmlSchemaException(code
, new string[] { arg1, arg2 }
, reader
.BaseURI
, positionInfo
.LineNumber
, positionInfo
.LinePosition
));
212 protected void SendValidationEvent(XmlSchemaException e
) {
213 SendValidationEvent(e
, XmlSeverityType
.Error
);
216 protected void SendValidationEvent(string code
, string msg
, XmlSeverityType severity
) {
217 SendValidationEvent(new XmlSchemaException(code
, msg
, reader
.BaseURI
, positionInfo
.LineNumber
, positionInfo
.LinePosition
), severity
);
220 protected void SendValidationEvent(string code
, string[] args
, XmlSeverityType severity
) {
221 SendValidationEvent(new XmlSchemaException(code
, args
, reader
.BaseURI
, positionInfo
.LineNumber
, positionInfo
.LinePosition
), severity
);
224 protected void SendValidationEvent(XmlSchemaException e
, XmlSeverityType severity
) {
225 if (eventHandling
!= null) {
226 eventHandling
.SendEvent(e
, severity
);
228 else if (severity
== XmlSeverityType
.Error
) {
233 protected static void ProcessEntity(SchemaInfo sinfo
, string name
, object sender
, ValidationEventHandler eventhandler
, string baseUri
, int lineNumber
, int linePosition
) {
235 XmlSchemaException e
= null;
236 if (!sinfo
.GeneralEntities
.TryGetValue(new XmlQualifiedName(name
), out en
)) {
237 // validation error, see xml spec [68]
238 e
= new XmlSchemaException(Res
.Sch_UndeclaredEntity
, name
, baseUri
, lineNumber
, linePosition
);
240 else if (en
.NData
.IsEmpty
) {
241 e
= new XmlSchemaException(Res
.Sch_UnparsedEntityRef
, name
, baseUri
, lineNumber
, linePosition
);
244 if (eventhandler
!= null) {
245 eventhandler(sender
, new ValidationEventArgs(e
));
253 protected static void ProcessEntity(SchemaInfo sinfo
, string name
, IValidationEventHandling eventHandling
, string baseUriStr
, int lineNumber
, int linePosition
) {
255 string errorResId
= null;
256 if (!sinfo
.GeneralEntities
.TryGetValue(new XmlQualifiedName(name
), out en
)) {
257 // validation error, see xml spec [68]
258 errorResId
= Res
.Sch_UndeclaredEntity
;
261 else if (en
.NData
.IsEmpty
) {
262 errorResId
= Res
.Sch_UnparsedEntityRef
;
264 if (errorResId
!= null) {
265 XmlSchemaException e
= new XmlSchemaException(errorResId
, name
, baseUriStr
, lineNumber
, linePosition
);
267 if (eventHandling
!= null) {
268 eventHandling
.SendEvent(e
, XmlSeverityType
.Error
);
276 public static BaseValidator
CreateInstance(ValidationType valType
, XmlValidatingReaderImpl reader
, XmlSchemaCollection schemaCollection
, IValidationEventHandling eventHandling
, bool processIdentityConstraints
) {
278 case ValidationType
.XDR
:
279 return new XdrValidator(reader
, schemaCollection
, eventHandling
);
281 case ValidationType
.Schema
:
282 return new XsdValidator(reader
, schemaCollection
, eventHandling
);
284 case ValidationType
.DTD
:
285 return new DtdValidator(reader
, eventHandling
, processIdentityConstraints
);
287 case ValidationType
.Auto
:
288 return new AutoValidator(reader
, schemaCollection
, eventHandling
);
290 case ValidationType
.None
:
291 return new BaseValidator(reader
, schemaCollection
, eventHandling
);
300 #pragma warning restore 618