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
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/
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
15 * The Original Code is mozilla.org code.
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.
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.
36 * ***** END LICENSE BLOCK ***** */
43 * This class is defines the basic interface between the
44 * parser and the content sink. The parser will iterate
45 * over the collection of tokens that it sees from the
46 * tokenizer, coverting each related "group" into one of
47 * these. This object gets passed to the sink, and is
48 * then immediately reused.
50 * If you want to hang onto one of these, you should
55 #ifndef NS_PARSERNODE__
56 #define NS_PARSERNODE__
58 #include "nsIParserNode.h"
61 #include "nsParserCIID.h"
63 #include "nsDTDUtils.h"
65 class nsTokenAllocator
;
67 class nsCParserNode
: public nsIParserNode
{
80 void Release(nsFixedSizeAllocator
& aPool
)
86 #ifndef HEAP_ALLOCATED_NODES
90 * Hide operator new; clients should use Create() instead.
92 static void* operator new(size_t) CPP_THROW_NEW
{ return 0; }
95 * Hide operator delete; clients should use Destroy() instead.
97 static void operator delete(void*,size_t) {}
102 static nsCParserNode
* Create(CToken
* aToken
,
103 nsTokenAllocator
* aTokenAllocator
,
104 nsNodeAllocator
* aNodeAllocator
)
106 #ifdef HEAP_ALLOCATED_NODES
109 nsFixedSizeAllocator
& pool
= aNodeAllocator
->GetArenaPool();
110 void* place
= pool
.Alloc(sizeof(nsCParserNode
));
111 NS_ENSURE_TRUE(place
, nsnull
);
114 nsCParserNode(aToken
, aTokenAllocator
, aNodeAllocator
);
117 static void Destroy(nsCParserNode
* aNode
, nsFixedSizeAllocator
& aPool
)
119 #ifdef HEAP_ALLOCATED_NODES
122 aNode
->~nsCParserNode();
123 aPool
.Free(aNode
, sizeof(*aNode
));
128 * Default constructor
134 * @update gess5/11/98
135 * @param aToken is the token this node "refers" to
137 nsCParserNode(CToken
* aToken
,
138 nsTokenAllocator
* aTokenAllocator
,
139 nsNodeAllocator
* aNodeAllocator
=0);
143 * @update gess5/11/98
145 virtual ~nsCParserNode();
149 * @update gess5/11/98
151 virtual nsresult
Init(CToken
* aToken
,
152 nsTokenAllocator
* aTokenAllocator
,
153 nsNodeAllocator
* aNodeAllocator
=0);
156 * Retrieve the name of the node
157 * @update gess5/11/98
158 * @return string containing node name
160 virtual const nsAString
& GetTagName() const;
163 * Retrieve the text from the given node
164 * @update gess5/11/98
165 * @return string containing node text
167 virtual const nsAString
& GetText() const;
170 * Retrieve the type of the parser node.
171 * @update gess5/11/98
174 virtual PRInt32
GetNodeType() const;
177 * Retrieve token type of parser node
178 * @update gess5/11/98
181 virtual PRInt32
GetTokenType() const;
184 //***************************************
185 //methods for accessing key/value pairs
186 //***************************************
189 * Retrieve the number of attributes in this node.
190 * @update gess5/11/98
191 * @return count of attributes (may be 0)
193 virtual PRInt32
GetAttributeCount(PRBool askToken
=PR_FALSE
) const;
196 * Retrieve the key (of key/value pair) at given index
197 * @update gess5/11/98
198 * @param anIndex is the index of the key you want
199 * @return string containing key.
201 virtual const nsAString
& GetKeyAt(PRUint32 anIndex
) const;
204 * Retrieve the value (of key/value pair) at given index
205 * @update gess5/11/98
206 * @param anIndex is the index of the value you want
207 * @return string containing value.
209 virtual const nsAString
& GetValueAt(PRUint32 anIndex
) const;
212 * NOTE: When the node is an entity, this will translate the entity
213 * to it's unicode value, and store it in aString.
214 * @update gess5/11/98
215 * @param aString will contain the resulting unicode string value
216 * @return int (unicode char or unicode index from table)
218 virtual PRInt32
TranslateToUnicodeStr(nsString
& aString
) const;
222 * @update gess5/11/98
226 virtual void AddAttribute(CToken
* aToken
);
229 * This getter retrieves the line number from the input source where
230 * the token occured. Lines are interpreted as occuring between \n characters.
231 * @update gess7/24/98
232 * @return int containing the line number the token was found on
234 virtual PRInt32
GetSourceLineNumber(void) const;
236 /** This method pop the attribute token from the given index
237 * @update harishd 03/25/99
238 * @return token at anIndex
240 virtual CToken
* PopAttributeToken();
242 /** Like PopAttributeToken, but pops off the front of the attribute list */
243 virtual CToken
* PopAttributeTokenFront();
245 /** Retrieve a string containing the tag and its attributes in "source" form
246 * @update rickg 06June2000
249 virtual void GetSource(nsString
& aString
) const;
252 * This pair of methods allows us to set a generic bit (for arbitrary use)
253 * on each node stored in the context.
254 * @update gess 11May2000
256 virtual PRBool
GetGenericState(void) const {return mGenericState
;}
257 virtual void SetGenericState(PRBool aState
) {mGenericState
=aState
;}
259 /** Release all the objects you're holding
260 * @update harishd 08/02/00
263 virtual nsresult
ReleaseAll();
265 PRPackedBool mGenericState
;
269 nsTokenAllocator
* mTokenAllocator
;
270 #ifdef HEAP_ALLOCATED_NODES
271 nsNodeAllocator
* mNodeAllocator
; // weak
276 class nsCParserStartNode
: public nsCParserNode
279 static nsCParserNode
* Create(CToken
* aToken
,
280 nsTokenAllocator
* aTokenAllocator
,
281 nsNodeAllocator
* aNodeAllocator
)
283 #ifdef HEAP_ALLOCATED_NODES
286 nsFixedSizeAllocator
& pool
= aNodeAllocator
->GetArenaPool();
287 void* place
= pool
.Alloc(sizeof(nsCParserStartNode
));
288 NS_ENSURE_TRUE(place
, nsnull
);
291 nsCParserStartNode(aToken
, aTokenAllocator
, aNodeAllocator
);
295 : nsCParserNode(), mAttributes(0) { }
297 nsCParserStartNode(CToken
* aToken
,
298 nsTokenAllocator
* aTokenAllocator
,
299 nsNodeAllocator
* aNodeAllocator
= 0)
300 : nsCParserNode(aToken
, aTokenAllocator
, aNodeAllocator
), mAttributes(0) { }
302 virtual ~nsCParserStartNode()
304 NS_ASSERTION(mTokenAllocator
|| mAttributes
.GetSize() == 0,
305 "Error: no token allocator");
306 CToken
* theAttrToken
= 0;
307 while ((theAttrToken
= static_cast<CToken
*>(mAttributes
.Pop()))) {
308 IF_FREE(theAttrToken
, mTokenAllocator
);
312 virtual nsresult
Init(CToken
* aToken
,
313 nsTokenAllocator
* aTokenAllocator
,
314 nsNodeAllocator
* aNodeAllocator
= 0);
315 virtual void AddAttribute(CToken
* aToken
);
316 virtual PRInt32
GetAttributeCount(PRBool askToken
= PR_FALSE
) const;
317 virtual const nsAString
& GetKeyAt(PRUint32 anIndex
) const;
318 virtual const nsAString
& GetValueAt(PRUint32 anIndex
) const;
319 virtual CToken
* PopAttributeToken();
320 virtual CToken
* PopAttributeTokenFront();
321 virtual void GetSource(nsString
& aString
) const;
322 virtual nsresult
ReleaseAll();