2015-05-03 22:32:37 +03: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 15:12:37 +04: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/. */
|
2005-02-18 22:18:37 +03:00
|
|
|
|
2006-03-31 12:41:49 +04:00
|
|
|
/*
|
|
|
|
* A class for keeping track of prefix-to-namespace-id mappings
|
|
|
|
*/
|
|
|
|
|
2005-02-19 03:10:38 +03:00
|
|
|
#include "nsXMLNameSpaceMap.h"
|
2005-02-18 22:18:37 +03:00
|
|
|
#include "nsContentUtils.h"
|
2007-01-30 03:06:41 +03:00
|
|
|
#include "nsGkAtoms.h"
|
2014-02-28 03:04:46 +04:00
|
|
|
#include "nsNameSpaceManager.h"
|
2014-06-20 06:01:40 +04:00
|
|
|
#include "mozilla/dom/NameSpaceConstants.h"
|
2005-02-18 22:18:37 +03:00
|
|
|
|
2011-12-13 18:17:59 +04:00
|
|
|
template <>
|
2017-10-03 01:05:19 +03:00
|
|
|
class nsDefaultComparator <nsNameSpaceEntry, nsAtom*> {
|
2009-03-26 11:29:49 +03:00
|
|
|
public:
|
2017-10-03 01:05:19 +03:00
|
|
|
bool Equals(const nsNameSpaceEntry& aEntry, nsAtom* const& aPrefix) const {
|
2009-03-26 11:29:49 +03:00
|
|
|
return aEntry.prefix == aPrefix;
|
|
|
|
}
|
|
|
|
};
|
2005-02-18 22:18:37 +03:00
|
|
|
|
2011-12-13 18:17:59 +04:00
|
|
|
template <>
|
2012-08-22 19:56:38 +04:00
|
|
|
class nsDefaultComparator <nsNameSpaceEntry, int32_t> {
|
2009-03-26 11:29:49 +03:00
|
|
|
public:
|
2012-08-22 19:56:38 +04:00
|
|
|
bool Equals(const nsNameSpaceEntry& aEntry, const int32_t& aNameSpace) const {
|
2009-03-26 11:29:49 +03:00
|
|
|
return aEntry.nameSpaceID == aNameSpace;
|
|
|
|
}
|
2005-02-18 22:18:37 +03:00
|
|
|
};
|
|
|
|
|
2009-03-26 11:29:49 +03:00
|
|
|
|
2005-02-19 03:10:38 +03:00
|
|
|
/* static */ nsXMLNameSpaceMap*
|
2011-09-29 10:19:26 +04:00
|
|
|
nsXMLNameSpaceMap::Create(bool aForXML)
|
2005-02-18 22:18:37 +03:00
|
|
|
{
|
2005-02-19 03:10:38 +03:00
|
|
|
nsXMLNameSpaceMap *map = new nsXMLNameSpaceMap();
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ENSURE_TRUE(map, nullptr);
|
2005-02-18 22:18:37 +03:00
|
|
|
|
2009-10-30 04:47:48 +03:00
|
|
|
if (aForXML) {
|
2012-07-27 18:03:08 +04:00
|
|
|
nsresult rv1 = map->AddPrefix(nsGkAtoms::xmlns,
|
|
|
|
kNameSpaceID_XMLNS);
|
|
|
|
nsresult rv2 = map->AddPrefix(nsGkAtoms::xml, kNameSpaceID_XML);
|
2005-02-18 22:18:37 +03:00
|
|
|
|
2012-07-27 18:03:08 +04:00
|
|
|
if (NS_FAILED(rv1) || NS_FAILED(rv2)) {
|
2009-10-30 04:47:48 +03:00
|
|
|
delete map;
|
2012-07-30 18:20:58 +04:00
|
|
|
map = nullptr;
|
2009-10-30 04:47:48 +03:00
|
|
|
}
|
2005-02-18 22:18:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
2005-02-19 03:10:38 +03:00
|
|
|
nsXMLNameSpaceMap::nsXMLNameSpaceMap()
|
2005-02-18 22:18:37 +03:00
|
|
|
: mNameSpaces(4)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2017-10-03 01:05:19 +03:00
|
|
|
nsXMLNameSpaceMap::AddPrefix(nsAtom *aPrefix, int32_t aNameSpaceID)
|
2005-02-18 22:18:37 +03:00
|
|
|
{
|
2009-03-26 11:29:49 +03:00
|
|
|
if (!mNameSpaces.Contains(aPrefix) && !mNameSpaces.AppendElement(aPrefix)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2005-02-18 22:18:37 +03:00
|
|
|
}
|
2009-03-26 11:29:49 +03:00
|
|
|
mNameSpaces[mNameSpaces.IndexOf(aPrefix)].nameSpaceID = aNameSpaceID;
|
2005-02-18 22:18:37 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2017-10-03 01:05:19 +03:00
|
|
|
nsXMLNameSpaceMap::AddPrefix(nsAtom *aPrefix, nsString &aURI)
|
2005-02-18 22:18:37 +03:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t id;
|
2005-09-11 14:08:43 +04:00
|
|
|
nsresult rv = nsContentUtils::NameSpaceManager()->RegisterNameSpace(aURI,
|
|
|
|
id);
|
2005-02-18 22:18:37 +03:00
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return AddPrefix(aPrefix, id);
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t
|
2017-10-03 01:05:19 +03:00
|
|
|
nsXMLNameSpaceMap::FindNameSpaceID(nsAtom *aPrefix) const
|
2005-02-18 22:18:37 +03:00
|
|
|
{
|
2014-05-09 05:03:35 +04:00
|
|
|
size_t index = mNameSpaces.IndexOf(aPrefix);
|
2009-03-26 11:29:49 +03:00
|
|
|
if (index != mNameSpaces.NoIndex) {
|
|
|
|
return mNameSpaces[index].nameSpaceID;
|
2005-02-18 22:18:37 +03: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;
|
|
|
|
}
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom*
|
2012-08-22 19:56:38 +04:00
|
|
|
nsXMLNameSpaceMap::FindPrefix(int32_t aNameSpaceID) const
|
2005-02-18 22:18:37 +03:00
|
|
|
{
|
2014-05-09 05:03:35 +04:00
|
|
|
size_t index = mNameSpaces.IndexOf(aNameSpaceID);
|
2009-03-26 11:29:49 +03:00
|
|
|
if (index != mNameSpaces.NoIndex) {
|
|
|
|
return mNameSpaces[index].prefix;
|
2005-02-18 22:18:37 +03:00
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2005-02-18 22:18:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2005-02-19 03:10:38 +03:00
|
|
|
nsXMLNameSpaceMap::Clear()
|
2005-02-18 22:18:37 +03:00
|
|
|
{
|
2009-03-20 11:15:35 +03:00
|
|
|
mNameSpaces.Clear();
|
2005-02-18 22:18:37 +03:00
|
|
|
}
|