Back to index

lightning-sunbird  0.9+nobinonly
nsXMLNameSpaceMap.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * IBM Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2005
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *  Brian Ryner <bryner@brianryner.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsXMLNameSpaceMap.h"
00040 #include "nsIAtom.h"
00041 #include "nsCOMPtr.h"
00042 #include "nsINameSpaceManager.h"
00043 #include "nsContentUtils.h"
00044 #include "nsLayoutAtoms.h"
00045 
00046 struct nsNameSpaceEntry
00047 {
00048   nsNameSpaceEntry(nsIAtom *aPrefix)
00049     : prefix(aPrefix) {}
00050 
00051   nsCOMPtr<nsIAtom> prefix;
00052   PRInt32 nameSpaceID;
00053 };
00054 
00055 /* static */ nsXMLNameSpaceMap*
00056 nsXMLNameSpaceMap::Create()
00057 {
00058   nsXMLNameSpaceMap *map = new nsXMLNameSpaceMap();
00059   NS_ENSURE_TRUE(map, nsnull);
00060 
00061   nsresult rv = map->AddPrefix(nsLayoutAtoms::xmlnsNameSpace,
00062                                kNameSpaceID_XMLNS);
00063   rv |= map->AddPrefix(nsLayoutAtoms::xmlNameSpace, kNameSpaceID_XML);
00064 
00065   if (NS_FAILED(rv)) {
00066     delete map;
00067     map = nsnull;
00068   }
00069 
00070   return map;
00071 }
00072 
00073 nsXMLNameSpaceMap::nsXMLNameSpaceMap()
00074   : mNameSpaces(4)
00075 {
00076 }
00077 
00078 nsresult
00079 nsXMLNameSpaceMap::AddPrefix(nsIAtom *aPrefix, PRInt32 aNameSpaceID)
00080 {
00081   PRInt32 count = mNameSpaces.Count();
00082   nsNameSpaceEntry *foundEntry = nsnull;
00083 
00084   for (PRInt32 i = 0; i < count; ++i) {
00085     nsNameSpaceEntry *entry = NS_STATIC_CAST(nsNameSpaceEntry*,
00086                                              mNameSpaces.FastElementAt(i));
00087 
00088     NS_ASSERTION(entry, "null entry in namespace map!");
00089 
00090     if (entry->prefix == aPrefix) {
00091       foundEntry = entry;
00092       break;
00093     }
00094   }
00095 
00096   if (!foundEntry) {
00097     foundEntry = new nsNameSpaceEntry(aPrefix);
00098     NS_ENSURE_TRUE(foundEntry, NS_ERROR_OUT_OF_MEMORY);
00099 
00100     if (!mNameSpaces.AppendElement(foundEntry)) {
00101       delete foundEntry;
00102       return NS_ERROR_OUT_OF_MEMORY;
00103     }
00104   }
00105 
00106   foundEntry->nameSpaceID = aNameSpaceID;
00107   return NS_OK;
00108 }
00109 
00110 nsresult
00111 nsXMLNameSpaceMap::AddPrefix(nsIAtom *aPrefix, nsString &aURI)
00112 {
00113   PRInt32 id;
00114   nsresult rv = nsContentUtils::GetNSManagerWeakRef()->RegisterNameSpace(aURI,
00115                                                                          id);
00116 
00117   NS_ENSURE_SUCCESS(rv, rv);
00118 
00119   return AddPrefix(aPrefix, id);
00120 }
00121 
00122 void
00123 nsXMLNameSpaceMap::RemovePrefix(nsIAtom *aPrefix)
00124 {
00125   PRInt32 count = mNameSpaces.Count();
00126 
00127   for (PRInt32 i = 0; i < count; ++i) {
00128     nsNameSpaceEntry *entry = NS_STATIC_CAST(nsNameSpaceEntry*,
00129                                              mNameSpaces.FastElementAt(i));
00130 
00131     NS_ASSERTION(entry, "null entry in namespace map!");
00132 
00133     if (entry->prefix == aPrefix) {
00134       mNameSpaces.RemoveElementAt(i);
00135       return;
00136     }
00137   }
00138 }
00139 
00140 PRInt32
00141 nsXMLNameSpaceMap::FindNameSpaceID(nsIAtom *aPrefix) const
00142 {
00143   PRInt32 count = mNameSpaces.Count();
00144 
00145   for (PRInt32 i = 0; i < count; ++i) {
00146     nsNameSpaceEntry *entry = NS_STATIC_CAST(nsNameSpaceEntry*,
00147                                              mNameSpaces.FastElementAt(i));
00148 
00149     NS_ASSERTION(entry, "null entry in namespace map!");
00150 
00151     if (entry->prefix == aPrefix) {
00152       return entry->nameSpaceID;
00153     }
00154   }
00155 
00156   // The default mapping for no prefix is no namespace.  If a non-null prefix
00157   // was specified and we didn't find it, we return an error.
00158 
00159   return aPrefix ? kNameSpaceID_Unknown : kNameSpaceID_None;
00160 }
00161 
00162 nsIAtom*
00163 nsXMLNameSpaceMap::FindPrefix(PRInt32 aNameSpaceID) const
00164 {
00165   PRInt32 count = mNameSpaces.Count();
00166 
00167   for (PRInt32 i = 0; i < count; ++i) {
00168     nsNameSpaceEntry *entry = NS_STATIC_CAST(nsNameSpaceEntry*,
00169                                              mNameSpaces.FastElementAt(i));
00170 
00171     NS_ASSERTION(entry, "null entry in namespace map!");
00172 
00173     if (entry->nameSpaceID == aNameSpaceID) {
00174       return entry->prefix;
00175     }
00176   }
00177 
00178   return nsnull;
00179 }
00180 
00181 PR_STATIC_CALLBACK(PRBool) DeleteEntry(void *aElement, void *aData)
00182 {
00183   delete NS_STATIC_CAST(nsNameSpaceEntry*, aElement);
00184   return PR_TRUE;
00185 }
00186 
00187 void
00188 nsXMLNameSpaceMap::Clear()
00189 {
00190   mNameSpaces.EnumerateForwards(DeleteEntry, nsnull);
00191 }