dom/xslt/base/txNamespaceMap.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/xslt/base/txNamespaceMap.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,98 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     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 +#include "txNamespaceMap.h"
    1.10 +#include "nsGkAtoms.h"
    1.11 +#include "txXPathNode.h"
    1.12 +
    1.13 +txNamespaceMap::txNamespaceMap()
    1.14 +{
    1.15 +}
    1.16 +
    1.17 +txNamespaceMap::txNamespaceMap(const txNamespaceMap& aOther)
    1.18 +    : mPrefixes(aOther.mPrefixes)
    1.19 +{
    1.20 +    mNamespaces = aOther.mNamespaces; //bah! I want a copy-constructor!
    1.21 +}
    1.22 +
    1.23 +nsresult
    1.24 +txNamespaceMap::mapNamespace(nsIAtom* aPrefix, const nsAString& aNamespaceURI)
    1.25 +{
    1.26 +    nsIAtom* prefix = aPrefix == nsGkAtoms::_empty ? nullptr : aPrefix;
    1.27 +
    1.28 +    int32_t nsId;
    1.29 +    if (prefix && aNamespaceURI.IsEmpty()) {
    1.30 +        // Remove the mapping
    1.31 +        int32_t index = mPrefixes.IndexOf(prefix);
    1.32 +        if (index >= 0) {
    1.33 +            mPrefixes.RemoveObjectAt(index);
    1.34 +            mNamespaces.RemoveElementAt(index);
    1.35 +        }
    1.36 +
    1.37 +        return NS_OK;
    1.38 +    }
    1.39 +
    1.40 +    if (aNamespaceURI.IsEmpty()) {
    1.41 +        // Set default to empty namespace
    1.42 +        nsId = kNameSpaceID_None;
    1.43 +    }
    1.44 +    else {
    1.45 +        nsId = txNamespaceManager::getNamespaceID(aNamespaceURI);
    1.46 +        NS_ENSURE_FALSE(nsId == kNameSpaceID_Unknown, NS_ERROR_FAILURE);
    1.47 +    }
    1.48 +
    1.49 +    // Check if the mapping already exists
    1.50 +    int32_t index = mPrefixes.IndexOf(prefix);
    1.51 +    if (index >= 0) {
    1.52 +        mNamespaces.ElementAt(index) = nsId;
    1.53 +
    1.54 +        return NS_OK;
    1.55 +    }
    1.56 +
    1.57 +    // New mapping
    1.58 +    if (!mPrefixes.AppendObject(prefix)) {
    1.59 +        return NS_ERROR_OUT_OF_MEMORY;
    1.60 +    }
    1.61 +    
    1.62 +    if (mNamespaces.AppendElement(nsId) == nullptr) {
    1.63 +        mPrefixes.RemoveObjectAt(mPrefixes.Count() - 1);
    1.64 +
    1.65 +        return NS_ERROR_OUT_OF_MEMORY;
    1.66 +    }
    1.67 +
    1.68 +    return NS_OK;
    1.69 +}
    1.70 +
    1.71 +int32_t
    1.72 +txNamespaceMap::lookupNamespace(nsIAtom* aPrefix)
    1.73 +{
    1.74 +    if (aPrefix == nsGkAtoms::xml) {
    1.75 +        return kNameSpaceID_XML;
    1.76 +    }
    1.77 +
    1.78 +    nsIAtom* prefix = aPrefix == nsGkAtoms::_empty ? 0 : aPrefix;
    1.79 +
    1.80 +    int32_t index = mPrefixes.IndexOf(prefix);
    1.81 +    if (index >= 0) {
    1.82 +        return mNamespaces.SafeElementAt(index, kNameSpaceID_Unknown);
    1.83 +    }
    1.84 +
    1.85 +    if (!prefix) {
    1.86 +        return kNameSpaceID_None;
    1.87 +    }
    1.88 +
    1.89 +    return kNameSpaceID_Unknown;
    1.90 +}
    1.91 +
    1.92 +int32_t
    1.93 +txNamespaceMap::lookupNamespaceWithDefault(const nsAString& aPrefix)
    1.94 +{
    1.95 +    nsCOMPtr<nsIAtom> prefix = do_GetAtom(aPrefix);
    1.96 +    if (prefix != nsGkAtoms::_poundDefault) {
    1.97 +        return lookupNamespace(prefix);
    1.98 +    }
    1.99 +
   1.100 +    return lookupNamespace(nullptr);
   1.101 +}

mercurial