Back to index

lightning-sunbird  0.9+nobinonly
nsINodeInfo.h
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  * 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 /*
00039  * nsINodeInfo is an interface to node info, such as name, prefix, namespace
00040  * ID and possibly other data that is shared between nodes (elements
00041  * and attributes) that have the same name, prefix and namespace ID within
00042  * the same document.
00043  *
00044  * nsNodeInfoManager's are internal objects that manage a list of
00045  * nsINodeInfo's, every document object should hold a strong reference to
00046  * a nsNodeInfoManager and every nsINodeInfo also holds a strong reference
00047  * to their owning manager. When a nsINodeInfo is no longer used it will
00048  * automatically remove itself from its owner manager, and when all
00049  * nsINodeInfo's have been removed from a nsNodeInfoManager and all external
00050  * references are released the nsNodeInfoManager deletes itself.
00051  *
00052  * -- jst@netscape.com
00053  */
00054 
00055 #ifndef nsINodeInfo_h___
00056 #define nsINodeInfo_h___
00057 
00058 #include "nsISupports.h"
00059 #include "nsIAtom.h"
00060 #include "nsAString.h"
00061 #include "nsDOMString.h"
00062 #include "nsINameSpaceManager.h"
00063 #include "nsCOMPtr.h"
00064 
00065 // Forward declarations
00066 class nsIDocument;
00067 class nsIURI;
00068 class nsIPrincipal;
00069 class nsNodeInfoManager;
00070 
00071 // IID for the nsINodeInfo interface
00072 #define NS_INODEINFO_IID      \
00073 { 0x290ecd20, 0xb3cb, 0x11d8, \
00074   { 0xb2, 0x67, 0x00, 0x0a, 0x95, 0xdc, 0x23, 0x4c } }
00075 
00076 class nsINodeInfo : public nsISupports
00077 {
00078 public:
00079   NS_DEFINE_STATIC_IID_ACCESSOR(NS_INODEINFO_IID)
00080 
00081   nsINodeInfo()
00082     : mInner(nsnull, nsnull, kNameSpaceID_None),
00083       mOwnerManager(nsnull)
00084   {
00085   }
00086 
00087   /*
00088    * Get the name from this node as a string, this does not include the prefix.
00089    *
00090    * For the HTML element "<body>" this will return "body" and for the XML
00091    * element "<html:body>" this will return "body".
00092    */
00093   void GetName(nsAString& aName) const
00094   {
00095     mInner.mName->ToString(aName);
00096   }
00097 
00098   /*
00099    * Get the name from this node as an atom, this does not include the prefix.
00100    * This function never returns a null atom.
00101    *
00102    * For the HTML element "<body>" this will return the "body" atom and for
00103    * the XML element "<html:body>" this will return the "body" atom.
00104    */
00105   nsIAtom* NameAtom() const
00106   {
00107     return mInner.mName;
00108   }
00109 
00110   /*
00111    * Get the qualified name from this node as a string, the qualified name
00112    * includes the prefix, if one exists.
00113    *
00114    * For the HTML element "<body>" this will return "body" and for the XML
00115    * element "<html:body>" this will return "html:body".
00116    */
00117   virtual void GetQualifiedName(nsAString& aQualifiedName) const = 0;
00118 
00119   /*
00120    * Get the local name from this node as a string, GetLocalName() gets the
00121    * same string as GetName() but only if the node has a prefix and/or a
00122    * namespace URI. If the node has neither a prefix nor a namespace URI the
00123    * local name is a null string.
00124    *
00125    * For the HTML element "<body>" in a HTML document this will return a null
00126    * string and for the XML element "<html:body>" this will return "body".
00127    */
00128   virtual void GetLocalName(nsAString& aLocalName) const = 0;
00129 
00130   /*
00131    * Get the prefix from this node as a string.
00132    *
00133    * For the HTML element "<body>" this will return a null string and for
00134    * the XML element "<html:body>" this will return the string "html".
00135    */
00136   void GetPrefix(nsAString& aPrefix) const
00137   {
00138     if (mInner.mPrefix) {
00139       mInner.mPrefix->ToString(aPrefix);
00140     } else {
00141       SetDOMStringToNull(aPrefix);
00142     }
00143   }
00144 
00145   /*
00146    * Get the prefix from this node as an atom.
00147    *
00148    * For the HTML element "<body>" this will return a null atom and for
00149    * the XML element "<html:body>" this will return the "html" atom.
00150    */
00151   nsIAtom* GetPrefixAtom() const
00152   {
00153     return mInner.mPrefix;
00154   }
00155 
00156   /*
00157    * Get the namespace URI for a node, if the node has a namespace URI.
00158    *
00159    * For the HTML element "<body>" in a HTML document this will return a null
00160    * string and for the XML element "<html:body>" (assuming that this element,
00161    * or one of it's ancestors has an
00162    * xmlns:html='http://www.w3.org/1999/xhtml' attribute) this will return
00163    * the string "http://www.w3.org/1999/xhtml".
00164    */
00165   virtual nsresult GetNamespaceURI(nsAString& aNameSpaceURI) const = 0;
00166 
00167   /*
00168    * Get the namespace ID for a node if the node has a namespace, if not this
00169    * returns kNameSpaceID_None.
00170    *
00171    * For the HTML element "<body>" in a HTML document this will return
00172    * kNameSpaceID_None and for the XML element "<html:body>" (assuming that
00173    * this element, or one of it's ancestors has an
00174    * xmlns:html='http://www.w3.org/1999/xhtml' attribute) this will return
00175    * the namespace ID for "http://www.w3.org/1999/xhtml".
00176    */
00177   PRInt32 NamespaceID() const
00178   {
00179     return mInner.mNamespaceID;
00180   }
00181 
00182   /*
00183    * Get and set the ID attribute atom for this node.
00184    * See http://www.w3.org/TR/1998/REC-xml-19980210#sec-attribute-types
00185    * for the definition of an ID attribute.
00186    *
00187    */
00188   nsIAtom* GetIDAttributeAtom() const
00189   {
00190     return mIDAttributeAtom;
00191   }
00192 
00193   void SetIDAttributeAtom(nsIAtom* aID)
00194   {
00195     mIDAttributeAtom = aID;
00196   }
00197 
00202   nsNodeInfoManager *NodeInfoManager() const
00203   {
00204     return mOwnerManager;
00205   }
00206 
00207   /*
00208    * Utility functions that can be used to check if a nodeinfo holds a specific
00209    * name, name and prefix, name and prefix and namespace ID, or just
00210    * namespace ID.
00211    */
00212   PRBool Equals(nsINodeInfo *aNodeInfo) const
00213   {
00214     return aNodeInfo == this || aNodeInfo->Equals(mInner.mName, mInner.mPrefix,
00215                                                   mInner.mNamespaceID);
00216   }
00217 
00218   PRBool NameAndNamespaceEquals(nsINodeInfo *aNodeInfo) const
00219   {
00220     return aNodeInfo == this || aNodeInfo->Equals(mInner.mName,
00221                                                   mInner.mNamespaceID);
00222   }
00223 
00224   PRBool Equals(nsIAtom *aNameAtom) const
00225   {
00226     return mInner.mName == aNameAtom;
00227   }
00228 
00229   PRBool Equals(nsIAtom *aNameAtom, nsIAtom *aPrefixAtom) const
00230   {
00231     return (mInner.mName == aNameAtom) && (mInner.mPrefix == aPrefixAtom);
00232   }
00233 
00234   PRBool Equals(nsIAtom *aNameAtom, PRInt32 aNamespaceID) const
00235   {
00236     return ((mInner.mName == aNameAtom) &&
00237             (mInner.mNamespaceID == aNamespaceID));
00238   }
00239 
00240   PRBool Equals(nsIAtom *aNameAtom, nsIAtom *aPrefixAtom,
00241                 PRInt32 aNamespaceID) const
00242   {
00243     return ((mInner.mName == aNameAtom) &&
00244             (mInner.mPrefix == aPrefixAtom) &&
00245             (mInner.mNamespaceID == aNamespaceID));
00246   }
00247 
00248   PRBool NamespaceEquals(PRInt32 aNamespaceID) const
00249   {
00250     return mInner.mNamespaceID == aNamespaceID;
00251   }
00252 
00253   virtual PRBool Equals(const nsAString& aName) const = 0;
00254   virtual PRBool Equals(const nsAString& aName,
00255                         const nsAString& aPrefix) const = 0;
00256   virtual PRBool Equals(const nsAString& aName,
00257                         PRInt32 aNamespaceID) const = 0;
00258   virtual PRBool Equals(const nsAString& aName, const nsAString& aPrefix,
00259                         PRInt32 aNamespaceID) const = 0;
00260   virtual PRBool NamespaceEquals(const nsAString& aNamespaceURI) const = 0;
00261   // switch to UTF8 - this allows faster access for consumers
00262   virtual PRBool QualifiedNameEquals(const nsACString& aQualifiedName) const = 0;
00263 
00264   /*
00265    * Retrieve a pointer to the document that owns this node info.
00266    */
00267   virtual nsIDocument* GetDocument() const = 0;
00268 
00269   /*
00270    * Retrieve a pointer to the principal for the document of this node info.
00271    */
00272   virtual nsIPrincipal *GetDocumentPrincipal() const = 0;
00273 
00274 protected:
00275   /*
00276    * nsNodeInfoInner is used for two things:
00277    *
00278    *   1. as a member in nsNodeInfo for holding the name, prefix and
00279    *      namespace ID
00280    *   2. as the hash key in the hash table in nsNodeInfoManager
00281    *
00282    * nsNodeInfoInner does not do any kind of reference counting,
00283    * that's up to the user of this class. Since nsNodeInfoInner is
00284    * typically used as a member of nsNodeInfo, the hash table doesn't
00285    * need to delete the keys. When the value (nsNodeInfo) is deleted
00286    * the key is automatically deleted.
00287    */
00288 
00289   class nsNodeInfoInner
00290   {
00291   public:
00292     nsNodeInfoInner(nsIAtom *aName, nsIAtom *aPrefix, PRInt32 aNamespaceID)
00293       : mName(aName), mPrefix(aPrefix), mNamespaceID(aNamespaceID)
00294     {
00295     }
00296 
00297     nsIAtom*            mName;
00298     nsIAtom*            mPrefix;
00299     PRInt32             mNamespaceID;
00300   };
00301 
00302   // nsNodeInfoManager needs to pass mInner to the hash table.
00303   friend class nsNodeInfoManager;
00304 
00305   nsNodeInfoInner mInner;
00306 
00307   nsCOMPtr<nsIAtom> mIDAttributeAtom;
00308   nsNodeInfoManager* mOwnerManager; // Strong reference!
00309 };
00310 
00311 #endif /* nsINodeInfo_h___ */