⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 schemaparser.cs

📁 In the previous article, we presented an approach for capturing similarity between words that was co
💻 CS
📖 第 1 页 / 共 3 页
字号:
            TreeNode node = new TreeNode((GetBuiltInTypeName(qname)));
            inner.Nodes.Add(node);
        }

        string GetBuiltInTypeName(XmlQualifiedName qname)
        {
            if (qname.IsEmpty)
            {
                return "";
            }

            if (qname.Namespace == XmlSchema.Namespace)
                return qname.Name;

            XmlSchemaComplexType ct = GetComplexTypeByName(qname);
            if (ct != null)
            {
                XmlSchemaSimpleContent sc = ct.ContentModel as XmlSchemaSimpleContent;
                if (sc == null) throw new Exception("Invalid schema");
                return GetBuiltInTypeName(GetContentBaseType(sc.Content));
            }

            XmlSchemaSimpleType st = (XmlSchemaSimpleType)_schemas.Find(qname, typeof(XmlSchemaSimpleType));
            if (st != null)
                return GetBuiltInType(st);

            throw new Exception("Definition of type " + qname + " not found");
        }

        string GetBuiltInType(XmlSchemaSimpleType st)
        {
            if (st == null) return string.Empty;

            if (st.Content is XmlSchemaSimpleTypeRestriction)
            {
                return GetBuiltInTypeName(GetContentBaseType(st.Content));
            }
            else if (st.Content is XmlSchemaSimpleTypeList)
            {
                string s = GetBuiltInTypeName(GetContentBaseType(st.Content));
                return s + " " + s + " ...";// fun but i dont care this kind of types
            }
            else if (st.Content is XmlSchemaSimpleTypeUnion)
            {

                XmlSchemaSimpleTypeUnion uni = (XmlSchemaSimpleTypeUnion)st.Content;
                string utype = null;


                if (uni.BaseTypes.Count != 0 && uni.MemberTypes.Length != 0)
                    return "string";

                foreach (XmlQualifiedName mt in uni.MemberTypes)
                {
                    string qn = GetBuiltInTypeName(mt);
                    if (utype != null && qn != utype) return "string";
                    else utype = qn;
                }
                return utype;
            }
            else
                return "string";
        }


        XmlQualifiedName GetContentBaseType(XmlSchemaObject obj)
        {
            if (obj is XmlSchemaSimpleContentExtension)
                return ((XmlSchemaSimpleContentExtension)obj).BaseTypeName;
            else if (obj is XmlSchemaSimpleContentRestriction)
                return ((XmlSchemaSimpleContentRestriction)obj).BaseTypeName;
            else if (obj is XmlSchemaSimpleTypeRestriction)
                return ((XmlSchemaSimpleTypeRestriction)obj).BaseTypeName;
            else if (obj is XmlSchemaSimpleTypeList)
                return ((XmlSchemaSimpleTypeList)obj).ItemTypeName;
            else
                return null;
        }

        const string WsdlNamespace = "http://schemas.xmlsoap.org/wsdl/";
        const string SoapEncodingNamespace = "http://schemas.xmlsoap.org/soap/encoding/";

        void Parse_ComplexContent(TreeNode inner, string ns, XmlSchemaComplexContent content)
        {
            XmlQualifiedName qname;

            XmlSchemaComplexContentExtension ext = content.Content as XmlSchemaComplexContentExtension;
            if (ext != null) qname = ext.BaseTypeName;
            else
            {
                XmlSchemaComplexContentRestriction rest = (XmlSchemaComplexContentRestriction)content.Content;
                qname = rest.BaseTypeName;

                if (qname == arrayType)
                {
                    Parse_ArrayType(rest, out qname);
                    XmlSchemaElement elem = new XmlSchemaElement();
                    elem.Name = "Item";
                    elem.SchemaTypeName = qname;

                    TreeNode node = new TreeNode("arrayType" + SoapEncodingNamespace + qname.Name + "[2]");
                    inner.Nodes.Add(node);
                    Add_Item(inner, ns, elem, true);
                    return;
                }
                else
                    if (rest.Particle != null)
                    {
                        //?? Parse rest particle content	?					
                        Add_Attributes(inner, rest.Attributes, rest.AnyAttribute);
                        Parse_ParticleComplexContent(inner, ns, rest.Particle);
                    }

                return;
            }


            XmlSchemaComplexType cbase = GetComplexTypeByName(qname);

            if (cbase != null)
            {
                Add_ComplexAttributes(inner, cbase);
                //Add base content first
                Add_ComplexElements(inner, ns, cbase);
            }

            if (ext != null)
            {
                // Add the members of this map
                Add_Attributes(inner, ext.Attributes, ext.AnyAttribute);
                if (ext.Particle != null)
                    Parse_ParticleComplexContent(inner, ns, ext.Particle);
            }


        }

        void Parse_ArrayType(XmlSchemaComplexContentRestriction rest, out XmlQualifiedName qtype)
        {
            XmlSchemaAttribute arrayTypeAt = GetArrayAttribute(rest.Attributes);
            XmlAttribute[] uatts = arrayTypeAt.UnhandledAttributes;
            if (uatts == null || uatts.Length == 0) throw new Exception("arrayType attribute not specified in array declaration");

            XmlAttribute xat = null;
            foreach (XmlAttribute at in uatts)
                if (at.LocalName == "arrayType" && at.NamespaceURI == WsdlNamespace)
                { xat = at; break; }

            if (xat == null)
                throw new Exception("arrayType attribute not specified in array declaration");

            string arrayType = xat.Value;
            string type, ns;
            int i = arrayType.LastIndexOf(":");
            if (i == -1) ns = string.Empty;
            else ns = arrayType.Substring(0, i);

            int j = arrayType.IndexOf("[", i + 1);
            if (j == -1) throw new Exception("Cannot parse WSDL array type: " + arrayType);
            type = arrayType.Substring(i + 1);
            type = type.Substring(0, type.Length - 2);

            qtype = new XmlQualifiedName(type, ns);
        }

        XmlSchemaAttribute GetArrayAttribute(XmlSchemaObjectCollection atts)
        {
            foreach (object ob in atts)
            {
                XmlSchemaAttribute att = ob as XmlSchemaAttribute;
                if (att != null && att.RefName == arrayTypeRefName) return att;

                XmlSchemaAttributeGroupRef gref = ob as XmlSchemaAttributeGroupRef;
                if (gref != null)
                {
                    XmlSchemaAttributeGroup grp = (XmlSchemaAttributeGroup)_schemas.Find(gref.RefName, typeof(XmlSchemaAttributeGroup));
                    att = GetArrayAttribute(grp.Attributes);
                    if (att != null) return att;
                }
            }
            return null;

        }

        void Add_Facet(TreeNode simpleNode, XmlSchemaFacet facet)
        {
            string objType = facet.GetType().ToString();
            string fname = objType.Substring(objType.LastIndexOf(".") + 1, (objType.Length - 1) - objType.LastIndexOf("."));
            TreeNode node = new TreeNode(fname + "=" + facet.Value);
            simpleNode.Nodes.Add(node);

        }

        void Parse_AnyObject(TreeNode inner, XmlSchemaObject obj)
        {
            XmlSchemaFacet facet = obj as XmlSchemaFacet;
            XmlSchemaAttribute attrib = obj as XmlSchemaAttribute;

            if (facet != null)
                Add_Facet(inner, facet);

            Type type = obj.GetType();
            PropertyInfo[] properties = type.GetProperties();

            foreach (PropertyInfo property in properties)
            {
                if (property.PropertyType.FullName.Equals(typeof(XmlSchemaObjectCollection).ToString()))
                {
                    XmlSchemaObjectCollection somList = property.GetValue(obj, null) as XmlSchemaObjectCollection;

                    if (somList != null)
                        foreach (XmlSchemaObject somObj in somList)
                        {
                            Parse_AnyObject(inner, somObj);
                        }
                }
            }

        }

        void Parse_SimpleType(TreeNode inner, XmlSchemaSimpleType stype)
        {
            if (inner.Text == string.Empty)
                inner.Text = (GetBuiltInType(stype));

            if (stype.Content != null)
                Parse_AnyObject(inner, stype.Content);


            if (inner.Tag == null)
            {
                string desc = GetAnnotation(stype.Annotation);
                NodeData data = new NodeData(stype, stype.QualifiedName, string.Empty, desc, string.Empty);
                inner.Tag = data;
            }

        }

        XmlSchemaParticle GetRefGroupParticle(XmlSchemaGroupRef refGroup)
        {
            XmlSchemaGroup grp = (XmlSchemaGroup)_schemas.Find(refGroup.RefName, typeof(XmlSchemaGroup));
            return grp.Particle;
        }

        XmlSchemaElement GetRefElement(XmlSchemaElement elem)
        {
            if (elem.RefName.Namespace == XmlSchema.Namespace)
            {
                if (_anyElement != null) return _anyElement;
                return _anyElement;
            }
            return (XmlSchemaElement)_schemas.Find(elem.RefName, typeof(XmlSchemaElement));
        }



        XmlSchemaAttribute GetRefAttribute(XmlQualifiedName refName)
        {
            if (refName.Namespace == XmlSchema.Namespace)
            {
                XmlSchemaAttribute at = new XmlSchemaAttribute();
                at.Name = refName.Name;
                at.SchemaTypeName = new XmlQualifiedName("string", XmlSchema.Namespace);
                return at;
            }
            return (XmlSchemaAttribute)_schemas.Find(refName, typeof(XmlSchemaAttribute));
        }

        void Add_RefType(TreeNode inner, string ns, XmlSchemaElement elem)
        {
            if (elem.SchemaTypeName.Namespace == XmlSchema.Namespace || _schemas.Find(elem.SchemaTypeName, typeof(XmlSchemaSimpleType)) != null)
                Add_Element(inner, ns, elem);
            else
            {
                TreeNode node = new TreeNode(elem.Name + ns);
                TreeNode node2 = new TreeNode("href" + "#id" + (_queue.Count + 1));
                node.Nodes.Add(node2);
                inner.Nodes.Add(node);
                _queue.Add(new Encoded(ns, elem));
            }
        }

        void Parse_QueuedType(TreeNode inner)
        {
            for (int n = 0; n < _queue.Count; n++)
            {
                Encoded ec = (Encoded)_queue[n];
                XmlSchemaComplexType st = GetComplexTypeByName(ec.Element.SchemaTypeName);
                Parse_ComplexType(inner, st, ec.Element.SchemaTypeName, n + 1);
            }
        }

        XmlSchemaComplexType GetComplexTypeByName(XmlQualifiedName qname)
        {
            if (qname.Name.IndexOf("[]") != -1)
            {
                XmlSchemaComplexType stype = new XmlSchemaComplexType();
                stype.ContentModel = new XmlSchemaComplexContent();

                XmlSchemaComplexContentRestriction res = new XmlSchemaComplexContentRestriction();
                stype.ContentModel.Content = res;
                res.BaseTypeName = arrayType;

                XmlSchemaAttribute att = new XmlSchemaAttribute();
                att.RefName = arrayTypeRefName;
                res.Attributes.Add(att);


                return stype;
            }

            return (XmlSchemaComplexType)_schemas.Find(qname, typeof(XmlSchemaComplexType));
        }

        string GetQualifiedNameString(TreeNode inner, XmlQualifiedName qname)
        {
            TreeNode node = new TreeNode("xmlns q1=" + qname.Namespace);
            return "q1:" + qname.Name;
        }

    }


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -