baseelement.cs

来自「xpdl的c#解析器 完全依照xpdl标准」· CS 代码 · 共 148 行

CS
148
字号
using System;

namespace com.use.wfp.xpdl.elements
{
	using System.Reflection;
	using System.Collections;
	using Nucleus.Common.Util;
	/// <summary>
	/// BaseElement 的摘要说明。
	/// </summary>
	[Serializable]
	public class BaseElement
	{
		private Node node = NodeFactory.CreateNode();
		protected void SetNode(Node node){
			this.node = node;
		}

		protected BaseElement GetChild(BaseElement child, out BaseElement refChild, string xPath){
			if(child == null){
				refChild = (BaseElement)Type.GetType("com.use.wfp.xpdl.elements." + xPath).GetConstructor(new Type[]{}).Invoke(new object[]{});
				refChild.SetNode(node.SelectSingleNode(xPath));
			}else{
				refChild = child;
			}
			return refChild;
		}
		protected BaseElement ChoiceChild(BaseElement child, out BaseElement refChild, string[] xPath)
		{
			if(child == null)
			{
				foreach(string name in xPath){
					if( !node.SelectSingleNode(name).IsEmpty() ){
						refChild = (BaseElement)Type.GetType("com.use.wfp.xpdl.elements." + name).GetConstructor(new Type[]{}).Invoke(new object[]{});
						refChild.SetNode(node.SelectSingleNode(name));
						return refChild;
					}
				}
				throw new ObjectNotFound("Object Not Found In " + xPath.ToString());
			}
			else
			{
				refChild = child;
			}
			
			return refChild;
		}
		protected IList GetChildren(IList children, out IList refChildren, Type type, string xPath)
		{
			if(children == null)
			{
				IList nodes = node.SelectNodes(xPath);
				ArrayList retList = new ArrayList();
				foreach (Node xmlNode in nodes)
				{
					BaseElement baseElement = (BaseElement)type.GetConstructor(new Type[]{}).Invoke(new object[]{});
					baseElement.SetNode(xmlNode);
					retList.Add(baseElement);
				}
				refChildren = retList;
			}
			else
			{
				refChildren = children;
			}
			return refChildren;
		}
		protected IList GetChildren(IList children, out IList refChildren)
		{
			if(children == null)
			{
				refChildren = node.GetChildren();
			}
			else
			{
				refChildren = children;
			}
			return refChildren;
		}
		protected IList GetChildrenTexts()
		{
			IList nodes = null;
			GetChildren(nodes, out nodes);
			IList texts = new ArrayList();
			foreach(Node node in nodes)
			{
				texts.Add(node.GetText());
			}
			return texts;
		}
		protected String GetChildText(String text, out String refText, string childName)
		{
			if(text == null)
			{
				refText = node.SelectSingleNode(childName + "/text()[1]").GetText();
			}
			else
			{
				refText = text;
			}
			return refText;
		}
		protected String GetAttribute(String attr, out String refAttr, string xPath)
		{
			return GetAttribute(attr, out refAttr, xPath, "");
		}
		protected String GetAttribute(String attr, out String refAttr, string xPath, String defaultValue)
		{
			if(attr == null)
			{
				refAttr = (String)node.GetAttributes()[xPath];
				if(refAttr == null){
					refAttr = defaultValue;
				}
			}
			else
			{
				refAttr = attr;
			}
			return refAttr;
		}
		protected String GetText(){
			return node.GetText();
		}
	}
	public class BaseElementWithIdName:BaseElement{
		private String Name;
		public String GetName()
		{
			return GetAttribute(Name, out Name, "Name");
		}
		public void SetName(string strValue)
		{
			this.Name = strValue;
		}

		private String Id;
		public String GetId()
		{
			return GetAttribute(Id, out Id, "Id");
		}	
		public void SetId(string strValue)
		{
			this.Id = strValue;
		}
	}
}

⌨️ 快捷键说明

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