1998-04-13 20:24:54 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2001-09-26 00:40:45 +00:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
|
1998-04-13 20:24:54 +00:00
|
|
|
*
|
2001-09-26 00:40:45 +00:00
|
|
|
* The contents of this file are subject to the Netscape Public License
|
|
|
|
* Version 1.1 (the "License"); you may not use this file except in
|
|
|
|
* compliance with the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/NPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
1998-04-13 20:24:54 +00:00
|
|
|
*
|
1999-11-06 03:43:54 +00:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2001-09-26 00:40:45 +00:00
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the NPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the NPL, the GPL or the LGPL.
|
1999-11-06 03:43:54 +00:00
|
|
|
*
|
2001-09-26 00:40:45 +00:00
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1998-04-13 20:24:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MODULE NOTES:
|
|
|
|
* @update gess 4/1/98
|
|
|
|
*
|
|
|
|
* This class is defines the basic notion of a token
|
|
|
|
* within our system. All other tokens are derived from
|
|
|
|
* this one. It offers a few basic interfaces, but the
|
|
|
|
* most important is consume(). The consume() method gets
|
|
|
|
* called during the tokenization process when an instance
|
|
|
|
* of that particular token type gets detected in the
|
|
|
|
* input stream.
|
|
|
|
*
|
2001-04-04 05:00:08 +00:00
|
|
|
* CToken objects that are allocated from the heap _must_ be allocated
|
|
|
|
* using the nsTokenAllocator: the nsTokenAllocator object uses an
|
|
|
|
* arena to manage the tokens.
|
|
|
|
*
|
|
|
|
* The nsTokenAllocator object's arena implementation requires
|
|
|
|
* object size at destruction time to properly recycle the object;
|
|
|
|
* therefore, CToken::operator delete() is not public. Instead,
|
|
|
|
* heap-allocated tokens should be destroyed using the static
|
|
|
|
* Destroy() method, which accepts a token and the arena from which
|
|
|
|
* the token was allocated.
|
|
|
|
*
|
|
|
|
* Leaf classes (that are actually instantiated from the heap) must
|
|
|
|
* implement the SizeOf() method, which Destroy() uses to determine
|
|
|
|
* the size of the token in order to properly recycle it.
|
1998-04-13 20:24:54 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef CTOKEN__
|
|
|
|
#define CTOKEN__
|
|
|
|
|
|
|
|
#include "prtypes.h"
|
|
|
|
#include "nsString.h"
|
1999-03-22 10:19:09 +00:00
|
|
|
#include "nsError.h"
|
1999-09-02 18:30:31 +00:00
|
|
|
#include "nsFileSpec.h"
|
2000-08-30 18:22:03 +00:00
|
|
|
#include "nsFixedSizeAllocator.h"
|
1998-04-13 20:24:54 +00:00
|
|
|
|
2002-12-12 22:38:40 +00:00
|
|
|
#define NS_HTMLTOKENS_UNKNOWNFORM 0x00000001
|
|
|
|
#define NS_HTMLTOKENS_WELLFORMED 0x00000002
|
|
|
|
#define NS_HTMLTOKENS_MALFORMED 0x00000004
|
|
|
|
#define NS_HTMLTOKENS_EMPTYTOKEN 0x00000008
|
|
|
|
|
2001-07-05 22:20:34 +00:00
|
|
|
#define NS_HTMLTOKENS_NOT_AN_ENTITY \
|
|
|
|
NS_ERROR_GENERATE_SUCCESS(NS_ERROR_MODULE_HTMLPARSER,2000)
|
|
|
|
|
1999-01-09 01:09:02 +00:00
|
|
|
class nsScanner;
|
2001-04-04 05:00:08 +00:00
|
|
|
class nsTokenAllocator;
|
2000-09-08 23:06:13 +00:00
|
|
|
|
2001-04-04 05:00:08 +00:00
|
|
|
/**
|
|
|
|
* Implement the SizeOf() method; leaf classes derived from CToken
|
|
|
|
* must declare this.
|
|
|
|
*/
|
|
|
|
#define CTOKEN_IMPL_SIZEOF \
|
|
|
|
protected: \
|
|
|
|
virtual size_t SizeOf() const { return sizeof(*this); } \
|
|
|
|
public:
|
2000-09-08 23:06:13 +00:00
|
|
|
|
1998-04-30 20:23:07 +00:00
|
|
|
/**
|
1998-04-15 17:27:56 +00:00
|
|
|
* Token objects represent sequences of characters as they
|
|
|
|
* are consumed from the input stream (URL). While they're
|
|
|
|
* pretty general in nature, we use subclasses (found in
|
|
|
|
* nsHTMLTokens.h) to define <start>, </end>, <text>,
|
|
|
|
* <comment>, <&entity>, <newline>, and <whitespace> tokens.
|
|
|
|
*
|
|
|
|
* @update gess 3/25/98
|
1998-04-30 20:23:07 +00:00
|
|
|
*/
|
1998-04-13 20:24:54 +00:00
|
|
|
class CToken {
|
1998-04-25 19:45:14 +00:00
|
|
|
public:
|
1998-05-12 01:11:50 +00:00
|
|
|
|
1999-07-07 07:40:35 +00:00
|
|
|
enum eTokenOrigin {eSource,eResidualStyle};
|
|
|
|
|
2001-04-04 05:00:08 +00:00
|
|
|
protected:
|
|
|
|
|
|
|
|
// nsTokenAllocator should be the only class that tries to
|
|
|
|
// allocate tokens from the heap.
|
|
|
|
friend class nsTokenAllocator;
|
|
|
|
|
2000-08-30 18:22:03 +00:00
|
|
|
/**
|
2000-08-31 04:21:39 +00:00
|
|
|
*
|
|
|
|
* @update harishd 08/01/00
|
|
|
|
* @param aSize -
|
|
|
|
* @param aArena - Allocate memory from this pool.
|
2000-08-30 18:22:03 +00:00
|
|
|
*/
|
2002-07-02 20:25:30 +00:00
|
|
|
static void * operator new (size_t aSize,nsFixedSizeAllocator& anArena) CPP_THROW_NEW
|
2000-08-31 04:21:39 +00:00
|
|
|
{
|
|
|
|
return anArena.Alloc(aSize);
|
|
|
|
}
|
|
|
|
|
2000-08-30 18:22:03 +00:00
|
|
|
/**
|
2001-04-04 05:00:08 +00:00
|
|
|
* Hide operator delete; clients should use Destroy() instead.
|
2000-08-30 18:22:03 +00:00
|
|
|
*/
|
2001-04-04 05:00:08 +00:00
|
|
|
static void operator delete (void*,size_t) {}
|
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* destructor
|
|
|
|
* @update gess5/11/98
|
|
|
|
*/
|
|
|
|
virtual ~CToken();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroy a token.
|
|
|
|
*/
|
|
|
|
static void Destroy(CToken* aToken,nsFixedSizeAllocator& aArenaPool)
|
2000-08-31 04:21:39 +00:00
|
|
|
{
|
2001-04-04 05:00:08 +00:00
|
|
|
size_t sz = aToken->SizeOf();
|
|
|
|
aToken->~CToken();
|
|
|
|
aArenaPool.Free(aToken, sz);
|
2000-08-31 04:21:39 +00:00
|
|
|
}
|
2000-08-30 18:22:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Make a note on number of times you have been referenced
|
|
|
|
* @update harishd 08/02/00
|
|
|
|
*/
|
2002-09-03 22:23:22 +00:00
|
|
|
void AddRef() { ++mUseCount; }
|
2000-08-30 18:22:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Free yourself if no one is holding you.
|
|
|
|
* @update harishd 08/02/00
|
|
|
|
*/
|
2001-04-04 05:00:08 +00:00
|
|
|
void Release(nsFixedSizeAllocator& aArenaPool) {
|
2001-04-11 03:05:22 +00:00
|
|
|
if(--mUseCount==0)
|
2001-04-04 05:00:08 +00:00
|
|
|
Destroy(this, aArenaPool);
|
2000-08-30 18:22:03 +00:00
|
|
|
}
|
2001-04-04 05:00:08 +00:00
|
|
|
|
1998-05-12 01:11:50 +00:00
|
|
|
/**
|
|
|
|
* Default constructor
|
1998-07-23 08:21:02 +00:00
|
|
|
* @update gess7/21/98
|
|
|
|
*/
|
|
|
|
CToken(PRInt32 aTag=0);
|
|
|
|
|
1998-05-12 01:11:50 +00:00
|
|
|
/**
|
|
|
|
* Retrieve string value of the token
|
|
|
|
* @update gess5/11/98
|
|
|
|
* @return reference to string containing string value
|
|
|
|
*/
|
2002-03-24 00:16:18 +00:00
|
|
|
virtual const nsAString& GetStringValue(void) = 0;
|
1998-05-12 01:11:50 +00:00
|
|
|
|
1999-05-16 05:27:23 +00:00
|
|
|
/**
|
|
|
|
* Get string of full contents, suitable for debug dump.
|
|
|
|
* It should look exactly like the input source.
|
|
|
|
* @update gess5/11/98
|
|
|
|
* @return reference to string containing string value
|
|
|
|
*/
|
2002-12-12 22:38:40 +00:00
|
|
|
virtual void GetSource(nsAString& anOutputString);
|
1999-05-16 05:27:23 +00:00
|
|
|
|
2000-03-23 21:19:05 +00:00
|
|
|
/** @update harishd 03/23/00
|
|
|
|
* @return reference to string containing string value
|
|
|
|
*/
|
2002-04-10 22:16:46 +00:00
|
|
|
virtual void AppendSourceTo(nsAString& anOutputString);
|
2000-03-23 21:19:05 +00:00
|
|
|
|
1998-05-12 01:11:50 +00:00
|
|
|
/**
|
|
|
|
* Sets the ordinal value of this token (not currently used)
|
|
|
|
* @update gess5/11/98
|
|
|
|
* @param value is the new ord value for this token
|
|
|
|
*/
|
1998-06-19 01:00:27 +00:00
|
|
|
virtual void SetTypeID(PRInt32 aValue);
|
|
|
|
|
1998-05-12 01:11:50 +00:00
|
|
|
/**
|
|
|
|
* Getter which retrieves the current ordinal value for this token
|
|
|
|
* @update gess5/11/98
|
|
|
|
* @return current ordinal value
|
|
|
|
*/
|
1998-06-19 01:00:27 +00:00
|
|
|
virtual PRInt32 GetTypeID(void);
|
1998-05-21 20:38:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Getter which retrieves the current attribute count for this token
|
|
|
|
* @update gess5/11/98
|
|
|
|
* @return current attribute count
|
|
|
|
*/
|
|
|
|
virtual PRInt16 GetAttributeCount(void);
|
1998-05-12 01:11:50 +00:00
|
|
|
|
2002-12-12 22:38:40 +00:00
|
|
|
virtual void SetAttributeCount(PRInt16 aValue) { }
|
|
|
|
|
1998-05-12 01:11:50 +00:00
|
|
|
/**
|
|
|
|
* Causes token to consume data from given scanner.
|
|
|
|
* Note that behavior varies wildly between CToken subclasses.
|
|
|
|
* @update gess5/11/98
|
|
|
|
* @param aChar -- most recent char consumed
|
|
|
|
* @param aScanner -- input source where token should get data
|
|
|
|
* @return error code (0 means ok)
|
|
|
|
*/
|
1999-10-06 19:04:29 +00:00
|
|
|
virtual nsresult Consume(PRUnichar aChar,nsScanner& aScanner,PRInt32 aMode);
|
1998-05-12 01:11:50 +00:00
|
|
|
|
2001-10-16 05:31:36 +00:00
|
|
|
#ifdef DEBUG
|
1998-05-12 01:11:50 +00:00
|
|
|
/**
|
|
|
|
* Causes token to dump itself in debug form to given output stream
|
|
|
|
* @update gess5/11/98
|
|
|
|
* @param out is the output stream where token should write itself
|
|
|
|
*/
|
1999-09-02 18:30:31 +00:00
|
|
|
virtual void DebugDumpToken(nsOutputStream& out);
|
1998-05-12 01:11:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Causes token to dump itself in source form to given output stream
|
|
|
|
* @update gess5/11/98
|
|
|
|
* @param out is the output stream where token should write itself
|
|
|
|
*/
|
1999-09-02 18:30:31 +00:00
|
|
|
virtual void DebugDumpSource(nsOutputStream& out);
|
2002-12-12 22:38:40 +00:00
|
|
|
|
1998-05-12 01:11:50 +00:00
|
|
|
/**
|
|
|
|
* Getter which retrieves the class name for this token
|
|
|
|
* This method is only used for debug purposes.
|
|
|
|
* @update gess5/11/98
|
|
|
|
* @return const char* containing class name
|
|
|
|
*/
|
|
|
|
virtual const char* GetClassName(void);
|
2002-12-12 22:38:40 +00:00
|
|
|
#endif
|
1998-05-12 01:11:50 +00:00
|
|
|
|
2002-12-12 22:38:40 +00:00
|
|
|
/**
|
|
|
|
* Getter which retrieves type of token
|
|
|
|
* @update gess5/11/98
|
|
|
|
* @return int containing token type
|
|
|
|
*/
|
|
|
|
virtual PRInt32 GetTokenType(void);
|
2000-09-08 23:06:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* For tokens who care, this can tell us whether the token is
|
|
|
|
* well formed or not.
|
|
|
|
*
|
|
|
|
* @update gess 8/30/00
|
|
|
|
* @return PR_FALSE; subclasses MUST override if they care.
|
|
|
|
*/
|
|
|
|
virtual PRBool IsWellFormed(void) const {return PR_FALSE;}
|
|
|
|
|
2002-04-10 22:16:46 +00:00
|
|
|
virtual PRBool IsEmpty(void) { return PR_FALSE; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If aValue is TRUE then the token represents a short-hand tag
|
|
|
|
*/
|
|
|
|
virtual void SetEmpty(PRBool aValue) { return ; }
|
|
|
|
|
|
|
|
PRInt32 GetNewlineCount()
|
|
|
|
{
|
|
|
|
return mNewlineCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetNewlineCount(PRInt32 aCount)
|
|
|
|
{
|
|
|
|
mNewlineCount = aCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32 GetLineNumber()
|
|
|
|
{
|
|
|
|
return mLineNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetLineNumber(PRInt32 aLineNumber)
|
|
|
|
{
|
|
|
|
mLineNumber = mLineNumber == 0 ? aLineNumber : mLineNumber;
|
|
|
|
}
|
|
|
|
|
1998-05-12 01:11:50 +00:00
|
|
|
/**
|
|
|
|
* perform self test.
|
|
|
|
* @update gess5/11/98
|
|
|
|
*/
|
|
|
|
virtual void SelfTest(void);
|
1998-04-13 20:24:54 +00:00
|
|
|
|
1998-08-12 08:53:55 +00:00
|
|
|
static int GetTokenCount();
|
|
|
|
|
2002-04-10 22:16:46 +00:00
|
|
|
|
1999-07-07 07:40:35 +00:00
|
|
|
|
1998-04-13 20:24:54 +00:00
|
|
|
protected:
|
2001-04-04 05:00:08 +00:00
|
|
|
/**
|
|
|
|
* Returns the size of the token object.
|
|
|
|
*/
|
|
|
|
virtual size_t SizeOf() const = 0;
|
2000-08-30 18:22:03 +00:00
|
|
|
|
2002-12-12 22:38:40 +00:00
|
|
|
PRInt32 mTypeID;
|
|
|
|
PRInt32 mUseCount;
|
|
|
|
PRInt32 mNewlineCount;
|
|
|
|
PRInt32 mLineNumber;
|
|
|
|
PRUint16 mFlags;
|
1998-04-13 20:24:54 +00:00
|
|
|
};
|
|
|
|
|
1998-06-17 23:13:28 +00:00
|
|
|
|
|
|
|
|
1998-04-13 20:24:54 +00:00
|
|
|
#endif
|
1998-04-25 19:45:14 +00:00
|
|
|
|
|
|
|
|