1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
//------------------------------------------------------------------------------
// <copyright file="BaseValidator.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
//------------------------------------------------------------------------------
namespace System.Xml.Schema {
using System.IO;
using System.Diagnostics;
using System.Xml;
using System.Text;
using System.Collections;
#pragma warning disable 618
internal class BaseValidator {
XmlSchemaCollection schemaCollection;
IValidationEventHandling eventHandling;
XmlNameTable nameTable;
SchemaNames schemaNames;
PositionInfo positionInfo;
XmlResolver xmlResolver;
Uri baseUri;
protected SchemaInfo schemaInfo;
protected XmlValidatingReaderImpl reader;
protected XmlQualifiedName elementName;
protected ValidationState context;
protected StringBuilder textValue;
protected string textString;
protected bool hasSibling;
protected bool checkDatatype;
public BaseValidator(BaseValidator other) {
reader = other.reader;
schemaCollection = other.schemaCollection;
eventHandling = other.eventHandling;
nameTable = other.nameTable;
schemaNames = other.schemaNames;
positionInfo = other.positionInfo;
xmlResolver = other.xmlResolver;
baseUri = other.baseUri;
elementName = other.elementName;
}
public BaseValidator(XmlValidatingReaderImpl reader, XmlSchemaCollection schemaCollection, IValidationEventHandling eventHandling) {
Debug.Assert(schemaCollection == null || schemaCollection.NameTable == reader.NameTable);
this.reader = reader;
this.schemaCollection = schemaCollection;
this.eventHandling = eventHandling;
nameTable = reader.NameTable;
positionInfo = PositionInfo.GetPositionInfo(reader);
elementName = new XmlQualifiedName();
}
public XmlValidatingReaderImpl Reader {
get { return reader; }
}
public XmlSchemaCollection SchemaCollection {
get { return schemaCollection; }
}
public XmlNameTable NameTable {
get { return nameTable; }
}
public SchemaNames SchemaNames {
get {
if (schemaNames != null) {
return schemaNames;
}
if (schemaCollection != null) {
schemaNames = schemaCollection.GetSchemaNames(nameTable);
}
else {
schemaNames = new SchemaNames(nameTable);
}
return schemaNames;
}
}
public PositionInfo PositionInfo {
get { return positionInfo; }
}
public XmlResolver XmlResolver {
get { return xmlResolver; }
set { xmlResolver = value; }
}
public Uri BaseUri {
get { return baseUri; }
set { baseUri = value; }
}
public ValidationEventHandler EventHandler {
get { return (ValidationEventHandler)eventHandling.EventHandler; }
}
public SchemaInfo SchemaInfo {
get {
return schemaInfo;
}
set {
schemaInfo = value;
}
}
public IDtdInfo DtdInfo {
get {
return schemaInfo;
}
set {
SchemaInfo tmpSchemaInfo = value as SchemaInfo;
if (tmpSchemaInfo == null) {
throw new XmlException(Res.Xml_InternalError, string.Empty);
}
this.schemaInfo = tmpSchemaInfo;
}
}
public virtual bool PreserveWhitespace {
get {
return false;
}
}
public virtual void Validate() {
}
public virtual void CompleteValidation() {
}
public virtual object FindId(string name) {
return null;
}
public void ValidateText() {
if (context.NeedValidateChildren) {
if (context.IsNill) {
SendValidationEvent(Res.Sch_ContentInNill, XmlSchemaValidator.QNameString(context.LocalName, context.Namespace));
return;
}
ContentValidator contentValidator = context.ElementDecl.ContentValidator;
XmlSchemaContentType contentType = contentValidator.ContentType;
if (contentType == XmlSchemaContentType.ElementOnly) {
ArrayList names = contentValidator.ExpectedElements(context, false);
if (names == null) {
SendValidationEvent(Res.Sch_InvalidTextInElement, XmlSchemaValidator.BuildElementName(context.LocalName, context.Namespace));
}
else {
Debug.Assert(names.Count > 0);
SendValidationEvent(Res.Sch_InvalidTextInElementExpecting, new string[] { XmlSchemaValidator.BuildElementName(context.LocalName, context.Namespace), XmlSchemaValidator.PrintExpectedElements(names, false) });
}
}
else if (contentType == XmlSchemaContentType.Empty) {
SendValidationEvent(Res.Sch_InvalidTextInEmpty, string.Empty);
}
if (checkDatatype) {
SaveTextValue(reader.Value);
}
}
}
public void ValidateWhitespace() {
if (context.NeedValidateChildren) {
XmlSchemaContentType contentType = context.ElementDecl.ContentValidator.ContentType;
if (context.IsNill) {
SendValidationEvent(Res.Sch_ContentInNill, XmlSchemaValidator.QNameString(context.LocalName, context.Namespace));
}
if (contentType == XmlSchemaContentType.Empty) {
SendValidationEvent(Res.Sch_InvalidWhitespaceInEmpty, string.Empty);
}
if (checkDatatype) {
SaveTextValue(reader.Value);
}
}
}
private void SaveTextValue(string value) {
if (textString.Length == 0) {
textString = value;
}
else {
if (!hasSibling) {
textValue.Append(textString);
hasSibling = true;
}
textValue.Append(value);
}
}
protected void SendValidationEvent(string code) {
SendValidationEvent(code, string.Empty);
}
protected void SendValidationEvent(string code, string[] args) {
SendValidationEvent(new XmlSchemaException(code, args, reader.BaseURI, positionInfo.LineNumber, positionInfo.LinePosition));
}
protected void SendValidationEvent(string code, string arg) {
SendValidationEvent(new XmlSchemaException(code, arg, reader.BaseURI, positionInfo.LineNumber, positionInfo.LinePosition));
}
protected void SendValidationEvent(string code, string arg1, string arg2) {
SendValidationEvent(new XmlSchemaException(code, new string[] { arg1, arg2 }, reader.BaseURI, positionInfo.LineNumber, positionInfo.LinePosition));
}
protected void SendValidationEvent(XmlSchemaException e) {
SendValidationEvent(e, XmlSeverityType.Error);
}
protected void SendValidationEvent(string code, string msg, XmlSeverityType severity) {
SendValidationEvent(new XmlSchemaException(code, msg, reader.BaseURI, positionInfo.LineNumber, positionInfo.LinePosition), severity);
}
protected void SendValidationEvent(string code, string[] args, XmlSeverityType severity) {
SendValidationEvent(new XmlSchemaException(code, args, reader.BaseURI, positionInfo.LineNumber, positionInfo.LinePosition), severity);
}
protected void SendValidationEvent(XmlSchemaException e, XmlSeverityType severity) {
if (eventHandling != null) {
eventHandling.SendEvent(e, severity);
}
else if (severity == XmlSeverityType.Error) {
throw e;
}
}
protected static void ProcessEntity(SchemaInfo sinfo, string name, object sender, ValidationEventHandler eventhandler, string baseUri, int lineNumber, int linePosition) {
SchemaEntity en;
XmlSchemaException e = null;
if (!sinfo.GeneralEntities.TryGetValue(new XmlQualifiedName(name), out en)) {
// validation error, see xml spec [68]
e = new XmlSchemaException(Res.Sch_UndeclaredEntity, name, baseUri, lineNumber, linePosition);
}
else if (en.NData.IsEmpty) {
e = new XmlSchemaException(Res.Sch_UnparsedEntityRef, name, baseUri, lineNumber, linePosition);
}
if (e != null) {
if (eventhandler != null) {
eventhandler(sender, new ValidationEventArgs(e));
}
else {
throw e;
}
}
}
protected static void ProcessEntity(SchemaInfo sinfo, string name, IValidationEventHandling eventHandling, string baseUriStr, int lineNumber, int linePosition) {
SchemaEntity en;
string errorResId = null;
if (!sinfo.GeneralEntities.TryGetValue(new XmlQualifiedName(name), out en)) {
// validation error, see xml spec [68]
errorResId = Res.Sch_UndeclaredEntity;
}
else if (en.NData.IsEmpty) {
errorResId = Res.Sch_UnparsedEntityRef;
}
if (errorResId != null) {
XmlSchemaException e = new XmlSchemaException(errorResId, name, baseUriStr, lineNumber, linePosition);
if (eventHandling != null) {
eventHandling.SendEvent(e, XmlSeverityType.Error);
}
else {
throw e;
}
}
}
public static BaseValidator CreateInstance(ValidationType valType, XmlValidatingReaderImpl reader, XmlSchemaCollection schemaCollection, IValidationEventHandling eventHandling, bool processIdentityConstraints) {
switch(valType) {
case ValidationType.XDR:
return new XdrValidator(reader, schemaCollection, eventHandling);
case ValidationType.Schema:
return new XsdValidator(reader, schemaCollection, eventHandling);
case ValidationType.DTD:
return new DtdValidator(reader, eventHandling, processIdentityConstraints);
case ValidationType.Auto:
return new AutoValidator(reader, schemaCollection, eventHandling);
case ValidationType.None:
return new BaseValidator(reader, schemaCollection, eventHandling);
default:
break;
}
return null;
}
}
#pragma warning restore 618
}
|