Esempio n. 1
0
        /// <summary>
        /// Generate a System.Xml.XmlAttribute instance and populate with
        /// correct information.
        /// </summary>
        /// <param name="doc">XmlDocument this attribute will be part of.</param>
        /// <param name="parent">The parent XmlNode</param>
        /// <returns>Returns a valid instance of an XmlAttribute.</returns>
        public virtual System.Xml.XmlAttribute GenerateXmlAttribute(PeachXmlDoc doc, XmlNode parent)
        {
            System.Diagnostics.Debug.Assert(Count > 0);
            var elem      = this[0];
            var xmlAttrib = doc.doc.CreateAttribute(attributeName, ns);

            xmlAttrib.Value = "|||" + elem.fullName + "|||";
            doc.values.Add(xmlAttrib.Value, elem.InternalValue);
            return(xmlAttrib);
        }
Esempio n. 2
0
        public virtual XmlNode GenerateXmlNode(PeachXmlDoc doc, XmlNode parent)
        {
            XmlNode xmlNode = doc.doc.CreateElement(elementName, ns);

            foreach (DataElement child in this)
            {
                if (child is XmlAttribute)
                {
                    XmlAttribute attrib = child as XmlAttribute;
                    if ((child.mutationFlags & DataElement.MUTATE_OVERRIDE_TYPE_TRANSFORM) != 0)
                    {
                        // Happend when data element is duplicated.  Duplicate attributes are invalid so ignore.
                        continue;
                    }

                    if (attrib.Count > 0)
                    {
                        xmlNode.Attributes.Append(attrib.GenerateXmlAttribute(doc, xmlNode));
                    }
                }
                else if (child is String)
                {
                    var fullName = child.fullName;
                    xmlNode.InnerText = "|||" + fullName + "|||";
                    doc.values.Add(xmlNode.InnerText, new Variant(child.Value));
                }
                else if (child is Number)
                {
                    xmlNode.InnerText = (string)child.InternalValue;
                }
                else if (child is XmlElement)
                {
                    if ((child.mutationFlags & DataElement.MUTATE_OVERRIDE_TYPE_TRANSFORM) != 0)
                    {
                        var key  = "|||" + child.fullName + "|||";
                        var text = doc.doc.CreateTextNode(key);
                        xmlNode.AppendChild(text);
                        doc.values.Add(key, child.InternalValue);
                    }
                    else
                    {
                        xmlNode.AppendChild(((XmlElement)child).GenerateXmlNode(doc, xmlNode));
                    }
                }
                else
                {
                    throw new PeachException("Error, XmlElements can only contain XmlElement, XmlAttribute, and a single Number or String element.");
                }
            }

            return(xmlNode);
        }
Esempio n. 3
0
        protected override Variant GenerateInternalValue()
        {
            if ((mutationFlags & DataElement.MUTATE_OVERRIDE_TYPE_TRANSFORM) != 0)
            {
                return(MutatedValue);
            }

            PeachXmlDoc doc = new PeachXmlDoc();

            doc.doc.AppendChild(GenerateXmlNode(doc, null));
            string template = doc.doc.OuterXml;

            string[] parts = template.Split(new string[] { "|||" }, StringSplitOptions.RemoveEmptyEntries);

            BitStream bs = new BitStream();

            foreach (string item in parts)
            {
                BitStream toWrite = null;
                Variant   var     = null;
                string    key     = "|||" + item + "|||";
                if (doc.values.TryGetValue(key, out var))
                {
                    var type = var.GetVariantType();

                    if (type == Variant.VariantType.BitStream)
                    {
                        toWrite = (BitStream)var;
                    }
                    else if (type == Variant.VariantType.ByteString)
                    {
                        toWrite = new BitStream((byte[])var);
                    }
                    else
                    {
                        toWrite = new BitStream(Encoding.ASCII.GetRawBytes((string)var));
                    }
                }
                else
                {
                    toWrite = new BitStream(Encoding.ASCII.GetRawBytes(item));
                }

                bs.Write(toWrite);
            }

            return(new Variant(bs));
        }