Beispiel #1
0
        // 根据一个前缀字符串, 从起点元素开始查找, 看这个前缀字符串是在哪里定义的URI。
        // 也就是要找到xmlns:???=???这样的属性对象,返回在namespaceAttr参数中。
        // 本来从返回的namespaceAttr参数中可以找到命中URI信息,但是为了使用起来方便,
        // 本函数也直接在strURI参数中返回了命中的URI
        // parameters:
        //		startItem	起点element对象
        //		strPrefix	要查找的前缀字符串
        //		strURI		[out]返回的URI
        //		namespaceAttr	[out]返回的AttrItem节点对象
        // return:
        //		ture	找到(strURI和namespaceAttr中有返回值)
        //		false	没有找到
        public static bool LocateNamespaceByPrefix(ElementItem startItem,
                                                   string strPrefix,
                                                   out string strURI,
                                                   out AttrItem namespaceAttr)
        {
            strURI        = "";
            namespaceAttr = null;

            /*
             * Debug.Assert(strPrefix != "", "strPrefix参数不应当为空。前缀为空时,无需调用本函数就知道没有找到");
             * if (strPrefix == "")
             *      return false;
             */

            ElementItem currentItem = startItem;

            while (true)
            {
                if (currentItem == null)
                {
                    break;
                }

                foreach (AttrItem attr in currentItem.attrs)
                {
                    if (attr.IsNamespace == false)
                    {
                        continue;
                    }

                    string strLocalName = "";
                    int    nIndex       = attr.Name.IndexOf(":");
                    if (nIndex >= 0)
                    {
                        strLocalName = attr.Name.Substring(nIndex + 1);
                    }
                    else
                    {
                        Debug.Assert(attr.Name == "xmlns", "名字空间型的属性,如果name中无冒号,必然为xmlns。");
                        if (attr.Name == "xmlns")
                        {
                            strLocalName = "";
                        }
                    }

                    if (strLocalName == strPrefix)
                    {
                        strURI        = attr.GetValue();
                        namespaceAttr = attr;
                        return(true);
                    }
                }

                currentItem = (ElementItem)currentItem.parent;
            }

            return(false);
        }
Beispiel #2
0
        public override Item newItem(XmlNode node,
                                     XmlEditor document)
        {
            Item item = null;

            if (node.NodeType == XmlNodeType.Element)
            {
                item = new ElementItem(document);
            }
            else if (node.NodeType == XmlNodeType.Attribute)
            {
                item = new AttrItem(document);
            }
            else if (node.NodeType == XmlNodeType.Text)
            {
                item = new TextItem(document);
            }
            else if (node.NodeType == XmlNodeType.ProcessingInstruction)
            {
                item = new ProcessingInstructionItem(document);
            }
            else if (node.NodeType == XmlNodeType.XmlDeclaration)
            {
                item = new DeclarationItem(document);
            }
            else if (node.NodeType == XmlNodeType.Comment)
            {
                item = new CommentItem(document);
            }
            else if (node.NodeType == XmlNodeType.CDATA)
            {
                item = new CDATAItem(document);
            }
            else if (node.NodeType == XmlNodeType.DocumentType)
            {
                item = new DocumentTypeItem(document);
            }
            else if (node.NodeType == XmlNodeType.EntityReference)
            {
                item = new EntityReferenceItem(document);
            }

            //item.m_document = document;
            return(item);
        }
Beispiel #3
0
        public override bool MoveToNextNamespace(XPathNamespaceScope namespaceScope)
        {
            //StreamUtil.WriteText("I:\\debug.txt","进到 MoveToNextNamespace()\r\n");
            Debug.Assert(this.m_navigatorState.CurItem != null, "");


            if (this.m_navigatorState.CurItem == this.m_navigatorState.VirtualRoot)
            {
                return(false);
            }

            if (!(this.m_navigatorState.CurItem is AttrItem))
            {
                return(false);
            }

            AttrItem attr = (AttrItem)this.m_navigatorState.CurItem;

            if (attr.IsNamespace == false)
            {
                return(false);
            }

            ElementItem element = (ElementItem)attr.parent;

            ItemList namespaceList = element.NamespaceList;

            if (namespaceList.Count > 0)
            {
                int nIndex = namespaceList.IndexOf(this.m_navigatorState.CurItem);
                if (nIndex == -1)
                {
                    return(false);
                }
                if (nIndex + 1 >= namespaceList.Count)
                {
                    return(false);
                }

                this.m_navigatorState.CurItem = namespaceList[nIndex + 1];
                return(true);
            }

            return(false);
        }
Beispiel #4
0
		public override Item newItem(XmlNode node,
			XmlEditor document)
		{
			Item item = null;

			if (node.NodeType == XmlNodeType.Element) 
			{
				item = new ElementItem(document);
			}
			else if (node.NodeType == XmlNodeType.Attribute )
			{
				item = new AttrItem(document);
			}
			else if (node.NodeType == XmlNodeType.Text) 
			{
				item = new TextItem(document);
			}
			else if (node.NodeType == XmlNodeType.ProcessingInstruction )
			{
				item = new ProcessingInstructionItem(document);
			}
			else if (node.NodeType == XmlNodeType.XmlDeclaration )
			{
				item = new DeclarationItem(document);
			}
			else if (node.NodeType == XmlNodeType.Comment)
			{
				item = new CommentItem(document);
			}
			else if (node.NodeType == XmlNodeType.CDATA)
			{
				item = new CDATAItem(document);
			}
			else if (node.NodeType == XmlNodeType.DocumentType)
			{
				item = new DocumentTypeItem(document);
			}
			else if (node.NodeType == XmlNodeType.EntityReference)
			{
				item = new EntityReferenceItem(document);
			}

			//item.m_document = document;
			return item;
		}
Beispiel #5
0
		// 删属性
		internal void RemoveAttrInternal(AttrItem attr,
			bool bAddObjTimestamp)
		{
			Debug.Assert(attr != null,"RemoveAttr() attr不能为null,调用出错");

			// 当前节点 不属于儿子集合
			int nIndex = this.attrs.IndexOf(attr);
			if (nIndex == -1)
			{
				Debug.Assert(false,"RemoveChild() attr不属于属性集合,调入出错");
				return;
			}

			////////////////////////////////////////////////
			// BeforeItemDelete
			///////////////////////////////////////////////
			string strXPath = attr.GetXPath();  // 先得到Xpath,否则删除后就没有了
			BeforeItemDeleteEventArgs beforeArgs = 
				new BeforeItemDeleteEventArgs();
			beforeArgs.item = attr;
			this.m_document.fireBeforeItemDelete(this.m_document,beforeArgs);


			// 把一些有用的初值设好,例如NamespaceURi
			this.SetNamespaceURI((ElementAttrBase)attr);  
			attr.m_paraValue1 = attr.GetValue();


			// 进行Remove操作
			this.attrs.Remove(attr);


			////////////////////////////////////////////////
			// ItemDeleted
			///////////////////////////////////////////////
			ItemDeletedEventArgs args = 
				new ItemDeletedEventArgs();
			args.item = attr;
			args.XPath = strXPath;

			// 每次按off算,外面需要时设为on
			args.RiseAttrsEvents = false;
			args.RecursiveChildEvents = false;
			this.m_document.fireItemDeleted(this.m_document,args);

			if (bAddObjTimestamp == true)
				this.m_objAttrsTimestamp ++;
		}
Beispiel #6
0
		internal bool RemoveAttr(AttrItem attr)
		{
			if (attr == null)
			{
				Debug.Assert(false,"RemoveAttr() attr参数不能为null");
				return false;
			}
			if (this == this.m_document.VirtualRoot)
			{
				Debug.Assert(false,"this不能为虚根");
				return false;
			}
	
			// 当前活动节点是不是就是要删除的节点
			// 如果是,则从attr附近找到一个相领的节点。最后把活动节点 , curText , edit设正确
			bool bBeLong = false;
			Item hereAboutItem = null;
			if (this.m_document.m_selectedItem == attr)
			{
				hereAboutItem = ItemUtil.GetNearItem(attr,MoveMember.Auto);
				bBeLong = true;
			}

				
			this.RemoveAttrInternal(attr,true);


			if (this.attrs.Count == 0)
			{
				// 目的是为了把"收缩条"也去掉
				this.InitialVisual();
			}
			else
			{
				this.AttributesReInitial();
			}

			int nWidth , nHeight;
			this.Layout(this.Rect.X,
				this.Rect.Y,
				this.Rect.Width,
				0,   //设为0,主要是高度变化
				this.m_document.nTimeStampSeed++,
				out nWidth,
				out nHeight,
				LayoutMember.Layout | LayoutMember.Up );


			//设为当前值
			if (bBeLong == true)
			{
				this.m_document.SetCurText(hereAboutItem,null);
				this.m_document.SetActiveItem(hereAboutItem);
			}
			else
			{
				this.m_document.SetEditPos ();
			}


			this.m_document.AfterDocumentChanged(ScrollBarMember.Both);
			this.m_document.Invalidate();

			// 文档发生变化
			this.m_document.FireTextChanged();

			this.Flush();

			return true;
		}
Beispiel #7
0
		// 追加属性
		public int AppendAttr(AttrItem attr,
			out string strError)
		{
			Cursor oldCursor = this.m_document.Cursor;
			this.m_document.Cursor =  Cursors.WaitCursor;
			try
			{
				strError = "";
				this.AppendAttrInternal(attr,true,false);

				this.AfterAttrCreateOrChange(attr);
				return 0;
			}
			finally
			{
				this.m_document.Cursor = oldCursor;
			}
		}
Beispiel #8
0
		// 改变属性:上级增加了属性,或者修改的属性值调此函数,做残余的事情
		public void AfterAttrCreateOrChange(AttrItem attr)
		{
			Cursor oldCursor = this.m_document.Cursor;
			this.m_document.Cursor =  Cursors.WaitCursor;
			try
			{
				// 设父亲状态
				this.m_bWantAttrsInitial = 1;
				if (this.attrs.Count > 0
					&& this.AttrsExpand == ExpandStyle.None)
				{
					this.AttrsExpand = ExpandStyle.Expand;
				}
			
				// 重新初始化Attributes区域
				this.AttributesReInitial();



				int nWidth, nHeight;
				this.Layout(this.Rect.X,
					this.Rect.Y,
					this.Rect.Width,
					0,
					this.m_document.nTimeStampSeed++,
					out nWidth,
					out nHeight,
					LayoutMember.Layout | LayoutMember.Up );

				// 把新插入的属性变为当前活动的对象
				// this.m_document.SetActiveItem(attr);
				// this.m_document.SetCurText(attr);

				// 设卷滚条
				this.m_document.AfterDocumentChanged(ScrollBarMember.Both);
				this.m_document.Invalidate();  //??多大范围
			
				// 文档发生变化了
				this.m_document.FireTextChanged();

				this.Flush();
			}
			finally
			{
				this.m_document.Cursor = oldCursor;
			}
		}
Beispiel #9
0
		// 功能: 给一个元素向前插入一个同级元素或者属性
		// parameter:
		//		newItem     要插入的新Item
		//		refChild    参考位置的元素
		//		strError    out参数,返回出错信息
		// return:
        //      -1  出错
        //      0   成功
		public int InsertAttr(AttrItem startAttr,
			AttrItem newAttr, 
			out string strError)
		{
			Cursor oldCursor = this.m_document.Cursor;
			this.m_document.Cursor =  Cursors.WaitCursor;
			try
			{
				strError = "";
				if (startAttr == null)
				{
					Debug.Assert(false,"InsertAttr()时,传入的startAttr为null");
					strError = "InsertAttr()时,传入的startAttr为null";
					return -1;
				}
				if (newAttr == null)
				{
					Debug.Assert(false,"InsertAttr()时,传入的newAttr为null");
					strError = "InsertAttr()时,传入的newAttr为null";
					return -1;
				}


				// 1.调InsertAttr()函数,把父亲关系建好
				this.InsertAttrInternal(startAttr,
					newAttr,
					true,
					false);




				this.AfterAttrCreateOrChange(newAttr);

				return 0;
			}
			finally
			{
				this.m_document.Cursor = oldCursor;
			}
		}
Beispiel #10
0
		// parameter:
		//		element 被处理的属性节点
		// return:
		//		-1	error
		//		0	successed
		internal int ProcessAttrNsURI(AttrItem attr,
			bool bInitial,
			out string strError)
		{
			strError = "";

			// 不带名字名间 或缺省名字空间
			if (attr.Prefix == null
				|| attr.Prefix == "")
			{
				return 0;
			}

			string strSaveURI = attr.NamespaceURI;

			attr.m_strTempURI = null;

			string strUpDefineURI = attr.NamespaceURI;
			if (strUpDefineURI != null)
				return 0;

			if (bInitial == true)
				return 0;

			attr.m_strTempURI = strSaveURI;

			// 说明已经定义好了,不是临时变量
			if (attr.m_strTempURI == null)
			{
				strError = "未找到前缀'" + attr.Prefix + "'对应的URI";
				return 0;
			}

			AttrItem attrNs = this.m_document.CreateAttrItem("xmlns:" + attr.Prefix);
			attrNs.SetValue(attr.m_strTempURI);
			attrNs.IsNamespace = true;
			attr.parent.AppendAttrInternal(attrNs,true,false);

			// 把参数置空
			attr.m_strTempURI = null;
			return 0;
		}
Beispiel #11
0
		// 插入属性或者下级元素(按整数序号)
		internal void InsertAttrInternal(int nIndex,
			AttrItem newAttr,
			bool bAddObjTimestamp,
			bool bInitial)
		{
			newAttr.parent = this;

			this.attrs.Insert(nIndex,newAttr);

			if (bInitial == false)
			{
				string strError;
				int nRet = this.ProcessAttrNsURI(newAttr,
					bInitial,
					out strError);
				if (nRet == -1)
				{
					this.RemoveAttrInternal(newAttr,true);
					throw(new PrefixNotDefineException(strError));
				}
			}
			// 发ItemCreated()
			if (this.m_bConnected == true)
			{
				// ItemCreated事件
				ItemCreatedEventArgs endArgs = new ItemCreatedEventArgs();
				endArgs.item = newAttr;
				//endArgs.bInitial = false;
				this.m_document.fireItemCreated(this.m_document,endArgs);	
				newAttr.m_bConnected = true;
			}
			
			if (bAddObjTimestamp == true)
				this.m_objAttrsTimestamp ++;  
		}
Beispiel #12
0
		// 插入属性或者下级元素(按节点指针)
		internal void InsertAttrInternal(AttrItem startAttr,
			AttrItem newAttr,
			bool bAddObjTimestamp,
			bool bInitial)
		{
			int nIndex = this.attrs.IndexOf (startAttr);
			if (nIndex == -1)
			{
				Debug.Assert (false,"Insert时,startItem竟然不在children里");
				return;
			}
			this.InsertAttrInternal(nIndex,
				newAttr,
				bAddObjTimestamp,
				bInitial);
		}
Beispiel #13
0
		void ChangeFileAttr(AttrItem attr,
			string strOldValue,
			string strNewValue)
		{
			ElementItem parent = attr.parent;

			string strID = parent.GetAttrValue("id");
			if (strID == null)
				strID = "";

			if (attr.Name == "id") 
			{
				ChangeLine(strOldValue, strNewValue, null, null, null, null, null);
			}

			else if (attr.Name == "__mime") 
			{

				ChangeLine(strID, null, null, null, strNewValue, null, null);
			}


			else if (attr.Name == "__localpath") 
			{
				ChangeLine(strID, null, null, strNewValue, null, null, null);
			}

			else if (attr.Name == "__state") 
			{
				ChangeLine(strID, null, strNewValue, null, null, null, null);
			}

			else if (attr.Name == "__size") 
			{
				ChangeLine(strID, null, null, null, null, strNewValue, null);
			}
			else if (attr.Name == "__timestamp") 
			{
				ChangeLine(strID, null, null, null, null, null, strNewValue);
			}
		}
Beispiel #14
0
        /*
        static void FireItemCreatedTree(ElementItem element)
        {
            ItemCreatedEventArgs endArgs = new ItemCreatedEventArgs();
            endArgs.item = element;
            element.m_bConnected = true;

            element.m_document.fireItemCreated(element.m_document, endArgs);

        
            // 递归
            for (int i = 0; i < element.children.Count; i++)
            {
                Item item = element.Children[i];
                if (!(item is ElementItem))
                    continue;
                FireItemCreatedTree((ElementItem)item);
            }
        
        }
        */


        // 追加属性节点
        public AttrItem AppendAttrInternal(AttrItem attr,
			bool bAddObjTimestamp,
			bool bInitial)
		{
			// 去掉原同名的属性节点
			AttrItem oldAttr = this.GetAttrItem(attr.Name);
			if (oldAttr != null)
				this.RemoveAttrInternal(oldAttr,false);  //可以和追加节点视作一次

			attr.parent = this;
			this.attrs.Add(attr);

			if (bInitial == false)
			{
				string strError;
				int nRet = this.ProcessAttrNsURI(attr,
					bInitial,
					out strError);
				if (nRet == -1)
				{
					this.RemoveAttrInternal(attr,true);
					throw(new PrefixNotDefineException(strError));
				}
			}

			// 发ItemCreated消息
			if (this.m_bConnected == true)
			{
				// ItemCreated事件
				ItemCreatedEventArgs endArgs = new ItemCreatedEventArgs();
				endArgs.item = attr;
				endArgs.bInitial = false;
				this.m_document.fireItemCreated(this.m_document,endArgs);	

				attr.m_bConnected = true;
			}

			if (bAddObjTimestamp == true)
				this.m_objAttrsTimestamp ++;

			return attr;
		}
Beispiel #15
0
		// 根据一个前缀字符串, 从起点元素开始查找, 看这个前缀字符串是在哪里定义的URI。
		// 也就是要找到xmlns:???=???这样的属性对象,返回在namespaceAttr参数中。
		// 本来从返回的namespaceAttr参数中可以找到命中URI信息,但是为了使用起来方便,
		// 本函数也直接在strURI参数中返回了命中的URI
		// parameters:
		//		startItem	起点element对象
		//		strPrefix	要查找的前缀字符串
		//		strURI		[out]返回的URI
		//		namespaceAttr	[out]返回的AttrItem节点对象
		// return:
		//		ture	找到(strURI和namespaceAttr中有返回值)
		//		false	没有找到
		public static bool LocateNamespaceByPrefix(ElementItem startItem,
			string strPrefix,
			out string strURI,
			out AttrItem namespaceAttr)
		{
			strURI = "";
			namespaceAttr = null;

			/*
			Debug.Assert(strPrefix != "", "strPrefix参数不应当为空。前缀为空时,无需调用本函数就知道没有找到");
			if (strPrefix == "")
				return false;
			*/

			ElementItem currentItem = startItem;
			while(true)
			{
				if (currentItem == null)
					break;

				foreach(AttrItem attr in currentItem.attrs)
				{
					if (attr.IsNamespace == false)
						continue;

					string strLocalName = "";
					int nIndex = attr.Name.IndexOf(":");
					if (nIndex >= 0)
					{
						strLocalName = attr.Name.Substring(nIndex + 1);
					}
					else
					{
						Debug.Assert(attr.Name == "xmlns", "名字空间型的属性,如果name中无冒号,必然为xmlns。");
						if (attr.Name == "xmlns")
						{
							strLocalName = "";
						}
					}

					if (strLocalName == strPrefix)
					{
						strURI = attr.GetValue();
						namespaceAttr = attr;
						return true;
					}
				}

				currentItem = (ElementItem)currentItem.parent;
			}

			return false;

		}
Beispiel #16
0
		// 创建一个属性
		// 注意本函数可以改造成创建带前缀的属性节点 strName格式为: abc:test
		// 前缀及对应URI的定义从上级元素节点找,如果找到,则创建成功,如果未找到,创建失败。
		public AttrItem CreateAttrItem(string strName)
		{
			AttrItem item = new AttrItem(this);
			item.Name = strName;
			item.Prefix = "";
			item.LocalName = ItemUtil.GetLocalName(strName);

			return item;
		}
Beispiel #17
0
		// return:
		//		-1	error
		//		0	successed
		//		-2	取消
		private int InsertAttrWithDlg(AttrItem startAttr,
			out string strError)
		{
			strError = "";

			ElementItem element = (ElementItem)(startAttr.parent);
			if (element == null)
			{
				strError = "InsertAttrWithDlg(),element不可能为null。";
				return -1;
			}

			AttrNameDlg dlg = new AttrNameDlg ();
			dlg.SetInfo ("新同级属性",
				"给'" + startAttr.Name + "'增加新同级属性",
				element);
			dlg.ShowDialog  ();
			if (dlg.DialogResult != DialogResult.OK )
				return -2;

			AttrItem newAttr = null;
			int nRet = this.CreateAttrItemFromUI(dlg.textBox_strElementName.Text,
				dlg.textBox_URI.Text,
				out newAttr,
				out strError);
			if (nRet == -1)
				return -1;

			newAttr.SetValue(dlg.textBox_value.Text);


			return element.InsertAttr(startAttr,
				newAttr,
				out strError);
		}
Beispiel #18
0
		// parameter:
		//		strFullName: 可以带前缀 prefix:name
		//		strURi: null 或者 空字符串 不带URI
		public int CreateAttrItemFromUI(string strFullName,
			string strURI,
			out AttrItem attr,
			out string strError)
		{
			strError = "";
			attr = null;

			int nIndex = strFullName.IndexOf(":");
			if (nIndex == 0)
			{
				strError = "元素名称'" + strFullName + "'不合法";
				return -1;
			}
			else if (nIndex > 0)
			{
				string strPrefix = strFullName.Substring(0,nIndex);
				string strLocalName = strFullName.Substring(nIndex+1);
				if (strLocalName == "")
				{
					strError = "元素名称'" + strFullName + "'不合法";
					return -1;
				}
				if (strPrefix == "xmlns")
				{
					attr = this.CreateAttrItem(strFullName);
					attr.IsNamespace = true;
					//attr.LocalName = ""
				}
				else
				{
					if (strURI != null && strURI != "")
					{
						attr =  this.CreateAttrItem(strPrefix,
							strLocalName,
							strURI);
					}
					else
					{
						attr = this.CreateAttrItem(strPrefix,
							strLocalName);
					}
				}
			}
			else
			{
				if (strURI != null && strURI != "")
				{
					strError = "属性名称'" + strFullName + "'未指定前缀";
					return -1;
				}
				attr = this.CreateAttrItem(strFullName);
			}
			return 0;
		}