txNamespaceMap.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 "txNamespaceMap.h"
  6. #include "nsGkAtoms.h"
  7. #include "txXPathNode.h"
  8. txNamespaceMap::txNamespaceMap()
  9. {
  10. }
  11. txNamespaceMap::txNamespaceMap(const txNamespaceMap& aOther)
  12. : mPrefixes(aOther.mPrefixes)
  13. {
  14. mNamespaces = aOther.mNamespaces; //bah! I want a copy-constructor!
  15. }
  16. nsresult
  17. txNamespaceMap::mapNamespace(nsIAtom* aPrefix, const nsAString& aNamespaceURI)
  18. {
  19. nsIAtom* prefix = aPrefix == nsGkAtoms::_empty ? nullptr : aPrefix;
  20. int32_t nsId;
  21. if (prefix && aNamespaceURI.IsEmpty()) {
  22. // Remove the mapping
  23. int32_t index = mPrefixes.IndexOf(prefix);
  24. if (index >= 0) {
  25. mPrefixes.RemoveObjectAt(index);
  26. mNamespaces.RemoveElementAt(index);
  27. }
  28. return NS_OK;
  29. }
  30. if (aNamespaceURI.IsEmpty()) {
  31. // Set default to empty namespace
  32. nsId = kNameSpaceID_None;
  33. }
  34. else {
  35. nsId = txNamespaceManager::getNamespaceID(aNamespaceURI);
  36. NS_ENSURE_FALSE(nsId == kNameSpaceID_Unknown, NS_ERROR_FAILURE);
  37. }
  38. // Check if the mapping already exists
  39. int32_t index = mPrefixes.IndexOf(prefix);
  40. if (index >= 0) {
  41. mNamespaces.ElementAt(index) = nsId;
  42. return NS_OK;
  43. }
  44. // New mapping
  45. if (!mPrefixes.AppendObject(prefix)) {
  46. return NS_ERROR_OUT_OF_MEMORY;
  47. }
  48. if (mNamespaces.AppendElement(nsId) == nullptr) {
  49. mPrefixes.RemoveObjectAt(mPrefixes.Count() - 1);
  50. return NS_ERROR_OUT_OF_MEMORY;
  51. }
  52. return NS_OK;
  53. }
  54. int32_t
  55. txNamespaceMap::lookupNamespace(nsIAtom* aPrefix)
  56. {
  57. if (aPrefix == nsGkAtoms::xml) {
  58. return kNameSpaceID_XML;
  59. }
  60. nsIAtom* prefix = aPrefix == nsGkAtoms::_empty ? 0 : aPrefix;
  61. int32_t index = mPrefixes.IndexOf(prefix);
  62. if (index >= 0) {
  63. return mNamespaces.SafeElementAt(index, kNameSpaceID_Unknown);
  64. }
  65. if (!prefix) {
  66. return kNameSpaceID_None;
  67. }
  68. return kNameSpaceID_Unknown;
  69. }
  70. int32_t
  71. txNamespaceMap::lookupNamespaceWithDefault(const nsAString& aPrefix)
  72. {
  73. nsCOMPtr<nsIAtom> prefix = NS_Atomize(aPrefix);
  74. if (prefix != nsGkAtoms::_poundDefault) {
  75. return lookupNamespace(prefix);
  76. }
  77. return lookupNamespace(nullptr);
  78. }