Esempio n. 1
0
        public static IEnumerable<MappedClassInfo> LoadFile(string nhibernateFilePath)
        {
            MappingDocumentParser parser = new MappingDocumentParser();
            HbmMapping mapping;

            try
            {
                using (FileStream stream = File.OpenRead(nhibernateFilePath))
                {
                    mapping = parser.Parse(stream);
                }
            }
            catch (Exception)
            {
                Console.WriteLine(nhibernateFilePath);
                throw;
            }

            if (mapping.Items.Length == 0)
            {
                throw new ParserException("NHibernate file has NO data: " + nhibernateFilePath);
            }

            return mapping.Items
                .Cast<HbmClass>()
                .Select(item => new MappedClassInfo(item, nhibernateFilePath));
        }
Esempio n. 2
0
        public static MappedClassInfo LoadFile(string nhibernateFilePath)
        {
            MappingDocumentParser parser = new MappingDocumentParser();
            HbmMapping mapping;

            try
            {
                using (FileStream stream = File.OpenRead(nhibernateFilePath))
                {
                    mapping = parser.Parse(stream);
                }
            }
            catch (Exception)
            {
                Console.WriteLine(nhibernateFilePath);
                throw;
            }

            if (mapping.Items.Length != 1)
            {
                throw new ParserException("NHibernate file has NO data: " + nhibernateFilePath);
            }

            MappedClassInfo classInfo = new MappedClassInfo((HbmClass) mapping.Items[0], nhibernateFilePath);
            return classInfo;
        }
Esempio n. 3
0
        public static MappedClassInfo LoadFromString(string hbmData)
        {
            MappingDocumentParser parser = new MappingDocumentParser();
            MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(hbmData));
            HbmMapping mapping = parser.Parse(stream);

            if (mapping.Items.Length != 1)
            {
                throw new ParserException("NO data in: " + hbmData);
            }

            MappedClassInfo classInfo = new MappedClassInfo((HbmClass) mapping.Items[0], "from text");
            return classInfo;
        }
		public void CanDeserializeHBM()
		{
			string[] someEmbeddedResources = {
				"NHibernate.DomainModel.ABC.hbm.xml",
				"NHibernate.DomainModel.ABCProxy.hbm.xml",
			};

			Assembly domainModelAssembly = typeof (DomainModel.A).Assembly;
			MappingDocumentParser parser = new MappingDocumentParser();

			foreach (string embeddedResource in someEmbeddedResources)
				using (Stream stream = domainModelAssembly.GetManifestResourceStream(embeddedResource))
				{
					HbmMapping mapping = parser.Parse(stream);
					Assert.IsNotNull(mapping, "Mapping: " + embeddedResource);
				}
		}
		public static ClassInfo LoadFromFile (string fileName, string basePath)
		{
			ClassInfo c = null;
			
			MappingDocumentParser p = new MappingDocumentParser ();
			using (FileStream stream = new FileStream (System.IO.Path.Combine (basePath, fileName), FileMode.Open)) {
				HbmMapping m = p.Parse (stream);
				foreach (object o in m.Items) {
					if (o is HbmClass) {
						HbmClass cl = (HbmClass)o;
						
						c = new ClassInfo ();
						c.Namespace = m.@namespace;
						c.Name = cl.name;
						c.Properties = new List<PropertyInfo> ();
						
						if (cl.Id != null) {
							var id = new PropertyInfo { Name = cl.Id.name, PropertyType = cl.Id.type1 != null ? cl.Id.type1 : "String", RelationType = RelationType.Property, IsNullable = true};
							c.Properties.Add (id);
							c.Id = id;
						}
						
						foreach (object po in cl.Items) {
							if (po is HbmProperty) {
								HbmProperty prop = (HbmProperty)po;
								c.Properties.Add (new PropertyInfo { Name = prop.name, PropertyType = prop.type1 != null ? prop.type1 : "String", RelationType = RelationType.Property, IsNullable = !prop.notnull });
							} else if (po is HbmManyToOne) {
								HbmManyToOne mo = (HbmManyToOne)po;
								c.Properties.Add (new PropertyInfo { Name = mo.name, PropertyType = mo.@class, RelationType = RelationType.ManyToOne, IsNullable = !mo.notnull });
							} else if (po is HbmSet) {
								HbmSet se = (HbmSet)po;
								
								c.Properties.Add (new PropertyInfo { Name = se.name, PropertyType = ((HbmOneToMany)se.Item).@class, RelationType = RelationType.ManyToMany });
							}
						}
					}
				}
			}
			return c;
		}
		public void XmlSerialization()
		{
			// NH-1865 (have a look to comments in JIRA)
			var mdp = new MappingDocumentParser();
			using (Stream stream = GetType().Assembly.GetManifestResourceStream("NHibernate.Test.MappingTest.Wicked.hbm.xml"))
			{
				HbmMapping mapping = mdp.Parse(stream);
				Assert.That(mapping, Is.XmlSerializable);
			}
		}
        public List<ClassItem> RenderClassFromHbm(string basePath, string fileName)
        {
            List<ClassItem> classList = new List<ClassItem>();
            MappingDocumentParser p = new MappingDocumentParser();
            HbmMapping m = p.Parse(System.IO.File.Open(System.IO.Path.Combine(basePath, fileName), System.IO.FileMode.Open));
            foreach (object o in m.Items)
            {
                if (o is HbmClass)
                {
                    HbmClass cl = (HbmClass)o;

                    ClassItem c = new ClassItem();
                    classList.Add(c);
                    c.Namespace = m.@namespace;
                    c.Name = cl.name;
                    c.Properties = new List<PropertyItem>();

                    if (cl.Id != null)
                    {
                        c.Properties.Add(
                                    new PropertyItem
                                    {
                                        Name = cl.Id.name,
                                        PropertyType = cl.Id.type != null ? cl.Id.type1 : "String",
                                        RelationType = RelationType.Property,
                                        IsNullable = true
                                    }
                                );
                    }

                    foreach (object po in cl.Items)
                    {
                        if (po is HbmProperty)
                        {
                            HbmProperty prop = (HbmProperty)po;
                            c.Properties.Add(
                                new PropertyItem
                                {
                                    Name = prop.name,
                                    PropertyType = prop.type1 != null ? prop.type1 : "String",
                                    RelationType = RelationType.Property,
                                    IsNullable = !prop.notnull
                                }
                            );
                        }
                        else if (po is HbmManyToOne)
                        {
                            HbmManyToOne mo = (HbmManyToOne)po;
                            c.Properties.Add(
                                new PropertyItem
                                {
                                    Name = mo.name,
                                    PropertyType = mo.@class,
                                    RelationType = RelationType.ManyToOne,
                                    IsNullable = !mo.notnull
                                }
                            );
                        }
                        else if (po is HbmSet)
                        {
                            HbmSet se = (HbmSet)po;

                            c.Properties.Add(
                                new PropertyItem
                                {
                                    Name = se.name,
                                    PropertyType = ((HbmOneToMany)se.Item).@class,
                                    RelationType = RelationType.ManyToMany
                                }
                            );
                        }
                    }
                }
            }
            return classList;
        }