1 //------------------------------------------------------------------------------
2 // <copyright file="VariableQuery.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.Globalization;
16 internal sealed class VariableQuery : ExtensionQuery {
17 private IXsltContextVariable variable;
19 public VariableQuery(string name, string prefix) : base(prefix, name) {}
20 private VariableQuery(VariableQuery other) : base(other) {
21 this.variable = other.variable;
24 public override void SetXsltContext(XsltContext context) {
25 if (context == null) {
26 throw XPathException.Create(Res.Xp_NoContext);
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);
39 public override object Evaluate(XPathNodeIterator nodeIterator) {
40 if (xsltContext == null) {
41 throw XPathException.Create(Res.Xp_NoContext);
44 return ProcessResult(variable.Evaluate(xsltContext));
47 public override XPathResultType StaticType { get {
48 if (variable != null) { // Temp. fix to overcome dependency on static type
49 return GetXPathType(Evaluate(null));
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;
59 public override XPathNodeIterator Clone() { return new VariableQuery(this); }
61 public override void PrintQuery(XmlWriter w) {
62 w.WriteStartElement(this.GetType().Name);
63 w.WriteAttributeString("name", prefix.Length != 0 ? prefix + ':' + name : name);