bug 315927: Clean up nsHTMLTokenizer to not have so many virtual methods. Also clean up CParserContext::GetTokenizer. r+sr=jst

This commit is contained in:
mrbkap%gmail.com 2005-11-11 19:11:36 +00:00
Родитель c3f37e594d
Коммит 804d3df1a0
6 изменённых файлов: 69 добавлений и 194 удалений

Просмотреть файл

@ -1,4 +1,5 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 sw=2 et tw=80: */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
@ -68,12 +69,10 @@ CParserContext::CParserContext(nsScanner* aScanner,
mKey=aKey;
mPrevContext=0;
mListener=aListener;
NS_IF_ADDREF(mListener);
mDTDMode=eDTDMode_unknown;
mAutoDetectStatus=aStatus;
mTransferBuffer=0;
mDTD=aDTD;
NS_IF_ADDREF(mDTD);
mTokenizer = 0;
mTransferBufferSize=eTransferBufferSize;
mStreamListenerState=eNone;
@ -99,16 +98,13 @@ CParserContext::CParserContext(const CParserContext &aContext) : mMimeType() {
mKey=aContext.mKey;
mPrevContext=0;
mListener=aContext.mListener;
NS_IF_ADDREF(mListener);
mDTDMode=aContext.mDTDMode;
mAutoDetectStatus=aContext.mAutoDetectStatus;
mTransferBuffer=aContext.mTransferBuffer;
mDTD=aContext.mDTD;
NS_IF_ADDREF(mDTD);
mTokenizer = aContext.mTokenizer;
NS_IF_ADDREF(mTokenizer);
mTransferBufferSize=eTransferBufferSize;
mStreamListenerState=aContext.mStreamListenerState;
@ -122,27 +118,19 @@ CParserContext::CParserContext(const CParserContext &aContext) : mMimeType() {
/**
* Destructor for parser context
* NOTE: DO NOT destroy the dtd here.
* @update gess7/11/98
*/
CParserContext::~CParserContext(){
CParserContext::~CParserContext()
{
MOZ_COUNT_DTOR(CParserContext);
if(mScanner) {
delete mScanner;
mScanner=nsnull;
}
if(mTransferBuffer)
if(mTransferBuffer) {
delete [] mTransferBuffer;
NS_IF_RELEASE(mDTD);
NS_IF_RELEASE(mListener);
NS_IF_RELEASE(mTokenizer);
}
//Remember that it's ok to simply ingore the PrevContext.
}
@ -172,7 +160,8 @@ void CParserContext::SetMimeType(const nsACString& aMimeType){
nsresult
CParserContext::GetTokenizer(PRInt32 aType,
nsIContentSink* aSink,
nsITokenizer*& aTokenizer) {
nsITokenizer*& aTokenizer)
{
nsresult result = NS_OK;
if(!mTokenizer) {
@ -195,17 +184,21 @@ CParserContext::GetTokenizer(PRInt32 aType,
}
}
result = NS_NewHTMLTokenizer(&mTokenizer,mDTDMode,mDocType,
mParserCommand,theFlags);
mTokenizer = new nsHTMLTokenizer(mDTDMode, mDocType,
mParserCommand, theFlags);
if (!mTokenizer) {
return NS_ERROR_OUT_OF_MEMORY;
}
// Make sure the new tokenizer has all of the necessary information.
// XXX this might not be necessary.
if (mTokenizer && mPrevContext) {
if (mPrevContext) {
mTokenizer->CopyState(mPrevContext->mTokenizer);
}
}
else if (aType == NS_IPARSER_FLAG_XML)
{
result = CallQueryInterface(mDTD, &mTokenizer);
mTokenizer = do_QueryInterface(mDTD, &result);
}
}

Просмотреть файл

@ -83,11 +83,11 @@ public:
nsCOMPtr<nsIRequest> mRequest; // provided by necko to differnciate different input streams
// why is mRequest strongly referenced? see bug 102376.
nsIDTD* mDTD;
nsIRequestObserver* mListener;
nsCOMPtr<nsIDTD> mDTD;
nsCOMPtr<nsIRequestObserver> mListener;
char* mTransferBuffer;
void* mKey;
nsITokenizer* mTokenizer;
nsCOMPtr<nsITokenizer> mTokenizer;
CParserContext* mPrevContext;
nsScanner* mScanner;
@ -105,8 +105,4 @@ public:
PRUint32 mTransferBufferSize;
};
#endif

Просмотреть файл

@ -59,74 +59,10 @@
And now for the main class -- nsHTMLTokenizer...
************************************************************************/
static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
static NS_DEFINE_IID(kITokenizerIID, NS_ITOKENIZER_IID);
static NS_DEFINE_IID(kClassIID, NS_HTMLTOKENIZER_IID);
/**
* This method gets called as part of our COM-like interfaces.
* Its purpose is to create an interface to parser object
* of some type.
*
* @param aIID id of object to discover
* @param aInstancePtr ptr to newly discovered interface
* @return NS_xxx result code
* Satisfy the nsISupports interface.
*/
nsresult nsHTMLTokenizer::QueryInterface(const nsIID& aIID, void** aInstancePtr)
{
if (NULL == aInstancePtr) {
return NS_ERROR_NULL_POINTER;
}
if(aIID.Equals(kISupportsIID)) { // Do IUnknown...
*aInstancePtr = (nsISupports*)(this);
}
else if(aIID.Equals(kITokenizerIID)) { // Do ITokenizer base class...
*aInstancePtr = (nsITokenizer*)(this);
}
else if(aIID.Equals(kClassIID)) { // Do this class...
*aInstancePtr = (nsHTMLTokenizer*)(this);
}
else {
*aInstancePtr=0;
return NS_NOINTERFACE;
}
NS_ADDREF_THIS();
return NS_OK;
}
/**
* This method is defined in nsHTMLTokenizer.h. It is used to
* cause the COM-like construction of an HTMLTokenizer.
*
* @param aInstancePtrResult** ptr to newly instantiated parser
* @param aFlag Parser flags the tokenizer should be aware of
* @param aDocType The doctype of the current document.
* @param aCommand The current command (view-source, fragment, etc).
* @return NS_xxx error result
*/
nsresult NS_NewHTMLTokenizer(nsITokenizer** aInstancePtrResult,
PRInt32 aFlag,
eParserDocType aDocType,
eParserCommands aCommand,
PRInt32 aFlags)
{
NS_PRECONDITION(nsnull != aInstancePtrResult, "null ptr");
if (nsnull == aInstancePtrResult) {
return NS_ERROR_NULL_POINTER;
}
nsHTMLTokenizer* it = new nsHTMLTokenizer(aFlag,aDocType,aCommand,aFlags);
if (nsnull == it) {
return NS_ERROR_OUT_OF_MEMORY;
}
return it->QueryInterface(kClassIID, (void **) aInstancePtrResult);
}
NS_IMPL_ADDREF(nsHTMLTokenizer)
NS_IMPL_RELEASE(nsHTMLTokenizer)
NS_IMPL_ISUPPORTS1(nsHTMLTokenizer, nsITokenizer);
/**
* Default constructor

Просмотреть файл

@ -54,10 +54,6 @@
#include "nsHTMLTokens.h"
#include "nsDTDUtils.h"
#define NS_HTMLTOKENIZER_IID \
{0xe4238ddd, 0x9eb6, 0x11d2, \
{0xba, 0xa5, 0x0, 0x10, 0x4b, 0x98, 0x3f, 0xd4 }}
/***************************************************************
Notes:
***************************************************************/
@ -79,17 +75,17 @@ public:
protected:
virtual nsresult ConsumeTag(PRUnichar aChar,CToken*& aToken,nsScanner& aScanner,PRBool& aFlushTokens);
virtual nsresult ConsumeStartTag(PRUnichar aChar,CToken*& aToken,nsScanner& aScanner,PRBool& aFlushTokens);
virtual nsresult ConsumeEndTag(PRUnichar aChar,CToken*& aToken,nsScanner& aScanner);
virtual nsresult ConsumeAttributes(PRUnichar aChar, CToken* aToken, nsScanner& aScanner);
virtual nsresult ConsumeEntity(PRUnichar aChar,CToken*& aToken,nsScanner& aScanner);
virtual nsresult ConsumeWhitespace(PRUnichar aChar,CToken*& aToken,nsScanner& aScanner);
virtual nsresult ConsumeComment(PRUnichar aChar,CToken*& aToken,nsScanner& aScanner);
virtual nsresult ConsumeNewline(PRUnichar aChar,CToken*& aToken,nsScanner& aScanner);
virtual nsresult ConsumeText(CToken*& aToken,nsScanner& aScanner);
virtual nsresult ConsumeSpecialMarkup(PRUnichar aChar,CToken*& aToken,nsScanner& aScanner);
virtual nsresult ConsumeProcessingInstruction(PRUnichar aChar,CToken*& aToken,nsScanner& aScanner);
nsresult ConsumeTag(PRUnichar aChar,CToken*& aToken,nsScanner& aScanner,PRBool& aFlushTokens);
nsresult ConsumeStartTag(PRUnichar aChar,CToken*& aToken,nsScanner& aScanner,PRBool& aFlushTokens);
nsresult ConsumeEndTag(PRUnichar aChar,CToken*& aToken,nsScanner& aScanner);
nsresult ConsumeAttributes(PRUnichar aChar, CToken* aToken, nsScanner& aScanner);
nsresult ConsumeEntity(PRUnichar aChar,CToken*& aToken,nsScanner& aScanner);
nsresult ConsumeWhitespace(PRUnichar aChar,CToken*& aToken,nsScanner& aScanner);
nsresult ConsumeComment(PRUnichar aChar,CToken*& aToken,nsScanner& aScanner);
nsresult ConsumeNewline(PRUnichar aChar,CToken*& aToken,nsScanner& aScanner);
nsresult ConsumeText(CToken*& aToken,nsScanner& aScanner);
nsresult ConsumeSpecialMarkup(PRUnichar aChar,CToken*& aToken,nsScanner& aScanner);
nsresult ConsumeProcessingInstruction(PRUnichar aChar,CToken*& aToken,nsScanner& aScanner);
nsresult ScanDocStructure(PRBool aIsFinalChunk);
@ -105,10 +101,6 @@ protected:
PRUint32 mFlags;
};
extern nsresult NS_NewHTMLTokenizer(nsITokenizer** aInstancePtrResult,
PRInt32 aMode,eParserDocType aDocType,
eParserCommands aCommand, PRInt32 aFlags);
#endif

Просмотреть файл

@ -1111,7 +1111,7 @@ FindSuitableDTD(CParserContext& aParserContext,
}
if(theBestDTD) {
rv = theBestDTD->CreateNewInstance(&aParserContext.mDTD);
rv = theBestDTD->CreateNewInstance(getter_AddRefs(aParserContext.mDTD));
NS_ENSURE_SUCCESS(rv, rv);
*aReturn = PR_TRUE;
@ -1174,7 +1174,9 @@ nsParser::WillBuildModel(nsString& aFilename)
return rv;
nsITokenizer* tokenizer;
mParserContext->GetTokenizer(mParserContext->mDTD->GetType(), mSink, tokenizer);
rv = mParserContext->GetTokenizer(mParserContext->mDTD->GetType(), mSink, tokenizer);
NS_ENSURE_SUCCESS(rv, rv);
return mParserContext->mDTD->WillBuildModel(*mParserContext, tokenizer, mSink);
}
@ -1981,18 +1983,18 @@ nsresult nsParser::ResumeParse(PRBool allowIteration, PRBool aIsFinalChunk, PRBo
* @param
* @return error code -- 0 if ok, non-zero if error.
*/
nsresult nsParser::BuildModel() {
nsresult nsParser::BuildModel()
{
CParserContext* theRootContext = mParserContext;
nsITokenizer* theTokenizer = 0;
nsresult result = NS_OK;
if (mParserContext) {
PRInt32 type = mParserContext->mDTD ? mParserContext->mDTD->GetType() : NS_IPARSER_FLAG_HTML;
mParserContext->GetTokenizer(type, mSink, theTokenizer);
result = mParserContext->GetTokenizer(type, mSink, theTokenizer);
}
if (theTokenizer) {
if (NS_SUCCEEDED(result)) {
//Get the root DTD for use in model building...
while (theRootContext->mPrevContext) {
theRootContext = theRootContext->mPrevContext;
@ -2007,38 +2009,16 @@ nsresult nsParser::BuildModel() {
MOZ_TIMER_STOP(mDTDTime);
}
}
else{
else {
mInternalState = result = NS_ERROR_HTMLPARSER_BADTOKENIZER;
}
return result;
}
/**
*
* @update gess1/22/99
* @param
* @return
*/
nsresult nsParser::GetTokenizer(nsITokenizer*& aTokenizer) {
nsresult result = NS_OK;
aTokenizer = nsnull;
if(mParserContext) {
PRInt32 type = mParserContext->mDTD ? mParserContext->mDTD->GetType() : NS_IPARSER_FLAG_HTML;
result = mParserContext->GetTokenizer(type, mSink, aTokenizer);
}
return result;
}
/*******************************************************************
These methods are used to talk to the netlib system...
*******************************************************************/
#ifdef rickgdebug
#include <fstream.h>
fstream* gOutFile;
#endif
/**
*
*
@ -2071,10 +2051,6 @@ nsresult nsParser::OnStartRequest(nsIRequest *request, nsISupports* aContext) {
mParserContext->SetMimeType(contentType);
}
#ifdef rickgdebug
gOutFile= new fstream("c:/temp/out.file",ios::trunc);
#endif
rv = NS_OK;
if (sParserDataListeners && mSink) {
@ -2621,14 +2597,6 @@ nsresult nsParser::OnStopRequest(nsIRequest *request, nsISupports* aContext,
mObserver->OnStopRequest(request, aContext, status);
}
#ifdef rickgdebug
if(gOutFile){
gOutFile->close();
delete gOutFile;
gOutFile = 0;
}
#endif
if (sParserDataListeners && mSink) {
nsISupports *ctx = mSink->GetTarget();
PRInt32 count = sParserDataListeners->Count();
@ -2657,18 +2625,18 @@ nsresult nsParser::OnStopRequest(nsIRequest *request, nsISupports* aContext,
* @param
* @return TRUE if it's ok to proceed
*/
PRBool nsParser::WillTokenize(PRBool aIsFinalChunk){
nsITokenizer* theTokenizer=0;
nsresult result = NS_OK;
if (mParserContext) {
PRInt32 type = mParserContext->mDTD ? mParserContext->mDTD->GetType() : NS_IPARSER_FLAG_HTML;
mParserContext->GetTokenizer(type, mSink, theTokenizer);
PRBool nsParser::WillTokenize(PRBool aIsFinalChunk)
{
if (!mParserContext) {
return PR_TRUE;
}
if (theTokenizer) {
result = theTokenizer->WillTokenize(aIsFinalChunk,&mTokenAllocator);
}
return result;
nsITokenizer* theTokenizer;
PRInt32 type = mParserContext->mDTD ? mParserContext->mDTD->GetType() : NS_IPARSER_FLAG_HTML;
nsresult result = mParserContext->GetTokenizer(type, mSink, theTokenizer);
NS_ENSURE_SUCCESS(result, PR_FALSE);
return NS_SUCCEEDED(theTokenizer->WillTokenize(aIsFinalChunk,&mTokenAllocator));
}
@ -2680,18 +2648,17 @@ PRBool nsParser::WillTokenize(PRBool aIsFinalChunk){
* @update gess 01/04/99
* @return error code -- 0 if ok, non-zero if error.
*/
nsresult nsParser::Tokenize(PRBool aIsFinalChunk){
nsITokenizer* theTokenizer = 0;
nsresult nsParser::Tokenize(PRBool aIsFinalChunk)
{
nsITokenizer* theTokenizer;
nsresult result = NS_OK;
nsresult result = NS_ERROR_NOT_AVAILABLE;
if (mParserContext) {
PRInt32 type = mParserContext->mDTD ? mParserContext->mDTD->GetType() : NS_IPARSER_FLAG_HTML;
mParserContext->GetTokenizer(type, mSink, theTokenizer);
result = mParserContext->GetTokenizer(type, mSink, theTokenizer);
}
if (theTokenizer) {
if (NS_SUCCEEDED(result)) {
if (mFlags & NS_PARSER_FLAG_FLUSH_TOKENS) {
// For some reason tokens didn't get flushed ( probably
// the parser got blocked before all the tokens in the
@ -2736,7 +2703,7 @@ nsresult nsParser::Tokenize(PRBool aIsFinalChunk){
MOZ_TIMER_STOP(mTokenizeTime);
}
}
else{
else {
result = mInternalState = NS_ERROR_HTMLPARSER_BADTOKENIZER;
}
@ -2752,20 +2719,19 @@ nsresult nsParser::Tokenize(PRBool aIsFinalChunk){
* @param
* @return TRUE if all went well
*/
PRBool nsParser::DidTokenize(PRBool aIsFinalChunk){
PRBool result=PR_TRUE;
nsITokenizer* theTokenizer=0;
nsresult rv = NS_OK;
if (mParserContext) {
PRInt32 type = mParserContext->mDTD ? mParserContext->mDTD->GetType() : NS_IPARSER_FLAG_HTML;
mParserContext->GetTokenizer(type, mSink, theTokenizer);
PRBool nsParser::DidTokenize(PRBool aIsFinalChunk)
{
if (!mParserContext) {
return PR_TRUE;
}
if (NS_SUCCEEDED(rv) && theTokenizer) {
result = theTokenizer->DidTokenize(aIsFinalChunk);
}
return result;
nsITokenizer* theTokenizer;
PRInt32 type = mParserContext->mDTD ? mParserContext->mDTD->GetType() : NS_IPARSER_FLAG_HTML;
nsresult rv = mParserContext->GetTokenizer(type, mSink, theTokenizer);
NS_ENSURE_SUCCESS(rv, PR_FALSE);
rv = theTokenizer->DidTokenize(aIsFinalChunk);
return NS_SUCCEEDED(rv);
}
/**
@ -2794,7 +2760,7 @@ nsParser::GetDTD(nsIDTD** aDTD)
{
if (mParserContext) {
*aDTD = mParserContext->mDTD;
NS_IF_ADDREF(mParserContext->mDTD);
NS_IF_ADDREF(*aDTD);
}
return NS_OK;

Просмотреть файл

@ -313,14 +313,6 @@ class nsParser : public nsIParser,
CParserContext* PopContext();
CParserContext* PeekContext() {return mParserContext;}
/**
*
* @update gess 1/22/99
* @param
* @return
*/
nsresult GetTokenizer(nsITokenizer*& aTokenizer);
/**
* Get the channel associated with this parser
* @update harishd,gagan 07/17/01