Back to index

lightning-sunbird  0.9+nobinonly
nsParserNode.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.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 
00055 #ifndef NS_PARSERNODE__
00056 #define NS_PARSERNODE__
00057 
00058 #include "nsIParserNode.h"
00059 #include "nsToken.h"
00060 #include "nsString.h"
00061 #include "nsParserCIID.h"
00062 #include "nsDeque.h"
00063 #include "nsDTDUtils.h"
00064 
00065 class nsTokenAllocator;
00066 
00067 class nsCParserNode :  public nsIParserNode {
00068 
00069   protected:
00070 
00071     PRInt32 mRefCnt;
00072 
00073   public:
00074 
00075     void AddRef()
00076     {
00077       ++mRefCnt;
00078     }
00079 
00080     void Release(nsFixedSizeAllocator& aPool)
00081     {
00082       if (--mRefCnt == 0)
00083         Destroy(this, aPool);
00084     }
00085 
00086 #ifndef HEAP_ALLOCATED_NODES
00087   protected:
00088 
00092     static void* operator new(size_t) CPP_THROW_NEW { return 0; }
00093 
00097     static void operator delete(void*,size_t) {}
00098 
00099 #endif
00100 
00101   public:
00102     static nsCParserNode* Create(CToken* aToken,
00103                                  nsTokenAllocator* aTokenAllocator,
00104                                  nsNodeAllocator* aNodeAllocator)
00105     {
00106 #ifdef HEAP_ALLOCATED_NODES
00107       return new
00108 #else
00109       nsFixedSizeAllocator& pool = aNodeAllocator->GetArenaPool();
00110       void* place = pool.Alloc(sizeof(nsCParserNode));
00111       return ::new (place)
00112 #endif
00113         nsCParserNode(aToken, aTokenAllocator, aNodeAllocator);
00114     }
00115 
00116     static void Destroy(nsCParserNode* aNode, nsFixedSizeAllocator& aPool)
00117     {
00118 #ifdef HEAP_ALLOCATED_NODES
00119       delete aNode;
00120 #else
00121       aNode->~nsCParserNode();
00122       aPool.Free(aNode, sizeof(*aNode));
00123 #endif
00124     }
00125 
00129     nsCParserNode();
00130 
00136     nsCParserNode(CToken* aToken,
00137                   nsTokenAllocator* aTokenAllocator,
00138                   nsNodeAllocator* aNodeAllocator=0);
00139 
00144     virtual ~nsCParserNode();
00145 
00150     virtual nsresult Init(CToken* aToken,
00151                           nsTokenAllocator* aTokenAllocator,
00152                           nsNodeAllocator* aNodeAllocator=0);
00153 
00159     virtual const nsAString& GetTagName() const;
00160 
00166     virtual const nsAString& GetText() const;
00167 
00173     virtual PRInt32 GetNodeType()  const;
00174 
00180     virtual PRInt32 GetTokenType()  const;
00181 
00182 
00183     //***************************************
00184     //methods for accessing key/value pairs
00185     //***************************************
00186 
00192     virtual PRInt32 GetAttributeCount(PRBool askToken=PR_FALSE) const;
00193 
00200     virtual const nsAString& GetKeyAt(PRUint32 anIndex) const;
00201 
00208     virtual const nsAString& GetValueAt(PRUint32 anIndex) const;
00209 
00217     virtual PRInt32 TranslateToUnicodeStr(nsString& aString) const;
00218 
00225     virtual void AddAttribute(CToken* aToken);
00226 
00233     virtual PRInt32 GetSourceLineNumber(void) const;
00234 
00239     virtual CToken* PopAttributeToken();
00240 
00242     virtual CToken* PopAttributeTokenFront();
00243 
00248     virtual void GetSource(nsString& aString);
00249 
00255     virtual PRBool  GetGenericState(void) const {return mGenericState;}
00256     virtual void    SetGenericState(PRBool aState) {mGenericState=aState;}
00257 
00262     virtual nsresult ReleaseAll();
00263 
00264     PRPackedBool mGenericState;  
00265     PRInt32      mUseCount;
00266     CToken*      mToken;
00267    
00268     nsTokenAllocator* mTokenAllocator;
00269 #ifdef HEAP_ALLOCATED_NODES
00270    nsNodeAllocator*  mNodeAllocator; // weak 
00271 #endif
00272 };
00273 
00274 
00275 class nsCParserStartNode :  public nsCParserNode 
00276 {
00277 public:
00278     static nsCParserNode* Create(CToken* aToken,
00279                                  nsTokenAllocator* aTokenAllocator,
00280                                  nsNodeAllocator* aNodeAllocator)
00281     {
00282 #ifdef HEAP_ALLOCATED_NODES
00283       return new
00284 #else
00285       nsFixedSizeAllocator& pool = aNodeAllocator->GetArenaPool();
00286       void* place = pool.Alloc(sizeof(nsCParserStartNode));
00287       return ::new (place)
00288 #endif
00289         nsCParserStartNode(aToken, aTokenAllocator, aNodeAllocator);
00290     }
00291 
00292     nsCParserStartNode() 
00293       : nsCParserNode(), mAttributes(0) { }
00294 
00295     nsCParserStartNode(CToken* aToken, 
00296                        nsTokenAllocator* aTokenAllocator, 
00297                        nsNodeAllocator* aNodeAllocator = 0) 
00298       : nsCParserNode(aToken, aTokenAllocator, aNodeAllocator), mAttributes(0) { }
00299 
00300     virtual ~nsCParserStartNode() 
00301     {
00302       NS_ASSERTION(mTokenAllocator || mAttributes.GetSize() == 0,
00303                    "Error: no token allocator");
00304       CToken* theAttrToken = 0;
00305       while ((theAttrToken = NS_STATIC_CAST(CToken*, mAttributes.Pop()))) {
00306         IF_FREE(theAttrToken, mTokenAllocator);
00307       }
00308     }
00309 
00310     virtual nsresult Init(CToken* aToken,
00311                           nsTokenAllocator* aTokenAllocator,
00312                           nsNodeAllocator* aNodeAllocator = 0);
00313     virtual void     AddAttribute(CToken* aToken);
00314     virtual PRInt32  GetAttributeCount(PRBool askToken = PR_FALSE) const;
00315     virtual const    nsAString& GetKeyAt(PRUint32 anIndex) const;
00316     virtual const    nsAString& GetValueAt(PRUint32 anIndex) const;
00317     virtual CToken*  PopAttributeToken();
00318     virtual CToken*  PopAttributeTokenFront();
00319     virtual void     GetSource(nsString& aString);
00320     virtual nsresult ReleaseAll();
00321 protected:
00322     nsDeque  mAttributes;
00323 };
00324 
00325 #endif
00326