Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Services / Web / System / Web / Services / Description / SchemaCompiler.cs / 1305376 / SchemaCompiler.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- namespace System.Web.Services.Description { using System; using System.Collections; using System.Collections.Specialized; using System.Xml; using System.Xml.Schema; using System.Xml.Serialization; internal class SchemaCompiler { static StringCollection warnings; internal static StringCollection Warnings { get { if (warnings == null) warnings = new StringCollection(); return warnings; } } internal static StringCollection Compile(XmlSchemas schemas) { AddImports(schemas); Warnings.Clear(); schemas.Compile(new ValidationEventHandler(ValidationCallbackWithErrorCode), true); return Warnings; } static void AddImport(XmlSchema schema, string ns) { if (schema.TargetNamespace == ns) return; foreach(XmlSchemaExternal ex in schema.Includes) { XmlSchemaImport import = ex as XmlSchemaImport; if (import != null && import.Namespace == ns) { return; } } XmlSchemaImport newImport = new XmlSchemaImport(); newImport.Namespace = ns; schema.Includes.Add(newImport); } static void AddImports(XmlSchemas schemas) { foreach (XmlSchema schema in schemas) { AddImport(schema, Soap.Encoding); AddImport(schema, ServiceDescription.Namespace); } } internal static string WarningDetails(XmlSchemaException exception, string message) { string details; XmlSchemaObject source = exception.SourceSchemaObject; if (exception.LineNumber == 0 && exception.LinePosition == 0) { details = GetSchemaItem(source, null, message); } else { string ns = null; if (source != null) { while (source.Parent != null) { source = source.Parent; } if (source is XmlSchema) { ns = ((XmlSchema)source).TargetNamespace; } } details = Res.GetString(Res.SchemaSyntaxErrorDetails, ns, message, exception.LineNumber, exception.LinePosition); } return details; } static string GetSchemaItem(XmlSchemaObject o, string ns, string details) { if (o == null) { return null; } while (o.Parent != null && !(o.Parent is XmlSchema)) { o = o.Parent; } if (ns == null || ns.Length == 0) { XmlSchemaObject tmp = o; while (tmp.Parent != null) { tmp = tmp.Parent; } if (tmp is XmlSchema) { ns = ((XmlSchema)tmp).TargetNamespace; } } string item = null; if (o is XmlSchemaNotation) { item = Res.GetString(Res.XmlSchemaNamedItem, ns, "notation", ((XmlSchemaNotation)o).Name, details); } else if (o is XmlSchemaGroup) { item = Res.GetString(Res.XmlSchemaNamedItem, ns, "group", ((XmlSchemaGroup)o).Name, details); } else if (o is XmlSchemaElement) { XmlSchemaElement e = ((XmlSchemaElement)o); if (e.Name == null || e.Name.Length == 0) { XmlQualifiedName parentName = GetParentName(o); // Element reference '{0}' declared in schema type '{1}' from namespace '{2}' item = Res.GetString(Res.XmlSchemaElementReference, e.RefName.ToString(), parentName.Name, parentName.Namespace); } else { item = Res.GetString(Res.XmlSchemaNamedItem, ns, "element", e.Name, details); } } else if (o is XmlSchemaType) { item = Res.GetString(Res.XmlSchemaNamedItem, ns, o.GetType() == typeof(XmlSchemaSimpleType) ? "simpleType" : "complexType", ((XmlSchemaType)o).Name, details); } else if (o is XmlSchemaAttributeGroup) { item = Res.GetString(Res.XmlSchemaNamedItem, ns, "attributeGroup", ((XmlSchemaAttributeGroup)o).Name, details); } else if (o is XmlSchemaAttribute) { XmlSchemaAttribute a = ((XmlSchemaAttribute)o); if (a.Name == null || a.Name.Length == 0) { XmlQualifiedName parentName = GetParentName(o); // Attribure reference '{0}' declared in schema type '{1}' from namespace '{2}' return Res.GetString(Res.XmlSchemaAttributeReference, a.RefName.ToString(), parentName.Name, parentName.Namespace); } else { item = Res.GetString(Res.XmlSchemaNamedItem, ns, "attribute", a.Name, details); } } else if (o is XmlSchemaContent) { XmlQualifiedName parentName = GetParentName(o); // Check content definition of schema type '{0}' from namespace '{1}'. {2} item = Res.GetString(Res.XmlSchemaContentDef, parentName.Name, parentName.Namespace, details); } else if (o is XmlSchemaExternal) { string itemType = o is XmlSchemaImport ? "import" : o is XmlSchemaInclude ? "include" : o is XmlSchemaRedefine ? "redefine" : o.GetType().Name; item = Res.GetString(Res.XmlSchemaItem, ns, itemType, details); } else if (o is XmlSchema) { item = Res.GetString(Res.XmlSchema, ns, details); } else { item = Res.GetString(Res.XmlSchemaNamedItem, ns, o.GetType().Name, null, details); } return item; } internal static XmlQualifiedName GetParentName(XmlSchemaObject item) { while (item.Parent != null) { if (item.Parent is XmlSchemaType) { XmlSchemaType type = (XmlSchemaType)item.Parent; if (type.Name != null && type.Name.Length != 0) { return type.QualifiedName; } } item = item.Parent; } return XmlQualifiedName.Empty; } private static void ValidationCallbackWithErrorCode (object sender, ValidationEventArgs args) { Warnings.Add(Res.GetString(args.Severity == XmlSeverityType.Error ? Res.SchemaValidationError : Res.SchemaValidationWarning, WarningDetails(args.Exception, args.Message))); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- namespace System.Web.Services.Description { using System; using System.Collections; using System.Collections.Specialized; using System.Xml; using System.Xml.Schema; using System.Xml.Serialization; internal class SchemaCompiler { static StringCollection warnings; internal static StringCollection Warnings { get { if (warnings == null) warnings = new StringCollection(); return warnings; } } internal static StringCollection Compile(XmlSchemas schemas) { AddImports(schemas); Warnings.Clear(); schemas.Compile(new ValidationEventHandler(ValidationCallbackWithErrorCode), true); return Warnings; } static void AddImport(XmlSchema schema, string ns) { if (schema.TargetNamespace == ns) return; foreach(XmlSchemaExternal ex in schema.Includes) { XmlSchemaImport import = ex as XmlSchemaImport; if (import != null && import.Namespace == ns) { return; } } XmlSchemaImport newImport = new XmlSchemaImport(); newImport.Namespace = ns; schema.Includes.Add(newImport); } static void AddImports(XmlSchemas schemas) { foreach (XmlSchema schema in schemas) { AddImport(schema, Soap.Encoding); AddImport(schema, ServiceDescription.Namespace); } } internal static string WarningDetails(XmlSchemaException exception, string message) { string details; XmlSchemaObject source = exception.SourceSchemaObject; if (exception.LineNumber == 0 && exception.LinePosition == 0) { details = GetSchemaItem(source, null, message); } else { string ns = null; if (source != null) { while (source.Parent != null) { source = source.Parent; } if (source is XmlSchema) { ns = ((XmlSchema)source).TargetNamespace; } } details = Res.GetString(Res.SchemaSyntaxErrorDetails, ns, message, exception.LineNumber, exception.LinePosition); } return details; } static string GetSchemaItem(XmlSchemaObject o, string ns, string details) { if (o == null) { return null; } while (o.Parent != null && !(o.Parent is XmlSchema)) { o = o.Parent; } if (ns == null || ns.Length == 0) { XmlSchemaObject tmp = o; while (tmp.Parent != null) { tmp = tmp.Parent; } if (tmp is XmlSchema) { ns = ((XmlSchema)tmp).TargetNamespace; } } string item = null; if (o is XmlSchemaNotation) { item = Res.GetString(Res.XmlSchemaNamedItem, ns, "notation", ((XmlSchemaNotation)o).Name, details); } else if (o is XmlSchemaGroup) { item = Res.GetString(Res.XmlSchemaNamedItem, ns, "group", ((XmlSchemaGroup)o).Name, details); } else if (o is XmlSchemaElement) { XmlSchemaElement e = ((XmlSchemaElement)o); if (e.Name == null || e.Name.Length == 0) { XmlQualifiedName parentName = GetParentName(o); // Element reference '{0}' declared in schema type '{1}' from namespace '{2}' item = Res.GetString(Res.XmlSchemaElementReference, e.RefName.ToString(), parentName.Name, parentName.Namespace); } else { item = Res.GetString(Res.XmlSchemaNamedItem, ns, "element", e.Name, details); } } else if (o is XmlSchemaType) { item = Res.GetString(Res.XmlSchemaNamedItem, ns, o.GetType() == typeof(XmlSchemaSimpleType) ? "simpleType" : "complexType", ((XmlSchemaType)o).Name, details); } else if (o is XmlSchemaAttributeGroup) { item = Res.GetString(Res.XmlSchemaNamedItem, ns, "attributeGroup", ((XmlSchemaAttributeGroup)o).Name, details); } else if (o is XmlSchemaAttribute) { XmlSchemaAttribute a = ((XmlSchemaAttribute)o); if (a.Name == null || a.Name.Length == 0) { XmlQualifiedName parentName = GetParentName(o); // Attribure reference '{0}' declared in schema type '{1}' from namespace '{2}' return Res.GetString(Res.XmlSchemaAttributeReference, a.RefName.ToString(), parentName.Name, parentName.Namespace); } else { item = Res.GetString(Res.XmlSchemaNamedItem, ns, "attribute", a.Name, details); } } else if (o is XmlSchemaContent) { XmlQualifiedName parentName = GetParentName(o); // Check content definition of schema type '{0}' from namespace '{1}'. {2} item = Res.GetString(Res.XmlSchemaContentDef, parentName.Name, parentName.Namespace, details); } else if (o is XmlSchemaExternal) { string itemType = o is XmlSchemaImport ? "import" : o is XmlSchemaInclude ? "include" : o is XmlSchemaRedefine ? "redefine" : o.GetType().Name; item = Res.GetString(Res.XmlSchemaItem, ns, itemType, details); } else if (o is XmlSchema) { item = Res.GetString(Res.XmlSchema, ns, details); } else { item = Res.GetString(Res.XmlSchemaNamedItem, ns, o.GetType().Name, null, details); } return item; } internal static XmlQualifiedName GetParentName(XmlSchemaObject item) { while (item.Parent != null) { if (item.Parent is XmlSchemaType) { XmlSchemaType type = (XmlSchemaType)item.Parent; if (type.Name != null && type.Name.Length != 0) { return type.QualifiedName; } } item = item.Parent; } return XmlQualifiedName.Empty; } private static void ValidationCallbackWithErrorCode (object sender, ValidationEventArgs args) { Warnings.Add(Res.GetString(args.Severity == XmlSeverityType.Error ? Res.SchemaValidationError : Res.SchemaValidationWarning, WarningDetails(args.Exception, args.Message))); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- SQLBytesStorage.cs
- TreeNodeCollection.cs
- ComponentEditorForm.cs
- ObjectListDesigner.cs
- XPathScanner.cs
- Triplet.cs
- Validator.cs
- EncryptedData.cs
- WebScriptMetadataMessageEncodingBindingElement.cs
- ReadOnlyCollectionBase.cs
- Screen.cs
- RouteParameter.cs
- PageParserFilter.cs
- CertificateElement.cs
- ExpressionVisitorHelpers.cs
- Vector3D.cs
- DataRecordInfo.cs
- TypeContext.cs
- ADConnectionHelper.cs
- ErrorStyle.cs
- XamlSerializerUtil.cs
- ReplyChannelAcceptor.cs
- NullReferenceException.cs
- TextStore.cs
- EntityContainerEntitySet.cs
- CheckBoxRenderer.cs
- BitmapDecoder.cs
- NamespaceEmitter.cs
- EqualityComparer.cs
- ConfigXmlText.cs
- ServiceActivationException.cs
- XmlAttribute.cs
- XmlStringTable.cs
- SqlProfileProvider.cs
- TypeElementCollection.cs
- Attributes.cs
- Help.cs
- SizeIndependentAnimationStorage.cs
- ArrayElementGridEntry.cs
- SmiMetaDataProperty.cs
- HiddenFieldPageStatePersister.cs
- StructuredType.cs
- ListViewGroupConverter.cs
- Line.cs
- XmlHelper.cs
- WmpBitmapEncoder.cs
- ToggleButton.cs
- LocalBuilder.cs
- SelectionEditor.cs
- StructureChangedEventArgs.cs
- WebControlsSection.cs
- BrowserCapabilitiesFactory.cs
- Int16AnimationBase.cs
- Marshal.cs
- ParagraphVisual.cs
- Rijndael.cs
- CompatibleIComparer.cs
- EncoderParameters.cs
- XmlWhitespace.cs
- CallbackHandler.cs
- TreeNodeCollectionEditor.cs
- MembershipUser.cs
- NonVisualControlAttribute.cs
- DebugHandleTracker.cs
- BmpBitmapEncoder.cs
- ExpressionList.cs
- EntityParameterCollection.cs
- Trace.cs
- SafeArchiveContext.cs
- DataSetUtil.cs
- ContextMarshalException.cs
- WebPartConnection.cs
- AsymmetricKeyExchangeFormatter.cs
- GeometryValueSerializer.cs
- ButtonField.cs
- HttpRequest.cs
- NonNullItemCollection.cs
- DurableDispatcherAddressingFault.cs
- CodeCastExpression.cs
- IgnoreDeviceFilterElement.cs
- RegexCompiler.cs
- LinkUtilities.cs
- Propagator.JoinPropagator.JoinPredicateVisitor.cs
- SqlCacheDependencySection.cs
- Unit.cs
- DesignerView.Commands.cs
- safelinkcollection.cs
- UnconditionalPolicy.cs
- WmlPageAdapter.cs
- Matrix.cs
- DynamicRenderer.cs
- ReleaseInstanceMode.cs
- URLString.cs
- CommonDialog.cs
- ipaddressinformationcollection.cs
- TTSVoice.cs
- SqlNodeAnnotations.cs
- EdgeModeValidation.cs
- GridErrorDlg.cs
- RadioButton.cs