📄 schemaparser.cs
字号:
//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
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 string Description = string.Empty;
public XmlSchemaObject baseObj;
public bool MultiValue = false;
public bool Nullable = false;
public string Value = string.Empty;
public string ObjectType = string.Empty;
public NodeData(XmlSchemaObject obj, XmlQualifiedName name, string type, string desc, string defaultValue)
{
baseObj = obj;
Name = name;
Type = type;
Namespace = name.Namespace;
Description = desc;
ObjectType = obj.GetType().ToString();
}
}
XmlSchemas _schemas;
public static int TimeOver = 2000;
const int MAX_DEPTH = 6;
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;
Do_PrefixTagging();
}
public TreeNode Translate(XmlQualifiedName qname)
{
processHasExited = false;
_node = new TreeNode();
_node.ImageIndex = 17;
_node.SelectedImageIndex = 17;
_qname = qname;
_lookupType = null;
StartParsing();
//Run_Thread ()
return _node;
}
public TreeNode Translate(XmlQualifiedName qname, XmlSchemaObject obj)
{
processHasExited = false;
_node = new TreeNode();
_node.ImageIndex = 17;
_node.SelectedImageIndex = 17;
_qname = qname;
_lookupType = obj.GetType();
StartParsing();
//Run_Thread ()
return _node;
}
void Run_Thread()
{
ThreadStart threadStart = new ThreadStart(StartParsing);
Thread thread = new Thread(threadStart);
thread.Start();
thread.Join(TimeOver);
}
XmlSerializerNamespaces _namespaces = new XmlSerializerNamespaces();
void Do_PrefixTagging()
{
_namespaces = new XmlSerializerNamespaces();
for (int i = 0; i < _schemas.Count; i++)
{
XmlSchema schema = _schemas[i];
if (schema.TargetNamespace != string.Empty && SchemaHelper.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 = SchemaHelper.LoopupPrefix(_namespaces, schema.TargetNamespace);
if (prefix != string.Empty)
{
xmlns.Add(prefix, qname.Namespace);
}
else
{
xmlns.Add("tns", qname.Namespace);
}
}
else
{
string prefix = SchemaHelper.LoopupPrefix(_namespaces, schema.TargetNamespace);
if (prefix != string.Empty)
{
xmlns.Add(prefix, qname.Namespace);
}
else
{
xmlns.Add("m" + j, qname.Namespace);
}
}
}
}
schema.Namespaces = xmlns;
}
}
XmlQualifiedName _qname;
TreeNode _node;
Type _lookupType;
bool processHasExited = false;
void StartParsing()
{
try
{
processHasExited = false;
object lookup = null;
if (_lookupType == null)
lookup = _schemas.Find(_qname, typeof(XmlSchemaElement));
else
lookup = _schemas.Find(_qname, _lookupType);
if (lookup is XmlSchemaElement)
Create_RootElement(_node, _qname);
else
Create_CustomType(_node, _qname);
processHasExited = true;
}
catch (Exception e)
{
throw e;
}
}
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);
}
string GetAnnotation(Annotation annot)
{
if (annot == null) return "";
string annotation = string.Empty;
foreach (XmlSchemaObject obj in annot.Items)
{
XmlSchemaDocumentation doc = obj as XmlSchemaDocumentation;
XmlSchemaAppInfo app = obj as XmlSchemaAppInfo;
if (doc != null)
annotation += GetDocumentContent(doc.Markup);
if (app != null)
annotation += GetDocumentContent(app.Markup);
}
return annotation;
}
string GetDocumentContent(XmlNode[] nodes)
{
if (nodes == null) return string.Empty;
string strContent = "";
foreach (XmlNode node in nodes)
{
if (node.InnerText != null)
strContent += node.InnerText.Trim() + Environment.NewLine;
else
if (node.InnerXml != null)
strContent += node.InnerXml.Trim() + Environment.NewLine;
}
return strContent;
}
bool IsInfiniteRecursiveLoop(TreeNode parent, XmlQualifiedName qname)
{
if (qname.IsEmpty) return false;
TreeNode ancestor = parent;
int count = 0;
int level = 0;
while (ancestor != null)
{
if (ancestor.Tag != null && ancestor.Tag is NodeData)
{
XmlQualifiedName aqname = ((NodeData)ancestor.Tag).Name;
if (aqname.Equals(qname) && qname != XmlQualifiedName.Empty)
++count;
}
ancestor = ancestor.Parent;
++level;
}
return (count > 1 || level > MAX_DEPTH);
}
XmlSchema GetSchemaByName(string targetNamespace)
{
for (int i = 0; i < _schemas.Count; i++)
if (_schemas[i].TargetNamespace == targetNamespace)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -