e6331502c17707061ca8d7b4d7476e34e057d9ac
[mono.git] / mcs / class / referencesource / System.Xml / System / Xml / XPath / Internal / CacheOutputQuery.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="CacheOutputQuery.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>                                                                
5 // <owner current="true" primary="true">Microsoft</owner>
6 //------------------------------------------------------------------------------
7
8 namespace MS.Internal.Xml.XPath {
9     using System;
10     using System.Xml;
11     using System.Xml.XPath;
12     using System.Diagnostics;
13     using System.Xml.Xsl;
14     using System.Collections.Generic;
15
16     internal abstract class CacheOutputQuery : Query {
17         internal Query input;
18         // int count; -- we reusing it here
19         protected List<XPathNavigator> outputBuffer;
20
21         public CacheOutputQuery(Query input) {
22             this.input = input;
23             this.outputBuffer = new List<XPathNavigator>();
24             this.count = 0;
25         }
26         protected CacheOutputQuery(CacheOutputQuery other) : base(other) {
27             this.input        = Clone(other.input);
28             this.outputBuffer = new List<XPathNavigator>(other.outputBuffer);
29             this.count = other.count;
30         }
31
32         public override void Reset() {
33             this.count = 0;
34         }
35
36         public override void SetXsltContext(XsltContext context){
37             input.SetXsltContext(context);
38         }
39
40         public override object Evaluate(XPathNodeIterator context) {            
41             outputBuffer.Clear();
42             count = 0;
43             return input.Evaluate(context);// This is trick. IDQuery needs this value. Otherwise we would return this.
44                                             // All subclasses should and would anyway override thismethod and return this.
45         }
46
47         public override XPathNavigator Advance() {
48             Debug.Assert(0 <= count && count <= outputBuffer.Count);
49             if (count < outputBuffer.Count) {
50                 return outputBuffer[count++];
51             }
52             return null;
53         }
54         
55         public override XPathNavigator Current { 
56             get {
57                 Debug.Assert(0 <= count && count <= outputBuffer.Count);
58                 if (count == 0) {
59                     return null;
60                 }
61                 return outputBuffer[count - 1];
62             } 
63         }
64
65         public override XPathResultType StaticType { get { return XPathResultType.NodeSet; } }
66         public override int CurrentPosition   { get { return count; } }
67         public override int Count             { get { return outputBuffer.Count; } }
68         public override QueryProps Properties { get { return QueryProps.Merge | QueryProps.Cached | QueryProps.Position | QueryProps.Count; } }
69
70         public override void PrintQuery(XmlWriter w) {
71             w.WriteStartElement(this.GetType().Name);
72             input.PrintQuery(w);
73             w.WriteEndElement();
74         }
75     }
76 }