Beispiel #1
0
        /// <summary>
        /// Load child node(s) from file
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public new string Load(string file, VXmlNode parent = null)
        {
            string s     = "";
            string error = "";

            try
            {
                s = File.ReadAllText(file, Encoding.Default);
            }
            catch (Exception e)
            {
                error = e.Message;
            }
            if (error != "")
            {
                return(VXmlException.GetMessage(VXmlException.E0025_XML_FILE_ERROR_CODE) + ": " + error);
            }

            //return LoadXml(s, Path.GetFileName(file), parent);
            return(load_xml_data(s, Path.GetFileNameWithoutExtension(file), file, parent));
        }
Beispiel #2
0
        /// <summary>
        /// Load child node from xml string
        /// </summary>
        /// <param name="xmlstring">XML string to load</param>
        /// <param name="name">Node name to create</param>
        /// <param name="fileref">Reference to file</param>
        /// <param name="parent">Parent VXML node</param>
        /// <returns></returns>
        private string load_xml_data(string xmlstring, string name, string xml_fileref, VXmlNode parent)
        {
            if (parent == null)
            {
                if (this.DocumentElement != null)
                {
                    this.DocumentElement.Remove();
                }
            }

            VXmlNode n = (parent == null) ? this : parent;

            if (!DEFX.BR_XML_IS_VALID_TYPE(n.NodeTypeCode))
            {
                return(VXmlException.GetMessage(VXmlException.E0004_INVALID_NODE_TYPE_CODE) + ": " + n.NodeType);
            }

            VXMLTemplate t = null;

            if (template_cache == null)
            {
                template_cache = new List <VXMLTemplate>(32);
            }
            for (int i = 0; i < template_cache.Count; i++)
            {
                if (template_cache[i].template_name == name)
                {
                    t = template_cache[i];
                }
            }
            if (t == null)                                          // Not found in cache
            {
                t = VXmlParser.Parse(xmlstring, name);
                if (t.Count == 1)
                {
                    if (t[0].def == VXmlParser.DEF_ERROR)
                    {
                        return(VXmlException.GetMessage(VXmlException.E0020_XML_PARSE_ERROR_CODE) + ": " + t[0].value);                          // Return error
                    }
                }
            }

            if (t.Count == 0)
            {
                return("");
            }

            int             j     = 0;
            List <VXmlNode> stack = new List <VXmlNode>();

            stack.Add(n);
            while (j < t.Count)
            {
                int stack_n = stack.Count - 1;

                if (t[j].def == VXmlParser.DEF_INSTRUCTION)
                {
                    // Just ignore
                }
                else if (t[j].def == VXmlParser.DEF_COMMENT)
                {
                    stack[stack_n].CreateComment(t[j].value);
                }
                else if (t[j].def == VXmlParser.DEF_ATTRIBUTE)
                {
                    stack[stack_n].SetAttribute(t[j].name, t[j].value);
                }
                else if (t[j].def == VXmlParser.DEF_START)
                {
                    if (t[j].type == DEFX.NODE_TYPE_TEXT)
                    {
                        stack[stack_n].CreateTextNode(t[j].value);
                    }
                    else
                    {
                        VXmlNode new_node = null;
                        switch (t[j].type)
                        {
                        case DEFX.NODE_TYPE_CONTENT:
                            new_node = stack[stack_n].CreateContent("");
                            break;

                        case DEFX.NODE_TYPE_ELEMENT:
                            new_node = stack[stack_n].CreateElement(t[j].name, t[j].value);
                            break;

                        default:
                            break;
                        }

                        stack.Add(new_node);
                    }
                }
                else if (t[j].def == VXmlParser.DEF_END)
                {
                    if (stack[stack_n].NodeTypeCode == DEFX.NODE_TYPE_CONTENT)
                    {
                        VXmlContent c = ((VXmlContent)stack[stack_n]);

                        if (c.fileref != "")
                        {
                            string xp = Path.GetDirectoryName(xml_fileref);
                            string cp = Path.GetDirectoryName(c.fileref);
                            if (cp == "")
                            {
                                cp = xp;                // If path is not specified in the 'fileref' - use xml file path
                            }
                            string nm = Path.GetFileName(c.fileref);
                            if (cp != "")
                            {
                                nm = cp + @"\" + nm;
                            }

                            if (File.Exists(nm))
                            {
                                c.Upload(nm, true);
                                c.fileref = nm;
                            }
                            else
                            {
                                return("Content file '" + nm + " is not found");
                            }
                        }
                    }
                    if (t[j].type != DEFX.NODE_TYPE_TEXT)
                    {
                        stack.RemoveAt(stack_n);
                    }
                }
                else
                {
                    throw new VXmlException(VXmlException.E0020_XML_PARSE_ERROR_CODE, "- invalid parsed sequence: " + t[j].def);
                }

                j++;
            }

            return("");
        }