Back to index

lightning-sunbird  0.9+nobinonly
nsToken.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 
00068 #ifndef CTOKEN__
00069 #define CTOKEN__
00070 
00071 #include "prtypes.h"
00072 #include "nsString.h"
00073 #include "nsError.h"
00074 #include "nsFixedSizeAllocator.h"
00075 
00076 #define NS_HTMLTOKENS_NOT_AN_ENTITY \
00077   NS_ERROR_GENERATE_SUCCESS(NS_ERROR_MODULE_HTMLPARSER,2000)
00078 
00079 class nsScanner;
00080 class nsTokenAllocator;
00081 
00082 enum eContainerInfo {
00083   eWellFormed,
00084   eMalformed,
00085   eFormUnknown
00086 };
00087 
00092 #define CTOKEN_IMPL_SIZEOF                                \
00093 protected:                                                \
00094   virtual size_t SizeOf() const { return sizeof(*this); } \
00095 public:
00096 
00106 class CToken {
00107   public:
00108 
00109     enum  eTokenOrigin {eSource,eResidualStyle};
00110 
00111   protected:
00112 
00113     // nsTokenAllocator should be the only class that tries to
00114     // allocate tokens from the heap.
00115     friend class nsTokenAllocator;
00116 
00123     static void * operator new (size_t aSize,nsFixedSizeAllocator& anArena) CPP_THROW_NEW
00124     {
00125       return anArena.Alloc(aSize);
00126     }
00127 
00131     static void operator delete (void*,size_t) {}
00132 
00133   public:
00138     virtual ~CToken();
00139 
00143     static void Destroy(CToken* aToken,nsFixedSizeAllocator& aArenaPool)
00144     {
00145       size_t sz = aToken->SizeOf();
00146       aToken->~CToken();
00147       aArenaPool.Free(aToken, sz);
00148     }
00149 
00154     void AddRef() { ++mUseCount; }
00155     
00160     void Release(nsFixedSizeAllocator& aArenaPool) {
00161       if(--mUseCount==0)
00162         Destroy(this, aArenaPool);
00163     }
00164 
00169     CToken(PRInt32 aTag=0);
00170 
00176     virtual const nsSubstring& GetStringValue(void) = 0;
00177 
00184     virtual void GetSource(nsString& anOutputString);
00185 
00189     virtual void AppendSourceTo(nsAString& anOutputString);
00190 
00196     void SetTypeID(PRInt32 aValue) {
00197       mTypeID = aValue;
00198     }
00199     
00205     virtual PRInt32 GetTypeID(void);
00206 
00212     virtual PRInt16 GetAttributeCount(void);
00213 
00222     virtual nsresult Consume(PRUnichar aChar,nsScanner& aScanner,PRInt32 aMode);
00223 
00229     virtual PRInt32 GetTokenType(void);
00230 
00238     virtual PRBool IsWellFormed(void) const {return PR_FALSE;}
00239 
00240     virtual PRBool IsEmpty(void) { return PR_FALSE; }
00241     
00245     virtual void SetEmpty(PRBool aValue) { return ; }
00246 
00247     PRInt32 GetNewlineCount() 
00248     { 
00249       return mNewlineCount; 
00250     }
00251 
00252     void SetNewlineCount(PRInt32 aCount)
00253     {
00254       mNewlineCount = aCount;
00255     }
00256 
00257     PRInt32 GetLineNumber() 
00258     { 
00259       return mLineNumber;
00260     }
00261 
00262     void SetLineNumber(PRInt32 aLineNumber) 
00263     { 
00264       mLineNumber = mLineNumber == 0 ? aLineNumber : mLineNumber;
00265     }
00266 
00267     void SetInError(PRBool aInError)
00268     {
00269       mInError = aInError;
00270     }
00271 
00272     PRBool IsInError()
00273     {
00274       return mInError;
00275     }
00276 
00277     void SetAttributeCount(PRInt16 aValue) {  mAttrCount = aValue; }
00278 
00283     virtual void SelfTest(void);
00284 
00285     static int GetTokenCount();
00286 
00287     
00288 
00289 protected:
00293     virtual size_t SizeOf() const = 0;
00294 
00295     PRInt32 mTypeID;
00296     PRInt32 mUseCount;
00297     PRInt32 mNewlineCount;
00298     PRUint32 mLineNumber : 31;
00299     PRUint32 mInError : 1;
00300     PRInt16 mAttrCount;
00301 };
00302 
00303 
00304 
00305 #endif
00306 
00307