Back to index

lightning-sunbird  0.9+nobinonly
nsParserNode.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 
00039 #include "nsIAtom.h"
00040 #include "nsParserNode.h" 
00041 #include <string.h>
00042 #include "nsHTMLTokens.h"
00043 #include "nsITokenizer.h"
00044 #include "nsDTDUtils.h"
00045 
00046 
00050 nsCParserNode::nsCParserNode()
00051   : mRefCnt(0), mGenericState(PR_FALSE), mUseCount(0), mToken(nsnull),
00052     mTokenAllocator(nsnull)
00053 {
00054   MOZ_COUNT_CTOR(nsCParserNode);
00055 #ifdef HEAP_ALLOCATED_NODES
00056   mNodeAllocator = nsnull;
00057 #endif
00058 }
00059 
00067 nsCParserNode::nsCParserNode(CToken* aToken,
00068                              nsTokenAllocator* aTokenAllocator,
00069                              nsNodeAllocator* aNodeAllocator)
00070   : mRefCnt(0), mGenericState(PR_FALSE), mUseCount(0), mToken(aToken),
00071     mTokenAllocator(aTokenAllocator)
00072 {
00073   MOZ_COUNT_CTOR(nsCParserNode);
00074 
00075   static int theNodeCount = 0;
00076   ++theNodeCount;
00077   IF_HOLD(mToken);
00078 
00079 #ifdef HEAP_ALLOCATED_NODES
00080   mNodeAllocator = aNodeAllocator;
00081 #endif
00082 }
00083 
00092 nsCParserNode::~nsCParserNode() {
00093   MOZ_COUNT_DTOR(nsCParserNode);
00094   ReleaseAll();
00095 #ifdef HEAP_ALLOCATED_NODES
00096   if(mNodeAllocator) {
00097     mNodeAllocator->Recycle(this);
00098   }
00099   mNodeAllocator = nsnull;
00100 #endif
00101   mTokenAllocator = 0;
00102 }
00103 
00104 
00113 nsresult
00114 nsCParserNode::Init(CToken* aToken,
00115                     nsTokenAllocator* aTokenAllocator,
00116                     nsNodeAllocator* aNodeAllocator) 
00117 {
00118   mTokenAllocator = aTokenAllocator;
00119   mToken = aToken;
00120   IF_HOLD(mToken);
00121   mGenericState = PR_FALSE;
00122   mUseCount=0;
00123 #ifdef HEAP_ALLOCATED_NODES
00124   mNodeAllocator = aNodeAllocator;
00125 #endif
00126   return NS_OK;
00127 }
00128 
00129 void
00130 nsCParserNode::AddAttribute(CToken* aToken) 
00131 {
00132 }
00133 
00134 
00142 const nsAString&
00143 nsCParserNode::GetTagName() const {
00144   return EmptyString();
00145 }
00146 
00147 
00156 const nsAString& 
00157 nsCParserNode::GetText() const 
00158 {
00159   if (mToken) {
00160     return mToken->GetStringValue();
00161   }
00162   return EmptyString();
00163 }
00164 
00173 PRInt32 
00174 nsCParserNode::GetNodeType(void) const
00175 {
00176   return (mToken) ? mToken->GetTypeID() : 0;
00177 }
00178 
00179 
00188 PRInt32 
00189 nsCParserNode::GetTokenType(void) const
00190 {
00191   return (mToken) ? mToken->GetTokenType() : 0;
00192 }
00193 
00194 
00202 PRInt32 
00203 nsCParserNode::GetAttributeCount(PRBool askToken) const
00204 {
00205   return 0;
00206 }
00207 
00216 const nsAString&
00217 nsCParserNode::GetKeyAt(PRUint32 anIndex) const 
00218 {
00219   return EmptyString();
00220 }
00221 
00222 
00230 const nsAString&
00231 nsCParserNode::GetValueAt(PRUint32 anIndex) const 
00232 {
00233   return EmptyString();
00234 }
00235 
00236 PRInt32 
00237 nsCParserNode::TranslateToUnicodeStr(nsString& aString) const
00238 {
00239   if (eToken_entity == mToken->GetTokenType()) {
00240     return ((CEntityToken*)mToken)->TranslateToUnicodeStr(aString);
00241   }
00242   return -1;
00243 }
00244 
00251 PRInt32
00252 nsCParserNode::GetSourceLineNumber(void) const {
00253   return mToken ? mToken->GetLineNumber() : 0;
00254 }
00255 
00262 CToken* 
00263 nsCParserNode::PopAttributeToken() {
00264   return 0;
00265 }
00266 
00267 CToken* 
00268 nsCParserNode::PopAttributeTokenFront() {
00269   return 0;
00270 }
00271 
00276 void 
00277 nsCParserNode::GetSource(nsString& aString) 
00278 {
00279   eHTMLTags theTag = mToken ? (eHTMLTags)mToken->GetTypeID() : eHTMLTag_unknown;
00280   aString.Assign(PRUnichar('<'));
00281   const PRUnichar* theTagName = nsHTMLTags::GetStringValue(theTag);
00282   if(theTagName) {
00283     aString.Append(theTagName);
00284   }
00285   aString.Append(PRUnichar('>'));
00286 }
00287 
00292 nsresult 
00293 nsCParserNode::ReleaseAll() 
00294 {
00295   if(mTokenAllocator) {
00296     IF_FREE(mToken,mTokenAllocator);
00297   }
00298   return NS_OK;
00299 }
00300 
00301 nsresult 
00302 nsCParserStartNode::Init(CToken* aToken,
00303                          nsTokenAllocator* aTokenAllocator,
00304                          nsNodeAllocator* aNodeAllocator) 
00305 {
00306   NS_ASSERTION(mAttributes.GetSize() == 0, "attributes not recycled!");
00307   return nsCParserNode::Init(aToken, aTokenAllocator, aNodeAllocator);
00308 }
00309 
00310 void nsCParserStartNode::AddAttribute(CToken* aToken) 
00311 {
00312   NS_ASSERTION(0 != aToken, "Error: Token shouldn't be null!");
00313   mAttributes.Push(aToken);
00314 }
00315 
00316 PRInt32 
00317 nsCParserStartNode::GetAttributeCount(PRBool askToken) const
00318 {
00319   PRInt32 result = 0;
00320   if (askToken) {
00321     result = mToken ? mToken->GetAttributeCount() : 0;
00322   }
00323   else {
00324     result = mAttributes.GetSize();
00325   }
00326   return result;
00327 }
00328 
00329 const nsAString&
00330 nsCParserStartNode::GetKeyAt(PRUint32 anIndex) const 
00331 {
00332   if ((PRInt32)anIndex < mAttributes.GetSize()) {
00333     CAttributeToken* attr = 
00334       NS_STATIC_CAST(CAttributeToken*, mAttributes.ObjectAt(anIndex));
00335     if (attr) {
00336       return attr->GetKey();
00337     }
00338   }
00339   return EmptyString();
00340 }
00341 
00342 const nsAString&
00343 nsCParserStartNode::GetValueAt(PRUint32 anIndex) const 
00344 {
00345   if (PRInt32(anIndex) < mAttributes.GetSize()) {
00346     CAttributeToken* attr = 
00347       NS_STATIC_CAST(CAttributeToken*, mAttributes.ObjectAt(anIndex));
00348     if (attr) {
00349       return attr->GetValue();
00350     }
00351   }
00352   return EmptyString();
00353 }
00354 
00355 CToken* 
00356 nsCParserStartNode::PopAttributeToken() 
00357 {
00358   return NS_STATIC_CAST(CToken*, mAttributes.Pop());
00359 }
00360 
00361 CToken* 
00362 nsCParserStartNode::PopAttributeTokenFront() 
00363 {
00364   return NS_STATIC_CAST(CToken*, mAttributes.PopFront());
00365 }
00366 
00367 void nsCParserStartNode::GetSource(nsString& aString) 
00368 {
00369   aString.Assign(PRUnichar('<'));
00370   const PRUnichar* theTagName = 
00371     nsHTMLTags::GetStringValue(nsHTMLTag(mToken->GetTypeID()));
00372   if (theTagName) {
00373     aString.Append(theTagName);
00374   }
00375   PRInt32 index;
00376   PRInt32 size = mAttributes.GetSize();
00377   for (index = 0 ; index < size; ++index) {
00378     CAttributeToken *theToken = 
00379       NS_STATIC_CAST(CAttributeToken*, mAttributes.ObjectAt(index));
00380     if (theToken) {
00381       theToken->AppendSourceTo(aString);
00382       aString.Append(PRUnichar(' ')); //this will get removed...
00383     }
00384   }
00385   aString.Append(PRUnichar('>'));
00386 }
00387 
00388 nsresult nsCParserStartNode::ReleaseAll() 
00389 {
00390   NS_ASSERTION(0!=mTokenAllocator, "Error: no token allocator");
00391   CToken* theAttrToken;
00392   while ((theAttrToken = NS_STATIC_CAST(CToken*, mAttributes.Pop()))) {
00393     IF_FREE(theAttrToken, mTokenAllocator);
00394   }
00395   nsCParserNode::ReleaseAll();
00396   return NS_OK; 
00397 }
00398