1 //------------------------------------------------------------------------------
2 // <copyright file="XPathAncestorQuery.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 // <owner current="true" primary="true">Microsoft</owner>
6 //------------------------------------------------------------------------------
8 namespace MS.Internal.Xml.XPath {
11 using System.Xml.XPath;
12 using System.Diagnostics;
13 using System.Collections.Generic;
15 internal sealed class XPathAncestorQuery : CacheAxisQuery {
16 private bool matchSelf;
18 public XPathAncestorQuery(Query qyInput, string name, string prefix, XPathNodeType typeTest, bool matchSelf) : base(qyInput, name, prefix, typeTest) {
19 this.matchSelf = matchSelf;
21 private XPathAncestorQuery(XPathAncestorQuery other) : base(other) {
22 this.matchSelf = other.matchSelf;
25 public override object Evaluate(XPathNodeIterator context) {
26 base.Evaluate(context);
28 XPathNavigator ancestor = null;
30 while ((input = qyInput.Advance()) != null) {
33 if (!Insert(outputBuffer, input)) {
34 // If input is already in output buffer all its ancestors are in a buffer as well.
39 if (ancestor == null || ! ancestor.MoveTo(input)) {
40 ancestor = input.Clone();
42 while (ancestor.MoveToParent()) {
43 if (matches(ancestor)) {
44 if (!Insert(outputBuffer, ancestor)) {
45 // If input is already in output buffer all its ancestors are in a buffer as well.
54 public override XPathNodeIterator Clone() { return new XPathAncestorQuery(this); }
55 public override int CurrentPosition { get { return outputBuffer.Count - count + 1; } }
56 public override QueryProps Properties { get { return base.Properties | QueryProps.Reverse; } }
58 public override void PrintQuery(XmlWriter w) {
59 w.WriteStartElement(this.GetType().Name);
61 w.WriteAttributeString("self", "yes");
64 w.WriteAttributeString("name", Prefix.Length != 0 ? Prefix + ':' + Name : Name);
66 if (TypeTest != XPathNodeType.Element) {
67 w.WriteAttributeString("nodeType", TypeTest.ToString());
69 qyInput.PrintQuery(w);