1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace System.ServiceModel.Dispatcher
{
using System.Runtime;
using System.Xml.XPath;
public sealed class XPathResult : IDisposable
{
bool boolResult;
SafeNodeSequenceIterator internalIterator;
XPathNodeIterator nodeSetResult;
double numberResult;
XPathResultType resultType;
string stringResult;
internal XPathResult(XPathNodeIterator nodeSetResult)
: this()
{
this.nodeSetResult = nodeSetResult;
this.internalIterator = nodeSetResult as SafeNodeSequenceIterator;
this.resultType = XPathResultType.NodeSet;
}
internal XPathResult(string stringResult) : this()
{
this.stringResult = stringResult;
this.resultType = XPathResultType.String;
}
internal XPathResult(bool boolResult) : this()
{
this.boolResult = boolResult;
this.resultType = XPathResultType.Boolean;
}
internal XPathResult(double numberResult) : this()
{
this.numberResult = numberResult;
this.resultType = XPathResultType.Number;
}
XPathResult()
{
}
public XPathResultType ResultType
{
get
{
return this.resultType;
}
}
public void Dispose()
{
if (this.internalIterator != null)
{
this.internalIterator.Dispose();
}
}
public bool GetResultAsBoolean()
{
switch (this.resultType)
{
case XPathResultType.Boolean:
return this.boolResult;
case XPathResultType.NodeSet:
return QueryValueModel.Boolean(this.nodeSetResult);
case XPathResultType.Number:
return QueryValueModel.Boolean(this.numberResult);
case XPathResultType.String:
return QueryValueModel.Boolean(this.stringResult);
default:
throw Fx.AssertAndThrow("Unexpected result type.");
}
}
public XPathNodeIterator GetResultAsNodeset()
{
if (this.resultType != XPathResultType.NodeSet)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.CannotRepresentResultAsNodeset)));
}
return this.nodeSetResult;
}
public double GetResultAsNumber()
{
switch (this.resultType)
{
case XPathResultType.Boolean:
return QueryValueModel.Double(this.boolResult);
case XPathResultType.NodeSet:
return QueryValueModel.Double(this.nodeSetResult);
case XPathResultType.Number:
return this.numberResult;
case XPathResultType.String:
return QueryValueModel.Double(this.stringResult);
default:
throw Fx.AssertAndThrow("Unexpected result type.");
}
}
public string GetResultAsString()
{
switch (this.resultType)
{
case XPathResultType.Boolean:
return QueryValueModel.String(this.boolResult);
case XPathResultType.NodeSet:
return QueryValueModel.String(this.nodeSetResult);
case XPathResultType.Number:
return QueryValueModel.String(this.numberResult);
case XPathResultType.String:
return this.stringResult;
default:
throw Fx.AssertAndThrow("Unexpected result type.");
}
}
internal XPathResult Copy()
{
XPathResult result = new XPathResult();
result.resultType = this.resultType;
switch (this.resultType)
{
case XPathResultType.Boolean:
result.boolResult = this.boolResult;
break;
case XPathResultType.NodeSet:
result.nodeSetResult = this.nodeSetResult.Clone();
break;
case XPathResultType.Number:
result.numberResult = this.numberResult;
break;
case XPathResultType.String:
result.stringResult = this.stringResult;
break;
default:
throw Fx.AssertAndThrow("Unexpected result type.");
}
return result;
}
}
}
|