コード例 #1
0
        /// <summary> Writes the mapping of the specified class in the specified stream. </summary>
        /// <param name="stream">Where the xml is written; can be null if you send the writer.</param>
        /// <param name="type">User-defined type containing a valid attribute (can be [Class] or [xSubclass]).</param>
        /// <param name="writer">The XmlTextWriter used to write the xml; can be null if you send the stream. You can also create it yourself, but don't forget to write the StartElement ("hibernate-mapping") before.</param>
        /// <param name="writeEndDocument">Tells if the EndElement of "hibernate-mapping" must be written; send false to append many classes in the same stream.</param>
        /// <returns>The parameter writer if it was not null; else it is a new writer (created using the stream). Send it back to append many classes in this stream.</returns>
        public virtual System.Xml.XmlTextWriter Serialize(System.IO.Stream stream, System.Type type, System.Xml.XmlTextWriter writer, bool writeEndDocument)
        {
            if (stream == null && writer == null)
            {
                throw new System.ArgumentNullException("stream");
            }
            if (type == null)
            {
                throw new System.ArgumentNullException("type");
            }

            if (writer == null)
            {
                writer            = new System.Xml.XmlTextWriter(stream, System.Text.Encoding.UTF8);
                writer.Formatting = System.Xml.Formatting.Indented;
                writer.WriteStartDocument();
                if (WriteDateComment)
                {
                    writer.WriteComment(string.Format("Generated by NHibernate.Mapping.Attributes on {0}.", System.DateTime.Now.ToString("u")));
                }
                WriteHibernateMapping(writer, type);
            }

            if (IsClass(type))
            {
                HbmWriter.WriteClass(writer, type);
            }
            else if (IsSubclass(type, typeof(SubclassAttribute)))
            {
                HbmWriter.WriteSubclass(writer, type);
            }
            else if (IsSubclass(type, typeof(JoinedSubclassAttribute)))
            {
                HbmWriter.WriteJoinedSubclass(writer, type);
            }
            else if (IsSubclass(type, typeof(UnionSubclassAttribute)))
            {
                HbmWriter.WriteUnionSubclass(writer, type);
            }
            else
            {
                throw new System.ArgumentException("No valid attribute; looking for [Class] or [xSubclass].", "type");
            }

            if (writeEndDocument)
            {
                writer.WriteEndElement();                 // </hibernate-mapping>
                writer.WriteEndDocument();
                writer.Flush();

                if (!Validate)
                {
                    return(writer);
                }

                // Validate the generated XML stream
                try
                {
                    writer.BaseStream.Position = 0;
                    var tr     = new System.Xml.XmlTextReader(writer.BaseStream);
                    var reader = CreateReader(tr);

                    _stop = false;
                    while (reader.Read() && !_stop)                    // Read to validate (stop at the first error)
                    {
                        ;
                    }
                }
                catch (System.Exception ex)
                {
                    Error.Append(ex.ToString()).Append(System.Environment.NewLine + System.Environment.NewLine);
                }
            }
            else
            {
                writer.Flush();
            }

            return(writer);
        }
コード例 #2
0
        private void MapSubclasses(System.Collections.ArrayList subclasses, System.Collections.Specialized.StringCollection extendedClassesNames, System.Collections.ArrayList mappedClassesNames, System.Xml.XmlTextWriter writer)
        {
            System.Collections.ArrayList mappedSubclassesNames = new System.Collections.ArrayList();
            // Map each subclass after the class it extends
            while (subclasses.Count > 0)
            {
                for (int i = subclasses.Count - 1; i >= 0; i--)
                {
                    System.Type type = subclasses[i] as System.Type;
                    string      extendedClassName = extendedClassesNames[i];
                    if (extendedClassName == null)
                    {
                        throw new MappingException("You must specify the Extends attribute of the Subclass: " + type.FullName);
                    }

                    if (!mappedClassesNames.Contains(extendedClassName) &&
                        !mappedSubclassesNames.Contains(extendedClassName))
                    {
                        bool extendedClassFoundButNotMappedYet = false;
                        // Make sure that the extended class is mapped (in this assembly)
                        foreach (System.Type subclass in subclasses)
                        {
                            if (HbmWriterHelper.GetNameWithAssembly(subclass)
                                == extendedClassName)
                            {
                                if (subclass == type)
                                {
                                    throw new MappingException("The Subclass " + type.FullName + " extends itself.");
                                }
                                else
                                {
                                    extendedClassFoundButNotMappedYet = true;
                                    break;
                                }
                            }
                        }
                        if (extendedClassFoundButNotMappedYet)
                        {
                            continue;                             // Map this one later
                        }
                        // Else unknown extended class:
                        //   Assume it is mapped somewhere else and map this subclass
                    }

                    if (IsSubclass(type, typeof(SubclassAttribute)))
                    {
                        HbmWriter.WriteSubclass(writer, type);
                    }
                    else if (IsSubclass(type, typeof(JoinedSubclassAttribute)))
                    {
                        HbmWriter.WriteJoinedSubclass(writer, type);
                    }
                    else if (IsSubclass(type, typeof(UnionSubclassAttribute)))
                    {
                        HbmWriter.WriteUnionSubclass(writer, type);
                    }

                    // Note: Do not add to mappedClassesNames because it is for x-subclasses (and a x-subclasses shouldn't extend another x-subclasses)
                    mappedSubclassesNames.Add(HbmWriterHelper.GetNameWithAssembly(type));
                    subclasses.RemoveAt(i);
                    extendedClassesNames.RemoveAt(i);
                }
            }
        }
コード例 #3
0
        /// <summary> Writes the mapping of the specified class in the specified stream. </summary>
        /// <param name="stream">Where the xml is written; can be null if you send the writer.</param>
        /// <param name="type">User-defined type containing a valid attribute (can be [Class] or [xSubclass]).</param>
        /// <param name="writer">The XmlTextWriter used to write the xml; can be null if you send the stream. You can also create it yourself, but don't forget to write the StartElement ("hibernate-mapping") before.</param>
        /// <param name="writeEndDocument">Tells if the EndElement of "hibernate-mapping" must be written; send false to append many classes in the same stream.</param>
        /// <returns>The parameter writer if it was not null; else it is a new writer (created using the stream). Send it back to append many classes in this stream.</returns>
        public virtual System.Xml.XmlTextWriter Serialize(System.IO.Stream stream, System.Type type, System.Xml.XmlTextWriter writer, bool writeEndDocument)
        {
            if (stream == null && writer == null)
            {
                throw new System.ArgumentNullException("stream");
            }
            if (type == null)
            {
                throw new System.ArgumentNullException("type");
            }

            if (writer == null)
            {
                writer            = new System.Xml.XmlTextWriter(stream, System.Text.Encoding.UTF8);
                writer.Formatting = System.Xml.Formatting.Indented;
                writer.WriteStartDocument();
                if (WriteDateComment)
                {
                    writer.WriteComment(string.Format("Generated by NHibernate.Mapping.Attributes on {0}.", System.DateTime.Now.ToString("u")));
                }
                WriteHibernateMapping(writer, type);
            }

            if (IsClass(type))
            {
                HbmWriter.WriteClass(writer, type);
            }
            else if (IsSubclass(type, typeof(SubclassAttribute)))
            {
                HbmWriter.WriteSubclass(writer, type);
            }
            else if (IsSubclass(type, typeof(JoinedSubclassAttribute)))
            {
                HbmWriter.WriteJoinedSubclass(writer, type);
            }
            else if (IsSubclass(type, typeof(UnionSubclassAttribute)))
            {
                HbmWriter.WriteUnionSubclass(writer, type);
            }
            else
            {
                throw new System.ArgumentException("No valid attribute; looking for [Class] or [xSubclass].", "type");
            }

            if (writeEndDocument)
            {
                writer.WriteEndElement();                 // </hibernate-mapping>
                writer.WriteEndDocument();
                writer.Flush();

                if (!Validate)
                {
                    return(writer);
                }

                // Validate the generated XML stream
                try
                {
                    writer.BaseStream.Position = 0;
                    System.Xml.XmlTextReader       tr = new System.Xml.XmlTextReader(writer.BaseStream);
                    System.Xml.XmlValidatingReader vr = new System.Xml.XmlValidatingReader(tr);

                    // Open the Schema
                    System.IO.Stream schema = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("NHibernate.Mapping.Attributes.nhibernate-mapping.xsd");
                    vr.Schemas.Add("urn:nhibernate-mapping-2.2", new System.Xml.XmlTextReader(schema));
                    vr.ValidationType          = System.Xml.ValidationType.Schema;
                    vr.ValidationEventHandler += new System.Xml.Schema.ValidationEventHandler(XmlValidationHandler);

                    _stop = false;
                    while (vr.Read() && !_stop)                    // Read to validate (stop at the first error)
                    {
                        ;
                    }
                }
                catch (System.Exception ex)
                {
                    Error.Append(ex.ToString()).Append(System.Environment.NewLine + System.Environment.NewLine);
                }
            }
            else
            {
                writer.Flush();
            }

            return(writer);
        }