Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : *
3 : * This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #include "nsNameSpaceMap.h"
8 : #include "nsReadableUtils.h"
9 :
10 0 : nsNameSpaceMap::nsNameSpaceMap()
11 0 : : mEntries(nullptr)
12 : {
13 0 : MOZ_COUNT_CTOR(nsNameSpaceMap);
14 0 : }
15 :
16 0 : nsNameSpaceMap::~nsNameSpaceMap()
17 : {
18 0 : MOZ_COUNT_DTOR(nsNameSpaceMap);
19 :
20 0 : while (mEntries) {
21 0 : Entry* doomed = mEntries;
22 0 : mEntries = mEntries->mNext;
23 0 : delete doomed;
24 : }
25 0 : }
26 :
27 : nsresult
28 0 : nsNameSpaceMap::Put(const nsAString& aURI, nsIAtom* aPrefix)
29 : {
30 0 : nsCString uriUTF8;
31 0 : AppendUTF16toUTF8(aURI, uriUTF8);
32 0 : return Put(uriUTF8, aPrefix);
33 : }
34 :
35 : nsresult
36 0 : nsNameSpaceMap::Put(const nsACString& aURI, nsIAtom* aPrefix)
37 : {
38 : Entry* entry;
39 :
40 : // Make sure we're not adding a duplicate
41 0 : for (entry = mEntries; entry != nullptr; entry = entry->mNext) {
42 0 : if (entry->mURI == aURI || entry->mPrefix == aPrefix)
43 0 : return NS_ERROR_FAILURE;
44 : }
45 :
46 0 : entry = new Entry(aURI, aPrefix);
47 0 : if (! entry)
48 0 : return NS_ERROR_OUT_OF_MEMORY;
49 :
50 0 : entry->mNext = mEntries;
51 0 : mEntries = entry;
52 0 : return NS_OK;
53 : }
54 :
55 : nsNameSpaceMap::const_iterator
56 0 : nsNameSpaceMap::GetNameSpaceOf(const nsACString& aURI) const
57 : {
58 0 : for (Entry* entry = mEntries; entry != nullptr; entry = entry->mNext) {
59 0 : if (StringBeginsWith(aURI, entry->mURI))
60 0 : return const_iterator(entry);
61 : }
62 :
63 0 : return last();
64 : }
|