2015-05-03 12:32:37 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* A class for keeping track of prefix-to-namespace-id mappings
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsXMLNameSpaceMap.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsGkAtoms.h"
|
2014-02-27 15:04:46 -08:00
|
|
|
#include "nsNameSpaceManager.h"
|
2014-06-19 19:01:40 -07:00
|
|
|
#include "mozilla/dom/NameSpaceConstants.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-12-13 06:17:59 -08:00
|
|
|
template <>
|
2009-03-26 01:29:49 -07:00
|
|
|
class nsDefaultComparator <nsNameSpaceEntry, nsIAtom*> {
|
|
|
|
public:
|
2011-09-28 23:19:26 -07:00
|
|
|
bool Equals(const nsNameSpaceEntry& aEntry, nsIAtom* const& aPrefix) const {
|
2009-03-26 01:29:49 -07:00
|
|
|
return aEntry.prefix == aPrefix;
|
|
|
|
}
|
|
|
|
};
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-12-13 06:17:59 -08:00
|
|
|
template <>
|
2012-08-22 08:56:38 -07:00
|
|
|
class nsDefaultComparator <nsNameSpaceEntry, int32_t> {
|
2009-03-26 01:29:49 -07:00
|
|
|
public:
|
2012-08-22 08:56:38 -07:00
|
|
|
bool Equals(const nsNameSpaceEntry& aEntry, const int32_t& aNameSpace) const {
|
2009-03-26 01:29:49 -07:00
|
|
|
return aEntry.nameSpaceID == aNameSpace;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2009-03-26 01:29:49 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/* static */ nsXMLNameSpaceMap*
|
2011-09-28 23:19:26 -07:00
|
|
|
nsXMLNameSpaceMap::Create(bool aForXML)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsXMLNameSpaceMap *map = new nsXMLNameSpaceMap();
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ENSURE_TRUE(map, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-10-29 18:47:48 -07:00
|
|
|
if (aForXML) {
|
2012-07-27 07:03:08 -07:00
|
|
|
nsresult rv1 = map->AddPrefix(nsGkAtoms::xmlns,
|
|
|
|
kNameSpaceID_XMLNS);
|
|
|
|
nsresult rv2 = map->AddPrefix(nsGkAtoms::xml, kNameSpaceID_XML);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-27 07:03:08 -07:00
|
|
|
if (NS_FAILED(rv1) || NS_FAILED(rv2)) {
|
2009-10-29 18:47:48 -07:00
|
|
|
delete map;
|
2012-07-30 07:20:58 -07:00
|
|
|
map = nullptr;
|
2009-10-29 18:47:48 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsXMLNameSpaceMap::nsXMLNameSpaceMap()
|
|
|
|
: mNameSpaces(4)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsXMLNameSpaceMap::AddPrefix(nsIAtom *aPrefix, int32_t aNameSpaceID)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-03-26 01:29:49 -07:00
|
|
|
if (!mNameSpaces.Contains(aPrefix) && !mNameSpaces.AppendElement(aPrefix)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-03-26 01:29:49 -07:00
|
|
|
mNameSpaces[mNameSpaces.IndexOf(aPrefix)].nameSpaceID = aNameSpaceID;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsXMLNameSpaceMap::AddPrefix(nsIAtom *aPrefix, nsString &aURI)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t id;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv = nsContentUtils::NameSpaceManager()->RegisterNameSpace(aURI,
|
|
|
|
id);
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return AddPrefix(aPrefix, id);
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t
|
2007-03-22 10:30:00 -07:00
|
|
|
nsXMLNameSpaceMap::FindNameSpaceID(nsIAtom *aPrefix) const
|
|
|
|
{
|
2014-05-08 18:03:35 -07:00
|
|
|
size_t index = mNameSpaces.IndexOf(aPrefix);
|
2009-03-26 01:29:49 -07:00
|
|
|
if (index != mNameSpaces.NoIndex) {
|
|
|
|
return mNameSpaces[index].nameSpaceID;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// The default mapping for no prefix is no namespace. If a non-null prefix
|
|
|
|
// was specified and we didn't find it, we return an error.
|
|
|
|
|
|
|
|
return aPrefix ? kNameSpaceID_Unknown : kNameSpaceID_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIAtom*
|
2012-08-22 08:56:38 -07:00
|
|
|
nsXMLNameSpaceMap::FindPrefix(int32_t aNameSpaceID) const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-05-08 18:03:35 -07:00
|
|
|
size_t index = mNameSpaces.IndexOf(aNameSpaceID);
|
2009-03-26 01:29:49 -07:00
|
|
|
if (index != mNameSpaces.NoIndex) {
|
|
|
|
return mNameSpaces[index].prefix;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsXMLNameSpaceMap::Clear()
|
|
|
|
{
|
2009-03-20 01:15:35 -07:00
|
|
|
mNameSpaces.Clear();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|