michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "txNamespaceMap.h" michael@0: #include "nsGkAtoms.h" michael@0: #include "txXPathNode.h" michael@0: michael@0: txNamespaceMap::txNamespaceMap() michael@0: { michael@0: } michael@0: michael@0: txNamespaceMap::txNamespaceMap(const txNamespaceMap& aOther) michael@0: : mPrefixes(aOther.mPrefixes) michael@0: { michael@0: mNamespaces = aOther.mNamespaces; //bah! I want a copy-constructor! michael@0: } michael@0: michael@0: nsresult michael@0: txNamespaceMap::mapNamespace(nsIAtom* aPrefix, const nsAString& aNamespaceURI) michael@0: { michael@0: nsIAtom* prefix = aPrefix == nsGkAtoms::_empty ? nullptr : aPrefix; michael@0: michael@0: int32_t nsId; michael@0: if (prefix && aNamespaceURI.IsEmpty()) { michael@0: // Remove the mapping michael@0: int32_t index = mPrefixes.IndexOf(prefix); michael@0: if (index >= 0) { michael@0: mPrefixes.RemoveObjectAt(index); michael@0: mNamespaces.RemoveElementAt(index); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: if (aNamespaceURI.IsEmpty()) { michael@0: // Set default to empty namespace michael@0: nsId = kNameSpaceID_None; michael@0: } michael@0: else { michael@0: nsId = txNamespaceManager::getNamespaceID(aNamespaceURI); michael@0: NS_ENSURE_FALSE(nsId == kNameSpaceID_Unknown, NS_ERROR_FAILURE); michael@0: } michael@0: michael@0: // Check if the mapping already exists michael@0: int32_t index = mPrefixes.IndexOf(prefix); michael@0: if (index >= 0) { michael@0: mNamespaces.ElementAt(index) = nsId; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: // New mapping michael@0: if (!mPrefixes.AppendObject(prefix)) { michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: michael@0: if (mNamespaces.AppendElement(nsId) == nullptr) { michael@0: mPrefixes.RemoveObjectAt(mPrefixes.Count() - 1); michael@0: michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: int32_t michael@0: txNamespaceMap::lookupNamespace(nsIAtom* aPrefix) michael@0: { michael@0: if (aPrefix == nsGkAtoms::xml) { michael@0: return kNameSpaceID_XML; michael@0: } michael@0: michael@0: nsIAtom* prefix = aPrefix == nsGkAtoms::_empty ? 0 : aPrefix; michael@0: michael@0: int32_t index = mPrefixes.IndexOf(prefix); michael@0: if (index >= 0) { michael@0: return mNamespaces.SafeElementAt(index, kNameSpaceID_Unknown); michael@0: } michael@0: michael@0: if (!prefix) { michael@0: return kNameSpaceID_None; michael@0: } michael@0: michael@0: return kNameSpaceID_Unknown; michael@0: } michael@0: michael@0: int32_t michael@0: txNamespaceMap::lookupNamespaceWithDefault(const nsAString& aPrefix) michael@0: { michael@0: nsCOMPtr prefix = do_GetAtom(aPrefix); michael@0: if (prefix != nsGkAtoms::_poundDefault) { michael@0: return lookupNamespace(prefix); michael@0: } michael@0: michael@0: return lookupNamespace(nullptr); michael@0: }