Beispiel #1
0
            public void AddElement(IComplexElement element)
            {
                internalRenderScheduler.AddTask(element.Generate());

                elements.Add(element);
                internalRenderScheduler.AddTask(RenderAddedElement(element), onRenderDone);

                AddRuntimeUpdatable(element as IRuntimeUpdatable);
            }
Beispiel #2
0
        private IComplexElement DeserializeComplexElement(IElementSchema schema, XmlElement xmlElement)
        {
            if (typeof(ComplexElement).IsAssignableFrom(schema.ElementType))
            {
                var ctor = schema.ElementType.GetConstructor(new[] { typeof(IElementSchema) });
                if (ctor == null)
                {
                    throw new NotImplementedException();
                }

                IComplexElement element = (IComplexElement)ctor.Invoke(new object[] { schema });

                // attributes
                foreach (XmlAttribute xmlElementAttribute in xmlElement.Attributes)
                {
                    element.SetAttribute(xmlElementAttribute.Name, xmlElementAttribute.Value);
                }

                // child elements
                foreach (var xmlChildNode in xmlElement.ChildNodes)
                {
                    if (xmlChildNode is XmlComment)
                    {
                        continue;
                    }

                    if (xmlChildNode is XmlElement xmlChildElement)
                    {
                        var childSchema = schema.GetChildElement(xmlChildElement.Name);

                        if (childSchema.ContainsTextNode())
                        {
                            ITextNodeElement childElement = this.DeserializeTextNodeElement(childSchema, xmlChildElement);

                            ((ElementList)(element.Children)).Add(childElement, false);
                        }
                        else
                        {
                            var childElement = this.DeserializeComplexElement(childSchema, xmlChildElement);
                            ((ElementList)(element.Children)).Add(childElement, false);
                        }
                    }
                    else
                    {
                        throw new NotImplementedException();
                    }
                }

                return(element);
            }
            else
            {
                throw new NotImplementedException();
            }
        }
Beispiel #3
0
        public XmlDocument Serialize(IComplexElement rootElement)
        {
            // todo checks

            var xmlDocument = new XmlDocument();

            var attr = rootElement.GetType().GetCustomAttribute <XmlDocumentDeclarationAttribute>();

            if (attr != null)
            {
                var xmlDeclaration = xmlDocument.CreateXmlDeclaration("1.0", attr.Encoding, attr.Standalone);
                xmlDocument.AppendChild(xmlDeclaration);
            }

            this.SerializeComplexElement(xmlDocument, rootElement);

            return(xmlDocument);
        }
Beispiel #4
0
        private XmlElement SerializeComplexElement(XmlNode xmlParentNode, IComplexElement element)
        {
            // todo: check element has correct schema etc

            XmlDocument ownerDocument;

            if (xmlParentNode is XmlDocument xmlDocument)
            {
                ownerDocument = xmlDocument;
            }
            else if (xmlParentNode is XmlElement xmlParentElement)
            {
                ownerDocument = xmlParentElement.OwnerDocument ?? throw new NotImplementedException();
            }
            else
            {
                throw new NotImplementedException();
            }

            var xmlElement = ownerDocument.CreateElement(element.Name);

            // deal with attributes
            foreach (var attributeName in element.GetAttributeNames())
            {
                var attributeValue = element.GetAttribute(attributeName);
                xmlElement.SetAttribute(attributeName, attributeValue);
            }

            // deal with child elements
            foreach (var childElement in element.Children)
            {
                // todo: check element has correct schema etc

                if (childElement is TextNodeElement childTextNodeElement)
                {
                    var textElement = ownerDocument.CreateElement(childTextNodeElement.Name);

                    if (string.IsNullOrEmpty(childTextNodeElement.Value))
                    {
                        // do nothing
                    }
                    else
                    {
                        textElement.InnerText = childTextNodeElement.Value;
                    }

                    foreach (var attributeName in childTextNodeElement.GetAttributeNames())
                    {
                        var attributeValue = childTextNodeElement.GetAttribute(attributeName);
                        textElement.SetAttribute(attributeName, attributeValue);
                    }

                    xmlElement.AppendChild(textElement);
                }
                else if (childElement is ComplexElement childComplexElement)
                {
                    var childXmlElement = this.SerializeComplexElement(xmlElement, childComplexElement);
                    xmlElement.AppendChild(childXmlElement);
                }
                else
                {
                    throw new NotImplementedException();
                }
            }

            xmlParentNode.AppendChild(xmlElement);

            return(xmlElement);
        }