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