| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 |
- //------------------------------------------------------------
- // Copyright (c) Microsoft Corporation. All rights reserved.
- //------------------------------------------------------------
- namespace System.Runtime.Serialization
- {
- using System;
- //using System.ServiceModel.Channels;
- using System.Xml;
- using System.Xml.Schema;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Reflection;
- using System.Diagnostics;
- using System.ServiceModel.Diagnostics;
- using System.Runtime.Serialization.Diagnostics;
- public class XsdDataContractExporter
- {
- ExportOptions options;
- XmlSchemaSet schemas;
- DataContractSet dataContractSet;
- public XsdDataContractExporter()
- {
- }
- public XsdDataContractExporter(XmlSchemaSet schemas)
- {
- this.schemas = schemas;
- }
- public ExportOptions Options
- {
- get { return options; }
- set { options = value; }
- }
- public XmlSchemaSet Schemas
- {
- get
- {
- XmlSchemaSet schemaSet = GetSchemaSet();
- SchemaImporter.CompileSchemaSet(schemaSet);
- return schemaSet;
- }
- }
- XmlSchemaSet GetSchemaSet()
- {
- if (schemas == null)
- {
- schemas = new XmlSchemaSet();
- schemas.XmlResolver = null;
- }
- return schemas;
- }
- DataContractSet DataContractSet
- {
- get
- {
- if (dataContractSet == null)
- {
- dataContractSet = new DataContractSet((Options == null) ? null : Options.GetSurrogate());
- }
- return dataContractSet;
- }
- }
- void TraceExportBegin()
- {
- if (DiagnosticUtility.ShouldTraceInformation)
- {
- TraceUtility.Trace(TraceEventType.Information, TraceCode.XsdExportBegin, SR.GetString(SR.TraceCodeXsdExportBegin));
- }
- }
- void TraceExportEnd()
- {
- if (DiagnosticUtility.ShouldTraceInformation)
- {
- TraceUtility.Trace(TraceEventType.Information, TraceCode.XsdExportEnd, SR.GetString(SR.TraceCodeXsdExportEnd));
- }
- }
- void TraceExportError(Exception exception)
- {
- if (DiagnosticUtility.ShouldTraceError)
- {
- TraceUtility.Trace(TraceEventType.Error, TraceCode.XsdExportError, SR.GetString(SR.TraceCodeXsdExportError), null, exception);
- }
- }
- public void Export(ICollection<Assembly> assemblies)
- {
- if (assemblies == null)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("assemblies"));
- TraceExportBegin();
- DataContractSet oldValue = (dataContractSet == null) ? null : new DataContractSet(dataContractSet);
- try
- {
- foreach (Assembly assembly in assemblies)
- {
- if (assembly == null)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.CannotExportNullAssembly, "assemblies")));
- Type[] types = assembly.GetTypes();
- for (int j = 0; j < types.Length; j++)
- CheckAndAddType(types[j]);
- }
- Export();
- }
- catch (Exception ex)
- {
- if (Fx.IsFatal(ex))
- {
- throw;
- }
- dataContractSet = oldValue;
- TraceExportError(ex);
- throw;
- }
- TraceExportEnd();
- }
- public void Export(ICollection<Type> types)
- {
- if (types == null)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("types"));
- TraceExportBegin();
- DataContractSet oldValue = (dataContractSet == null) ? null : new DataContractSet(dataContractSet);
- try
- {
- foreach (Type type in types)
- {
- if (type == null)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.CannotExportNullType, "types")));
- AddType(type);
- }
- Export();
- }
- catch (Exception ex)
- {
- if (Fx.IsFatal(ex))
- {
- throw;
- }
- dataContractSet = oldValue;
- TraceExportError(ex);
- throw;
- }
- TraceExportEnd();
- }
- public void Export(Type type)
- {
- if (type == null)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("type"));
- TraceExportBegin();
- DataContractSet oldValue = (dataContractSet == null) ? null : new DataContractSet(dataContractSet);
- try
- {
- AddType(type);
- Export();
- }
- catch (Exception ex)
- {
- if (Fx.IsFatal(ex))
- {
- throw;
- }
- dataContractSet = oldValue;
- TraceExportError(ex);
- throw;
- }
- TraceExportEnd();
- }
- public XmlQualifiedName GetSchemaTypeName(Type type)
- {
- if (type == null)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("type"));
- type = GetSurrogatedType(type);
- DataContract dataContract = DataContract.GetDataContract(type);
- DataContractSet.EnsureTypeNotGeneric(dataContract.UnderlyingType);
- XmlDataContract xmlDataContract = dataContract as XmlDataContract;
- if (xmlDataContract != null && xmlDataContract.IsAnonymous)
- return XmlQualifiedName.Empty;
- return dataContract.StableName;
- }
- public XmlSchemaType GetSchemaType(Type type)
- {
- if (type == null)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("type"));
- type = GetSurrogatedType(type);
- DataContract dataContract = DataContract.GetDataContract(type);
- DataContractSet.EnsureTypeNotGeneric(dataContract.UnderlyingType);
- XmlDataContract xmlDataContract = dataContract as XmlDataContract;
- if (xmlDataContract != null && xmlDataContract.IsAnonymous)
- return xmlDataContract.XsdType;
- return null;
- }
- public XmlQualifiedName GetRootElementName(Type type)
- {
- if (type == null)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("type"));
- type = GetSurrogatedType(type);
- DataContract dataContract = DataContract.GetDataContract(type);
- DataContractSet.EnsureTypeNotGeneric(dataContract.UnderlyingType);
- if (dataContract.HasRoot)
- {
- return new XmlQualifiedName(dataContract.TopLevelElementName.Value, dataContract.TopLevelElementNamespace.Value);
- }
- else
- {
- return null;
- }
- }
- Type GetSurrogatedType(Type type)
- {
- IDataContractSurrogate dataContractSurrogate;
- if (options != null && (dataContractSurrogate = Options.GetSurrogate()) != null)
- type = DataContractSurrogateCaller.GetDataContractType(dataContractSurrogate, type);
- return type;
- }
- void CheckAndAddType(Type type)
- {
- type = GetSurrogatedType(type);
- if (!type.ContainsGenericParameters && DataContract.IsTypeSerializable(type))
- AddType(type);
- }
- void AddType(Type type)
- {
- DataContractSet.Add(type);
- }
- void Export()
- {
- AddKnownTypes();
- SchemaExporter schemaExporter = new SchemaExporter(GetSchemaSet(), DataContractSet);
- schemaExporter.Export();
- }
- void AddKnownTypes()
- {
- if (Options != null)
- {
- Collection<Type> knownTypes = Options.KnownTypes;
- if (knownTypes != null)
- {
- for (int i = 0; i < knownTypes.Count; i++)
- {
- Type type = knownTypes[i];
- if (type == null)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.CannotExportNullKnownType)));
- AddType(type);
- }
- }
- }
- }
- public bool CanExport(ICollection<Assembly> assemblies)
- {
- if (assemblies == null)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("assemblies"));
- DataContractSet oldValue = (dataContractSet == null) ? null : new DataContractSet(dataContractSet);
- try
- {
- foreach (Assembly assembly in assemblies)
- {
- if (assembly == null)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.CannotExportNullAssembly, "assemblies")));
- Type[] types = assembly.GetTypes();
- for (int j = 0; j < types.Length; j++)
- CheckAndAddType(types[j]);
- }
- AddKnownTypes();
- return true;
- }
- catch (InvalidDataContractException)
- {
- dataContractSet = oldValue;
- return false;
- }
- catch (Exception ex)
- {
- if (Fx.IsFatal(ex))
- {
- throw;
- }
- dataContractSet = oldValue;
- TraceExportError(ex);
- throw;
- }
- }
- public bool CanExport(ICollection<Type> types)
- {
- if (types == null)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("types"));
- DataContractSet oldValue = (dataContractSet == null) ? null : new DataContractSet(dataContractSet);
- try
- {
- foreach (Type type in types)
- {
- if (type == null)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.CannotExportNullType, "types")));
- AddType(type);
- }
- AddKnownTypes();
- return true;
- }
- catch (InvalidDataContractException)
- {
- dataContractSet = oldValue;
- return false;
- }
- catch (Exception ex)
- {
- if (Fx.IsFatal(ex))
- {
- throw;
- }
- dataContractSet = oldValue;
- TraceExportError(ex);
- throw;
- }
- }
- public bool CanExport(Type type)
- {
- if (type == null)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("type"));
- DataContractSet oldValue = (dataContractSet == null) ? null : new DataContractSet(dataContractSet);
- try
- {
- AddType(type);
- AddKnownTypes();
- return true;
- }
- catch (InvalidDataContractException)
- {
- dataContractSet = oldValue;
- return false;
- }
- catch (Exception ex)
- {
- if (Fx.IsFatal(ex))
- {
- throw;
- }
- dataContractSet = oldValue;
- TraceExportError(ex);
- throw;
- }
- }
- #if USE_REFEMIT
- //Returns warnings
- public IList<string> GenerateCode(IList<Assembly> assemblies)
- {
- if (assemblies == null)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("assemblies"));
- List<string> warnings = new List<string>();
- DataContractSet oldValue = (dataContractSet == null) ? null : new DataContractSet(dataContractSet);
- try
- {
- for (int i=0; i < assemblies.Count; i++)
- {
- Assembly assembly = assemblies[i];
- if (assembly == null)
- throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.CannotExportNullAssembly, "assemblies")));
- Type[] types = assembly.GetTypes();
- for (int j=0; j < types.Length; j++)
- {
- try
- {
- CheckAndAddType(types[j]);
- }
- catch (Exception ex)
- {
- warnings.Add("Error on exporting Type " + DataContract.GetClrTypeFullName(types[j]) + ". " + ex.Message);
- }
-
- }
- }
- foreach (KeyValuePair<XmlQualifiedName, DataContract> pair in dataContractSet)
- {
- DataContract dataContract = pair.Value;
- if (dataContract is ClassDataContract)
- {
- try
- {
- XmlFormatClassWriterDelegate writerMethod = ((ClassDataContract)dataContract).XmlFormatWriterDelegate;
- XmlFormatClassReaderDelegate readerMethod = ((ClassDataContract)dataContract).XmlFormatReaderDelegate;
- }
- catch (Exception ex)
- {
- warnings.Add("Error on exporting Type " + dataContract.UnderlyingType + ". " + ex.Message);
- }
- }
- else if (dataContract is CollectionDataContract)
- {
- try
- {
- XmlFormatCollectionWriterDelegate writerMethod = ((CollectionDataContract)dataContract).XmlFormatWriterDelegate;
- XmlFormatCollectionReaderDelegate readerMethod = ((CollectionDataContract)dataContract).XmlFormatReaderDelegate;
- }
- catch (Exception ex)
- {
- warnings.Add("Error on exporting Type " + dataContract.UnderlyingType + ". " + ex.Message);
- }
- }
- }
- return warnings;
- }
- catch (Exception ex)
- {
- if (Fx.IsFatal(ex))
- {
- throw;
- }
- dataContractSet = oldValue;
- TraceExportError(ex);
- throw;
- }
- }
- #endif
- }
- }
|