Пример #1
0
        private void WriteInternal(Stream stream, DomObject rootDomObject, bool bTemp)
        {
            XmlWriterSettings settings = new XmlWriterSettings();

            settings.Indent          = true;
            settings.IndentChars     = IndentChars;
            settings.NewLineHandling = NewLineHandling.Replace;
            settings.NewLineChars    = NewLineChars;

            using (XmlWriter writer = XmlWriter.Create(stream, settings))
            {
                if (writer == null)
                {
                    return;
                }

                writer.WriteStartDocument();

                DomSchema schema = rootDomObject.Collection.Schema;
                WriteElement(rootDomObject, writer, schema, bTemp);

                writer.WriteEndDocument();
                writer.Close();
            }
        }
Пример #2
0
        public void CreateAttribute_uses_primitive_value_specified_by_schema()
        {
            var schema  = new DomSchema("custom");
            var attrDef = schema.AttributeDefinitions.AddNew("lcid");

            attrDef.ValueType = typeof(int);

            var doc = new DomDocumentFragment().WithSchema(schema);

            Assert.IsInstanceOf <DomValue <int> >(
                doc.CreateAttribute("lcid").DomValue
                );
        }
Пример #3
0
        public void CreateAttribute_uses_dom_value_specified_by_schema()
        {
            var schema  = new DomSchema("custom");
            var attrDef = schema.AttributeDefinitions.AddNew("class");

            attrDef.ValueType = typeof(PDomValue);

            var doc = new DomDocument().WithSchema(schema);

            Assert.IsInstanceOf <PDomValue>(
                doc.CreateAttribute("class").DomValue
                );
        }
Пример #4
0
        /// <summary>
        /// Write element
        /// </summary>
        /// <param name="element"></param>
        /// <param name="writer"></param>
        /// <param name="schema"></param>
        /// <param name="bTemp"></param>
        protected void WriteElement(DomObject element, XmlWriter writer, DomSchema schema, bool bTemp)
        {
            // Test if the element should be saved or not
            if (!bTemp && !element.CanCreateInterface <ISledProjectFilesSaveableType>())
            {
                return;
            }

            string elemNs = element.MetaElement.QualifiedName.Namespace;
            string elemPrefix;

            // Is this the root DomObject?
            if (schema != null)
            {
                elemPrefix = schema.GetPrefix(elemNs) ?? GeneratePrefix(elemNs);

                writer.WriteStartElement(elemPrefix, element.MetaElement.Name, elemNs);

                // Define the xsi namespace
                writer.WriteAttributeString("xmlns", "xsi", null, XmlSchema.InstanceNamespace);

                // Define schema namespaces
                foreach (XmlQualifiedName name in schema.Namespaces)
                {
                    if (name.Name != elemPrefix) // don't redefine the element namespace
                    {
                        writer.WriteAttributeString("xmlns", name.Name, null, name.Namespace);
                    }
                }
            }
            else
            {
                // Not the root, so all schema namespaces have been defined
                elemPrefix = writer.LookupPrefix(elemNs) ?? GeneratePrefix(elemNs);

                writer.WriteStartElement(elemPrefix, element.MetaElement.Name, elemNs);
            }

            // Write type name if this is a polymorphic type
            DomComplexType elementType = element.Type;

            if (element.MetaElement.Type != elementType)
            {
                string typeName   = elementType.Name.Name;
                string typeNs     = elementType.Name.Namespace;
                string typePrefix = writer.LookupPrefix(typeNs);
                if (typePrefix == null)
                {
                    typePrefix = GeneratePrefix(typeNs);
                    writer.WriteAttributeString("xmlns", typePrefix, null, typeNs);
                }

                if (typePrefix != string.Empty)
                {
                    typeName = typePrefix + ":" + typeName;
                }

                writer.WriteAttributeString("xsi", "type", XmlSchema.InstanceNamespace, typeName);
            }

            // When writing project file we build a list of attributes to NOT save
            List <string> lstAttrsDontSave = new List <string>();

            // Do some stuff if writing project file
            if (!bTemp)
            {
                // Get all annotations (including base type annotations [and their base type annotations etc.])
                List <XmlNode> lstAnnotations = new List <XmlNode>();
                GetAllAnnotations(element.Type, ref lstAnnotations);

                // Go through pulling out which attributes to not save based on the annotation
                foreach (XmlNode annotation in lstAnnotations)
                {
                    // Do something if it's a "dont save" annotation!
                    if (string.Compare(annotation.Name, DontSaveAnnotation, true) == 0)
                    {
                        XmlNode node = annotation.Attributes.GetNamedItem("name");
                        if (node != null)
                        {
                            string szAttribute = node.Value;
                            if (!lstAttrsDontSave.Contains(szAttribute))
                            {
                                lstAttrsDontSave.Add(szAttribute);
                            }
                        }
                    }
                }
            }

            // Write normal attributes
            foreach (DomMetaAttribute attribute in elementType.Attributes)
            {
                // Do some stuff if writing project file
                if (!bTemp && (lstAttrsDontSave.Count > 0) && !string.IsNullOrEmpty(attribute.Name))
                {
                    if (lstAttrsDontSave.Contains(attribute.Name))
                    {
                        continue;
                    }
                }

                object value = element.GetAttribute(attribute);
                if (attribute.Required)
                {
                    if (value == null)
                    {
                        throw new InvalidOperationException("missing attribute");
                    }

                    writer.WriteAttributeString(attribute.Name, attribute.Type.Convert(value));
                }
                else if (value != null && value != attribute.Type.GetDefault())
                {
                    writer.WriteAttributeString(attribute.Name, attribute.Type.Convert(value));
                }
            }

            // Write element value
            if (element.Value != null &&
                element.Value != elementType.ValueType.GetDefault())
            {
                writer.WriteString(elementType.ValueType.Convert(element.Value));
            }

            // write child elements
            foreach (DomMetaElement elem in elementType.Children)
            {
                foreach (DomObject child in element.GetChildren(elem.Name))
                {
                    WriteElement(child, writer, null, bTemp);
                }
            }

            writer.WriteEndElement();
        }