Bug 384898: Cleanup nsScanner. r=mrbkap, sr=jst

git-svn-id: svn://10.0.0.236/trunk@228788 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
sharparrow1%yahoo.com 2007-06-27 00:21:47 +00:00
parent 9676b09bdf
commit 9057c0b45b
6 changed files with 37 additions and 412 deletions

View File

@ -50,7 +50,6 @@
#include "nsISupports.h"
#include "nsIStreamListener.h"
#include "nsIDTD.h"
#include "nsIInputStream.h"
#include "nsStringGlue.h"
#include "nsVoidArray.h"
@ -228,10 +227,6 @@ class nsIParser : public nsISupports {
nsIRequestObserver* aListener = nsnull,
void* aKey = 0,
nsDTDMode aMode = eDTDMode_autodetect) = 0;
NS_IMETHOD Parse(nsIInputStream* aStream,
const nsACString& aMimeType,
void* aKey = 0,
nsDTDMode aMode = eDTDMode_autodetect) = 0;
NS_IMETHOD Parse(const nsAString& aSourceBuffer,
void* aKey,
const nsACString& aMimeType,

View File

@ -1177,7 +1177,7 @@ nsExpatDriver::ConsumeToken(nsScanner& aScanner, PRBool& aFlushTokens)
("Remaining in expat's buffer: %i, remaining in scanner: %i.",
mExpatBuffered, Distance(currentExpatPosition, end)));
return NS_SUCCEEDED(mInternalState) ? aScanner.FillBuffer() : NS_OK;
return NS_SUCCEEDED(mInternalState) ? kEOF : NS_OK;
}
NS_IMETHODIMP

View File

@ -1280,43 +1280,6 @@ nsParser::Parse(nsIURI* aURL,
return result;
}
/**
* Cause parser to parse input from given stream
*/
NS_IMETHODIMP
nsParser::Parse(nsIInputStream* aStream,
const nsACString& aMimeType,
void* aKey,
nsDTDMode aMode)
{
nsresult result = NS_ERROR_OUT_OF_MEMORY;
// Ok, time to create our tokenizer and begin the process
nsAutoString theUnknownFilename(NS_LITERAL_STRING("unknown"));
nsScanner* theScanner = new nsScanner(theUnknownFilename, aStream, mCharset,
mCharsetSource);
CParserContext* pc = new CParserContext(theScanner, aKey, mCommand, 0);
if (pc && theScanner) {
PushContext(*pc);
pc->SetMimeType(aMimeType);
pc->mStreamListenerState = eOnStart;
pc->mMultipart = PR_FALSE;
pc->mContextType = CParserContext::eCTStream;
pc->mDTDMode = aMode;
mParserContext->mScanner->FillBuffer();
result = ResumeParse();
pc = PopContext();
delete pc;
} else {
result = mInternalState = NS_ERROR_HTMLPARSER_BADCONTEXT;
}
return result;
}
/**
* Call this method if all you want to do is parse 1 string full of HTML text.
* In particular, this method should be called by the DOM when it has an HTML

View File

@ -199,17 +199,6 @@ class nsParser : public nsIParser,
void* aKey = 0,
nsDTDMode aMode = eDTDMode_autodetect);
/**
* Cause parser to parse input from given stream
* @update gess5/11/98
* @param aStream is the i/o source
* @return TRUE if all went well -- FALSE otherwise
*/
NS_IMETHOD Parse(nsIInputStream* aStream,
const nsACString& aMimeType,
void* aKey = 0,
nsDTDMode aMode = eDTDMode_autodetect);
/**
* @update gess5/11/98
* @param anHTMLString contains a string-full of real HTML

View File

@ -94,7 +94,6 @@ nsScanner::nsScanner(const nsAString& anHTMLString, const nsACString& aCharset,
{
MOZ_COUNT_CTOR(nsScanner);
mTotalRead = anHTMLString.Length();
mSlidingBuffer = nsnull;
mCountRemaining = 0;
mFirstNonWhitespacePosition = -1;
@ -121,6 +120,7 @@ nsScanner::nsScanner(nsString& aFilename,PRBool aCreateStream,
: mFilename(aFilename), mParser(nsnull)
{
MOZ_COUNT_CTOR(nsScanner);
NS_ASSERTION(!aCreateStream, "This is always true.");
mSlidingBuffer = nsnull;
@ -136,59 +136,12 @@ nsScanner::nsScanner(nsString& aFilename,PRBool aCreateStream,
mIncremental = PR_TRUE;
mFirstNonWhitespacePosition = -1;
mCountRemaining = 0;
mTotalRead=0;
if(aCreateStream) {
nsCOMPtr<nsILocalFile> file;
nsCOMPtr<nsIInputStream> fileStream;
NS_NewLocalFile(aFilename, PR_TRUE, getter_AddRefs(file));
if (file)
NS_NewLocalFileInputStream(getter_AddRefs(mInputStream), file);
} //if
mUnicodeDecoder = 0;
mCharsetSource = kCharsetUninitialized;
SetDocumentCharset(aCharset, aSource);
}
/**
* Use this constructor if you want i/o to be stream based.
*
* @update gess 5/12/98
* @param aStream --
* @param assumeOwnership --
* @param aFilename --
* @return
*/
nsScanner::nsScanner(const nsAString& aFilename, nsIInputStream* aStream,
const nsACString& aCharset, PRInt32 aSource)
: mFilename(aFilename), mParser(nsnull)
{
MOZ_COUNT_CTOR(nsScanner);
mSlidingBuffer = nsnull;
// XXX This is a big hack. We need to initialize the iterators to something.
// What matters is that mCurrentPosition == mEndPosition, so that our methods
// believe that we are at EOF (see bug 182067). We null out mCurrentPosition
// so that we have some hope of catching null pointer dereferences associated
// with this hack. --darin
memset(&mCurrentPosition, 0, sizeof(mCurrentPosition));
mMarkPosition = mCurrentPosition;
mEndPosition = mCurrentPosition;
mIncremental = PR_FALSE;
mFirstNonWhitespacePosition = -1;
mCountRemaining = 0;
mTotalRead=0;
mInputStream=aStream;
mUnicodeDecoder = 0;
mCharsetSource = kCharsetUninitialized;
SetDocumentCharset(aCharset, aSource);
}
nsresult nsScanner::SetDocumentCharset(const nsACString& aCharset , PRInt32 aSource) {
nsresult res = NS_OK;
@ -251,11 +204,6 @@ nsScanner::~nsScanner() {
MOZ_COUNT_DTOR(nsScanner);
if(mInputStream) {
mInputStream->Close();
mInputStream = 0;
}
NS_IF_RELEASE(mUnicodeDecoder);
}
@ -313,7 +261,6 @@ PRBool nsScanner::UngetReadable(const nsAString& aBuffer) {
PRUint32 length = aBuffer.Length();
mCountRemaining += length; // Ref. bug 117441
mTotalRead += length;
return PR_TRUE;
}
@ -325,7 +272,6 @@ PRBool nsScanner::UngetReadable(const nsAString& aBuffer) {
* @return error code
*/
nsresult nsScanner::Append(const nsAString& aBuffer) {
mTotalRead += aBuffer.Length();
AppendToBuffer(aBuffer);
return NS_OK;
}
@ -342,7 +288,7 @@ nsresult nsScanner::Append(const char* aBuffer, PRUint32 aLen,
{
nsresult res=NS_OK;
PRUnichar *unichars, *start;
if(mUnicodeDecoder) {
if (mUnicodeDecoder) {
PRInt32 unicharBufLen = 0;
mUnicodeDecoder->GetMaxLength(aBuffer, aLen, &unicharBufLen);
nsScannerString::Buffer* buffer = nsScannerString::AllocBuffer(unicharBufLen + 1);
@ -389,7 +335,6 @@ nsresult nsScanner::Append(const char* aBuffer, PRUint32 aLen,
buffer->SetDataLength(totalChars);
AppendToBuffer(buffer, aRequest);
mTotalRead += totalChars;
// Don't propagate return code of unicode decoder
// since it doesn't reflect on our success or failure
@ -397,47 +342,13 @@ nsresult nsScanner::Append(const char* aBuffer, PRUint32 aLen,
res = NS_OK;
}
else {
AppendASCIItoBuffer(aBuffer, aLen, aRequest);
mTotalRead+=aLen;
NS_WARNING("No decoder found.");
res = NS_ERROR_FAILURE;
}
return res;
}
/**
* Grab data from underlying stream.
*
* @update gess4/3/98
* @return error code
*/
nsresult nsScanner::FillBuffer(void) {
nsresult result=NS_OK;
if(!mInputStream) {
result=kEOF;
}
else {
PRUint32 numread=0;
char buf[kBufsize+1];
buf[kBufsize]=0;
// XXX use ReadSegments to avoid extra buffer copy? --darin
result = mInputStream->Read(buf, kBufsize, &numread);
if (0 == numread) {
return kEOF;
}
if((0<numread) && NS_SUCCEEDED(result)) {
AppendASCIItoBuffer(buf, numread, nsnull);
}
mTotalRead+=numread;
}
return result;
}
/**
* retrieve next char from scanners internal input stream
*
@ -446,22 +357,15 @@ nsresult nsScanner::FillBuffer(void) {
* @return error code reflecting read status
*/
nsresult nsScanner::GetChar(PRUnichar& aChar) {
nsresult result=NS_OK;
aChar=0;
if (!mSlidingBuffer) {
if (!mSlidingBuffer || mCurrentPosition == mEndPosition) {
aChar = 0;
return kEOF;
}
if (mCurrentPosition == mEndPosition) {
result = FillBuffer();
}
aChar = *mCurrentPosition++;
--mCountRemaining;
if(NS_OK == result){
aChar = *mCurrentPosition++;
--mCountRemaining;
}
return result;
return NS_OK;
}
@ -474,47 +378,33 @@ nsresult nsScanner::GetChar(PRUnichar& aChar) {
* @return
*/
nsresult nsScanner::Peek(PRUnichar& aChar, PRUint32 aOffset) {
nsresult result=NS_OK;
aChar=0;
if (!mSlidingBuffer) {
aChar = 0;
if (!mSlidingBuffer || mCurrentPosition == mEndPosition) {
return kEOF;
}
if (mCurrentPosition == mEndPosition) {
result = FillBuffer();
if (aOffset > 0) {
if (mCountRemaining <= aOffset)
return kEOF;
nsScannerIterator pos = mCurrentPosition;
pos.advance(aOffset);
aChar=*pos;
}
else {
aChar=*mCurrentPosition;
}
if(NS_OK == result){
if (aOffset) {
while ((NS_OK == result) && (mCountRemaining <= aOffset)) {
result = FillBuffer();
}
if (NS_OK == result) {
nsScannerIterator pos = mCurrentPosition;
pos.advance(aOffset);
aChar=*pos;
}
}
else {
aChar=*mCurrentPosition;
}
}
return result;
return NS_OK;
}
nsresult nsScanner::Peek(nsAString& aStr, PRInt32 aNumChars, PRInt32 aOffset)
{
if (!mSlidingBuffer) {
if (!mSlidingBuffer || mCurrentPosition == mEndPosition) {
return kEOF;
}
if (mCurrentPosition == mEndPosition) {
return FillBuffer();
}
nsScannerIterator start, end;
start = mCurrentPosition;
@ -591,7 +481,7 @@ nsresult nsScanner::SkipWhitespace(PRInt32& aNewlinesSkipped) {
if (skipped) {
SetPosition(current);
if (current == mEndPosition) {
result = FillBuffer();
result = kEOF;
}
}
@ -628,68 +518,6 @@ nsresult nsScanner::SkipOver(PRUnichar aSkipChar){
}
/**
* Skip over chars as long as they're in aSkipSet
*
* @update gess 3/25/98
* @param aSkipSet is an ordered string.
* @return error code
*/
nsresult nsScanner::SkipOver(nsString& aSkipSet){
if (!mSlidingBuffer) {
return kEOF;
}
PRUnichar theChar=0;
nsresult result=NS_OK;
while(NS_OK==result) {
result=Peek(theChar);
if(NS_OK == result) {
PRInt32 pos=aSkipSet.FindChar(theChar);
if(kNotFound==pos) {
break;
}
GetChar(theChar);
}
else break;
} //while
return result;
}
/**
* Skip over chars until they're in aValidSet
*
* @update gess 3/25/98
* @param aValid set is an ordered string that
* contains chars you're looking for
* @return error code
*/
nsresult nsScanner::SkipTo(nsString& aValidSet){
if (!mSlidingBuffer) {
return kEOF;
}
PRUnichar ch=0;
nsresult result=NS_OK;
while(NS_OK==result) {
result=Peek(ch);
if(NS_OK == result) {
PRInt32 pos=aValidSet.FindChar(ch);
if(kNotFound!=pos) {
break;
}
GetChar(ch);
}
else break;
} //while
return result;
}
#if 0
void DoErrTest(nsString& aString) {
PRInt32 pos=aString.FindChar(0);
@ -708,19 +536,6 @@ void DoErrTest(nsCString& aString) {
}
#endif
/**
* Skip over chars as long as they're in aValidSet
*
* @update gess 3/25/98
* @param aValidSet is an ordered string containing the
* characters you want to skip
* @return error code
*/
nsresult nsScanner::SkipPast(nsString& aValidSet){
NS_NOTYETIMPLEMENTED("Error: SkipPast not yet implemented.");
return NS_OK;
}
/**
* Consume characters until you run into space, a '<', a '>', or a '/'.
*
@ -780,7 +595,7 @@ nsresult nsScanner::ReadTagIdentifier(nsScannerSharedSubstring& aString) {
SetPosition(current);
if (current == end) {
result = FillBuffer();
result = kEOF;
}
//DoErrTest(aString);
@ -840,7 +655,7 @@ nsresult nsScanner::ReadEntityIdentifier(nsString& aString) {
SetPosition(current);
if (current == end) {
AppendUnicodeTo(origin, current, aString);
return FillBuffer();
return kEOF;
}
//DoErrTest(aString);
@ -889,7 +704,7 @@ nsresult nsScanner::ReadNumber(nsString& aString,PRInt32 aBase) {
SetPosition(current);
if (current == end) {
AppendUnicodeTo(origin, current, aString);
return FillBuffer();
return kEOF;
}
//DoErrTest(aString);
@ -967,7 +782,7 @@ nsresult nsScanner::ReadWhitespace(nsScannerSharedSubstring& aString,
SetPosition(current);
if (current == end) {
AppendUnicodeTo(origin, current, aString);
result = FillBuffer();
result = kEOF;
}
aHaveCR = haveCR;
@ -1026,69 +841,12 @@ nsresult nsScanner::ReadWhitespace(nsScannerIterator& aStart,
if (current == end) {
aStart = origin;
aEnd = current;
result = FillBuffer();
result = kEOF;
}
return result;
}
/**
* Consume chars as long as they are <i>in</i> the
* given validSet of input chars.
*
* @update gess 3/25/98
* @param aString will contain the result of this method
* @param aValidSet is an ordered string that contains the
* valid characters
* @return error code
*/
nsresult nsScanner::ReadWhile(nsString& aString,
nsString& aValidSet,
PRBool addTerminal){
if (!mSlidingBuffer) {
return kEOF;
}
PRUnichar theChar=0;
nsresult result=Peek(theChar);
nsScannerIterator origin, current, end;
origin = mCurrentPosition;
current = origin;
end = mEndPosition;
while(current != end) {
theChar=*current;
if (theChar == '\0') {
ReplaceCharacter(current, sInvalid);
theChar = sInvalid;
}
if(theChar) {
PRInt32 pos=aValidSet.FindChar(theChar);
if(kNotFound==pos) {
if(addTerminal)
++current;
AppendUnicodeTo(origin, current, aString);
break;
}
}
++current;
}
SetPosition(current);
if (current == end) {
AppendUnicodeTo(origin, current, aString);
return FillBuffer();
}
//DoErrTest(aString);
return result;
}
/**
* Consume characters until you encounter one contained in given
* input set.
@ -1156,7 +914,7 @@ nsresult nsScanner::ReadUntil(nsAString& aString,
// current = mEndPosition
SetPosition(current);
AppendUnicodeTo(origin, current, aString);
return FillBuffer();
return kEOF;
}
nsresult nsScanner::ReadUntil(nsScannerSharedSubstring& aString,
@ -1216,7 +974,7 @@ nsresult nsScanner::ReadUntil(nsScannerSharedSubstring& aString,
// current = mEndPosition
SetPosition(current);
AppendUnicodeTo(origin, current, aString);
return FillBuffer();
return kEOF;
}
nsresult nsScanner::ReadUntil(nsScannerIterator& aStart,
@ -1277,7 +1035,7 @@ nsresult nsScanner::ReadUntil(nsScannerIterator& aStart,
SetPosition(current);
aStart = origin;
aEnd = current;
return FillBuffer();
return kEOF;
}
/**
@ -1328,7 +1086,7 @@ nsresult nsScanner::ReadUntil(nsAString& aString,
// current = mEndPosition
AppendUnicodeTo(origin, current, aString);
SetPosition(current);
return FillBuffer();
return kEOF;
}
@ -1425,19 +1183,6 @@ void nsScanner::AppendToBuffer(nsScannerString::Buffer* aBuf,
}
}
void nsScanner::AppendASCIItoBuffer(const char* aData, PRUint32 aLen,
nsIRequest *aRequest)
{
nsScannerString::Buffer* buf = nsScannerString::AllocBuffer(aLen);
if (buf)
{
LossyConvertEncoding<char, PRUnichar> converter(buf->DataStart());
converter.write(aData, aLen);
converter.write_terminator();
AppendToBuffer(buf, aRequest);
}
}
/**
* call this to copy bytes out of the scanner that have not yet been consumed
* by the tokenization process.

View File

@ -43,8 +43,8 @@
* The scanner is a low-level service class that knows
* how to consume characters out of an (internal) stream.
* This class also offers a series of utility methods
* that most tokenizers want, such as readUntil(),
* readWhile() and SkipWhitespace().
* that most tokenizers want, such as readUntil()
* and SkipWhitespace().
*/
@ -57,7 +57,6 @@
#include "prtypes.h"
#include "nsIUnicodeDecoder.h"
#include "nsScannerString.h"
#include "nsIInputStream.h"
class nsParser;
@ -99,18 +98,6 @@ class nsScanner {
*/
nsScanner(nsString& aFilename,PRBool aCreateStream, const nsACString& aCharset, PRInt32 aSource);
/**
* Use this constructor if you want i/o to be stream based.
*
* @update ftang 3/02/99
* @param aCharset charset
* @param aCharsetSource - where the charset info came from
* @param aMode represents the parser mode (nav, other)
* @return
*/
nsScanner(const nsAString& aFilename, nsIInputStream* aStream, const nsACString& aCharset, PRInt32 aSource);
~nsScanner();
/**
@ -134,15 +121,6 @@ class nsScanner {
nsresult Peek(nsAString& aStr, PRInt32 aNumChars, PRInt32 aOffset = 0);
/**
* Skip over chars as long as they're in aSkipSet
*
* @update gess 3/25/98
* @param set of chars to be skipped
* @return error code
*/
nsresult SkipOver(nsString& SkipChars);
/**
* Skip over chars as long as they equal given char
*
@ -152,24 +130,6 @@ class nsScanner {
*/
nsresult SkipOver(PRUnichar aSkipChar);
/**
* Skip over chars until they're in aValidSet
*
* @update gess 3/25/98
* @param aValid set contains chars you're looking for
* @return error code
*/
nsresult SkipTo(nsString& aValidSet);
/**
* Skip over chars as long as they're in aSequence
*
* @update gess 3/25/98
* @param contains sequence to be skipped
* @return error code
*/
nsresult SkipPast(nsString& aSequence);
/**
* Skip whitespace on scanner input stream
*
@ -238,18 +198,6 @@ class nsScanner {
const nsReadEndCondition& aEndCondition,
PRBool addTerminal);
/**
* Consume characters while they're members of anInputSet
*
* @update gess 3/25/98
* @param aString receives new data from stream
* @param anInputSet contains valid chars
* @param addTerminal tells us whether to append terminal to aString
* @return error code
*/
nsresult ReadWhile(nsString& aString,nsString& anInputSet,PRBool addTerminal);
/**
* Records current offset position in input stream. This allows us
* to back up to this point if the need should arise, such as when
@ -367,28 +315,14 @@ class nsScanner {
mParser = aParser;
}
/**
* Fill internal buffer with new data, returns an error if
* no new data is read.
*
* @update gess4/3/98
*/
nsresult FillBuffer(void);
protected:
enum {eBufferSizeThreshold=0x1000}; //4K
void AppendToBuffer(nsScannerString::Buffer *, nsIRequest *aRequest);
void AppendToBuffer(const nsAString& aStr)
{
AppendToBuffer(nsScannerString::AllocBufferFromString(aStr), nsnull);
}
void AppendASCIItoBuffer(const char* aData, PRUint32 aLen,
nsIRequest *aRequest);
nsCOMPtr<nsIInputStream> mInputStream;
nsScannerString* mSlidingBuffer;
nsScannerIterator mCurrentPosition; // The position we will next read from in the scanner buffer
nsScannerIterator mMarkPosition; // The position last marked (we may rewind to here)
@ -396,7 +330,6 @@ class nsScanner {
nsString mFilename;
PRUint32 mCountRemaining; // The number of bytes still to be read
// from the scanner buffer
PRUint32 mTotalRead;
PRPackedBool mIncremental;
PRInt32 mFirstNonWhitespacePosition;
PRInt32 mCharsetSource;