1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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/. */
7 * A class for keeping track of prefix-to-namespace-id mappings
10 #include "nsXMLNameSpaceMap.h"
11 #include "nsContentUtils.h"
12 #include "nsGkAtoms.h"
13 #include "nsNameSpaceManager.h"
14 #include "mozilla/dom/NameSpaceConstants.h"
17 class nsDefaultComparator
<nsNameSpaceEntry
, nsIAtom
*> {
19 bool Equals(const nsNameSpaceEntry
& aEntry
, nsIAtom
* const& aPrefix
) const {
20 return aEntry
.prefix
== aPrefix
;
25 class nsDefaultComparator
<nsNameSpaceEntry
, int32_t> {
27 bool Equals(const nsNameSpaceEntry
& aEntry
, const int32_t& aNameSpace
) const {
28 return aEntry
.nameSpaceID
== aNameSpace
;
33 /* static */ nsXMLNameSpaceMap
*
34 nsXMLNameSpaceMap::Create(bool aForXML
)
36 nsXMLNameSpaceMap
*map
= new nsXMLNameSpaceMap();
37 NS_ENSURE_TRUE(map
, nullptr);
40 nsresult rv1
= map
->AddPrefix(nsGkAtoms::xmlns
,
42 nsresult rv2
= map
->AddPrefix(nsGkAtoms::xml
, kNameSpaceID_XML
);
44 if (NS_FAILED(rv1
) || NS_FAILED(rv2
)) {
53 nsXMLNameSpaceMap::nsXMLNameSpaceMap()
59 nsXMLNameSpaceMap::AddPrefix(nsIAtom
*aPrefix
, int32_t aNameSpaceID
)
61 if (!mNameSpaces
.Contains(aPrefix
) && !mNameSpaces
.AppendElement(aPrefix
)) {
62 return NS_ERROR_OUT_OF_MEMORY
;
64 mNameSpaces
[mNameSpaces
.IndexOf(aPrefix
)].nameSpaceID
= aNameSpaceID
;
69 nsXMLNameSpaceMap::AddPrefix(nsIAtom
*aPrefix
, nsString
&aURI
)
72 nsresult rv
= nsContentUtils::NameSpaceManager()->RegisterNameSpace(aURI
,
75 NS_ENSURE_SUCCESS(rv
, rv
);
77 return AddPrefix(aPrefix
, id
);
81 nsXMLNameSpaceMap::FindNameSpaceID(nsIAtom
*aPrefix
) const
83 size_t index
= mNameSpaces
.IndexOf(aPrefix
);
84 if (index
!= mNameSpaces
.NoIndex
) {
85 return mNameSpaces
[index
].nameSpaceID
;
88 // The default mapping for no prefix is no namespace. If a non-null prefix
89 // was specified and we didn't find it, we return an error.
91 return aPrefix
? kNameSpaceID_Unknown
: kNameSpaceID_None
;
95 nsXMLNameSpaceMap::FindPrefix(int32_t aNameSpaceID
) const
97 size_t index
= mNameSpaces
.IndexOf(aNameSpaceID
);
98 if (index
!= mNameSpaces
.NoIndex
) {
99 return mNameSpaces
[index
].prefix
;
106 nsXMLNameSpaceMap::Clear()