Imported Upstream version 4.0.0~alpha1

Former-commit-id: 806294f5ded97629b74c85c09952f2a74fe182d9
This commit is contained in:
Jo Shields
2015-04-07 09:35:12 +01:00
parent 283343f570
commit 3c1f479b9d
22469 changed files with 2931443 additions and 869343 deletions

View File

@@ -0,0 +1,46 @@
;==++==
;
; Copyright (c) Microsoft Corporation. All rights reserved.
;
;==--==
; NOTE: do not use \", use ' instead
; NOTE: Use # or ; for comments
; These are the managed resources for System.Xml.Linq.dll. See
; ResourceManager documentation and the ResGen tool.
Argument_AddAttribute=An attribute cannot be added to content.
Argument_AddNode=A node of type {0} cannot be added to content.
Argument_AddNonWhitespace=Non white space characters cannot be added to content.
Argument_ConvertToString=The argument cannot be converted to a string.
Argument_CreateNavigator=This XPathNavigator cannot be created on a node of type {0}.
Argument_InvalidExpandedName='{0}' is an invalid expanded name.
Argument_InvalidPIName='{0}' is an invalid name for a processing instruction.
Argument_InvalidPrefix='{0}' is an invalid prefix.
Argument_MustBeDerivedFrom=The argument must be derived from {0}.
Argument_NamespaceDeclarationPrefixed=The prefix '{0}' cannot be bound to the empty namespace name.
Argument_NamespaceDeclarationXml=The prefix 'xml' is bound to the namespace name 'http://www.w3.org/XML/1998/namespace'. Other prefixes must not be bound to this namespace name, and it must not be declared as the default namespace.
Argument_NamespaceDeclarationXmlns=The prefix 'xmlns' is bound to the namespace name 'http://www.w3.org/2000/xmlns/'. It must not be declared. Other prefixes must not be bound to this namespace name, and it must not be declared as the default namespace.
Argument_XObjectValue=An XObject cannot be used as a value.
InvalidOperation_BadNodeType=This operation is not valid on a node of type {0}.
InvalidOperation_DocumentStructure=This operation would create an incorrectly structured document.
InvalidOperation_DuplicateAttribute=Duplicate attribute.
InvalidOperation_ExpectedEndOfFile=The XmlReader state should be EndOfFile after this operation.
InvalidOperation_ExpectedInteractive=The XmlReader state should be Interactive.
InvalidOperation_ExpectedNodeType=The XmlReader must be on a node of type {0} instead of a node of type {1}.
InvalidOperation_ExternalCode=This operation was corrupted by external code.
InvalidOperation_DeserializeInstance=This instance cannot be deserialized.
InvalidOperation_MissingAncestor=A common ancestor is missing.
InvalidOperation_MissingParent=The parent is missing.
InvalidOperation_MissingRoot=The root element is missing.
InvalidOperation_UnexpectedEvaluation=The XPath expression evaluated to unexpected type {0}.
InvalidOperation_UnexpectedNodeType=The XmlReader should not be on a node of type {0}.
InvalidOperation_UnresolvedEntityReference=The XmlReader cannot resolve entity references.
InvalidOperation_WriteAttribute=An attribute cannot be written after content.
NotSupported_CheckValidity=This XPathNavigator does not support XSD validation.
NotSupported_MoveToId=This XPathNavigator does not support IDs.
NotSupported_WriteBase64=This XmlWriter does not support base64 encoded data.
NotSupported_WriteEntityRef=This XmlWriter does not support entity references.

View File

@@ -0,0 +1,463 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Xml.Linq;
namespace MS.Internal.Xml.Linq.ComponentModel
{
[SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses", Justification="False positive.")]
class XTypeDescriptionProvider<T> : TypeDescriptionProvider
{
public XTypeDescriptionProvider() : base(TypeDescriptor.GetProvider(typeof(T))) {
}
public override ICustomTypeDescriptor GetTypeDescriptor(Type type, object instance) {
return new XTypeDescriptor<T>(base.GetTypeDescriptor(type, instance));
}
}
class XTypeDescriptor<T> : CustomTypeDescriptor
{
public XTypeDescriptor(ICustomTypeDescriptor parent) : base(parent) {
}
public override PropertyDescriptorCollection GetProperties() {
return GetProperties(null);
}
public override PropertyDescriptorCollection GetProperties(Attribute[] attributes) {
PropertyDescriptorCollection properties = new PropertyDescriptorCollection(null);
if (attributes == null) {
if (typeof(T) == typeof(XElement)) {
properties.Add(new XElementAttributePropertyDescriptor());
properties.Add(new XElementDescendantsPropertyDescriptor());
properties.Add(new XElementElementPropertyDescriptor());
properties.Add(new XElementElementsPropertyDescriptor());
properties.Add(new XElementValuePropertyDescriptor());
properties.Add(new XElementXmlPropertyDescriptor());
}
else if (typeof(T) == typeof(XAttribute)) {
properties.Add(new XAttributeValuePropertyDescriptor());
}
}
foreach (PropertyDescriptor property in base.GetProperties(attributes)) {
properties.Add(property);
}
return properties;
}
}
abstract class XPropertyDescriptor<T, TProperty> : PropertyDescriptor where T : XObject
{
public XPropertyDescriptor(string name) : base(name, null) {
}
public override Type ComponentType {
get { return typeof(T); }
}
public override bool IsReadOnly {
get { return true; }
}
public override Type PropertyType {
get { return typeof(TProperty); }
}
public override bool SupportsChangeEvents {
get { return true; }
}
public override void AddValueChanged(object component, EventHandler handler) {
bool hasValueChangedHandler = GetValueChangedHandler(component) != null;
base.AddValueChanged(component, handler);
if (hasValueChangedHandler) return;
T c = component as T;
if (c != null && GetValueChangedHandler(component) != null) {
c.Changing += new EventHandler<XObjectChangeEventArgs>(OnChanging);
c.Changed += new EventHandler<XObjectChangeEventArgs>(OnChanged);
}
}
public override bool CanResetValue(object component) {
return false;
}
public override void RemoveValueChanged(object component, EventHandler handler) {
base.RemoveValueChanged(component, handler);
T c = component as T;
if (c != null && GetValueChangedHandler(component) == null) {
c.Changing -= new EventHandler<XObjectChangeEventArgs>(OnChanging);
c.Changed -= new EventHandler<XObjectChangeEventArgs>(OnChanged);
}
}
public override void ResetValue(object component) {
}
public override void SetValue(object component, object value) {
}
public override bool ShouldSerializeValue(object component) {
return false;
}
protected virtual void OnChanged(object sender, XObjectChangeEventArgs args) {
}
protected virtual void OnChanging(object sender, XObjectChangeEventArgs args) {
}
}
class XElementAttributePropertyDescriptor : XPropertyDescriptor<XElement, object>
{
XDeferredSingleton<XAttribute> value;
XAttribute changeState;
public XElementAttributePropertyDescriptor() : base("Attribute") {
}
public override object GetValue(object component) {
return value = new XDeferredSingleton<XAttribute>((e, n) => e.Attribute(n), component as XElement, null);
}
protected override void OnChanged(object sender, XObjectChangeEventArgs args) {
if (value == null) return;
switch (args.ObjectChange) {
case XObjectChange.Add:
XAttribute a = sender as XAttribute;
if (a != null && value.element == a.parent && value.name == a.Name) {
OnValueChanged(value.element, EventArgs.Empty);
}
break;
case XObjectChange.Remove:
a = sender as XAttribute;
if (a != null && changeState == a) {
changeState = null;
OnValueChanged(value.element, EventArgs.Empty);
}
break;
}
}
protected override void OnChanging(object sender, XObjectChangeEventArgs args) {
if (value == null) return;
switch (args.ObjectChange) {
case XObjectChange.Remove:
XAttribute a = sender as XAttribute;
changeState = a != null && value.element == a.parent && value.name == a.Name ? a : null;
break;
}
}
}
class XElementDescendantsPropertyDescriptor : XPropertyDescriptor<XElement, IEnumerable<XElement>>
{
XDeferredAxis<XElement> value;
XName changeState;
public XElementDescendantsPropertyDescriptor() : base("Descendants") {
}
public override object GetValue(object component) {
return value = new XDeferredAxis<XElement>((e, n) => n != null ? e.Descendants(n) : e.Descendants(), component as XElement, null);
}
protected override void OnChanged(object sender, XObjectChangeEventArgs args) {
if (value == null) return;
switch (args.ObjectChange) {
case XObjectChange.Add:
case XObjectChange.Remove:
XElement e = sender as XElement;
if (e != null && (value.name == e.Name || value.name == null)) {
OnValueChanged(value.element, EventArgs.Empty);
}
break;
case XObjectChange.Name:
e = sender as XElement;
if (e != null && value.element != e && value.name != null && (value.name == e.Name || value.name == changeState)) {
changeState = null;
OnValueChanged(value.element, EventArgs.Empty);
}
break;
}
}
protected override void OnChanging(object sender, XObjectChangeEventArgs args) {
if (value == null) return;
switch (args.ObjectChange) {
case XObjectChange.Name:
XElement e = sender as XElement;
changeState = e != null ? e.Name : null;
break;
}
}
}
class XElementElementPropertyDescriptor : XPropertyDescriptor<XElement, object>
{
XDeferredSingleton<XElement> value;
XElement changeState;
public XElementElementPropertyDescriptor() : base("Element") {
}
public override object GetValue(object component) {
return value = new XDeferredSingleton<XElement>((e, n) => e.Element(n), component as XElement, null);
}
protected override void OnChanged(object sender, XObjectChangeEventArgs args) {
if (value == null) return;
switch (args.ObjectChange) {
case XObjectChange.Add:
XElement e = sender as XElement;
if (e != null && value.element == e.parent && value.name == e.Name && value.element.Element(value.name) == e) {
OnValueChanged(value.element, EventArgs.Empty);
}
break;
case XObjectChange.Remove:
e = sender as XElement;
if (e != null && changeState == e) {
changeState = null;
OnValueChanged(value.element, EventArgs.Empty);
}
break;
case XObjectChange.Name:
e = sender as XElement;
if (e != null) {
if (value.element == e.parent && value.name == e.Name && value.element.Element(value.name) == e) {
OnValueChanged(value.element, EventArgs.Empty);
}
else if (changeState == e) {
changeState = null;
OnValueChanged(value.element, EventArgs.Empty);
}
}
break;
}
}
protected override void OnChanging(object sender, XObjectChangeEventArgs args) {
if (value == null) return;
switch (args.ObjectChange) {
case XObjectChange.Remove:
case XObjectChange.Name:
XElement e = sender as XElement;
changeState = e != null && value.element == e.parent && value.name == e.Name && value.element.Element(value.name) == e ? e : null;
break;
}
}
}
class XElementElementsPropertyDescriptor : XPropertyDescriptor<XElement, IEnumerable<XElement>>
{
XDeferredAxis<XElement> value;
object changeState;
public XElementElementsPropertyDescriptor() : base("Elements") {
}
public override object GetValue(object component) {
return value = new XDeferredAxis<XElement>((e, n) => n != null ? e.Elements(n) : e.Elements(), component as XElement, null);
}
protected override void OnChanged(object sender, XObjectChangeEventArgs args) {
if (value == null) return;
switch (args.ObjectChange) {
case XObjectChange.Add:
XElement e = sender as XElement;
if (e != null && value.element == e.parent && (value.name == e.Name || value.name == null)) {
OnValueChanged(value.element, EventArgs.Empty);
}
break;
case XObjectChange.Remove:
e = sender as XElement;
if (e != null && value.element == (changeState as XContainer) && (value.name == e.Name || value.name == null)) {
changeState = null;
OnValueChanged(value.element, EventArgs.Empty);
}
break;
case XObjectChange.Name:
e = sender as XElement;
if (e != null && value.element == e.parent && value.name != null && (value.name == e.Name || value.name == (changeState as XName))) {
changeState = null;
OnValueChanged(value.element, EventArgs.Empty);
}
break;
}
}
protected override void OnChanging(object sender, XObjectChangeEventArgs args) {
if (value == null) return;
switch (args.ObjectChange) {
case XObjectChange.Remove:
XElement e = sender as XElement;
changeState = e != null ? e.parent : null;
break;
case XObjectChange.Name:
e = sender as XElement;
changeState = e != null ? e.Name : null;
break;
}
}
}
class XElementValuePropertyDescriptor : XPropertyDescriptor<XElement, string>
{
XElement element;
public XElementValuePropertyDescriptor() : base("Value") {
}
public override bool IsReadOnly {
get { return false; }
}
public override object GetValue(object component) {
element = component as XElement;
if (element == null) return string.Empty;
return element.Value;
}
public override void SetValue(object component, object value) {
element = component as XElement;
if (element == null) return;
element.Value = value as string;
}
protected override void OnChanged(object sender, XObjectChangeEventArgs args) {
if (element == null) return;
switch (args.ObjectChange) {
case XObjectChange.Add:
case XObjectChange.Remove:
if (sender is XElement || sender is XText) {
OnValueChanged(element, EventArgs.Empty);
}
break;
case XObjectChange.Value:
if (sender is XText) {
OnValueChanged(element, EventArgs.Empty);
}
break;
}
}
}
class XElementXmlPropertyDescriptor : XPropertyDescriptor<XElement, string>
{
XElement element;
public XElementXmlPropertyDescriptor() : base("Xml") {
}
public override object GetValue(object component) {
element = component as XElement;
if (element == null) return string.Empty;
return element.ToString(SaveOptions.DisableFormatting);
}
protected override void OnChanged(object sender, XObjectChangeEventArgs args) {
if (element == null) return;
OnValueChanged(element, EventArgs.Empty);
}
}
class XAttributeValuePropertyDescriptor : XPropertyDescriptor<XAttribute, string>
{
XAttribute attribute;
public XAttributeValuePropertyDescriptor() : base("Value") {
}
public override bool IsReadOnly {
get { return false; }
}
public override object GetValue(object component) {
attribute = component as XAttribute;
if (attribute == null) return string.Empty;
return attribute.Value;
}
public override void SetValue(object component, object value) {
attribute = component as XAttribute;
if (attribute == null) return;
attribute.Value = value as string;
}
protected override void OnChanged(object sender, XObjectChangeEventArgs args) {
if (attribute == null) return;
if (args.ObjectChange == XObjectChange.Value) {
OnValueChanged(attribute, EventArgs.Empty);
}
}
}
class XDeferredAxis<T> : IEnumerable<T>, IEnumerable where T : XObject
{
Func<XElement, XName, IEnumerable<T>> func;
internal XElement element;
internal XName name;
public XDeferredAxis(Func<XElement, XName, IEnumerable<T>> func, XElement element, XName name) {
if (func == null) throw new ArgumentNullException("func");
if (element == null) throw new ArgumentNullException("element");
this.func = func;
this.element = element;
this.name = name;
}
public IEnumerator<T> GetEnumerator() {
return func(element, name).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}
public IEnumerable<T> this[string expandedName] {
get {
if (expandedName == null) throw new ArgumentNullException("expandedName");
if (name == null) {
name = expandedName;
}
else if (name != expandedName) {
return Enumerable.Empty<T>();
}
return this;
}
}
}
class XDeferredSingleton<T> where T : XObject
{
Func<XElement, XName, T> func;
internal XElement element;
internal XName name;
public XDeferredSingleton(Func<XElement, XName, T> func, XElement element, XName name) {
if (func == null) throw new ArgumentNullException("func");
if (element == null) throw new ArgumentNullException("element");
this.func = func;
this.element = element;
this.name = name;
}
public T this[string expandedName] {
get {
if (expandedName == null) throw new ArgumentNullException("expandedName");
if (name == null) {
name = expandedName;
}
else if (name != expandedName) {
return null;
}
return func(element, name);
}
}
}
}

View File

@@ -0,0 +1 @@
3ee2f5565bb7e628ec1aac3722fc4af9fb183d65

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,485 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Xml;
using System.Xml.Linq;
using System.Runtime.Versioning;
namespace System.Xml.Schema
{
internal class XNodeValidator
{
XmlSchemaSet schemas;
ValidationEventHandler validationEventHandler;
XObject source;
bool addSchemaInfo;
XmlNamespaceManager namespaceManager;
XmlSchemaValidator validator;
Dictionary<XmlSchemaInfo, XmlSchemaInfo> schemaInfos;
ArrayList defaultAttributes;
XName xsiTypeName;
XName xsiNilName;
public XNodeValidator(XmlSchemaSet schemas, ValidationEventHandler validationEventHandler) {
this.schemas = schemas;
this.validationEventHandler = validationEventHandler;
XNamespace xsi = XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance");
xsiTypeName = xsi.GetName("type");
xsiNilName = xsi.GetName("nil");
}
public void Validate(XObject source, XmlSchemaObject partialValidationType, bool addSchemaInfo) {
this.source = source;
this.addSchemaInfo = addSchemaInfo;
XmlSchemaValidationFlags validationFlags = XmlSchemaValidationFlags.AllowXmlAttributes;
XmlNodeType nt = source.NodeType;
switch (nt) {
case XmlNodeType.Document:
source = ((XDocument)source).Root;
if (source == null) throw new InvalidOperationException(System.Xml.Linq.Res.GetString(System.Xml.Linq.Res.InvalidOperation_MissingRoot));
validationFlags |= XmlSchemaValidationFlags.ProcessIdentityConstraints;
break;
case XmlNodeType.Element:
break;
case XmlNodeType.Attribute:
if (((XAttribute)source).IsNamespaceDeclaration) goto default;
if (source.Parent == null) throw new InvalidOperationException(System.Xml.Linq.Res.GetString(System.Xml.Linq.Res.InvalidOperation_MissingParent));
break;
default:
throw new InvalidOperationException(System.Xml.Linq.Res.GetString(System.Xml.Linq.Res.InvalidOperation_BadNodeType, nt));
}
namespaceManager = new XmlNamespaceManager(schemas.NameTable);
PushAncestorsAndSelf(source.Parent);
validator = new XmlSchemaValidator(schemas.NameTable, schemas, namespaceManager, validationFlags);
validator.ValidationEventHandler += new ValidationEventHandler(ValidationCallback);
validator.XmlResolver = null;
if (partialValidationType != null) {
validator.Initialize(partialValidationType);
}
else {
validator.Initialize();
}
IXmlLineInfo orginal = SaveLineInfo(source);
if (nt == XmlNodeType.Attribute) {
ValidateAttribute((XAttribute)source);
}
else {
ValidateElement((XElement)source);
}
validator.EndValidation();
RestoreLineInfo(orginal);
}
XmlSchemaInfo GetDefaultAttributeSchemaInfo(XmlSchemaAttribute sa) {
XmlSchemaInfo si = new XmlSchemaInfo();
si.IsDefault = true;
si.IsNil = false;
si.SchemaAttribute = sa;
XmlSchemaSimpleType st = sa.AttributeSchemaType;
si.SchemaType = st;
if (st.Datatype.Variety == XmlSchemaDatatypeVariety.Union) {
string value = GetDefaultValue(sa);
foreach (XmlSchemaSimpleType mt in ((XmlSchemaSimpleTypeUnion)st.Content).BaseMemberTypes) {
object typedValue = null;
try {
typedValue = mt.Datatype.ParseValue(value, schemas.NameTable, namespaceManager);
}
catch (XmlSchemaException) {
}
if (typedValue != null) {
si.MemberType = mt;
break;
}
}
}
si.Validity = XmlSchemaValidity.Valid;
return si;
}
string GetDefaultValue(XmlSchemaAttribute sa) {
XmlQualifiedName name = sa.RefName;
if (!name.IsEmpty) {
sa = schemas.GlobalAttributes[name] as XmlSchemaAttribute;
if (sa == null) return null;
}
string s = sa.FixedValue;
if (s != null) return s;
return sa.DefaultValue;
}
string GetDefaultValue(XmlSchemaElement se) {
XmlQualifiedName name = se.RefName;
if (!name.IsEmpty) {
se = schemas.GlobalElements[name] as XmlSchemaElement;
if (se == null) return null;
}
string s = se.FixedValue;
if (s != null) return s;
return se.DefaultValue;
}
void ReplaceSchemaInfo(XObject o, XmlSchemaInfo schemaInfo) {
if (schemaInfos == null) {
schemaInfos = new Dictionary<XmlSchemaInfo, XmlSchemaInfo>(new XmlSchemaInfoEqualityComparer());
}
XmlSchemaInfo si = o.Annotation<XmlSchemaInfo>();
if (si != null) {
if (!schemaInfos.ContainsKey(si)) {
schemaInfos.Add(si, si);
}
o.RemoveAnnotations<XmlSchemaInfo>();
}
if (!schemaInfos.TryGetValue(schemaInfo, out si)) {
si = schemaInfo;
schemaInfos.Add(si, si);
}
o.AddAnnotation(si);
}
void PushAncestorsAndSelf(XElement e) {
while (e != null) {
XAttribute a = e.lastAttr;
if (a != null) {
do {
a = a.next;
if (a.IsNamespaceDeclaration) {
string localName = a.Name.LocalName;
if (localName == "xmlns") {
localName = string.Empty;
}
if (!namespaceManager.HasNamespace(localName)) {
namespaceManager.AddNamespace(localName, a.Value);
}
}
} while (a != e.lastAttr);
}
e = e.parent as XElement;
}
}
void PushElement(XElement e, ref string xsiType, ref string xsiNil) {
namespaceManager.PushScope();
XAttribute a = e.lastAttr;
if (a != null) {
do {
a = a.next;
if (a.IsNamespaceDeclaration) {
string localName = a.Name.LocalName;
if (localName == "xmlns") {
localName = string.Empty;
}
namespaceManager.AddNamespace(localName, a.Value);
}
else {
XName name = a.Name;
if (name == xsiTypeName) {
xsiType = a.Value;
}
else if (name == xsiNilName) {
xsiNil = a.Value;
}
}
} while (a != e.lastAttr);
}
}
IXmlLineInfo SaveLineInfo(XObject source) {
IXmlLineInfo previousLineInfo = validator.LineInfoProvider;
validator.LineInfoProvider = source as IXmlLineInfo;
return previousLineInfo;
}
void RestoreLineInfo(IXmlLineInfo originalLineInfo) {
validator.LineInfoProvider = originalLineInfo;
}
void ValidateAttribute(XAttribute a) {
IXmlLineInfo original = SaveLineInfo(a);
XmlSchemaInfo si = addSchemaInfo ? new XmlSchemaInfo() : null;
source = a;
validator.ValidateAttribute(a.Name.LocalName, a.Name.NamespaceName, a.Value, si);
if (addSchemaInfo) {
ReplaceSchemaInfo(a, si);
}
RestoreLineInfo(original);
}
void ValidateAttributes(XElement e) {
XAttribute a = e.lastAttr;
IXmlLineInfo orginal = SaveLineInfo(a);
if (a != null) {
do {
a = a.next;
if (!a.IsNamespaceDeclaration) {
ValidateAttribute(a);
}
} while (a != e.lastAttr);
source = e;
}
if (addSchemaInfo) {
if (defaultAttributes == null) {
defaultAttributes = new ArrayList();
}
else {
defaultAttributes.Clear();
}
validator.GetUnspecifiedDefaultAttributes(defaultAttributes);
foreach (XmlSchemaAttribute sa in defaultAttributes) {
a = new XAttribute(XNamespace.Get(sa.QualifiedName.Namespace).GetName(sa.QualifiedName.Name), GetDefaultValue(sa));
ReplaceSchemaInfo(a, GetDefaultAttributeSchemaInfo(sa));
e.Add(a);
}
}
RestoreLineInfo(orginal);
}
// SxS: This method does not expose any resources to the caller and passes null as resource names to
// XmlSchemaValidator.ValidateElement (annotated with ResourceExposure(ResourceScope.None).
// It's OK to suppress the SxS warning.
[ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
[ResourceExposure(ResourceScope.None)]
void ValidateElement(XElement e) {
XmlSchemaInfo si = addSchemaInfo ? new XmlSchemaInfo() : null;
string xsiType = null;
string xsiNil = null;
PushElement(e, ref xsiType, ref xsiNil);
IXmlLineInfo original = SaveLineInfo(e);
source = e;
validator.ValidateElement(e.Name.LocalName, e.Name.NamespaceName, si, xsiType, xsiNil, null, null);
ValidateAttributes(e);
validator.ValidateEndOfAttributes(si);
ValidateNodes(e);
validator.ValidateEndElement(si);
if (addSchemaInfo) {
if (si.Validity == XmlSchemaValidity.Valid && si.IsDefault) {
e.Value = GetDefaultValue(si.SchemaElement);
}
ReplaceSchemaInfo(e, si);
}
RestoreLineInfo(original);
namespaceManager.PopScope();
}
void ValidateNodes(XElement e) {
XNode n = e.content as XNode;
IXmlLineInfo orginal = SaveLineInfo(n);
if (n != null) {
do {
n = n.next;
XElement c = n as XElement;
if (c != null) {
ValidateElement(c);
}
else {
XText t = n as XText;
if (t != null) {
string s = t.Value;
if (s.Length > 0) {
validator.LineInfoProvider = t as IXmlLineInfo;
validator.ValidateText(s);
}
}
}
} while (n != e.content);
source = e;
}
else {
string s = e.content as string;
if (s != null && s.Length > 0) {
validator.ValidateText(s);
}
}
RestoreLineInfo(orginal);
}
void ValidationCallback(object sender, ValidationEventArgs e) {
if (validationEventHandler != null) {
validationEventHandler(source, e);
}
else if (e.Severity == XmlSeverityType.Error) {
throw e.Exception;
}
}
}
internal class XmlSchemaInfoEqualityComparer : IEqualityComparer<XmlSchemaInfo>
{
public bool Equals(XmlSchemaInfo si1, XmlSchemaInfo si2) {
if (si1 == si2) return true;
if (si1 == null || si2 == null) return false;
return si1.ContentType == si2.ContentType &&
si1.IsDefault == si2.IsDefault &&
si1.IsNil == si2.IsNil &&
(object)si1.MemberType == (object)si2.MemberType &&
(object)si1.SchemaAttribute == (object)si2.SchemaAttribute &&
(object)si1.SchemaElement == (object)si2.SchemaElement &&
(object)si1.SchemaType == (object)si2.SchemaType &&
si1.Validity == si2.Validity;
}
public int GetHashCode(XmlSchemaInfo si) {
if (si == null) return 0;
int h = (int)si.ContentType;
if (si.IsDefault) {
h ^= 1;
}
if (si.IsNil) {
h ^= 1;
}
XmlSchemaSimpleType memberType = si.MemberType;
if (memberType != null) {
h ^= memberType.GetHashCode();
}
XmlSchemaAttribute schemaAttribute = si.SchemaAttribute;
if (schemaAttribute != null) {
h ^= schemaAttribute.GetHashCode();
}
XmlSchemaElement schemaElement = si.SchemaElement;
if (schemaElement != null) {
h ^= schemaElement.GetHashCode();
}
XmlSchemaType schemaType = si.SchemaType;
if (schemaType != null) {
h ^= schemaType.GetHashCode();
}
h ^= (int)si.Validity;
return h;
}
}
/// <summary>
/// Extension methods
/// </summary>
public static class Extensions
{
/// <summary>
/// Gets the schema information that has been assigned to the <see cref="XElement"/> as a result of schema validation.
/// </summary>
/// <param name="source">Extension point</param>
[SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "Reviewed by the design group.")]
public static IXmlSchemaInfo GetSchemaInfo(this XElement source) {
if (source == null) throw new ArgumentNullException("source");
return source.Annotation<IXmlSchemaInfo>();
}
/// <summary>
/// Gets the schema information that has been assigned to the <see cref="XAttribute"/> as a result of schema validation.
/// </summary>
/// <param name="source">Extension point</param>
[SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "Reviewed by the design group.")]
public static IXmlSchemaInfo GetSchemaInfo(this XAttribute source) {
if (source == null) throw new ArgumentNullException("source");
return source.Annotation<IXmlSchemaInfo>();
}
/// <summary>
/// Validate a <see cref="XDocument"/>
/// </summary>
/// <param name="source">Extension point</param>
/// <param name="schemas">The <see cref="XmlSchemaSet"/> used for validation</param>
/// <param name="validationEventHandler">The <see cref="ValidationEventHandler"/>
/// that receives schema validation warnings and errors encountered during schema
/// validation</param>
public static void Validate(this XDocument source, XmlSchemaSet schemas, ValidationEventHandler validationEventHandler) {
source.Validate(schemas, validationEventHandler, false);
}
/// <summary>
/// Validate a <see cref="XDocument"/>
/// </summary>
/// <param name="source">Extension point</param>
/// <param name="schemas">The <see cref="XmlSchemaSet"/> used for validation</param>
/// <param name="validationEventHandler">The <see cref="ValidationEventHandler"/>
/// that receives schema validation warnings and errors encountered during schema
/// validation</param>
/// <param name="addSchemaInfo">If enabled the <see cref="XDocument"/> and the corresponding
/// subtree is augmented with PSVI in the form of <see cref="IXmlSchemaInfo"/> annotations,
/// default attributes and default element values</param>
[SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "Reviewed by the design group.")]
public static void Validate(this XDocument source, XmlSchemaSet schemas, ValidationEventHandler validationEventHandler, bool addSchemaInfo) {
if (source == null) throw new ArgumentNullException("source");
if (schemas == null) throw new ArgumentNullException("schemas");
new XNodeValidator(schemas, validationEventHandler).Validate(source, null, addSchemaInfo);
}
/// <summary>
/// Validate a <see cref="XElement"/>
/// </summary>
/// <param name="source">Extension point</param>
/// <param name="partialValidationType">An <see cref="XmlSchemaElement"/> or
/// <see cref="XmlSchemaType"/> object used to initialize the partial validation
/// context</param>
/// <param name="schemas">The <see cref="XmlSchemaSet"/> used for validation</param>
/// <param name="validationEventHandler">The <see cref="ValidationEventHandler"/> that
/// receives schema validation warnings and errors encountered during schema
/// validation</param>
public static void Validate(this XElement source, XmlSchemaObject partialValidationType, XmlSchemaSet schemas, ValidationEventHandler validationEventHandler) {
source.Validate(partialValidationType, schemas, validationEventHandler, false);
}
/// <summary>
/// Validate a <see cref="XElement"/>
/// </summary>
/// <param name="source">Extension point</param>
/// <param name="partialValidationType">An <see cref="XmlSchemaElement"/> or
/// <see cref="XmlSchemaType"/> object used to initialize the partial validation
/// context</param>
/// <param name="schemas">The <see cref="XmlSchemaSet"/> used for validation</param>
/// <param name="validationEventHandler">The <see cref="ValidationEventHandler"/> that
/// receives schema validation warnings and errors encountered during schema
/// validation</param>
/// <param name="addSchemaInfo">If enabled the <see cref="XElement"/> and the corresponding
/// subtree is augmented with PSVI in the form of <see cref="IXmlSchemaInfo"/> annotations,
/// default attributes and default element values</param>
[SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "Reviewed by the design group.")]
public static void Validate(this XElement source, XmlSchemaObject partialValidationType, XmlSchemaSet schemas, ValidationEventHandler validationEventHandler, bool addSchemaInfo) {
if (source == null) throw new ArgumentNullException("source");
if (partialValidationType == null) throw new ArgumentNullException("partialValidationType");
if (schemas == null) throw new ArgumentNullException("schemas");
new XNodeValidator(schemas, validationEventHandler).Validate(source, partialValidationType, addSchemaInfo);
}
/// <summary>
/// Validate a <see cref="XAttribute"/>
/// </summary>
/// <param name="source">Extension point</param>
/// <param name="partialValidationType">An <see cref="XmlSchemaAttribute"/> or
/// <see cref="XmlSchemaType"/> object used to initialize the partial validation
/// context</param>
/// <param name="schemas">The <see cref="XmlSchemaSet"/> used for validation</param>
/// <param name="validationEventHandler">The <see cref="ValidationEventHandler"/> that
/// receives schema validation warnings and errors encountered during schema
/// validation</param>
public static void Validate(this XAttribute source, XmlSchemaObject partialValidationType, XmlSchemaSet schemas, ValidationEventHandler validationEventHandler) {
source.Validate(partialValidationType, schemas, validationEventHandler, false);
}
/// <summary>
/// Validate a <see cref="XAttribute"/>
/// </summary>
/// <param name="source">Extension point</param>
/// <param name="partialValidationType">An <see cref="XmlSchemaAttribute"/> or
/// <see cref="XmlSchemaType"/> object used to initialize the partial validation
/// context</param>
/// <param name="schemas">The <see cref="XmlSchemaSet"/> used for validation</param>
/// <param name="validationEventHandler">The <see cref="ValidationEventHandler"/> that
/// receives schema validation warnings and errors encountered during schema
/// validation</param>
/// <param name="addSchemaInfo">If enabled the <see cref="XAttribute"/> is augmented with PSVI
/// in the form of <see cref="IXmlSchemaInfo"/> annotations, default attributes and
/// default element values</param>
[SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "Reviewed by the design group.")]
public static void Validate(this XAttribute source, XmlSchemaObject partialValidationType, XmlSchemaSet schemas, ValidationEventHandler validationEventHandler, bool addSchemaInfo) {
if (source == null) throw new ArgumentNullException("source");
if (partialValidationType == null) throw new ArgumentNullException("partialValidationType");
if (schemas == null) throw new ArgumentNullException("schemas");
new XNodeValidator(schemas, validationEventHandler).Validate(source, partialValidationType, addSchemaInfo);
}
}
}