txNodeSetAdaptor.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "txNodeSetAdaptor.h"
  6. #include "txXPathTreeWalker.h"
  7. txNodeSetAdaptor::txNodeSetAdaptor()
  8. : txXPathObjectAdaptor(),
  9. mWritable(true)
  10. {
  11. }
  12. txNodeSetAdaptor::txNodeSetAdaptor(txNodeSet *aNodeSet)
  13. : txXPathObjectAdaptor(aNodeSet),
  14. mWritable(false)
  15. {
  16. }
  17. NS_IMPL_ISUPPORTS_INHERITED(txNodeSetAdaptor, txXPathObjectAdaptor, txINodeSet)
  18. nsresult
  19. txNodeSetAdaptor::Init()
  20. {
  21. if (!mValue) {
  22. mValue = new txNodeSet(nullptr);
  23. }
  24. return NS_OK;
  25. }
  26. NS_IMETHODIMP
  27. txNodeSetAdaptor::Item(uint32_t aIndex, nsIDOMNode **aResult)
  28. {
  29. *aResult = nullptr;
  30. if (aIndex > (uint32_t)NodeSet()->size()) {
  31. return NS_ERROR_ILLEGAL_VALUE;
  32. }
  33. return txXPathNativeNode::getNode(NodeSet()->get(aIndex), aResult);
  34. }
  35. NS_IMETHODIMP
  36. txNodeSetAdaptor::ItemAsNumber(uint32_t aIndex, double *aResult)
  37. {
  38. if (aIndex > (uint32_t)NodeSet()->size()) {
  39. return NS_ERROR_ILLEGAL_VALUE;
  40. }
  41. nsAutoString result;
  42. txXPathNodeUtils::appendNodeValue(NodeSet()->get(aIndex), result);
  43. *aResult = txDouble::toDouble(result);
  44. return NS_OK;
  45. }
  46. NS_IMETHODIMP
  47. txNodeSetAdaptor::ItemAsString(uint32_t aIndex, nsAString &aResult)
  48. {
  49. if (aIndex > (uint32_t)NodeSet()->size()) {
  50. return NS_ERROR_ILLEGAL_VALUE;
  51. }
  52. txXPathNodeUtils::appendNodeValue(NodeSet()->get(aIndex), aResult);
  53. return NS_OK;
  54. }
  55. NS_IMETHODIMP
  56. txNodeSetAdaptor::GetLength(uint32_t *aLength)
  57. {
  58. *aLength = (uint32_t)NodeSet()->size();
  59. return NS_OK;
  60. }
  61. NS_IMETHODIMP
  62. txNodeSetAdaptor::Add(nsIDOMNode *aNode)
  63. {
  64. NS_ENSURE_TRUE(mWritable, NS_ERROR_FAILURE);
  65. nsAutoPtr<txXPathNode> node(txXPathNativeNode::createXPathNode(aNode,
  66. true));
  67. return node ? NodeSet()->add(*node) : NS_ERROR_OUT_OF_MEMORY;
  68. }