/src/mozilla-central/dom/xslt/base/txNamespaceMap.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | txNamespaceMap::txNamespaceMap() |
11 | 0 | { |
12 | 0 | } |
13 | | |
14 | | txNamespaceMap::txNamespaceMap(const txNamespaceMap& aOther) |
15 | | : mPrefixes(aOther.mPrefixes) |
16 | 0 | { |
17 | 0 | mNamespaces = aOther.mNamespaces; //bah! I want a copy-constructor! |
18 | 0 | } |
19 | | |
20 | | nsresult |
21 | | txNamespaceMap::mapNamespace(nsAtom* aPrefix, const nsAString& aNamespaceURI) |
22 | 0 | { |
23 | 0 | nsAtom* prefix = aPrefix == nsGkAtoms::_empty ? nullptr : aPrefix; |
24 | 0 |
|
25 | 0 | int32_t nsId; |
26 | 0 | if (prefix && aNamespaceURI.IsEmpty()) { |
27 | 0 | // Remove the mapping |
28 | 0 | int32_t index = mPrefixes.IndexOf(prefix); |
29 | 0 | if (index >= 0) { |
30 | 0 | mPrefixes.RemoveElementAt(index); |
31 | 0 | mNamespaces.RemoveElementAt(index); |
32 | 0 | } |
33 | 0 |
|
34 | 0 | return NS_OK; |
35 | 0 | } |
36 | 0 |
|
37 | 0 | if (aNamespaceURI.IsEmpty()) { |
38 | 0 | // Set default to empty namespace |
39 | 0 | nsId = kNameSpaceID_None; |
40 | 0 | } |
41 | 0 | else { |
42 | 0 | nsId = txNamespaceManager::getNamespaceID(aNamespaceURI); |
43 | 0 | NS_ENSURE_FALSE(nsId == kNameSpaceID_Unknown, NS_ERROR_FAILURE); |
44 | 0 | } |
45 | 0 |
|
46 | 0 | // 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 | 0 |
|
51 | 0 | return NS_OK; |
52 | 0 | } |
53 | 0 | |
54 | 0 | // New mapping |
55 | 0 | if (!mPrefixes.AppendElement(prefix)) { |
56 | 0 | return NS_ERROR_OUT_OF_MEMORY; |
57 | 0 | } |
58 | 0 | |
59 | 0 | if (mNamespaces.AppendElement(nsId) == nullptr) { |
60 | 0 | mPrefixes.RemoveLastElement(); |
61 | 0 |
|
62 | 0 | return NS_ERROR_OUT_OF_MEMORY; |
63 | 0 | } |
64 | 0 | |
65 | 0 | return NS_OK; |
66 | 0 | } |
67 | | |
68 | | int32_t |
69 | | txNamespaceMap::lookupNamespace(nsAtom* aPrefix) |
70 | 0 | { |
71 | 0 | if (aPrefix == nsGkAtoms::xml) { |
72 | 0 | return kNameSpaceID_XML; |
73 | 0 | } |
74 | 0 |
|
75 | 0 | nsAtom* prefix = aPrefix == nsGkAtoms::_empty ? 0 : aPrefix; |
76 | 0 |
|
77 | 0 | int32_t index = mPrefixes.IndexOf(prefix); |
78 | 0 | if (index >= 0) { |
79 | 0 | return mNamespaces.SafeElementAt(index, kNameSpaceID_Unknown); |
80 | 0 | } |
81 | 0 |
|
82 | 0 | if (!prefix) { |
83 | 0 | return kNameSpaceID_None; |
84 | 0 | } |
85 | 0 | |
86 | 0 | return kNameSpaceID_Unknown; |
87 | 0 | } |
88 | | |
89 | | int32_t |
90 | | txNamespaceMap::lookupNamespaceWithDefault(const nsAString& aPrefix) |
91 | 0 | { |
92 | 0 | RefPtr<nsAtom> prefix = NS_Atomize(aPrefix); |
93 | 0 | if (prefix != nsGkAtoms::_poundDefault) { |
94 | 0 | return lookupNamespace(prefix); |
95 | 0 | } |
96 | 0 | |
97 | 0 | return lookupNamespace(nullptr); |
98 | 0 | } |