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