protected override void RegisterChildMappers(IXmlMappingEngine engine) { this.RegisterContainerMapper <Owner, OwnerXmlMapper>(); this.RegisterContainerMapper <Animal, AnimalXmlMapper>(); this.RegisterContainerMapper <Dog, DogXmlMapper>(); this.RegisterContainerMapper <Cat, CatXmlMapper>(); }
protected override void RegisterChildMappers(IXmlMappingEngine engine) { engine.RegisterMapper(new OwnerXmlMapper(engine)); engine.RegisterMapper(new AnimalXmlMapper(engine)); engine.RegisterMapper(new DogXmlMapper(engine)); engine.RegisterMapper(new CatXmlMapper(engine)); }
public DogXmlMapper(IXmlMappingEngine engine) : base("Animal", engine) { this.Namespace = XmlNamespaces.PetNamespace; this.NamespacePrefix = XmlNamespaces.PetNamespacePrefix; this.InitializeXmlType(XmlNamespaces.PetNamespacePrefix, XmlNamespaces.PetNamespace, "Dog"); }
protected void RoundTripMapping(IXmlMappingEngine engine) { // Sample xml with namespaces to add complexity var xml = @"<Parent xmlns='http://www.sample.com/common' xmlns:Pet='http://www.sample.com/pet' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'> <Id>1</Id> <Name>Fred</Name> <Cost>1500.75</Cost> <Children xmlns='http://www.sample.com/app'> <Child> <Id>1</Id> <Value xmlns='http://www.sample.com/sales'>34.29</Value> <Start>2011-09-15T15:00:00</Start> <Animal xmlns='http://www.sample.com/app' xsi:type='Pet:Dog'> <Id>1</Id> <Name>Fido</Name> <Pet:Tricks>Fetch</Pet:Tricks> </Animal> </Child> <Child> <Id>2</Id> <Value xmlns='http://www.sample.com/sales'>12</Value> <Start>2020-12-31T04:00:00</Start> </Child> </Children> </Parent>"; var d = new Dog { Id = 1, Name = "Fido", Tricks = "Fetch" }; var expected = new Parent { Id = 1, Name = "Fred", Cost = 1500.75M, Children = new List <Child> { new Child { Id = 1, Value = 34.29f, Start = new DateTime(2011, 9, 15, 15, 0, 0), Dog = d }, new Child { Id = 2, Value = 12f, Start = new DateTime(2020, 12, 31, 4, 0, 0) }, } }; var processor = this.CreateProcessor(); processor.Initialize(xml); var candidate = engine.Map <XPathProcessor, Parent>(processor); this.Check(expected, candidate); var candidateXml = engine.Map <Parent, XElement>(candidate); this.CheckXml(candidateXml, xml); }
public ChildXmlMapper(IXmlMappingEngine engine) : base("Child", engine) { // this.RegisterNamespace(XmlMappingEngineFixture.SalesNamespace, XmlMappingEngineFixture.SalesNamespacePrefix); this.Namespace = XmlNamespaces.AppNamespace; this.NamespacePrefix = XmlNamespaces.AppNamespacePrefix; this.InitializeMap(x => x.Id); this.InitializeMap(x => x.Value, xmlNamespace: XmlNamespaces.SalesNamespace); this.InitializeMap(x => x.Start); }
public CatXmlMapper(IXmlMappingEngine engine) : base("Animal", engine) { Namespace = XmlNamespaces.PetNamespace; NamespacePrefix = XmlNamespaces.PetNamespacePrefix; InitializeXmlType(XmlNamespaces.PetNamespacePrefix, XmlNamespaces.PetNamespace, "Cat"); // InitializeParent<Animal>(); InitializeMap(x => x.Spayed); }
/// <summary> /// Create a new instance of the <see cref="XPathMapper{T}" /> class. /// </summary> /// <param name="nodeName">Name of the node.</param> /// <param name="engine">Mapping engine to use.</param> protected XPathMapper(string nodeName, IXmlMappingEngine engine) { NodeName = nodeName; Engine = engine; Namespace = string.Empty; NamespacePrefix = string.Empty; XmlType = string.Empty; Mappings = new List <XmlPropertyMap>(); namespaces = new List <Tuple <string, string> >(); }
/// <summary> /// Finds an IXmlMappingEngine for the specified version. /// <para> /// Will try to locate an earlier minor version if the exact version is not found e.g. can return Css.V2_0 if Css.V2_1 was requested. /// </para> /// </summary> /// <param name="version">Version of mapping engine to find, typically {Schema}.{Version} e.g. Css.V2_1</param> /// <param name="engine">Engine instance if found, null otherwise</param> /// <returns>true if found, false otherwise.</returns> public bool TryFind(string version, out IXmlMappingEngine engine) { try { engine = this.Find(version); return engine != null; } catch { engine = null; return false; } }
private IXmlMappingEngine ObtainLowerMinorVersion(string asmVersion) { IXmlMappingEngine engine = null; var schemaVersion = asmVersion.ToSchemaVersion(); while (schemaVersion.Version.Minor > 0 && engine == null) { schemaVersion.Version = new Version(schemaVersion.Version.Major, schemaVersion.Version.Minor - 1); engine = this.ObtainExactVersion(schemaVersion.ToAsmVersion()); } return(engine); }
public static XElement MapList <TSource>(this IXmlMappingEngine engine, IList <TSource> source, string collectionNode, bool outputDefault = false, string xmlNamespace = null) { var element = engine .MapList <TSource, XElement>(source, collectionNode, outputDefault) .InNamespace(xmlNamespace); if (!outputDefault && (element == null || element.IsEmpty)) { return(null); } return(element); }
/// <summary> /// Finds an IXmlMappingEngine for the specified version. /// <para> /// Will try to locate an earlier minor version if the exact version is not found e.g. can return Css.V2_0 if Css.V2_1 was requested. /// </para> /// </summary> /// <param name="version">Version of mapping engine to find, typically {Schema}.{Version} e.g. Css.V2_1</param> /// <param name="engine">Engine instance if found, null otherwise</param> /// <returns>true if found, false otherwise.</returns> public bool TryFind(string version, out IXmlMappingEngine engine) { try { engine = this.Find(version); return(engine != null); } catch { engine = null; return(false); } }
public AnimalXmlMapper(IXmlMappingEngine engine) : base("Animal", engine) { this.Namespace = XmlNamespaces.AppNamespace; this.NamespacePrefix = XmlNamespaces.AppNamespacePrefix; this.InitializeXmlType(string.Empty, XmlNamespaces.AppNamespace, "Animal"); this.Engine.RegisterXmlType(XmlNamespaces.PetNamespace, "Dog", typeof(Dog)); this.Engine.RegisterXmlType(XmlNamespaces.PetNamespace, "Cat", typeof(Cat)); // NB Due to children potentially being in different namespace, properties must specify prefix here this.InitializeMap(x => x.Id, xmlNamespace: XmlNamespaces.AppNamespace); this.InitializeMap(x => x.Name, xmlNamespace: XmlNamespaces.AppNamespace); }
/// <copydocfrom cref="IXmlMappingEngineFactory.TryFind" /> public bool TryFind(string version, out IXmlMappingEngine engine) { if (!this.engines.TryGetValue(version, out engine)) { engine = this.Get(version); if (engine != null) { // Cache it. this.engines[version] = engine; } } return(engine != null); }
/// <copydocfrom cref="IXmlMappingEngineFactory.TryFind" /> public bool TryFind(string version, out IXmlMappingEngine engine) { if (!this.engines.TryGetValue(version, out engine)) { engine = this.Get(version); if (engine != null) { // Cache it. this.engines[version] = engine; } } return engine != null; }
protected void RoundTripMapping(IXmlMappingEngine engine) { // Sample xml with namespaces to add complexity var xml = @"<Parent xmlns='http://www.sample.com/common' xmlns:Pet='http://www.sample.com/pet' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'> <Id>1</Id> <Name>Fred</Name> <Cost>1500.75</Cost> <Children xmlns='http://www.sample.com/app'> <Child> <Id>1</Id> <Value xmlns='http://www.sample.com/sales'>34.29</Value> <Start>2011-09-15T15:00:00</Start> <Animal xmlns='http://www.sample.com/app' xsi:type='Pet:Dog'> <Id>1</Id> <Name>Fido</Name> <Pet:Tricks>Fetch</Pet:Tricks> </Animal> </Child> <Child> <Id>2</Id> <Value xmlns='http://www.sample.com/sales'>12</Value> <Start>2020-12-31T04:00:00</Start> </Child> </Children> </Parent>"; var d = new Dog { Id = 1, Name = "Fido", Tricks = "Fetch" }; var expected = new Parent { Id = 1, Name = "Fred", Cost = 1500.75M, Children = new List<Child> { new Child { Id = 1, Value = 34.29f, Start = new DateTime(2011, 9, 15, 15, 0, 0), Dog = d }, new Child { Id = 2, Value = 12f, Start = new DateTime(2020, 12, 31, 4, 0, 0) }, } }; var processor = this.CreateProcessor(); processor.Initialize(xml); var candidate = engine.Map<XPathProcessor, Parent>(processor); this.Check(expected, candidate); var candidateXml = engine.Map<Parent, XElement>(candidate); this.CheckXml(candidateXml, xml); }
/// <copydocfrom cref="IXmlMappingEngineFactory.TryFind" /> public bool TryFind(string version, out IXmlMappingEngine engine) { if (!this.engines.TryGetValue(version, out engine)) { engine = this.Get(version); if (engine != null) { // set the Schema Version in the context so that we can use it in the mappers // shouldn't need this because implementation is never null but prevents having to alter a load of Unit tests if (engine.Context != null) { engine.Context.Set("SchemaReleaseVersion", version.ToSchemaVersion().Version); } // Cache it. this.engines[version] = engine; } } return(engine != null); }
/// <copydocfrom cref="IXmlMappingEngineFactory.TryFind" /> public bool TryFind(string version, out IXmlMappingEngine engine) { if (!this.engines.TryGetValue(version, out engine)) { engine = this.Get(version); if (engine != null) { // set the Schema Version in the context so that we can use it in the mappers // shouldn't need this because implementation is never null but prevents having to alter a load of Unit tests if (engine.Context != null) { engine.Context.Set("SchemaReleaseVersion", version.ToSchemaVersion().Version); } // Cache it. this.engines[version] = engine; } } return engine != null; }
protected override void OnSetup() { this.Container.RegisterType <IXmlMapper <XPathProcessor, Owner>, OwnerXmlMapper>(); this.Container.RegisterType <IXmlMapper <XPathProcessor, Identifier>, IdentifierXmlMapper>(); this.Container.RegisterType <IXmlMapper <XPathProcessor, Animal>, AnimalXmlMapper>(); this.Container.RegisterType <IXmlMapper <XPathProcessor, Dog>, DogXmlMapper>(); this.Container.RegisterType <IXmlMapper <XPathProcessor, Cat>, CatXmlMapper>(); this.Container.RegisterType <IXmlMapper <Owner, XElement>, OwnerXmlMapper>(); this.Container.RegisterType <IXmlMapper <Identifier, XElement>, IdentifierXmlMapper>(); this.Container.RegisterType <IXmlMapper <Animal, XElement>, AnimalXmlMapper>(); this.Container.RegisterType <IXmlMapper <Dog, XElement>, DogXmlMapper>(); this.Container.RegisterType <IXmlMapper <Cat, XElement>, CatXmlMapper>(); this.Container.RegisterType <IXmlMapper <XPathProcessor, Entity>, EntityXmlMapper>(); this.Container.RegisterType <IXmlMapper <Entity, XElement>, EntityXmlMapper>(); this.Container.RegisterType <IXmlMapperFactory, LocatorXmlMapperFactory>(); this.Container.RegisterType <IXmlMappingEngine, XmlMappingEngine>( new InjectionConstructor(typeof(IXmlMapperFactory))); this.Engine = this.ServiceLocator.GetInstance <IXmlMappingEngine>(); }
public OrderXmlMapper(IXmlMappingEngine engine) : base("order", engine) { }
protected override XmlMapper <Parent> CreateMapper(IXmlMappingEngine engine) { return(new ParentXmlMapper(engine)); }
protected override void RegisterChildMappers(IXmlMappingEngine engine) { engine.RegisterMapper(new IdXmlMapper()); }
protected override XmlMapper <Order> CreateMapper(IXmlMappingEngine engine) { return(new OrderXmlMapper(engine)); }
protected XmlMapper(string nodeName, IXmlMappingEngine engine) : base(nodeName, engine) { this.AttributeDefaultNamespace = true; }
protected XmlMapper(IXmlMappingEngine engine) : this(string.Empty, engine) { }
/// <summary> /// Check whether a mapper is in a version. /// </summary> /// <typeparam name="TSource">Source type</typeparam> /// <typeparam name="TDestination">Result type</typeparam> /// <typeparam name="TMapper">Type of mapper we expect</typeparam> /// <param name="engine">Engine to use.</param> public static void ResolveMapper <TSource, TDestination, TMapper>(this IXmlMappingEngine engine) { var xmlEngine = engine as XmlMappingEngine; xmlEngine.ResolveMapper <TSource, TDestination>(typeof(TMapper)); }
/// <summary> /// Registers a <see cref="XmlMapper{T}" /> against a <see cref="IXmlMappingEngine" />. /// </summary> /// <typeparam name="TEntity">Entity we are registering for</typeparam> /// <param name="engine">Engine to use</param> /// <param name="mapper">Mapper to use</param> public static void RegisterMapper <TEntity>(this IXmlMappingEngine engine, XmlMapper <TEntity> mapper) where TEntity : class, new() { engine.RegisterMap <XPathProcessor, TEntity>(mapper); engine.RegisterMap <TEntity, XElement>(mapper); }
public static TDestination Map <TDestination>(this IXmlMappingEngine engine, XPathProcessor xpathProcessor, string nodeName, string xmlPrefix = "") { return(engine.Map <XPathProcessor, TDestination>(xpathProcessor, nodeName, xmlPrefix: xmlPrefix)); }
protected override XmlMapper <Identifier> CreateMapper(IXmlMappingEngine engine) { return(new IdentifierXmlMapper()); }
public static XElement Map <TSource>(this IXmlMappingEngine engine, TSource source) { return(engine.Map <TSource, XElement>(source)); }
/// <summary> /// Check whether a mapper is in a version. /// </summary> /// <typeparam name="TSource">Source type</typeparam> /// <typeparam name="TDestination">Result type</typeparam> /// <param name="engine">Engine to use.</param> /// <param name="implementation">Type of mapper we expect</param> public static void ResolveMapper <TSource, TDestination>(this IXmlMappingEngine engine, Type implementation) { var xmlEngine = engine as XmlMappingEngine; xmlEngine.ResolveMapper <TSource, TDestination>(implementation); }
public static TDestination Map <TDestination>(this IXmlMappingEngine engine, XPathProcessor xpathProcessor) { return(engine.Map <XPathProcessor, TDestination>(xpathProcessor)); }
public OwnerXmlMapper2(IXmlMappingEngine engine) : base(engine) { this.InitializeMap(x => x.Name); this.InitializeMap(x => x.Pets, "Pets"); }
public static List <TDestination> MapList <TDestination>(this IXmlMappingEngine engine, XPathProcessor xpathProcessor, string collectionNode, string xmlPrefix = "", bool outputDefault = false) { return(engine.MapList <XPathProcessor, TDestination>(xpathProcessor, collectionNode, string.Empty, xmlPrefix, outputDefault: outputDefault)); }
protected override XmlMapper <Entity> CreateMapper(IXmlMappingEngine engine) { return(new EntityXmlMapper()); }