content/base/src/nsXMLNameSpaceMap.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/base/src/nsXMLNameSpaceMap.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,108 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +/*
    1.10 + * A class for keeping track of prefix-to-namespace-id mappings
    1.11 + */
    1.12 +
    1.13 +#include "nsXMLNameSpaceMap.h"
    1.14 +#include "nsContentUtils.h"
    1.15 +#include "nsGkAtoms.h"
    1.16 +#include "nsNameSpaceManager.h"
    1.17 +
    1.18 +template <>
    1.19 +class nsDefaultComparator <nsNameSpaceEntry, nsIAtom*> {
    1.20 +  public:
    1.21 +    bool Equals(const nsNameSpaceEntry& aEntry, nsIAtom* const& aPrefix) const {
    1.22 +      return aEntry.prefix == aPrefix;
    1.23 +    }
    1.24 +};
    1.25 +
    1.26 +template <>
    1.27 +class nsDefaultComparator <nsNameSpaceEntry, int32_t> {
    1.28 +  public:
    1.29 +    bool Equals(const nsNameSpaceEntry& aEntry, const int32_t& aNameSpace) const {
    1.30 +      return aEntry.nameSpaceID == aNameSpace;
    1.31 +    }
    1.32 +};
    1.33 +
    1.34 +
    1.35 +/* static */ nsXMLNameSpaceMap*
    1.36 +nsXMLNameSpaceMap::Create(bool aForXML)
    1.37 +{
    1.38 +  nsXMLNameSpaceMap *map = new nsXMLNameSpaceMap();
    1.39 +  NS_ENSURE_TRUE(map, nullptr);
    1.40 +
    1.41 +  if (aForXML) {
    1.42 +    nsresult rv1 = map->AddPrefix(nsGkAtoms::xmlns,
    1.43 +                                  kNameSpaceID_XMLNS);
    1.44 +    nsresult rv2 = map->AddPrefix(nsGkAtoms::xml, kNameSpaceID_XML);
    1.45 +
    1.46 +    if (NS_FAILED(rv1) || NS_FAILED(rv2)) {
    1.47 +      delete map;
    1.48 +      map = nullptr;
    1.49 +    }
    1.50 +  }
    1.51 +
    1.52 +  return map;
    1.53 +}
    1.54 +
    1.55 +nsXMLNameSpaceMap::nsXMLNameSpaceMap()
    1.56 +  : mNameSpaces(4)
    1.57 +{
    1.58 +}
    1.59 +
    1.60 +nsresult
    1.61 +nsXMLNameSpaceMap::AddPrefix(nsIAtom *aPrefix, int32_t aNameSpaceID)
    1.62 +{
    1.63 +  if (!mNameSpaces.Contains(aPrefix) && !mNameSpaces.AppendElement(aPrefix)) {
    1.64 +    return NS_ERROR_OUT_OF_MEMORY;
    1.65 +  }
    1.66 +  mNameSpaces[mNameSpaces.IndexOf(aPrefix)].nameSpaceID = aNameSpaceID;
    1.67 +  return NS_OK;
    1.68 +}
    1.69 +
    1.70 +nsresult
    1.71 +nsXMLNameSpaceMap::AddPrefix(nsIAtom *aPrefix, nsString &aURI)
    1.72 +{
    1.73 +  int32_t id;
    1.74 +  nsresult rv = nsContentUtils::NameSpaceManager()->RegisterNameSpace(aURI,
    1.75 +                                                                      id);
    1.76 +
    1.77 +  NS_ENSURE_SUCCESS(rv, rv);
    1.78 +
    1.79 +  return AddPrefix(aPrefix, id);
    1.80 +}
    1.81 +
    1.82 +int32_t
    1.83 +nsXMLNameSpaceMap::FindNameSpaceID(nsIAtom *aPrefix) const
    1.84 +{
    1.85 +  uint32_t index = mNameSpaces.IndexOf(aPrefix);
    1.86 +  if (index != mNameSpaces.NoIndex) {
    1.87 +    return mNameSpaces[index].nameSpaceID;
    1.88 +  }
    1.89 +
    1.90 +  // The default mapping for no prefix is no namespace.  If a non-null prefix
    1.91 +  // was specified and we didn't find it, we return an error.
    1.92 +
    1.93 +  return aPrefix ? kNameSpaceID_Unknown : kNameSpaceID_None;
    1.94 +}
    1.95 +
    1.96 +nsIAtom*
    1.97 +nsXMLNameSpaceMap::FindPrefix(int32_t aNameSpaceID) const
    1.98 +{
    1.99 +  uint32_t index = mNameSpaces.IndexOf(aNameSpaceID);
   1.100 +  if (index != mNameSpaces.NoIndex) {
   1.101 +    return mNameSpaces[index].prefix;
   1.102 +  }
   1.103 +
   1.104 +  return nullptr;
   1.105 +}
   1.106 +
   1.107 +void
   1.108 +nsXMLNameSpaceMap::Clear()
   1.109 +{
   1.110 +  mNameSpaces.Clear();
   1.111 +}

mercurial