Back to index

lightning-sunbird  0.9+nobinonly
nsNodeInfo.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.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nscore.h"
00039 #include "nsNodeInfo.h"
00040 #include "nsNodeInfoManager.h"
00041 #include "nsCOMPtr.h"
00042 #include "nsString.h"
00043 #include "nsIAtom.h"
00044 #include "nsDOMString.h"
00045 #include "nsCRT.h"
00046 #include "nsContentUtils.h"
00047 #include "nsReadableUtils.h"
00048 
00049 // static
00050 nsNodeInfo*
00051 nsNodeInfo::Create()
00052 {
00053   if (sCachedNodeInfo) {
00054     // We have cached unused instances of this class, return a cached
00055     // instance instead of always creating a new one.
00056     nsNodeInfo *nodeInfo = sCachedNodeInfo;
00057     sCachedNodeInfo = nsnull;
00058     return nodeInfo;
00059   }
00060 
00061   // Create a new one
00062   return new nsNodeInfo();
00063 }
00064 
00065 nsNodeInfo::nsNodeInfo()
00066 {
00067 }
00068 
00069 
00070 nsNodeInfo::~nsNodeInfo()
00071 {
00072   Clear();
00073 }
00074 
00075 void
00076 nsNodeInfo::Clear()
00077 {
00078   if (mOwnerManager) {
00079     mOwnerManager->RemoveNodeInfo(this);
00080     NS_RELEASE(mOwnerManager);
00081   }
00082 
00083   NS_IF_RELEASE(mInner.mName);
00084   NS_IF_RELEASE(mInner.mPrefix);
00085 }
00086 
00087 
00088 nsresult
00089 nsNodeInfo::Init(nsIAtom *aName, nsIAtom *aPrefix, PRInt32 aNamespaceID,
00090                  nsNodeInfoManager *aOwnerManager)
00091 {
00092   NS_ENSURE_TRUE(!mInner.mName && !mInner.mPrefix && !mOwnerManager,
00093                  NS_ERROR_ALREADY_INITIALIZED);
00094   NS_ENSURE_ARG_POINTER(aName);
00095   NS_ENSURE_ARG_POINTER(aOwnerManager);
00096 
00097   mInner.mName = aName;
00098   NS_ADDREF(mInner.mName);
00099 
00100   mInner.mPrefix = aPrefix;
00101   NS_IF_ADDREF(mInner.mPrefix);
00102 
00103   mInner.mNamespaceID = aNamespaceID;
00104 
00105   mOwnerManager = aOwnerManager;
00106   NS_ADDREF(mOwnerManager);
00107 
00108   return NS_OK;
00109 }
00110 
00111 
00112 // nsISupports
00113 
00114 NS_IMPL_ADDREF(nsNodeInfo)
00115 NS_IMPL_RELEASE_WITH_DESTROY(nsNodeInfo, LastRelease())
00116 NS_IMPL_QUERY_INTERFACE1(nsNodeInfo, nsINodeInfo)
00117 
00118 // nsINodeInfo
00119 
00120 void
00121 nsNodeInfo::GetQualifiedName(nsAString& aQualifiedName) const
00122 {
00123   if (mInner.mPrefix) {
00124     mInner.mPrefix->ToString(aQualifiedName);
00125 
00126     aQualifiedName.Append(PRUnichar(':'));
00127   } else {
00128     aQualifiedName.Truncate();
00129   }
00130 
00131   nsAutoString name;
00132   mInner.mName->ToString(name);
00133 
00134   aQualifiedName.Append(name);
00135 }
00136 
00137 
00138 void
00139 nsNodeInfo::GetLocalName(nsAString& aLocalName) const
00140 {
00141 #ifdef STRICT_DOM_LEVEL2_LOCALNAME
00142   if (mInner.mNamespaceID > 0) {
00143     mInner.mName->ToString(aLocalName);
00144   } else {
00145     SetDOMStringToNull(aLocalName);
00146   }
00147 #else
00148   mInner.mName->ToString(aLocalName);
00149 #endif
00150 }
00151 
00152 
00153 nsresult
00154 nsNodeInfo::GetNamespaceURI(nsAString& aNameSpaceURI) const
00155 {
00156   nsresult rv = NS_OK;
00157 
00158   if (mInner.mNamespaceID > 0) {
00159     rv = nsContentUtils::GetNSManagerWeakRef()->GetNameSpaceURI(mInner.mNamespaceID,
00160                                                                 aNameSpaceURI);
00161   } else {
00162     SetDOMStringToNull(aNameSpaceURI);
00163   }
00164 
00165   return rv;
00166 }
00167 
00168 
00169 PRBool
00170 nsNodeInfo::Equals(const nsAString& aName) const
00171 {
00172   return mInner.mName->Equals(aName);
00173 }
00174 
00175 
00176 PRBool
00177 nsNodeInfo::Equals(const nsAString& aName, const nsAString& aPrefix) const
00178 {
00179   if (!mInner.mName->Equals(aName)) {
00180     return PR_FALSE;
00181   }
00182 
00183   if (!mInner.mPrefix) {
00184     return aPrefix.IsEmpty();
00185   }
00186 
00187   return mInner.mPrefix->Equals(aPrefix);
00188 }
00189 
00190 
00191 PRBool
00192 nsNodeInfo::Equals(const nsAString& aName, PRInt32 aNamespaceID) const
00193 {
00194   return mInner.mNamespaceID == aNamespaceID &&
00195     mInner.mName->Equals(aName);
00196 }
00197 
00198 
00199 PRBool
00200 nsNodeInfo::Equals(const nsAString& aName, const nsAString& aPrefix,
00201                    PRInt32 aNamespaceID) const
00202 {
00203   if (!mInner.mNamespaceID == aNamespaceID ||
00204       !mInner.mName->Equals(aName))
00205     return PR_FALSE;
00206 
00207   return mInner.mPrefix ? mInner.mPrefix->Equals(aPrefix) :
00208     aPrefix.IsEmpty();
00209 }
00210 
00211 
00212 PRBool
00213 nsNodeInfo::NamespaceEquals(const nsAString& aNamespaceURI) const
00214 {
00215   PRInt32 nsid;
00216   nsContentUtils::GetNSManagerWeakRef()->GetNameSpaceID(aNamespaceURI, &nsid);
00217 
00218   return nsINodeInfo::NamespaceEquals(nsid);
00219 }
00220 
00221 PRBool
00222 nsNodeInfo::QualifiedNameEquals(const nsACString& aQualifiedName) const
00223 {
00224   
00225   if (!mInner.mPrefix)
00226     return mInner.mName->EqualsUTF8(aQualifiedName);
00227 
00228   nsACString::const_iterator start;
00229   aQualifiedName.BeginReading(start);
00230 
00231   nsACString::const_iterator colon(start);
00232 
00233   const char* prefix;
00234   mInner.mPrefix->GetUTF8String(&prefix);
00235 
00236   PRUint32 len = strlen(prefix);
00237 
00238   if (len >= aQualifiedName.Length()) {
00239     return PR_FALSE;
00240   }
00241 
00242   colon.advance(len);
00243 
00244   // If the character at the prefix length index is not a colon,
00245   // aQualifiedName is not equal to this string.
00246   if (*colon != ':') {
00247     return PR_FALSE;
00248   }
00249 
00250   // Compare the prefix to the string from the start to the colon
00251   if (!mInner.mPrefix->EqualsUTF8(Substring(start, colon)))
00252     return PR_FALSE;
00253 
00254   ++colon; // Skip the ':'
00255 
00256   nsACString::const_iterator end;
00257   aQualifiedName.EndReading(end);
00258 
00259   // Compare the local name to the string between the colon and the
00260   // end of aQualifiedName
00261   return mInner.mName->EqualsUTF8(Substring(colon, end));
00262 }
00263 
00264 // static
00265 nsNodeInfo *nsNodeInfo::sCachedNodeInfo = nsnull;
00266 
00267 // static
00268 void
00269 nsNodeInfo::ClearCache()
00270 {
00271   // Clear our cache.
00272   delete sCachedNodeInfo;
00273   sCachedNodeInfo = nsnull;
00274 }
00275 
00276 void
00277 nsNodeInfo::LastRelease()
00278 {
00279   if (sCachedNodeInfo) {
00280     // No room in cache
00281     delete this;
00282     return;
00283   }
00284 
00285   // There's space in the cache for one instance. Put
00286   // this instance in the cache instead of deleting it.
00287   sCachedNodeInfo = this;
00288 
00289   // Clear object so that we have no references to anything external
00290   Clear();
00291 
00292   // The refcount balancing and destructor re-entrancy protection
00293   // code in Release() sets mRefCnt to 1 so we have to set it to 0
00294   // here to prevent leaks
00295   mRefCnt = 0;
00296 }