// 复制构造函数 /// <summary> /// 初始化一个 NaviItem 对象 /// </summary> /// <param name="other">复制时参考的对象</param> public NaviItem(NaviItem other) { this.Type = other.Type; this.MarcNode = other.MarcNode; this.AttrName = other.AttrName; this.Text = other.Text; }
/* * * If the XPathNavigator is not currently positioned on an element, this method returns false and the position of the XPathNavigator does not change. * After a successful call to MoveToFirstAttribute, the LocalName, NamespaceURI and Prefix properties reflect the values of the attribute. When the XPathNavigator is positioned on an attribute, the methods MoveToNext, MoveToPrevious, and MoveToFirst are not applicable. These methods always return false and do not change the position of the XPathNavigator. Rather, you can call MoveToNextAttribute to move to the next attribute node. * After the XPathNavigator is positioned on an attribute, you can call MoveToParent to move to the owner element. * * */ /// <summary> /// 移动到第一个属性节点 /// </summary> /// <returns>如果移动成功,返回 true;否则返回 false。当返回 false 时,表示当前位置没有发生变动。</returns> public override bool MoveToFirstAttribute() { ////Debug.WriteLine("MoveToFirstAttribute"); ////Debug.WriteLine("*** Current " + this.m_navigatorState.Dump()); if (this.m_navigatorState.CurItem.Type != NaviItemType.Element) { return(false); } Debug.Assert(this.m_navigatorState.CurItem.Type == NaviItemType.Element, "必须是元素节点"); NaviItem current = this.m_navigatorState.CurItem; string strAttrName = current.FirstAttrName; if (strAttrName == null) { return(false); } this.m_navigatorState.CurItem.AttrName = strAttrName; this.m_navigatorState.CurItem.Type = NaviItemType.Attribute; ////Debug.WriteLine("*** Changed " + this.m_navigatorState.Dump()); return(true); }
public override string GetAttribute(string localName, string namespaceURI) { //StreamUtil.WriteText("I:\\debug.txt","进到 GetAttribute()\r\n"); if (HasAttributes == false) { return(String.Empty); } NaviItem current = this.m_navigatorState.CurItem; if (current.Type != NaviItemType.Element) { return(String.Empty); } string strAttr = current.GetAttrValue(localName); if (string.IsNullOrEmpty(strAttr) == false) { return(strAttr); } return(String.Empty); }
// 移动到最近的一个element,通常是上级 void MoveToElement() { //StreamUtil.WriteText("I:\\debug.txt","进到 MoveToElement()\r\n"); Debug.Assert(this.m_navigatorState.CurItem != null, ""); if (this.m_navigatorState.CurItem.Type == NaviItemType.VirtualRoot) { Debug.Assert(false, ""); return; } NaviItem current = this.m_navigatorState.CurItem; if (current.Type == NaviItemType.Element) { return; } if (current.Type == NaviItemType.Attribute || current.Type == NaviItemType.Text) { Debug.Assert(current.MarcNode != null, ""); this.m_navigatorState.CurItem = new NaviItem(current.MarcNode, NaviItemType.Element); return; } Debug.Assert(false, ""); }
public NavigatorState(NaviItem item) { CurItem = item; //VirtualRoot = new NaviItem(item.MarcNode.Root, NaviItemType.VirtualRoot); //Debug.Assert(VirtualRoot != null, "VirtualRoot不能为null"); DocRoot = item.MarcNode.Root; Debug.Assert(DocRoot != null, "DocRoot不能为null"); }
// 对一批互相没有树重叠关系的对象进行筛选 static MarcNodeList simpleSelect(MarcNodeList source, string strXPath, int nMaxCount = -1) { // 准备一个模拟的记录节点 // MarcRecord record = new MarcRecord(); MarcNode temp_root = new MarcNode(); temp_root.Name = "temp"; // 保存下集合中所有的Parent指针 List <MarcNode> parents = new List <MarcNode>(); foreach (MarcNode node in source) { // 保存指针 parents.Add(node.Parent); // 建立父子关系,但原来位置的ChidNodes并不摘除 temp_root.ChildNodes.baseAdd(node); node.Parent = temp_root; // Debug.Assert(node.Parent == temp_root, ""); } Debug.Assert(parents.Count == source.count, ""); try { MarcNodeList results = new MarcNodeList(); MarcNavigator nav = new MarcNavigator(temp_root); // 出发点在模拟的记录节点上 XPathNodeIterator ni = nav.Select(strXPath); while (ni.MoveNext() && (nMaxCount == -1 || results.count < nMaxCount)) { NaviItem item = ((MarcNavigator)ni.Current).Item; if (item.Type != NaviItemType.Element) { throw new Exception("xpath '" + strXPath + "' 命中了非元素类型的节点,这是不允许的"); continue; } if (results.indexOf(item.MarcNode) == -1) // 不重复的才加入 { results.add(item.MarcNode); } } return(results); } finally { // 恢复原先的 Parent 指针 Debug.Assert(parents.Count == source.count, ""); for (int i = 0; i < source.count; i++) { source[i].Parent = parents[i]; } } }
void Initial(NaviItem item) { Debug.Assert(item != null, "item不能为null"); this.m_navigatorState = new NavigatorState(); this.m_navigatorState.CurItem = item; this.m_navigatorState.DocRoot = item.MarcNode.Root; // this.m_navigatorState.VirtualRoot = new NaviItem(item.MarcNode.Root, NaviItemType.VirtualRoot); this.m_nametable = new NameTable(); this.m_nametable.Add(String.Empty); }
/// <summary> /// 初始化一个 MarcNavigator 类的实例。 /// </summary> /// <param name="node">出发点的 MarcNode 对象</param> public MarcNavigator(MarcNode node) { #if NO //StreamUtil.WriteText("I:\\debug.txt","进到 构造函数XmlEditorNavigator(editor)里\r\n"); Debug.Assert(node != null,"item不能为null"); this.m_navigatorState = new NavigatorState(); this.m_navigatorState.CurItem = new NaviItem(node, NaviItemType.Element); this.m_navigatorState.DocRoot = new NaviItem(node.Root, NaviItemType.Element); this.m_navigatorState.VirtualRoot = this.m_navigatorState.DocRoot; this.m_nametable = new NameTable(); this.m_nametable.Add(String.Empty); #endif Debug.Assert(node != null, "node不能为null"); NaviItem item = new NaviItem(node, NaviItemType.Element); Initial(item); }
/// <summary> /// 初始化一个 MarcNavigator 类的实例。 /// </summary> /// <param name="node">出发点的 MarcNode 对象</param> public MarcNavigator(MarcNode node) { #if NO //StreamUtil.WriteText("I:\\debug.txt","进到 构造函数XmlEditorNavigator(editor)里\r\n"); Debug.Assert(node != null, "item不能为null"); this.m_navigatorState = new NavigatorState(); this.m_navigatorState.CurItem = new NaviItem(node, NaviItemType.Element); this.m_navigatorState.DocRoot = new NaviItem(node.Root, NaviItemType.Element); this.m_navigatorState.VirtualRoot = this.m_navigatorState.DocRoot; this.m_nametable = new NameTable(); this.m_nametable.Add(String.Empty); #endif Debug.Assert(node != null, "node不能为null"); NaviItem item = new NaviItem(node, NaviItemType.Element); Initial(item); }
public override bool MoveToAttribute(string localName, string namespaceURI) { //StreamUtil.WriteText("I:\\debug.txt","进到 MoveToAttribute()\r\n"); NaviItem current = this.m_navigatorState.CurItem; Debug.Assert(current.MarcNode != null, ""); // 检查是否有这个属性名 if (current.ExistAttr(localName) == false) { return(false); } this.m_navigatorState.CurItem = new NaviItem(current.MarcNode, NaviItemType.Attribute); this.m_navigatorState.CurItem.AttrName = localName; return(true); }
// 针对DOM树进行 XPath 筛选 // parameters: // nMaxCount 至多选择开头这么多个元素。-1表示不限制 /// <summary> /// 用 XPath 字符串选择节点 /// </summary> /// <param name="strXPath">XPath 字符串</param> /// <param name="nMaxCount">限制命中的最多节点数。-1表示不限制</param> /// <returns>被选中的节点集合</returns> public MarcNodeList select(string strXPath, int nMaxCount /* = -1*/) { MarcNodeList results = new MarcNodeList(); MarcNavigator nav = new MarcNavigator(this); // 出发点在当前对象 XPathNodeIterator ni = nav.Select(strXPath); while (ni.MoveNext() && (nMaxCount == -1 || results.count < nMaxCount)) { NaviItem item = ((MarcNavigator)ni.Current).Item; if (item.Type != NaviItemType.Element) { // if (bSkipNoneElement == false) throw new Exception("xpath '" + strXPath + "' 命中了非元素类型的节点,这是不允许的"); continue; } results.add(item.MarcNode); } return(results); }
// 复制构造函数 public NavigatorState(NavigatorState NavState) { this.CurItem = new NaviItem(NavState.CurItem); // 复制。位置参数都需要复制,因为复制的对象里面的这些状态以后可能被修改 this.DocRoot = NavState.DocRoot; // 引用。因为都是针对的同一个文档,文档本身并不需要复制 // this.VirtualRoot = NavState.VirtualRoot; }
/// <summary> /// 初始化一个 MarcNavigator 类的实例。 /// </summary> /// <param name="item">出发点的 NaviItem 对象</param> public MarcNavigator(NaviItem item) { //StreamUtil.WriteText("I:\\debug.txt","进到 构造函数XmlEditorNavigator(editor)里\r\n"); Initial(item); }