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
- GridViewDeletedEventArgs.cs
- DATA_BLOB.cs
- WorkflowServiceNamespace.cs
- GeneralTransform3D.cs
- TextParagraphView.cs
- ImageSource.cs
- NamedPermissionSet.cs
- ClrProviderManifest.cs
- XmlWriterTraceListener.cs
- ToolStripContainerDesigner.cs
- XmlQueryContext.cs
- CodeIndexerExpression.cs
- DataContractFormatAttribute.cs
- PasswordTextNavigator.cs
- FilterQuery.cs
- StorageAssociationTypeMapping.cs
- DiscoveryDocumentSerializer.cs
- UidPropertyAttribute.cs
- Geometry.cs
- DateTimeOffsetStorage.cs
- MessageQueueException.cs
- ExpressionNode.cs
- OraclePermissionAttribute.cs
- WindowsToolbarAsMenu.cs
- SqlTypesSchemaImporter.cs
- StringPropertyBuilder.cs
- Constraint.cs
- SpecularMaterial.cs
- StrokeSerializer.cs
- DebugTracing.cs
- DebugView.cs
- FileStream.cs
- TypeKeyValue.cs
- Psha1DerivedKeyGeneratorHelper.cs
- RawStylusInputCustomData.cs
- Timer.cs
- CatalogPart.cs
- PropertyDescriptorCollection.cs
- DataServiceQueryProvider.cs
- NavigationWindow.cs
- DiscoveryProxy.cs
- ContextStack.cs
- _HeaderInfoTable.cs
- UInt64.cs
- NotifyInputEventArgs.cs
- QuotedPrintableStream.cs
- TimerElapsedEvenArgs.cs
- UntrustedRecipientException.cs
- LinqDataSourceDeleteEventArgs.cs
- DeviceSpecificChoice.cs
- TextEditorTyping.cs
- listitem.cs
- SerialPort.cs
- EtwTrace.cs
- FormViewInsertEventArgs.cs
- RecognizeCompletedEventArgs.cs
- MessageQueueAccessControlEntry.cs
- BaseValidator.cs
- ModifiableIteratorCollection.cs
- PointLight.cs
- PreDigestedSignedInfo.cs
- FlowDocumentView.cs
- TailPinnedEventArgs.cs
- SafeTokenHandle.cs
- XmlILIndex.cs
- FrugalList.cs
- TextSimpleMarkerProperties.cs
- CompositeControl.cs
- SetIterators.cs
- ImageMap.cs
- RenderData.cs
- XmlDataContract.cs
- Binding.cs
- SignatureResourcePool.cs
- userdatakeys.cs
- OracleCommandSet.cs
- HTMLTextWriter.cs
- DynamicResourceExtension.cs
- ServiceModelConfigurationSectionCollection.cs
- PerformanceCounter.cs
- ColumnHeaderCollectionEditor.cs
- CLRBindingWorker.cs
- CallbackDebugBehavior.cs
- GradientBrush.cs
- _SingleItemRequestCache.cs
- ContextActivityUtils.cs
- EventLogPermissionEntry.cs
- InfoCardRSAOAEPKeyExchangeDeformatter.cs
- IisHelper.cs
- GroupBoxRenderer.cs
- DemultiplexingDispatchMessageFormatter.cs
- SequenceDesigner.cs
- Avt.cs
- InstanceOwnerQueryResult.cs
- FragmentQueryKB.cs
- XmlSchema.cs
- DirectionalLight.cs
- BeginStoryboard.cs
- ListenerAdapterBase.cs
- Menu.cs