Thu, 15 Jan 2015 21:03:48 +0100
Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | /* |
michael@0 | 7 | * A class for keeping track of prefix-to-namespace-id mappings |
michael@0 | 8 | */ |
michael@0 | 9 | |
michael@0 | 10 | #include "nsXMLNameSpaceMap.h" |
michael@0 | 11 | #include "nsContentUtils.h" |
michael@0 | 12 | #include "nsGkAtoms.h" |
michael@0 | 13 | #include "nsNameSpaceManager.h" |
michael@0 | 14 | |
michael@0 | 15 | template <> |
michael@0 | 16 | class nsDefaultComparator <nsNameSpaceEntry, nsIAtom*> { |
michael@0 | 17 | public: |
michael@0 | 18 | bool Equals(const nsNameSpaceEntry& aEntry, nsIAtom* const& aPrefix) const { |
michael@0 | 19 | return aEntry.prefix == aPrefix; |
michael@0 | 20 | } |
michael@0 | 21 | }; |
michael@0 | 22 | |
michael@0 | 23 | template <> |
michael@0 | 24 | class nsDefaultComparator <nsNameSpaceEntry, int32_t> { |
michael@0 | 25 | public: |
michael@0 | 26 | bool Equals(const nsNameSpaceEntry& aEntry, const int32_t& aNameSpace) const { |
michael@0 | 27 | return aEntry.nameSpaceID == aNameSpace; |
michael@0 | 28 | } |
michael@0 | 29 | }; |
michael@0 | 30 | |
michael@0 | 31 | |
michael@0 | 32 | /* static */ nsXMLNameSpaceMap* |
michael@0 | 33 | nsXMLNameSpaceMap::Create(bool aForXML) |
michael@0 | 34 | { |
michael@0 | 35 | nsXMLNameSpaceMap *map = new nsXMLNameSpaceMap(); |
michael@0 | 36 | NS_ENSURE_TRUE(map, nullptr); |
michael@0 | 37 | |
michael@0 | 38 | if (aForXML) { |
michael@0 | 39 | nsresult rv1 = map->AddPrefix(nsGkAtoms::xmlns, |
michael@0 | 40 | kNameSpaceID_XMLNS); |
michael@0 | 41 | nsresult rv2 = map->AddPrefix(nsGkAtoms::xml, kNameSpaceID_XML); |
michael@0 | 42 | |
michael@0 | 43 | if (NS_FAILED(rv1) || NS_FAILED(rv2)) { |
michael@0 | 44 | delete map; |
michael@0 | 45 | map = nullptr; |
michael@0 | 46 | } |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | return map; |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | nsXMLNameSpaceMap::nsXMLNameSpaceMap() |
michael@0 | 53 | : mNameSpaces(4) |
michael@0 | 54 | { |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | nsresult |
michael@0 | 58 | nsXMLNameSpaceMap::AddPrefix(nsIAtom *aPrefix, int32_t aNameSpaceID) |
michael@0 | 59 | { |
michael@0 | 60 | if (!mNameSpaces.Contains(aPrefix) && !mNameSpaces.AppendElement(aPrefix)) { |
michael@0 | 61 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 62 | } |
michael@0 | 63 | mNameSpaces[mNameSpaces.IndexOf(aPrefix)].nameSpaceID = aNameSpaceID; |
michael@0 | 64 | return NS_OK; |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | nsresult |
michael@0 | 68 | nsXMLNameSpaceMap::AddPrefix(nsIAtom *aPrefix, nsString &aURI) |
michael@0 | 69 | { |
michael@0 | 70 | int32_t id; |
michael@0 | 71 | nsresult rv = nsContentUtils::NameSpaceManager()->RegisterNameSpace(aURI, |
michael@0 | 72 | id); |
michael@0 | 73 | |
michael@0 | 74 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 75 | |
michael@0 | 76 | return AddPrefix(aPrefix, id); |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | int32_t |
michael@0 | 80 | nsXMLNameSpaceMap::FindNameSpaceID(nsIAtom *aPrefix) const |
michael@0 | 81 | { |
michael@0 | 82 | uint32_t index = mNameSpaces.IndexOf(aPrefix); |
michael@0 | 83 | if (index != mNameSpaces.NoIndex) { |
michael@0 | 84 | return mNameSpaces[index].nameSpaceID; |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | // The default mapping for no prefix is no namespace. If a non-null prefix |
michael@0 | 88 | // was specified and we didn't find it, we return an error. |
michael@0 | 89 | |
michael@0 | 90 | return aPrefix ? kNameSpaceID_Unknown : kNameSpaceID_None; |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | nsIAtom* |
michael@0 | 94 | nsXMLNameSpaceMap::FindPrefix(int32_t aNameSpaceID) const |
michael@0 | 95 | { |
michael@0 | 96 | uint32_t index = mNameSpaces.IndexOf(aNameSpaceID); |
michael@0 | 97 | if (index != mNameSpaces.NoIndex) { |
michael@0 | 98 | return mNameSpaces[index].prefix; |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | return nullptr; |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | void |
michael@0 | 105 | nsXMLNameSpaceMap::Clear() |
michael@0 | 106 | { |
michael@0 | 107 | mNameSpaces.Clear(); |
michael@0 | 108 | } |