Пример #1
0
        public override void ToDefine(ref StringBuilder buff, int indent)
        {
            if (m_xmlDef.ListData.Count > 0)
            {
                var field  = m_xmlDef.ListData[0];
                var parser = XmlBaseParser.Parse(field, m_nodeName);
                parser.ToDefine(ref buff, indent);
            }

            buff.Append('\t', indent);
            buff.Append("private static ").Append(m_xmlDef.type).Append(" Parse").Append(m_xmlDef.SrcName)
            .Append("(XmlNode ").Append(m_nodeName).AppendLine(")");
            buff.Append('\t', indent).AppendLine("{");
            ++indent;
            {
                buff.Append('\t', indent);
                buff.Append(m_xmlDef.type).Append(" list = new ").Append(m_xmlDef.type).Append("();").AppendLine();

                buff.AppendLine();
                ToDefineChildNodes(ref buff, indent, "list");

                buff.AppendLine();
                buff.Append('\t', indent);
                buff.AppendLine("return list;");
            }
            --indent;
            buff.Append('\t', indent).AppendLine("}");
            buff.AppendLine();
        }
Пример #2
0
        private void ToDefineChildNodes(ref StringBuilder buff, int indent)
        {
            buff.Append('\t', indent);
            buff.Append("foreach (XmlNode node in ").Append(m_nodeName).AppendLine(".ChildNodes)");
            buff.Append('\t', indent).AppendLine("{");
            ++indent;
            {
                buff.Append('\t', indent);
                buff.Append("if (node.NodeType != XmlNodeType.Element)").AppendLine();
                buff.Append('\t', indent + 1).Append("continue;").AppendLine();

                buff.Append('\t', indent);
                buff.Append("string nodeName = \"m_\" + node.Name;").AppendLine();

                bool isFirst = true;
                for (int i = 0; i < m_xmlDef.Fields.Count; ++i)
                {
                    var field = m_xmlDef.Fields[i];
                    if (!field.IsAttr)
                    {
                        buff.Append('\t', indent);
                        if (!isFirst)
                        {
                            buff.Append("else ");
                        }
                        buff.Append("if(nodeName == \"").Append(field.name).AppendLine("\")");
                        buff.Append('\t', indent).AppendLine("{");
                        ++indent;
                        {
                            buff.Append('\t', indent);
                            var parser = XmlBaseParser.Parse(field, "node");
                            buff.Append("data.").Append(field.name).Append(" = ");
                            parser.ToMember(ref buff, 0);
                        }
                        --indent;
                        buff.Append('\t', indent).AppendLine("}");
                        isFirst = false;
                    }
                }
            }
            --indent;
            buff.Append('\t', indent).AppendLine("}");
        }
Пример #3
0
        public string ToDefine()
        {
            int           indent = 0;
            StringBuilder buff   = new StringBuilder();

            buff.AppendLine("using System;");
            buff.AppendLine("using System.Collections.Generic;");
            buff.AppendLine("using System.Xml;");
            buff.AppendLine("using XmlBuilder.Parser;");
            buff.AppendLine();
            buff.Append("namespace XmlBuilder.NS_").Append(m_xmlDef.SrcName).AppendLine();
            buff.AppendLine("{");
            ++indent;
            buff.Append('\t', indent);
            buff.Append("public class ").Append(m_xmlDef.SrcName).Append("Parser").AppendLine();
            buff.Append('\t', indent).AppendLine("{");
            ++indent;
            ToDefine(ref buff, indent);
            {
                buff.Append('\t', indent);
                buff.Append("public static ").Append(m_xmlDef.type).Append(" Parse(XmlNode rootNode)").AppendLine();
                buff.Append('\t', indent).AppendLine("{");
                ++indent;
                {
                    buff.Append('\t', indent);
                    buff.Append("var data = ");
                    var parser = XmlBaseParser.Parse(m_xmlDef, "rootNode");
                    parser.ToMember(ref buff, 0);

                    buff.Append('\t', indent);
                    buff.AppendLine("return data;");
                }
                --indent;
                buff.Append('\t', indent).AppendLine("}");
            }
            --indent;
            buff.Append('\t', indent).AppendLine("}");
            --indent;
            buff.AppendLine("}");
            return(buff.ToString());
        }
Пример #4
0
        private void ToDefineAttributes(ref StringBuilder buff, int indent)
        {
            buff.Append('\t', indent);
            buff.Append("foreach (XmlAttribute attr in ").Append(m_nodeName).AppendLine(".Attributes)");
            buff.Append('\t', indent).AppendLine("{");
            ++indent;
            {
                buff.Append('\t', indent);
                buff.Append("string attrName = \"m_\" + attr.Name;").AppendLine();

                bool isFirst = true;
                for (int i = 0; i < m_xmlDef.Fields.Count; ++i)
                {
                    var field = m_xmlDef.Fields[i];
                    if (field.IsAttr)
                    {
                        buff.Append('\t', indent);
                        if (!isFirst)
                        {
                            buff.Append("else ");
                        }
                        buff.Append("if(attrName == \"").Append(field.name).AppendLine("\")");
                        buff.Append('\t', indent).AppendLine("{");
                        ++indent;
                        {
                            buff.Append('\t', indent);
                            var parser = XmlBaseParser.Parse(field, "attr");
                            buff.Append("data.").Append(field.name).Append(" = ");
                            parser.ToMember(ref buff, 0);
                        }
                        --indent;
                        buff.Append('\t', indent).AppendLine("}");
                        isFirst = false;
                    }
                }
            }
            --indent;
            buff.Append('\t', indent).AppendLine("}");
        }
Пример #5
0
        private void ToDefineChildNodes(ref StringBuilder buff, int indent, string listName)
        {
            XmlBase       defItem = m_xmlDef.ListData[0];
            XmlBaseParser parser  = XmlBaseParser.Parse(defItem, "node");

            buff.Append('\t', indent);
            buff.Append("foreach (XmlNode node in ").Append(m_nodeName).AppendLine(".ChildNodes)");
            buff.Append('\t', indent).AppendLine("{");
            {
                buff.Append('\t', indent + 1);
                buff.Append("if (node.NodeType != XmlNodeType.Element)").AppendLine();
                buff.Append('\t', indent + 2).Append("continue;").AppendLine();

                buff.Append('\t', indent + 1);
                buff.Append("var dataItem = ");
                parser.ToMember(ref buff, 0);

                buff.Append('\t', indent + 1);
                buff.Append(listName).AppendLine(".Add(dataItem);");
            }
            buff.Append('\t', indent).AppendLine("}");
        }
Пример #6
0
        public override void ToDefine(ref StringBuilder buff, int indent)
        {
            for (int i = 0; i < m_xmlDef.Fields.Count; ++i)
            {
                var field  = m_xmlDef.Fields[i];
                var parser = XmlBaseParser.Parse(field, m_nodeName);
                parser.ToDefine(ref buff, indent);
            }

            buff.Append('\t', indent);
            buff.Append("private static ").Append(m_xmlDef.type).Append(" Parse").Append(m_xmlDef.type)
            .Append("(XmlNode ").Append(m_nodeName).AppendLine(")");
            buff.Append('\t', indent).AppendLine("{");
            ++indent;
            {
                buff.Append('\t', indent);
                buff.Append(m_xmlDef.type).Append(" data = new ").Append(m_xmlDef.type).Append("();").AppendLine();

                if (m_xmlDef.HasAttrField())
                {
                    buff.AppendLine();
                    ToDefineAttributes(ref buff, indent);
                }

                if (m_xmlDef.HasNodeField())
                {
                    buff.AppendLine();
                    ToDefineChildNodes(ref buff, indent);
                }

                buff.AppendLine();
                buff.Append('\t', indent);
                buff.AppendLine("return data;");
            }
            --indent;
            buff.Append('\t', indent).AppendLine("}");
            buff.AppendLine();
        }