Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/. */
5 :
6 : #include "txNamespaceMap.h"
7 : #include "nsGkAtoms.h"
8 : #include "txXPathNode.h"
9 :
10 0 : txNamespaceMap::txNamespaceMap()
11 : {
12 0 : }
13 :
14 0 : txNamespaceMap::txNamespaceMap(const txNamespaceMap& aOther)
15 0 : : mPrefixes(aOther.mPrefixes)
16 : {
17 0 : mNamespaces = aOther.mNamespaces; //bah! I want a copy-constructor!
18 0 : }
19 :
20 : nsresult
21 0 : txNamespaceMap::mapNamespace(nsIAtom* aPrefix, const nsAString& aNamespaceURI)
22 : {
23 0 : nsIAtom* prefix = aPrefix == nsGkAtoms::_empty ? nullptr : aPrefix;
24 :
25 : int32_t nsId;
26 0 : if (prefix && aNamespaceURI.IsEmpty()) {
27 : // Remove the mapping
28 0 : int32_t index = mPrefixes.IndexOf(prefix);
29 0 : if (index >= 0) {
30 0 : mPrefixes.RemoveObjectAt(index);
31 0 : mNamespaces.RemoveElementAt(index);
32 : }
33 :
34 0 : return NS_OK;
35 : }
36 :
37 0 : if (aNamespaceURI.IsEmpty()) {
38 : // Set default to empty namespace
39 0 : nsId = kNameSpaceID_None;
40 : }
41 : else {
42 0 : nsId = txNamespaceManager::getNamespaceID(aNamespaceURI);
43 0 : NS_ENSURE_FALSE(nsId == kNameSpaceID_Unknown, NS_ERROR_FAILURE);
44 : }
45 :
46 : // Check if the mapping already exists
47 0 : int32_t index = mPrefixes.IndexOf(prefix);
48 0 : if (index >= 0) {
49 0 : mNamespaces.ElementAt(index) = nsId;
50 :
51 0 : return NS_OK;
52 : }
53 :
54 : // New mapping
55 0 : if (!mPrefixes.AppendObject(prefix)) {
56 0 : return NS_ERROR_OUT_OF_MEMORY;
57 : }
58 :
59 0 : if (mNamespaces.AppendElement(nsId) == nullptr) {
60 0 : mPrefixes.RemoveObjectAt(mPrefixes.Count() - 1);
61 :
62 0 : return NS_ERROR_OUT_OF_MEMORY;
63 : }
64 :
65 0 : return NS_OK;
66 : }
67 :
68 : int32_t
69 0 : txNamespaceMap::lookupNamespace(nsIAtom* aPrefix)
70 : {
71 0 : if (aPrefix == nsGkAtoms::xml) {
72 0 : return kNameSpaceID_XML;
73 : }
74 :
75 0 : nsIAtom* prefix = aPrefix == nsGkAtoms::_empty ? 0 : aPrefix;
76 :
77 0 : int32_t index = mPrefixes.IndexOf(prefix);
78 0 : if (index >= 0) {
79 0 : return mNamespaces.SafeElementAt(index, kNameSpaceID_Unknown);
80 : }
81 :
82 0 : if (!prefix) {
83 0 : return kNameSpaceID_None;
84 : }
85 :
86 0 : return kNameSpaceID_Unknown;
87 : }
88 :
89 : int32_t
90 0 : txNamespaceMap::lookupNamespaceWithDefault(const nsAString& aPrefix)
91 : {
92 0 : nsCOMPtr<nsIAtom> prefix = NS_Atomize(aPrefix);
93 0 : if (prefix != nsGkAtoms::_poundDefault) {
94 0 : return lookupNamespace(prefix);
95 : }
96 :
97 0 : return lookupNamespace(nullptr);
98 : }
|