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

📄 schemaparser.cs

📁 an approach for capturing similarity between words that was concerned with the syntactic similarity
💻 CS
📖 第 1 页 / 共 2 页
字号:
//Author contact: Thanh.Dao@gmx.net

using System;
using System.Collections;
using System.Reflection;
using System.Threading;
using System.Web.Services.Description;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace WSDLParser
{
	#region Denote Prefix for long schem name
	//Denote prefix for long name data type
	//Simply later by I2
	using QName = XmlQualifiedName; 
	using Form = XmlSchemaForm; 
	using Use = XmlSchemaUse; 
	using SOM=XmlSchema;
	using SOMList = XmlSchemaObjectCollection; 
	using SOMObject = XmlSchemaObject; 
	using Element = XmlSchemaElement; 
	using Attr = XmlSchemaAttribute; 
	using AttrGroup = XmlSchemaAttributeGroup; 
	using AttrGroupRef = XmlSchemaAttributeGroupRef; 
	using SimpleType = XmlSchemaSimpleType; 
	using ComplexType = XmlSchemaComplexType; 
	using SimpleModel = XmlSchemaSimpleContent; 
	using SimpleExt = XmlSchemaSimpleContentExtension; 
	using SimpleRst = XmlSchemaSimpleContentRestriction; 
	using ComplexModel = XmlSchemaComplexContent; 
	using ComplexExt = XmlSchemaComplexContentExtension; 
	using ComplexRst = XmlSchemaComplexContentRestriction; 
	using SimpleTypeRst = XmlSchemaSimpleTypeRestriction; 
	using SimpleTypeModel = XmlSchemaSimpleTypeContent;
	using SimpleList = XmlSchemaSimpleTypeList; 
	using SimpleUnion = XmlSchemaSimpleTypeUnion; 
	using SchemaFacet = XmlSchemaFacet; 
	using EnumerationFacet = XmlSchemaEnumerationFacet; 
	using MinExcludesiveFacet=XmlSchemaMinExclusiveFacet ;
	using LengthFacet = XmlSchemaLengthFacet; 
	using MinLengthFacet = XmlSchemaMinLengthFacet; 
	using PatternFacet = XmlSchemaPatternFacet;
	using Particle = XmlSchemaParticle; 
	using Sequence = XmlSchemaSequence; 
	using Choice = XmlSchemaChoice;
	using Annotation=XmlSchemaAnnotation;
	using Documentation=XmlSchemaDocumentation;
	using AnyAttribute=XmlSchemaAnyAttribute;
	using AnyElement=XmlSchemaAny;
	using GroupRef=XmlSchemaGroupRef;
	using Group=XmlSchemaGroup;
	using All=XmlSchemaAll ;
	using Include=XmlSchemaInclude;
	#endregion

	/// <summary>
	/// Summary description for SchemaParser.
	/// </summary>	
	
	public class SchemaParser
	{					
		public class NodeData
		{
			public XmlQualifiedName Name;
			public string Type;
			public string Namespace;
			public bool Qualified;
			public XmlSchemaObject baseObj;

			public NodeData (XmlSchemaObject obj, XmlQualifiedName name, string type) 
			{ baseObj=obj; Name=name; Type=type; Namespace=name.Namespace; 			  
			}

		}			

		protected XmlSchemas _schemas;
		public static int TimeOver=2000;

		private static readonly XmlQualifiedName arrayType=new XmlQualifiedName ("Array","http://schemas.xmlsoap.org/soap/encoding/");
		private static readonly XmlQualifiedName arrayTypeRefName=new XmlQualifiedName ("arrayType","http://schemas.xmlsoap.org/soap/encoding/");

		XmlSchemaElement anyElement;
		public SoapBindingUse BindingUse=SoapBindingUse.Literal;		
		ArrayList _queue;
		

		class Encoded
		{
			public string Namespace;
			public XmlSchemaElement Element;
			public Encoded (string ns, XmlSchemaElement elem) 
			{ Namespace=ns; Element=elem; }
		}

		void MyInit()
		{			
			anyElement=new XmlSchemaElement ();
			anyElement.Name="any";
			anyElement.SchemaTypeName=new XmlQualifiedName ("anyType",XmlSchema.Namespace);
			_queue=new ArrayList ();
			 
		}
		
		public SchemaParser (XmlSchemas schemas)
		{
			MyInit ();
			this._schemas=schemas;			
			PrefixProcess();
		}
		
		public TreeNode Translate (XmlQualifiedName qname)
		{
			processHasExited=false;
			_node=new TreeNode() ;
			_qname=qname;
			StartParsing();

			ThreadStart threadStart=new ThreadStart(StartParsing);
			Thread thread=new Thread(threadStart);				
			
			thread.Start();		
							
			thread.Join(TimeOver) ;

			return _node;
		}

		XmlSerializerNamespaces _namespaces=new XmlSerializerNamespaces() ;

		void PrefixProcess()
		{
			_namespaces=new XmlSerializerNamespaces();
			for(int i=0; i < _schemas.Count; i++)
			{						
				XmlSchema schema=_schemas[i];
				
				if (schema.TargetNamespace != string.Empty && NamespaceHandler.LoopupPrefix(_namespaces, schema.TargetNamespace) == string.Empty)
				{
					_namespaces.Add("m" + i, schema.TargetNamespace) ;
				}

			}

			for(int i=0; i < _schemas.Count; i++)
			{
				XmlSchema schema=_schemas[i];
				XmlQualifiedName[] XmlQualifiedNameList=schema.Namespaces.ToArray();					
									
				XmlSerializerNamespaces xmlns=new XmlSerializerNamespaces() ;	

				int j=0;
				foreach (XmlQualifiedName qname in XmlQualifiedNameList )
				{
					//if (XmlQualifiedName.Name == string.Empty )
					{
						++j;
						if (qname.Namespace == schema.TargetNamespace)
						{
							string prefix=NamespaceHandler.LoopupPrefix(_namespaces, schema.TargetNamespace);

							if (prefix != string.Empty)
							{
								xmlns.Add(prefix, qname.Namespace) ;
							}
							else
							{
								xmlns.Add("tns", qname.Namespace) ;
							}
						}
						else
						{
							string prefix=NamespaceHandler.LoopupPrefix(_namespaces, schema.TargetNamespace);

							if (prefix != string.Empty)
							{
								xmlns.Add(prefix, qname.Namespace) ;
							}
							else
							{
								xmlns.Add("ns"+j, qname.Namespace) ;
							}
						}
					}
					
					//	xmlns.Add(XmlQualifiedName.Name, XmlQualifiedName.Namespace) ; 

				}
				schema.Namespaces = xmlns ;
			}
		}

		XmlQualifiedName _qname;
		TreeNode _node;
		bool processHasExited=false;

		void StartParsing()
		{
			try
			{					
				processHasExited=false;
				object lookup=_schemas.Find(_qname, typeof(XmlSchemaElement)) ;

				if (lookup != null)
					Create_RootElement (_node, _qname);	
				else
					Create_CustomType (_node, _qname);

				processHasExited=true;
			}
			catch(Exception e)
			{
				//MessageBox.Show(e.Message.ToString() ) ;
			}			
		}
				
		
		void Create_RootElement (TreeNode inner, XmlQualifiedName qname)
		{
			XmlSchemaElement elem=(XmlSchemaElement) _schemas.Find (qname, typeof(XmlSchemaElement));
			if (elem == null) throw new Exception ("Element not found: " + qname);
			Add_Element (inner, qname.Namespace, elem);
		}
		
		private bool IsInfiniteRecursiveLoop(TreeNode parent, XmlQualifiedName qname)
		{
			if (qname.IsEmpty) return false;

			TreeNode ancestor=parent;
			int count=0;
			while (ancestor != null)
			{				
				if (ancestor.Tag != null && ancestor.Tag is NodeData) 
				{
					XmlQualifiedName aqname=((NodeData) ancestor.Tag).Name ;
					if (aqname.Equals(qname)  )					
						++count;					
				}
				ancestor=ancestor.Parent;
			}

			if (count > 1)
			{
				int aaa=0;
			}

			return count > 1;
		}

		XmlSchema FindSchema(string targetNamespace)
		{
			for(int i=0; i < _schemas.Count; i++)
				if (_schemas[i].TargetNamespace == targetNamespace)
				{						
					return _schemas[i];					
				}
			return null;
		}


		//string Get
		void Add_Element (TreeNode complexNode, string ns, XmlSchemaElement elem)
		{
			//add to complexNode
			XmlQualifiedName nodeName=XmlQualifiedName.Empty;
			
			if (!elem.RefName.IsEmpty) 
			{
				XmlSchemaElement refElem=GetRefElement (elem);
				if (refElem == null) throw new Exception ("Global element not found: " + elem.RefName);
				nodeName=elem.RefName;
				elem=refElem;

			}
			else
			{				
				nodeName=elem.QualifiedName;
			}
			
			if (IsInfiniteRecursiveLoop (complexNode, nodeName)) return;

			TreeNode elemNode=new TreeNode(nodeName.Name) ;
			if (nodeName.Namespace != string.Empty)
			{
				elemNode.Text=NamespaceHandler.LoopupPrefix(_namespaces, nodeName.Namespace ) + ":" + nodeName.Name ;
			}			
			
			elemNode.Tag=nodeName;
			complexNode.Nodes.Add(elemNode) ;

			NodeData data=new NodeData(elem, nodeName, "") ;				
			elemNode.Tag=data;

			if (!elem.SchemaTypeName.IsEmpty)
			{
				XmlSchemaComplexType ct=GetComplexTypeByName (elem.SchemaTypeName);
				

				if (ct != null)
				{
					data.Type=ct.QualifiedName.Name;

					Parse_ComplexType (elemNode, ct, nodeName);						
				}
				else
					if (!elem.SchemaTypeName.IsEmpty)
				{																		
					data.Type=GetBuiltInTypeName (elem.SchemaTypeName);
					string test="";
					if (BindingUse == SoapBindingUse.Encoded) 
						 test="type"  + GetQualifiedNameString (complexNode, elem.SchemaTypeName)
							+( (GetBuiltInTypeName (elem.SchemaTypeName)));						
				}
			}
			else if (elem.SchemaType == null)
			{
				data.Type="any";
			}
			else
			{
				if (elem.SchemaType is XmlSchemaComplexType) 
					Parse_ComplexType (elemNode, (XmlSchemaComplexType) elem.SchemaType, nodeName);
				else
					if (elem.SchemaType is XmlSchemaSimpleType)
					{
						XmlSchemaSimpleType st=elem.SchemaType as XmlSchemaSimpleType;
						data.Type=GetBuiltInTypeName (st.QualifiedName );						
					}
			}


			
		}
		
		void Create_CustomType (TreeNode inner, XmlQualifiedName qname)
		{
			XmlSchemaComplexType ctype=GetComplexTypeByName (qname);
			TreeNode node=new TreeNode() ;			
			inner.Nodes.Add(node) ;
			node.Text=qname.Name;

			if (ctype != null) 
			{
				Parse_ComplexType (node, ctype, qname);
				return;
			}
				
			XmlSchemaSimpleType stype=(XmlSchemaSimpleType) _schemas.Find (qname, typeof(XmlSchemaSimpleType));
			if (stype != null) 
			{
				Parse_SimpleType (node, stype);
				return;
			}
			
			inner.Tag=((GetBuiltInTypeName (qname)));

			throw new Exception ("Type not found: " + qname);
		}
		
		void Parse_ComplexType (TreeNode inner, XmlSchemaComplexType stype, XmlQualifiedName rootName)
		{			
			Parse_ComplexType (inner, stype, rootName, -1);
		}
		
		void Parse_ComplexType (TreeNode innerNode, XmlSchemaComplexType stype, XmlQualifiedName rootName, int id)
		{
			string ns=rootName.Namespace;
			
			if (rootName.Name.IndexOf ("[]") != -1) rootName=arrayType;
			
			if (BindingUse == SoapBindingUse.Encoded) 
			{
				innerNode.Text=NamespaceHandler.LoopupPrefix(_namespaces, rootName.Namespace ) + ":" + rootName.Name ;
				//innerNode.Text = rootName.Namespace + ":" + rootName.Name;
				ns=string.Empty;
			}
			else
			{	
				if (rootName.Namespace != string.Empty)
					innerNode.Text=NamespaceHandler.LoopupPrefix(_namespaces, rootName.Namespace ) + ":" + rootName.Name ;
				else
					innerNode.Text=rootName.Name;
			}
		
			if (innerNode.Tag == null)
			{
				NodeData data=new NodeData(stype, rootName, "") ;
				innerNode.Tag=data;
			}
			
			if (id != -1)
			{

				TreeNode node=new TreeNode("id:" + id) ;
				innerNode.Nodes.Add(node) ;

				if (rootName != arrayType)
				{
					TreeNode node2=new TreeNode("Type" + GetQualifiedNameString (innerNode, rootName)) ;
					node.Nodes.Add(node2) ;
				}
			}
			
			Add_ComplexAttributes (innerNode, stype);
			Add_ComplexElements (innerNode, ns, stype);			
		}
		
		void Add_ComplexAttributes (TreeNode inner, XmlSchemaComplexType stype)
		{
			Add_Attributes (inner, stype.Attributes, stype.AnyAttribute);
		}
		
		void Add_ComplexElements (TreeNode complexNode, string ns, XmlSchemaComplexType stype)
		{
			if (stype.ContentTypeParticle != null)
			{
				Parse_ParticleComplexContent (complexNode, ns, stype.ContentTypeParticle);
			}
			else
				if (stype.Particle != null)
			{
				Parse_ParticleComplexContent (complexNode, ns, stype.Particle);
			}
			else
			{
				if (stype.ContentModel is XmlSchemaSimpleContent)
					Parse_SimpleContent (complexNode, (XmlSchemaSimpleContent)stype.ContentModel);

⌨️ 快捷键说明

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