1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Mozilla Communicator client code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * IBM Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 2005
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Brian Ryner <bryner@brianryner.com>
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either of the GNU General Public License Version 2 or later (the "GPL"),
27 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : /*
40 : * A class for keeping track of prefix-to-namespace-id mappings
41 : */
42 :
43 : #include "nsXMLNameSpaceMap.h"
44 : #include "nsINameSpaceManager.h"
45 : #include "nsContentUtils.h"
46 : #include "nsGkAtoms.h"
47 :
48 : template <>
49 : class nsDefaultComparator <nsNameSpaceEntry, nsIAtom*> {
50 : public:
51 0 : bool Equals(const nsNameSpaceEntry& aEntry, nsIAtom* const& aPrefix) const {
52 0 : return aEntry.prefix == aPrefix;
53 : }
54 : };
55 :
56 : template <>
57 : class nsDefaultComparator <nsNameSpaceEntry, PRInt32> {
58 : public:
59 0 : bool Equals(const nsNameSpaceEntry& aEntry, const PRInt32& aNameSpace) const {
60 0 : return aEntry.nameSpaceID == aNameSpace;
61 : }
62 : };
63 :
64 :
65 : /* static */ nsXMLNameSpaceMap*
66 0 : nsXMLNameSpaceMap::Create(bool aForXML)
67 : {
68 0 : nsXMLNameSpaceMap *map = new nsXMLNameSpaceMap();
69 0 : NS_ENSURE_TRUE(map, nsnull);
70 :
71 0 : if (aForXML) {
72 : nsresult rv = map->AddPrefix(nsGkAtoms::xmlns,
73 0 : kNameSpaceID_XMLNS);
74 0 : rv |= map->AddPrefix(nsGkAtoms::xml, kNameSpaceID_XML);
75 :
76 0 : if (NS_FAILED(rv)) {
77 0 : delete map;
78 0 : map = nsnull;
79 : }
80 : }
81 :
82 0 : return map;
83 : }
84 :
85 0 : nsXMLNameSpaceMap::nsXMLNameSpaceMap()
86 0 : : mNameSpaces(4)
87 : {
88 0 : }
89 :
90 : nsresult
91 0 : nsXMLNameSpaceMap::AddPrefix(nsIAtom *aPrefix, PRInt32 aNameSpaceID)
92 : {
93 0 : if (!mNameSpaces.Contains(aPrefix) && !mNameSpaces.AppendElement(aPrefix)) {
94 0 : return NS_ERROR_OUT_OF_MEMORY;
95 : }
96 0 : mNameSpaces[mNameSpaces.IndexOf(aPrefix)].nameSpaceID = aNameSpaceID;
97 0 : return NS_OK;
98 : }
99 :
100 : nsresult
101 0 : nsXMLNameSpaceMap::AddPrefix(nsIAtom *aPrefix, nsString &aURI)
102 : {
103 : PRInt32 id;
104 0 : nsresult rv = nsContentUtils::NameSpaceManager()->RegisterNameSpace(aURI,
105 0 : id);
106 :
107 0 : NS_ENSURE_SUCCESS(rv, rv);
108 :
109 0 : return AddPrefix(aPrefix, id);
110 : }
111 :
112 : PRInt32
113 0 : nsXMLNameSpaceMap::FindNameSpaceID(nsIAtom *aPrefix) const
114 : {
115 0 : PRUint32 index = mNameSpaces.IndexOf(aPrefix);
116 0 : if (index != mNameSpaces.NoIndex) {
117 0 : return mNameSpaces[index].nameSpaceID;
118 : }
119 :
120 : // The default mapping for no prefix is no namespace. If a non-null prefix
121 : // was specified and we didn't find it, we return an error.
122 :
123 0 : return aPrefix ? kNameSpaceID_Unknown : kNameSpaceID_None;
124 : }
125 :
126 : nsIAtom*
127 0 : nsXMLNameSpaceMap::FindPrefix(PRInt32 aNameSpaceID) const
128 : {
129 0 : PRUint32 index = mNameSpaces.IndexOf(aNameSpaceID);
130 0 : if (index != mNameSpaces.NoIndex) {
131 0 : return mNameSpaces[index].prefix;
132 : }
133 :
134 0 : return nsnull;
135 : }
136 :
137 : void
138 0 : nsXMLNameSpaceMap::Clear()
139 : {
140 0 : mNameSpaces.Clear();
141 0 : }
|