XPathResult.cs 4.9 KB

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