/// <summary>
        /// Desrializes an information from the array of the bytes.
        /// </summary>
        /// <param name="data"> The array. </param>
        /// <param name="className"> Name of the class. </param>
        /// <returns> Values of the class. </returns>
        protected virtual IDictionary <string, string> DeserializeInfo(byte[] data, out string className)
        {
            AltXmlDocument document = new AltXmlDocument(data.GetString());

            AltXmlNode      classNode     = document.RootNode[ClassNodeString];
            AltXmlAttribute nameAttribute = classNode.GetAttribute(NameAttributeString);

            if (nameAttribute == null)
            {
                throw new DataNotFoundException(NameAttributeString);
            }

            className = nameAttribute.Value;

            LinkedList <AltXmlNode> subNodes = classNode.SubNodes;

            IDictionary <string, string> values = new Dictionary <string, string>(subNodes.Count);

            AltXmlNode xmlNode;

            for (var node = classNode.SubNodes.First; node != null; node = node.Next)
            {
                xmlNode = node.Value;
                values.Add(xmlNode.Name, xmlNode.Value != NullIdentifier ? xmlNode.Value : null);
            }

            return(values);
        }
        public void ConvertXMLWithEscapeChars_SystemXML_AltXML()
        {
            XmlDocument document    = CreateDocument();
            XmlElement  rootElement = CreateElement(document, document, "root");
            XmlNode     classNode   = CreateNode(document, rootElement, "class");

            CreateAttribute(document, classNode, "name", "'\"&<>");

            XmlElement keysElement = CreateElement(document, classNode, "keys");

            XmlNode key1Node = CreateNode(document, keysElement, "key");

            CreateAttribute(document, key1Node, "name", "'\"&<>");
            XmlNode key1Value = CreateNode(document, key1Node, "value", "'\"&<>");

            UFT8StringWriter stringWriter = new UFT8StringWriter();

            document.Save(stringWriter);
            string xmlString = stringWriter.ToString();

            AltXmlDocument altDocument = new AltXmlDocument(xmlString);

            // Root node
            AltXmlNode altRootNode = altDocument.RootNode;

            Assert.NotNull(altRootNode);
            Assert.AreEqual("", altRootNode.Value);

            // Class node
            AltXmlNode altClassNode = altRootNode[classNode.Name];

            Assert.NotNull(altClassNode);
            Assert.AreEqual("", altClassNode.Value);

            AltXmlAttribute altClassNodeAttribute = altClassNode.GetAttribute("name");

            Assert.NotNull(altClassNodeAttribute);
            Assert.AreEqual("'\"&<>", altClassNodeAttribute.Value);

            // Keys node
            AltXmlNode altKeysNode = altClassNode[keysElement.Name];

            Assert.NotNull(altKeysNode);
            Assert.AreEqual("", altKeysNode.Value);

            // Key1 node
            AltXmlNode altKey1Node = altKeysNode.SubNodes.ElementAt(0);

            Assert.NotNull(altKey1Node);
            Assert.AreEqual("", altKey1Node.Value);

            AltXmlAttribute altKey1Attribute = altKey1Node.GetAttribute("name");

            Assert.NotNull(altKey1Attribute);
            Assert.AreEqual("'\"&<>", altKey1Attribute.Value);
        }
        public void SaveLoadXMLWithEscapeChars_AltXML_AltXML()
        {
            AltXmlDocument altDocument = new AltXmlDocument("root", "");
            AltXmlNode     node        = new AltXmlNode("node", "'\"&<>\t\n\r");

            altDocument.RootNode.SubNodes.AddLast(node);

            byte[] data = altDocument.GetXmlData();

            AltXmlDocument loadedDocument = new AltXmlDocument(data.GetString());

            Assert.AreEqual("'\"&<>\t\n\r", loadedDocument.RootNode.SubNodes.First.Value.Value);
        }
Example #4
0
        /// <summary>
        /// Creates a new XML document (from XML string).
        /// </summary>
        /// <param name="xmlString"> XML string. </param>
        public AltXmlDocument(string xmlString)
        {
            int    i = 0;
            string nodeName;
            char   xmlChar;

            while (true)
            {
                SkipSpaces(xmlString, ref i);

                if (xmlString[i] != '<')
                {
                    throw new UnexpectedTokenInsteadLessThanException(xmlString[i]);
                }

                xmlChar = xmlString[++i]; // skip <

                if (xmlChar == '?')       // declaration
                {
                    i++;                  // skip ?
                    this.Declarations = ParseAttributes(xmlString, ref i, '?', '>', out nodeName);
                    i++;                  // skip ending ?
                    i++;                  // skip ending >
                    continue;
                }

                if (xmlChar == '!')             // doctype
                {
                    while (xmlString[i] != '>') // skip doctype
                    {
                        i++;
                    }

                    i++; // skip >
                    continue;
                }

                this.RootNode = new AltXmlNode(xmlString, ref i);
                break;
            }
        }
Example #5
0
        /// <summary>
        /// Serializes an information to the array of the bytes.
        /// </summary>
        /// <param name="className"> Name of the class. </param>
        /// <param name="values"> Values (primitive). </param>
        /// <returns> Array of the bytes. </returns>
        protected virtual byte[] SerializeInfo(string className, IDictionary <string, string> values)
        {
            AltXmlDocument document = new AltXmlDocument(RootNodeString, "");

            AltXmlNode classNode = new AltXmlNode(ClassNodeString, "");

            document.RootNode.SubNodes.AddLast(classNode);

            AltXmlAttribute attribute = new AltXmlAttribute(NameAttributeString, className);

            classNode.Attributes.AddLast(attribute);

            foreach (var pair in values)
            {
                classNode.SubNodes.AddLast(new AltXmlNode(
                                               pair.Key,
                                               pair.Value ?? NullIdentifier));
            }

            return(document.GetXmlData());
        }
        public void ConvertXMLWithPredefinedEntities_SystemXML_AltXML()
        {
            XmlDocument document    = CreateDocument();
            XmlElement  rootElement = CreateElement(document, document, "root");
            XmlNode     classNode   = CreateNode(document, rootElement, "class");

            CreateAttribute(document, classNode, "name", "Test");

            XmlElement keysElement = CreateElement(document, classNode, "keys");

            XmlNode key1Node = CreateNode(document, keysElement, "key");

            CreateAttribute(document, key1Node, "name", "key1");
            XmlNode key1Value = CreateNode(document, key1Node, "value", "key1_value");

            // Key 2 node with predefined entities
            XmlNode key2Node = CreateNode(document, keysElement, "key");

            CreateAttribute(document, key2Node, "name", "\"&'");
            XmlNode key2Value = CreateNode(document, key2Node, "value", "<>");

            UFT8StringWriter stringWriter = new UFT8StringWriter();

            document.Save(stringWriter);
            string xmlString = stringWriter.ToString();

            AltXmlDocument altDocument = new AltXmlDocument(xmlString);

            // Root node
            AltXmlNode altRootNode = altDocument.RootNode;

            Assert.NotNull(altRootNode);
            Assert.AreEqual("", altRootNode.Value);

            // Class node
            AltXmlNode altClassNode = altRootNode[classNode.Name];

            Assert.NotNull(altClassNode);
            Assert.AreEqual("", altClassNode.Value);

            AltXmlAttribute altClassNodeAttribute = altClassNode.GetAttribute("name");

            Assert.NotNull(altClassNodeAttribute);
            Assert.AreEqual("Test", altClassNodeAttribute.Value);

            // Keys node
            AltXmlNode altKeysNode = altClassNode[keysElement.Name];

            Assert.NotNull(altKeysNode);
            Assert.AreEqual("", altKeysNode.Value);

            // Key1 node
            AltXmlNode altKey1Node = altKeysNode.SubNodes.ElementAt(0);

            Assert.NotNull(altKey1Node);
            Assert.AreEqual("", altKey1Node.Value);

            AltXmlAttribute altKey1Attribute = altKey1Node.GetAttribute("name");

            Assert.NotNull(altKey1Attribute);
            Assert.AreEqual("key1", altKey1Attribute.Value);

            AltXmlNode altKey1Value = altKey1Node[key1Value.Name];

            Assert.NotNull(altKey1Value);
            Assert.AreEqual(key1Value.InnerText, altKey1Value.Value);

            // Key2 node
            AltXmlNode altKey2Node = altKeysNode.SubNodes.ElementAt(1);

            Assert.NotNull(altKey2Node);
            Assert.AreEqual("", altKey2Node.Value);

            AltXmlAttribute altKey2Attribute = altKey2Node.GetAttribute("name");

            Assert.NotNull(altKey2Attribute);
            Assert.AreEqual("\"&'", altKey2Attribute.Value);

            AltXmlNode altKey2Value = altKey2Node[key2Value.Name];

            Assert.NotNull(altKey2Value);
            Assert.AreEqual(key2Value.InnerText, altKey2Value.Value);
        }
Example #7
0
 /// <summary>
 /// Creates a new XML document.
 /// </summary>
 /// <param name="name"> Name of the root node. </param>
 /// <param name="value"> Value of the root node. </param>
 public AltXmlDocument(string name, string value)
 {
     this.RootNode = new AltXmlNode(name, value);
 }