private static void AddKeyValue(Hashtable keyTable, String key, XPathNavigator value, bool checkDuplicates) { ArrayList list = (ArrayList)keyTable[key]; if (list == null) { list = new ArrayList(); keyTable.Add(key, list); } else { Debug.Assert( value.ComparePosition((XPathNavigator)list[list.Count - 1]) != XmlNodeOrder.Before, "The way we traversing nodes should garantees node-order" ); if (checkDuplicates) { // it's posible that this value already was ----osiated with current node // but if this happened the node is last in the list of values. if (value.ComparePosition((XPathNavigator)list[list.Count - 1]) == XmlNodeOrder.Same) { return; } } else { Debug.Assert( value.ComparePosition((XPathNavigator)list[list.Count - 1]) != XmlNodeOrder.Same, "checkDuplicates == false : We can't have duplicates" ); } } list.Add(value.Clone()); }
public static string TruncateHtml(string text, int maxWordCount, bool decodeHtmlBeforeParsing) { if (decodeHtmlBeforeParsing) { text = HttpUtility.HtmlDecode(text); } text = "<body>" + text + "</body>"; int wordsAvailable = maxWordCount; XmlDocument xml = new XmlDocument(); xml.LoadXml("" + text + ""); XPathNavigator navigator = xml.CreateNavigator(); XPathNavigator breakPoint = null; string lastText = string.Empty; // find the text node we need: while (navigator.MoveToFollowing(XPathNodeType.Text)) { if (navigator.Value.Trim() != string.Empty) { lastText = WordUtil.GetWordCount(navigator.Value.Trim()) > wordsAvailable ? WordUtil.GetSubStringWords(wordsAvailable, navigator.Value.Trim()) : navigator.Value; } wordsAvailable = wordsAvailable - WordUtil.GetWordCount(navigator.Value); if (wordsAvailable <= 0) { navigator.SetValue(lastText); breakPoint = navigator.Clone(); break; } } //first remove text nodes while (navigator.MoveToFollowing(XPathNodeType.Text)) { if (navigator.ComparePosition(breakPoint) == XmlNodeOrder.After) { navigator.DeleteSelf(); } } //// moves to parent, then move the rest navigator.MoveTo(breakPoint); while (navigator.MoveToFollowing(XPathNodeType.Element)) { if (navigator.ComparePosition(breakPoint) == XmlNodeOrder.After) { navigator.DeleteSelf(); } } navigator.MoveToRoot(); return(navigator.InnerXml); }
public override XmlNodeOrder ComparePosition(XPathNavigator nav) { XmlDocumentEditableNavigator e = nav as XmlDocumentEditableNavigator; if (e != null) { return(navigator.ComparePosition(e.navigator)); } else { return(navigator.ComparePosition(nav)); } }
public override XmlNodeOrder ComparePosition(long x, long y) { XPathNavigator navigator = this[x]; XPathNavigator nav = this[y]; return(navigator.ComparePosition(nav)); }
private static int FindAtPathIndex(XPathNavigator nav) { int index = 0; XPathNavigator buf = nav.Clone(); if (buf.MoveToParent()) { if (buf.MoveToChild(nav.NodeType)) { do { if (buf.LocalName == nav.LocalName) { index++; } XmlNodeOrder cmp = buf.ComparePosition(nav); if (cmp == XmlNodeOrder.Same) { break; } }while (buf.MoveToNext(nav.NodeType)); } } return(index); }
/// <param name="element">the element to find the index for</param> /// <returns>the element index (i.e. order number in the parent)</returns> private static int FindElementIndex(XPathNavigator element) { var parentNode = FindParent(element); if (parentNode.NodeType == XPathNodeType.Root) { return(1); } var index = 1; var children = parentNode.SelectChildren(XPathNodeType.Element); while (children.MoveNext()) { Debug.Assert(children.Current != null, "children.Current != null"); if (children.Current.Name != element.Name) { continue; } if (element.ComparePosition(children.Current) == XmlNodeOrder.Same) { return(index); } index++; } throw new ArgumentException( "FindElementIndex: Couldn't find element within parent. This was not expected to happen."); }
/// <summary> /// If the context node-set returns a node that is contained in the subtree of the previous node, /// then returning children of each node in "natural" order may not correspond to document order. /// Therefore, in order to guarantee document order, keep a stack in order to push the sibling of /// ancestor nodes. These siblings will not be returned until all of the descendants' children are /// returned first. /// </summary> private IteratorResult DocOrderMerge() { XmlNodeOrder cmp; Debug.Assert(_state == IteratorState.HaveCurrentHaveNext); // Compare location of navCurrent with navNext cmp = _navCurrent.ComparePosition(_navNext); // If navCurrent is before navNext in document order, // If cmp = XmlNodeOrder.Unknown, then navCurrent is before navNext (since input is is doc order) if (cmp == XmlNodeOrder.Before || cmp == XmlNodeOrder.Unknown) { // Then navCurrent can be returned (it is guaranteed to be first in document order) return(IteratorResult.HaveCurrentNode); } // If navCurrent is after navNext in document order, then delay returning navCurrent // Otherwise, discard navNext since it is positioned to the same node as navCurrent if (cmp == XmlNodeOrder.After) { _navStack.Push(_navCurrent); _navCurrent = _navNext; _navNext = null; } // Need next input node _state = IteratorState.HaveCurrentNeedNext; return(IteratorResult.NeedInputNode); }
public override XmlNodeOrder ComparePosition(long x, long y) { XPathNavigator nodeX = this[x]; XPathNavigator nodeY = this[y]; return(nodeX.ComparePosition(nodeY)); }
private bool IsContextUsed(XPathNavigator contextNode) { foreach (XPathNavigator nav in this.usedContext) { if (contextNode.ComparePosition(nav) == System.Xml.XmlNodeOrder.Same) { return(true); } } return(false); }
public int Compare(XPathItem x, XPathItem y) { XPathNavigator nav1 = x as XPathNavigator; XPathNavigator nav2 = y as XPathNavigator; if (nav1 != null && nav2 != null) { switch (nav1.ComparePosition(nav2)) { case XmlNodeOrder.Before: return(-1); case XmlNodeOrder.After: return(1); case XmlNodeOrder.Same: return(0); default: { XPathNavigator root1 = nav1.Clone(); root1.MoveToRoot(); XPathNavigator root2 = nav2.Clone(); root2.MoveToRoot(); int hashCode1 = XPathNavigator.NavigatorComparer.GetHashCode(root1); int hashCode2 = XPathNavigator.NavigatorComparer.GetHashCode(root2); if (hashCode1 < hashCode2) { return(-1); } else if (hashCode1 > hashCode2) { return(1); } else { throw new InvalidOperationException(); } } } } else { throw new XPath2Exception("XPTY0004", Properties.Resources.XPTY0004, "xs:anyAtomicType", "node()* in function op:union,op:intersect and op:except"); } }
private void AddResult(ArrayList ResultList, XPathNavigator nav) { int count = ResultList.Count; for (int i = 0; i < count; i++) { XmlNodeOrder docOrder = nav.ComparePosition(((XPathNavigator)ResultList[i])); if (docOrder == XmlNodeOrder.Same) { return; } if (docOrder == XmlNodeOrder.Before) { ResultList.Insert(i, nav.Clone()); return; } } ResultList.Add(nav.Clone()); }
public bool MoveNext() { if (_needFirst) { // Get first matching preceding-sibling node if (!_navCurrent.MoveToParent()) { return(false); } if (!_filter.MoveToContent(_navCurrent)) { return(false); } _needFirst = false; } else { // Get next matching preceding-sibling node if (!_filter.MoveToFollowingSibling(_navCurrent)) { return(false); } } // Accept matching sibling only if it precedes navEnd in document order if (_useCompPos) { return(_navCurrent.ComparePosition(_navEnd) == XmlNodeOrder.Before); } if (_navCurrent.IsSamePosition(_navEnd)) { // Found the original context node, so iteration is complete. If MoveNext // is called again, use ComparePosition so that false will continue to be // returned. _useCompPos = true; return(false); } return(true); }
public static bool Node1LiegtVorNode2(XmlNode node1, XmlNode node2) { if (node1 == null || node2 == null) { throw new ApplicationException("Keiner der beiden zu vergleichenden Nodes darf NULL sein (Node1LiegtVorNode2)"); } if (node1.OwnerDocument != node2.OwnerDocument) { return(false); } if (node1 == node2) { return(false); } XPathNavigator xPathNavigator = node1.CreateNavigator(); XPathNavigator nav = node2.CreateNavigator(); return(xPathNavigator.ComparePosition(nav) == XmlNodeOrder.Before); }
/// <summary> /// None of the two nodes to be compared must be NULL /// </summary> public static bool Node1LaisBeforeNode2(XmlNode node1, XmlNode node2) { if (node1 == null || node2 == null) { throw new ApplicationException("None of the two nodes to be compared must be NULL (Node1LaisBeforeNode2)"); } if (node1.OwnerDocument != node2.OwnerDocument) { return(false); } if (node1 == node2) { return(false); // Both nodes equal, then of course not node1 before node2 } XPathNavigator naviNode1 = node1.CreateNavigator(); XPathNavigator naviNode2 = node2.CreateNavigator(); return(naviNode1.ComparePosition(naviNode2) == System.Xml.XmlNodeOrder.Before); }
static string getKey(IXPathNavigable el) { XPathNavigator nav = el.CreateNavigator(); XPathNavigator navRoot = nav.Clone(); navRoot.MoveToRoot(); navRoot.MoveToFirstChild(); //DocumentElement StringBuilder bld = new StringBuilder(); do { if (bld.Length > 0) { bld.Insert(0, '/'); } bld.Insert(0, nav.GetAttribute("path2", string.Empty)); } while (nav.MoveToParent() && nav.ComparePosition(navRoot) != XmlNodeOrder.Same); return(bld.ToString().ToLower()); }
/// <summary> /// Add a node indexed by the specified key value. /// </summary> public void Add(string key, XPathNavigator navigator) { XmlQueryNodeSequence seq; if (!this.table.TryGetValue(key, out seq)) { // Create a new sequence and add it to the index seq = new XmlQueryNodeSequence(); seq.AddClone(navigator); this.table.Add(key, seq); } else { // The nodes are guaranteed to be added in document order with possible duplicates. // Add node to the existing sequence if it differs from the last one. Debug.Assert(navigator.ComparePosition(seq[seq.Count - 1]) >= 0, "Index nodes must be added in document order"); if (!navigator.IsSamePosition(seq[seq.Count - 1])) { seq.AddClone(navigator); } } }
public static XmlNodeOrder CompareNodes(XPathNavigator l, XPathNavigator r) { XmlNodeOrder order = l.ComparePosition(r); if (order != XmlNodeOrder.Unknown) { return(order); } XPathNavigator navigator = l.Clone(); navigator.MoveToRoot(); string baseURI = navigator.BaseURI; if (!navigator.MoveTo(r)) { navigator = r.Clone(); } navigator.MoveToRoot(); string strB = navigator.BaseURI; int num = string.CompareOrdinal(baseURI, strB); return((num < 0) ? XmlNodeOrder.Before : ((num > 0) ? XmlNodeOrder.After : XmlNodeOrder.Unknown)); }
public static XmlNodeOrder CompareNodes(XPathNavigator l, XPathNavigator r) { XmlNodeOrder cmp = l.ComparePosition(r); if (cmp == XmlNodeOrder.Unknown) { XPathNavigator copy = l.Clone(); copy.MoveToRoot(); string baseUriL = copy.BaseURI; if (!copy.MoveTo(r)) { copy = r.Clone(); } copy.MoveToRoot(); string baseUriR = copy.BaseURI; int cmpBase = string.CompareOrdinal(baseUriL, baseUriR); cmp = ( cmpBase < 0 ? XmlNodeOrder.Before : cmpBase > 0 ? XmlNodeOrder.After : /*default*/ XmlNodeOrder.Unknown ); } return(cmp); }
public IteratorResult MoveNext(XPathNavigator input) { switch (_state) { case IteratorState.NeedCandidateCurrent: // If there are no more input nodes, then iteration is complete if (input == null) { return(IteratorResult.NoMoreNodes); } // Save input node as current node _navCurrent = XmlQueryRuntime.SyncToNavigator(_navCurrent, input); // Scan for additional input nodes within the same document (since they are after navCurrent in docorder) _state = IteratorState.HaveCandidateCurrent; return(IteratorResult.NeedInputNode); case IteratorState.HaveCandidateCurrent: // If there are no more input nodes, if (input == null) { // Then candidate node has been selected, and there are no further input nodes _state = IteratorState.HaveCurrentNoNext; } else { // If the input node is in the same document as the current node, if (_navCurrent.ComparePosition(input) != XmlNodeOrder.Unknown) { // Then update the current node and get the next input node _navCurrent = XmlQueryRuntime.SyncToNavigator(_navCurrent, input); return(IteratorResult.NeedInputNode); } // Save the input node as navNext _navNext = XmlQueryRuntime.SyncToNavigator(_navNext, input); _state = IteratorState.HaveCurrentHaveNext; } PushAncestors(); break; } if (!_navStack.IsEmpty) { while (true) { // Move to the next matching node that is before the top node on the stack in document order if (_filter.MoveToFollowing(_navCurrent, _navStack.Peek())) { // Found match return(IteratorResult.HaveCurrentNode); } // Do not include ancestor nodes as part of the preceding axis _navCurrent.MoveTo(_navStack.Pop()); // No more preceding matches possible if (_navStack.IsEmpty) { break; } } } if (_state == IteratorState.HaveCurrentNoNext) { // No more nodes, so iteration is complete _state = IteratorState.NeedCandidateCurrent; return(IteratorResult.NoMoreNodes); } // Make next node the current node and start trying to find input node greatest in docorder _navCurrent = XmlQueryRuntime.SyncToNavigator(_navCurrent, _navNext); _state = IteratorState.HaveCandidateCurrent; return(IteratorResult.HaveCurrentNode); }
public IteratorResult MoveNext(XPathNavigator input) { switch (_state) { case IteratorState.NeedCandidateCurrent: // If there are no more input nodes, then iteration is complete if (input == null) { return(IteratorResult.NoMoreNodes); } // Save input node as current node _navCurrent = XmlQueryRuntime.SyncToNavigator(_navCurrent, input); // Still must check next input node to see if is a descendant of this one _state = IteratorState.HaveCandidateCurrent; return(IteratorResult.NeedInputNode); case IteratorState.HaveCandidateCurrent: // If there are no more input nodes, if (input == null) { // Then candidate node has been selected, and there are no further input nodes _state = IteratorState.HaveCurrentNoNext; return(MoveFirst()); } // If input node is in the subtree of the candidate node, then use the input node instead if (_navCurrent.IsDescendant(input)) { goto case IteratorState.NeedCandidateCurrent; } // Found node on which to perform following scan. Now skip past all input nodes in the same document. _state = IteratorState.HaveCurrentNeedNext; goto case IteratorState.HaveCurrentNeedNext; case IteratorState.HaveCurrentNeedNext: // If there are no more input nodes, if (input == null) { // Then candidate node has been selected, and there are no further input nodes _state = IteratorState.HaveCurrentNoNext; return(MoveFirst()); } // Skip input node unless it's in a different document than the node on which the following scan was performed if (_navCurrent.ComparePosition(input) != XmlNodeOrder.Unknown) { return(IteratorResult.NeedInputNode); } // Next node is in a different document, so save it _navNext = XmlQueryRuntime.SyncToNavigator(_navNext, input); _state = IteratorState.HaveCurrentHaveNext; return(MoveFirst()); } if (!_filter.MoveToFollowing(_navCurrent, null)) { return(MoveFailed()); } return(IteratorResult.HaveCurrentNode); }