~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/htmlparser/src/nsParserNode.h

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 interface between the
 
45
 * parser and the content sink. The parser will iterate
 
46
 * over the collection of tokens that it sees from the
 
47
 * tokenizer, coverting each related "group" into one of
 
48
 * these. This object gets passed to the sink, and is
 
49
 * then immediately reused.
 
50
 *
 
51
 * If you want to hang onto one of these, you should
 
52
 * make your own copy.
 
53
 *
 
54
 */
 
55
 
 
56
#ifndef NS_PARSERNODE__
 
57
#define NS_PARSERNODE__
 
58
 
 
59
#include "nsIParserNode.h"
 
60
#include "nsToken.h"
 
61
#include "nsString.h"
 
62
#include "nsParserCIID.h"
 
63
#include "nsDeque.h"
 
64
#include "nsDTDUtils.h"
 
65
 
 
66
class nsTokenAllocator;
 
67
 
 
68
class nsCParserNode :  public nsIParserNode {
 
69
 
 
70
  protected:
 
71
 
 
72
    PRInt32 mRefCnt;
 
73
 
 
74
  public:
 
75
 
 
76
    void AddRef()
 
77
    {
 
78
      ++mRefCnt;
 
79
    }
 
80
 
 
81
    void Release(nsFixedSizeAllocator& aPool)
 
82
    {
 
83
      if (--mRefCnt == 0)
 
84
        Destroy(this, aPool);
 
85
    }
 
86
 
 
87
#ifndef HEAP_ALLOCATED_NODES
 
88
  protected:
 
89
 
 
90
    /**
 
91
     * Hide operator new; clients should use Create() instead.
 
92
     */
 
93
    static void* operator new(size_t) CPP_THROW_NEW { return 0; }
 
94
 
 
95
    /**
 
96
     * Hide operator delete; clients should use Destroy() instead.
 
97
     */
 
98
    static void operator delete(void*,size_t) {}
 
99
 
 
100
#endif
 
101
 
 
102
  public:
 
103
    static nsCParserNode* Create(CToken* aToken,
 
104
                                 nsTokenAllocator* aTokenAllocator,
 
105
                                 nsNodeAllocator* aNodeAllocator)
 
106
    {
 
107
#ifdef HEAP_ALLOCATED_NODES
 
108
      return new
 
109
#else
 
110
      nsFixedSizeAllocator& pool = aNodeAllocator->GetArenaPool();
 
111
      void* place = pool.Alloc(sizeof(nsCParserNode));
 
112
      return ::new (place)
 
113
#endif
 
114
        nsCParserNode(aToken, aTokenAllocator, aNodeAllocator);
 
115
    }
 
116
 
 
117
    static void Destroy(nsCParserNode* aNode, nsFixedSizeAllocator& aPool)
 
118
    {
 
119
#ifdef HEAP_ALLOCATED_NODES
 
120
      delete aNode;
 
121
#else
 
122
      aNode->~nsCParserNode();
 
123
      aPool.Free(aNode, sizeof(*aNode));
 
124
#endif
 
125
    }
 
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(PRBool askToken=PR_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 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
 
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
    /** Retrieve a string containing the tag and its attributes in "source" form
 
243
     * @update  rickg 06June2000
 
244
     * @return  void
 
245
     */
 
246
    virtual void GetSource(nsString& aString);
 
247
 
 
248
    /**
 
249
     * This pair of methods allows us to set a generic bit (for arbitrary use)
 
250
     * on each node stored in the context.
 
251
     * @update  gess 11May2000
 
252
     */
 
253
    virtual PRBool  GetGenericState(void) const {return mGenericState;}
 
254
    virtual void    SetGenericState(PRBool aState) {mGenericState=aState;}
 
255
 
 
256
    /** Release all the objects you're holding
 
257
     * @update  harishd 08/02/00
 
258
     * @return  void
 
259
     */
 
260
    virtual nsresult ReleaseAll();
 
261
 
 
262
    CToken*      mToken;
 
263
    PRInt32      mUseCount;
 
264
    PRPackedBool mGenericState;  
 
265
   
 
266
    nsTokenAllocator* mTokenAllocator;
 
267
#ifdef HEAP_ALLOCATED_NODES
 
268
   nsNodeAllocator*  mNodeAllocator; // weak 
 
269
#endif
 
270
};
 
271
 
 
272
 
 
273
class nsCParserStartNode :  public nsCParserNode 
 
274
{
 
275
public:
 
276
    static nsCParserNode* Create(CToken* aToken,
 
277
                                 nsTokenAllocator* aTokenAllocator,
 
278
                                 nsNodeAllocator* aNodeAllocator)
 
279
    {
 
280
#ifdef HEAP_ALLOCATED_NODES
 
281
      return new
 
282
#else
 
283
      nsFixedSizeAllocator& pool = aNodeAllocator->GetArenaPool();
 
284
      void* place = pool.Alloc(sizeof(nsCParserStartNode));
 
285
      return ::new (place)
 
286
#endif
 
287
        nsCParserStartNode(aToken, aTokenAllocator, aNodeAllocator);
 
288
    }
 
289
 
 
290
    nsCParserStartNode() 
 
291
      : nsCParserNode(), mAttributes(0) { }
 
292
 
 
293
    nsCParserStartNode(CToken* aToken, 
 
294
                       nsTokenAllocator* aTokenAllocator, 
 
295
                       nsNodeAllocator* aNodeAllocator = 0) 
 
296
      : nsCParserNode(aToken, aTokenAllocator, aNodeAllocator), mAttributes(0) { }
 
297
 
 
298
    virtual ~nsCParserStartNode() 
 
299
    {
 
300
      NS_ASSERTION(0 != mTokenAllocator, "Error: no token allocator");
 
301
      CToken* theAttrToken = 0;
 
302
      while ((theAttrToken = NS_STATIC_CAST(CToken*, mAttributes.Pop()))) {
 
303
        IF_FREE(theAttrToken, mTokenAllocator);
 
304
      }
 
305
    }
 
306
 
 
307
    virtual nsresult Init(CToken* aToken,
 
308
                          nsTokenAllocator* aTokenAllocator,
 
309
                          nsNodeAllocator* aNodeAllocator = 0);
 
310
    virtual void     AddAttribute(CToken* aToken);
 
311
    virtual PRInt32  GetAttributeCount(PRBool askToken = PR_FALSE) const;
 
312
    virtual const    nsAString& GetKeyAt(PRUint32 anIndex) const;
 
313
    virtual const    nsAString& GetValueAt(PRUint32 anIndex) const;
 
314
    virtual CToken*  PopAttributeToken();
 
315
    virtual void     GetSource(nsString& aString);
 
316
    virtual nsresult ReleaseAll();
 
317
protected:
 
318
    nsDeque  mAttributes;
 
319
};
 
320
 
 
321
#endif
 
322