Initial commit
[mono.git] / mcs / class / referencesource / System.Xml / System / Xml / XPath / Internal / IdQuery.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="IDQuery.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 // <owner current="true" primary="true">[....]</owner>
6 //------------------------------------------------------------------------------
7
8 namespace MS.Internal.Xml.XPath {
9     using System;
10     using System.Xml;
11     using System.Xml.XPath;
12     using System.Xml.Xsl;
13     using System.Diagnostics;
14     using System.Collections.Generic;
15
16     internal sealed class IDQuery : CacheOutputQuery {
17
18         public IDQuery(Query arg) : base(arg) {}
19         private IDQuery(IDQuery other) : base(other) { }
20
21         public override object Evaluate(XPathNodeIterator context) {
22             object argVal = base.Evaluate(context);
23             XPathNavigator contextNode = context.Current.Clone();
24
25             switch (GetXPathType(argVal)) {
26             case XPathResultType.NodeSet:
27                 XPathNavigator temp;
28                 while ((temp = input.Advance()) != null) {
29                     ProcessIds(contextNode, temp.Value);
30                 }
31                 break;
32             case XPathResultType.String:
33                 ProcessIds(contextNode, (string)argVal);
34                 break;
35             case XPathResultType.Number:
36                 ProcessIds(contextNode, StringFunctions.toString((double)argVal));
37                 break;
38             case XPathResultType.Boolean:
39                 ProcessIds(contextNode, StringFunctions.toString((bool)argVal));
40                 break;
41             case XPathResultType_Navigator:
42                 ProcessIds(contextNode, ((XPathNavigator)argVal).Value);
43                 break;
44             }
45             return this;
46         }
47
48         void ProcessIds(XPathNavigator contextNode, string val) {
49             string[] ids = XmlConvert.SplitString(val);
50             for (int idx = 0; idx < ids.Length; idx++) {
51                 if (contextNode.MoveToId(ids[idx])) {
52                     Insert(outputBuffer, contextNode);
53                 }
54             }
55         }
56
57         public override XPathNavigator MatchNode(XPathNavigator context) {
58             Evaluate(new XPathSingletonIterator(context, /*moved:*/true));
59             XPathNavigator result;
60             while ((result = Advance()) != null) {
61                 if (result.IsSamePosition(context)) {
62                     return context;
63                 }
64             }
65             return null;
66         }
67
68         public override XPathNodeIterator Clone() { return new IDQuery(this); }
69     }
70 }