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 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.
49 : *
50 : * If you want to hang onto one of these, you should
51 : * make your own copy.
52 : *
53 : */
54 :
55 : #ifndef NS_PARSERNODE__
56 : #define NS_PARSERNODE__
57 :
58 : #include "nsIParserNode.h"
59 : #include "nsToken.h"
60 : #include "nsString.h"
61 : #include "nsParserCIID.h"
62 : #include "nsDeque.h"
63 : #include "nsDTDUtils.h"
64 :
65 : class nsTokenAllocator;
66 :
67 : class nsCParserNode : public nsIParserNode {
68 :
69 : protected:
70 :
71 : PRInt32 mRefCnt;
72 :
73 : public:
74 :
75 2114 : void AddRef()
76 : {
77 2114 : ++mRefCnt;
78 2114 : }
79 :
80 2114 : void Release(nsFixedSizeAllocator& aPool)
81 : {
82 2114 : if (--mRefCnt == 0)
83 1967 : Destroy(this, aPool);
84 2114 : }
85 :
86 : #ifndef HEAP_ALLOCATED_NODES
87 : protected:
88 :
89 : /**
90 : * Hide operator new; clients should use Create() instead.
91 : */
92 : static void* operator new(size_t) CPP_THROW_NEW { return 0; }
93 :
94 : /**
95 : * Hide operator delete; clients should use Destroy() instead.
96 : */
97 0 : static void operator delete(void*,size_t) {}
98 :
99 : #endif
100 :
101 : public:
102 1117 : static nsCParserNode* Create(CToken* aToken,
103 : nsTokenAllocator* aTokenAllocator,
104 : nsNodeAllocator* aNodeAllocator)
105 : {
106 : #ifdef HEAP_ALLOCATED_NODES
107 : return new
108 : #else
109 1117 : nsFixedSizeAllocator& pool = aNodeAllocator->GetArenaPool();
110 1117 : void* place = pool.Alloc(sizeof(nsCParserNode));
111 1117 : NS_ENSURE_TRUE(place, nsnull);
112 : return ::new (place)
113 : #endif
114 1117 : nsCParserNode(aToken, aTokenAllocator, aNodeAllocator);
115 : }
116 :
117 1967 : static void Destroy(nsCParserNode* aNode, nsFixedSizeAllocator& aPool)
118 : {
119 : #ifdef HEAP_ALLOCATED_NODES
120 : delete aNode;
121 : #else
122 1967 : aNode->~nsCParserNode();
123 1967 : aPool.Free(aNode, sizeof(*aNode));
124 : #endif
125 1967 : }
126 :
127 : /**
128 : * Default constructor
129 : */
130 : nsCParserNode();
131 :
132 : /**
133 : * Constructor
134 : * @update gess5/11/98
135 : * @param aToken is the token this node "refers" to
136 : */
137 : nsCParserNode(CToken* aToken,
138 : nsTokenAllocator* aTokenAllocator,
139 : nsNodeAllocator* aNodeAllocator=0);
140 :
141 : /**
142 : * Destructor
143 : * @update gess5/11/98
144 : */
145 : virtual ~nsCParserNode();
146 :
147 : /**
148 : * Init
149 : * @update gess5/11/98
150 : */
151 : virtual nsresult Init(CToken* aToken,
152 : nsTokenAllocator* aTokenAllocator,
153 : nsNodeAllocator* aNodeAllocator=0);
154 :
155 : /**
156 : * Retrieve the name of the node
157 : * @update gess5/11/98
158 : * @return string containing node name
159 : */
160 : virtual const nsAString& GetTagName() const;
161 :
162 : /**
163 : * Retrieve the text from the given node
164 : * @update gess5/11/98
165 : * @return string containing node text
166 : */
167 : virtual const nsAString& GetText() const;
168 :
169 : /**
170 : * Retrieve the type of the parser node.
171 : * @update gess5/11/98
172 : * @return node type.
173 : */
174 : virtual PRInt32 GetNodeType() const;
175 :
176 : /**
177 : * Retrieve token type of parser node
178 : * @update gess5/11/98
179 : * @return token type
180 : */
181 : virtual PRInt32 GetTokenType() const;
182 :
183 :
184 : //***************************************
185 : //methods for accessing key/value pairs
186 : //***************************************
187 :
188 : /**
189 : * Retrieve the number of attributes in this node.
190 : * @update gess5/11/98
191 : * @return count of attributes (may be 0)
192 : */
193 : virtual PRInt32 GetAttributeCount(bool askToken=false) const;
194 :
195 : /**
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.
200 : */
201 : virtual const nsAString& GetKeyAt(PRUint32 anIndex) const;
202 :
203 : /**
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.
208 : */
209 : virtual const nsAString& GetValueAt(PRUint32 anIndex) const;
210 :
211 : /**
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)
217 : */
218 : virtual PRInt32 TranslateToUnicodeStr(nsString& aString) const;
219 :
220 : /**
221 : *
222 : * @update gess5/11/98
223 : * @param
224 : * @return
225 : */
226 : virtual void AddAttribute(CToken* aToken);
227 :
228 : /**
229 : * This getter retrieves the line number from the input source where
230 : * the token occurred. Lines are interpreted as occurring between \n characters.
231 : * @update gess7/24/98
232 : * @return int containing the line number the token was found on
233 : */
234 : virtual PRInt32 GetSourceLineNumber(void) const;
235 :
236 : /** This method pop the attribute token from the given index
237 : * @update harishd 03/25/99
238 : * @return token at anIndex
239 : */
240 : virtual CToken* PopAttributeToken();
241 :
242 : /** Like PopAttributeToken, but pops off the front of the attribute list */
243 : virtual CToken* PopAttributeTokenFront();
244 :
245 : /** Retrieve a string containing the tag and its attributes in "source" form
246 : * @update rickg 06June2000
247 : * @return void
248 : */
249 : virtual void GetSource(nsString& aString) const;
250 :
251 : /**
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
255 : */
256 0 : virtual bool GetGenericState(void) const {return mGenericState;}
257 0 : virtual void SetGenericState(bool aState) {mGenericState=aState;}
258 :
259 : /** Release all the objects you're holding
260 : * @update harishd 08/02/00
261 : * @return void
262 : */
263 : virtual nsresult ReleaseAll();
264 :
265 : bool mGenericState;
266 : PRInt32 mUseCount;
267 : CToken* mToken;
268 :
269 : nsTokenAllocator* mTokenAllocator;
270 : #ifdef HEAP_ALLOCATED_NODES
271 : nsNodeAllocator* mNodeAllocator; // weak
272 : #endif
273 : };
274 :
275 :
276 : class nsCParserStartNode : public nsCParserNode
277 : {
278 : public:
279 850 : static nsCParserNode* Create(CToken* aToken,
280 : nsTokenAllocator* aTokenAllocator,
281 : nsNodeAllocator* aNodeAllocator)
282 : {
283 : #ifdef HEAP_ALLOCATED_NODES
284 : return new
285 : #else
286 850 : nsFixedSizeAllocator& pool = aNodeAllocator->GetArenaPool();
287 850 : void* place = pool.Alloc(sizeof(nsCParserStartNode));
288 850 : NS_ENSURE_TRUE(place, nsnull);
289 : return ::new (place)
290 : #endif
291 850 : nsCParserStartNode(aToken, aTokenAllocator, aNodeAllocator);
292 : }
293 :
294 : nsCParserStartNode()
295 : : nsCParserNode(), mAttributes(0) { }
296 :
297 850 : nsCParserStartNode(CToken* aToken,
298 : nsTokenAllocator* aTokenAllocator,
299 : nsNodeAllocator* aNodeAllocator = 0)
300 850 : : nsCParserNode(aToken, aTokenAllocator, aNodeAllocator), mAttributes(0) { }
301 :
302 850 : virtual ~nsCParserStartNode()
303 1700 : {
304 850 : NS_ASSERTION(mTokenAllocator || mAttributes.GetSize() == 0,
305 : "Error: no token allocator");
306 850 : CToken* theAttrToken = 0;
307 2432 : while ((theAttrToken = static_cast<CToken*>(mAttributes.Pop()))) {
308 732 : IF_FREE(theAttrToken, mTokenAllocator);
309 : }
310 850 : }
311 :
312 : virtual nsresult Init(CToken* aToken,
313 : nsTokenAllocator* aTokenAllocator,
314 : nsNodeAllocator* aNodeAllocator = 0);
315 : virtual void AddAttribute(CToken* aToken);
316 : virtual PRInt32 GetAttributeCount(bool askToken = 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();
323 : protected:
324 : nsDeque mAttributes;
325 : };
326 :
327 : #endif
328 :
|