Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System.Xml / System / Xml / XPath / Internal / VariableQuery.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="VariableQuery.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.Globalization;
14     using System.Xml.Xsl;
15
16     internal sealed class VariableQuery : ExtensionQuery {
17         private IXsltContextVariable variable;
18
19         public VariableQuery(string name, string prefix) : base(prefix, name) {}
20         private VariableQuery(VariableQuery other) : base(other) {
21             this.variable = other.variable;
22         }
23
24         public override void SetXsltContext(XsltContext context) {
25             if (context == null) {
26                 throw XPathException.Create(Res.Xp_NoContext);
27             }
28
29                         if (this.xsltContext != context) {
30                 xsltContext = context;
31                                 variable = xsltContext.ResolveVariable(prefix, name);
32                 // Since null is allowed for ResolveFunction, allow it for ResolveVariable as well
33                 if (variable == null) {
34                     throw XPathException.Create(Res.Xp_UndefVar, QName);
35                 }
36             }
37         }
38
39         public override object Evaluate(XPathNodeIterator nodeIterator) {
40                         if (xsltContext == null) {
41                                 throw XPathException.Create(Res.Xp_NoContext);
42                         }
43
44             return ProcessResult(variable.Evaluate(xsltContext));
45         }
46
47         public override XPathResultType StaticType { get {
48             if (variable != null) {  // Temp. fix to overcome dependency on static type
49                 return GetXPathType(Evaluate(null));
50             }
51             XPathResultType result = variable != null ? variable.VariableType : XPathResultType.Any;
52                         if (result == XPathResultType.Error) {
53                                 // In v.1 we confused Error & Any so now for backward compatibility we should allow users to return any of them.
54                                 result = XPathResultType.Any;
55                         }
56             return result;
57         } }
58
59         public override XPathNodeIterator Clone() { return new VariableQuery(this); }
60
61         public override void PrintQuery(XmlWriter w) {
62             w.WriteStartElement(this.GetType().Name);
63             w.WriteAttributeString("name", prefix.Length != 0 ? prefix + ':' + name : name);
64             w.WriteEndElement();
65         }
66     }
67 }