1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1998
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either of the GNU General Public License Version 2 or later (the "GPL"),
26 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 :
39 : /**
40 : * MODULE NOTES:
41 : * @update gess 4/1/98
42 : *
43 : * This class is defines the basic notion of a token
44 : * within our system. All other tokens are derived from
45 : * this one. It offers a few basic interfaces, but the
46 : * most important is consume(). The consume() method gets
47 : * called during the tokenization process when an instance
48 : * of that particular token type gets detected in the
49 : * input stream.
50 : *
51 : * CToken objects that are allocated from the heap _must_ be allocated
52 : * using the nsTokenAllocator: the nsTokenAllocator object uses an
53 : * arena to manage the tokens.
54 : *
55 : * The nsTokenAllocator object's arena implementation requires
56 : * object size at destruction time to properly recycle the object;
57 : * therefore, CToken::operator delete() is not public. Instead,
58 : * heap-allocated tokens should be destroyed using the static
59 : * Destroy() method, which accepts a token and the arena from which
60 : * the token was allocated.
61 : *
62 : * Leaf classes (that are actually instantiated from the heap) must
63 : * implement the SizeOf() method, which Destroy() uses to determine
64 : * the size of the token in order to properly recycle it.
65 : */
66 :
67 :
68 : #ifndef CTOKEN__
69 : #define CTOKEN__
70 :
71 : #include "prtypes.h"
72 : #include "nsString.h"
73 : #include "nsError.h"
74 : #include "nsFixedSizeAllocator.h"
75 :
76 : #define NS_HTMLTOKENS_NOT_AN_ENTITY \
77 : NS_ERROR_GENERATE_SUCCESS(NS_ERROR_MODULE_HTMLPARSER,2000)
78 :
79 : class nsScanner;
80 : class nsTokenAllocator;
81 :
82 : enum eContainerInfo {
83 : eWellFormed,
84 : eMalformed,
85 : eFormUnknown
86 : };
87 :
88 : /**
89 : * Implement the SizeOf() method; leaf classes derived from CToken
90 : * must declare this.
91 : */
92 : #define CTOKEN_IMPL_SIZEOF \
93 : protected: \
94 : virtual size_t SizeOf() const { return sizeof(*this); } \
95 : public:
96 :
97 : /**
98 : * Token objects represent sequences of characters as they
99 : * are consumed from the input stream (URL). While they're
100 : * pretty general in nature, we use subclasses (found in
101 : * nsHTMLTokens.h) to define <start>, </end>, <text>,
102 : * <comment>, <&entity>, <newline>, and <whitespace> tokens.
103 : *
104 : * @update gess 3/25/98
105 : */
106 : class CToken {
107 : public:
108 :
109 : enum eTokenOrigin {eSource,eResidualStyle};
110 :
111 : protected:
112 :
113 : // nsTokenAllocator should be the only class that tries to
114 : // allocate tokens from the heap.
115 : friend class nsTokenAllocator;
116 :
117 : /**
118 : *
119 : * @update harishd 08/01/00
120 : * @param aSize -
121 : * @param aArena - Allocate memory from this pool.
122 : */
123 3093 : static void * operator new (size_t aSize,nsFixedSizeAllocator& anArena) CPP_THROW_NEW
124 : {
125 3093 : return anArena.Alloc(aSize);
126 : }
127 :
128 : /**
129 : * Hide operator delete; clients should use Destroy() instead.
130 : */
131 0 : static void operator delete (void*,size_t) {}
132 :
133 : protected:
134 : /**
135 : * destructor
136 : * @update gess5/11/98
137 : */
138 : virtual ~CToken();
139 :
140 : private:
141 : /**
142 : * Destroy a token.
143 : */
144 3093 : static void Destroy(CToken* aToken,nsFixedSizeAllocator& aArenaPool)
145 : {
146 3093 : size_t sz = aToken->SizeOf();
147 3093 : aToken->~CToken();
148 3093 : aArenaPool.Free(aToken, sz);
149 3093 : }
150 :
151 : public:
152 : /**
153 : * Make a note on number of times you have been referenced
154 : * @update harishd 08/02/00
155 : */
156 1967 : void AddRef() {
157 1967 : ++mUseCount;
158 1967 : NS_LOG_ADDREF(this, mUseCount, "CToken", sizeof(*this));
159 1967 : }
160 :
161 : /**
162 : * Free yourself if no one is holding you.
163 : * @update harishd 08/02/00
164 : */
165 5060 : void Release(nsFixedSizeAllocator& aArenaPool) {
166 5060 : --mUseCount;
167 5060 : NS_LOG_RELEASE(this, mUseCount, "CToken");
168 5060 : if (mUseCount==0)
169 3093 : Destroy(this, aArenaPool);
170 5060 : }
171 :
172 : /**
173 : * Default constructor
174 : * @update gess7/21/98
175 : */
176 : CToken(PRInt32 aTag=0);
177 :
178 : /**
179 : * Retrieve string value of the token
180 : * @update gess5/11/98
181 : * @return reference to string containing string value
182 : */
183 : virtual const nsSubstring& GetStringValue(void) = 0;
184 :
185 : /**
186 : * Get string of full contents, suitable for debug dump.
187 : * It should look exactly like the input source.
188 : * @update gess5/11/98
189 : * @return reference to string containing string value
190 : */
191 : virtual void GetSource(nsString& anOutputString);
192 :
193 : /** @update harishd 03/23/00
194 : * @return reference to string containing string value
195 : */
196 : virtual void AppendSourceTo(nsAString& anOutputString);
197 :
198 : /**
199 : * Sets the ordinal value of this token (not currently used)
200 : * @update gess5/11/98
201 : * @param value is the new ord value for this token
202 : */
203 0 : void SetTypeID(PRInt32 aValue) {
204 0 : mTypeID = aValue;
205 0 : }
206 :
207 : /**
208 : * Getter which retrieves the current ordinal value for this token
209 : * @update gess5/11/98
210 : * @return current ordinal value
211 : */
212 : virtual PRInt32 GetTypeID(void);
213 :
214 : /**
215 : * Getter which retrieves the current attribute count for this token
216 : * @update gess5/11/98
217 : * @return current attribute count
218 : */
219 : virtual PRInt16 GetAttributeCount(void);
220 :
221 : /**
222 : * Causes token to consume data from given scanner.
223 : * Note that behavior varies wildly between CToken subclasses.
224 : * @update gess5/11/98
225 : * @param aChar -- most recent char consumed
226 : * @param aScanner -- input source where token should get data
227 : * @return error code (0 means ok)
228 : */
229 : virtual nsresult Consume(PRUnichar aChar,nsScanner& aScanner,PRInt32 aMode);
230 :
231 : /**
232 : * Getter which retrieves type of token
233 : * @update gess5/11/98
234 : * @return int containing token type
235 : */
236 : virtual PRInt32 GetTokenType(void);
237 :
238 : /**
239 : * For tokens who care, this can tell us whether the token is
240 : * well formed or not.
241 : *
242 : * @update gess 8/30/00
243 : * @return false; subclasses MUST override if they care.
244 : */
245 0 : virtual bool IsWellFormed(void) const {return false;}
246 :
247 0 : virtual bool IsEmpty(void) { return false; }
248 :
249 : /**
250 : * If aValue is TRUE then the token represents a short-hand tag
251 : */
252 0 : virtual void SetEmpty(bool aValue) { return ; }
253 :
254 3147 : PRInt32 GetNewlineCount()
255 : {
256 3147 : return mNewlineCount;
257 : }
258 :
259 54 : void SetNewlineCount(PRInt32 aCount)
260 : {
261 54 : mNewlineCount = aCount;
262 54 : }
263 :
264 0 : PRInt32 GetLineNumber()
265 : {
266 0 : return mLineNumber;
267 : }
268 :
269 2415 : void SetLineNumber(PRInt32 aLineNumber)
270 : {
271 2415 : mLineNumber = mLineNumber == 0 ? aLineNumber : mLineNumber;
272 2415 : }
273 :
274 25 : void SetInError(bool aInError)
275 : {
276 25 : mInError = aInError;
277 25 : }
278 :
279 0 : bool IsInError()
280 : {
281 0 : return mInError;
282 : }
283 :
284 237 : void SetAttributeCount(PRInt16 aValue) { mAttrCount = aValue; }
285 :
286 : /**
287 : * perform self test.
288 : * @update gess5/11/98
289 : */
290 : virtual void SelfTest(void);
291 :
292 : static int GetTokenCount();
293 :
294 :
295 :
296 : protected:
297 : /**
298 : * Returns the size of the token object.
299 : */
300 : virtual size_t SizeOf() const = 0;
301 :
302 : PRInt32 mTypeID;
303 : PRInt32 mUseCount;
304 : PRInt32 mNewlineCount;
305 : PRUint32 mLineNumber : 31;
306 : PRUint32 mInError : 1;
307 : PRInt16 mAttrCount;
308 : };
309 :
310 :
311 :
312 : #endif
313 :
314 :
|