File: txXPathTreeWalker.h

package info (click to toggle)
kompozer 1%3A0.8~b3.dfsg.1-0.1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 283,956 kB
  • ctags: 314,166
  • sloc: cpp: 1,763,181; ansic: 990,028; xml: 97,969; makefile: 46,334; asm: 34,989; perl: 26,943; sh: 20,165; cs: 6,232; java: 5,513; python: 3,221; pascal: 340; lex: 306; php: 244; csh: 132; objc: 97; yacc: 79; ada: 49; awk: 14; sql: 4; sed: 4
file content (350 lines) | stat: -rw-r--r-- 10,210 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is TransforMiiX XSLT processor code.
 *
 * The Initial Developer of the Original Code is
 * Netscape Communications Corporation.
 * Portions created by the Initial Developer are Copyright (C) 2003
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   Peter Van der Beken <peterv@propagandism.org>
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

#ifndef txXPathTreeWalker_h__
#define txXPathTreeWalker_h__

#include "txCore.h"
#include "txXPathNode.h"

class nsIAtom;

#ifndef TX_EXE
#include "nsINodeInfo.h"
#include "nsVoidArray.h"

class txUint32Array : public nsVoidArray
{
public:
    PRBool AppendValue(PRUint32 aValue)
    {
        return InsertElementAt(NS_INT32_TO_PTR(aValue), Count());
    }
    PRBool RemoveValueAt(PRUint32 aIndex)
    {
        return RemoveElementsAt(aIndex, 1);
    }
    PRInt32 ValueAt(PRUint32 aIndex) const
    {
        return NS_PTR_TO_INT32(ElementAt(aIndex));
    }
};

class nsIDOMDocument;
#endif

class txXPathTreeWalker
{
public:
    txXPathTreeWalker(const txXPathTreeWalker& aOther);
    explicit txXPathTreeWalker(const txXPathNode& aNode);
    ~txXPathTreeWalker();

    PRBool getAttr(nsIAtom* aLocalName, PRInt32 aNSID, nsAString& aValue) const;
    PRInt32 getNamespaceID() const;
    PRUint16 getNodeType() const;
    void appendNodeValue(nsAString& aResult) const;
    void getNodeName(nsAString& aName) const;

    void moveTo(const txXPathTreeWalker& aWalker);

    PRBool moveToParent();
    PRBool moveToElementById(const nsAString& aID);
    PRBool moveToFirstAttribute();
    PRBool moveToNextAttribute();
    PRBool moveToFirstChild();
    PRBool moveToLastChild();
    PRBool moveToNextSibling();
    PRBool moveToPreviousSibling();

    PRBool isOnNode(const txXPathNode& aNode) const;

    const txXPathNode& getCurrentPosition() const;

private:
    txXPathNode mPosition;

#ifndef TX_EXE
    PRBool moveToValidAttribute(PRUint32 aStartIndex);
    PRBool moveToSibling(PRInt32 aDir);

    PRUint32 mCurrentIndex;
    txUint32Array mDescendants;
#endif
};

class txXPathNodeUtils
{
public:
    static PRBool getAttr(const txXPathNode& aNode, nsIAtom* aLocalName,
                          PRInt32 aNSID, nsAString& aValue);
    static already_AddRefed<nsIAtom> getLocalName(const txXPathNode& aNode);
    static void getLocalName(const txXPathNode& aNode, nsAString& aLocalName);
    static void getNodeName(const txXPathNode& aNode,
                            nsAString& aName);
    static PRInt32 getNamespaceID(const txXPathNode& aNode);
    static void getNamespaceURI(const txXPathNode& aNode, nsAString& aURI);
    static PRUint16 getNodeType(const txXPathNode& aNode);
    static void appendNodeValue(const txXPathNode& aNode, nsAString& aResult);
    static PRBool isWhitespace(const txXPathNode& aNode);
    static txXPathNode* getDocument(const txXPathNode& aNode);
    static txXPathNode* getOwnerDocument(const txXPathNode& aNode);
    static PRInt32 getUniqueIdentifier(const txXPathNode& aNode);
    static nsresult getXSLTId(const txXPathNode& aNode, nsAString& aResult);
    static void release(txXPathNode* aNode);
    static void getBaseURI(const txXPathNode& aNode, nsAString& aURI);
    static PRIntn comparePosition(const txXPathNode& aNode,
                                  const txXPathNode& aOtherNode);
    static PRBool localNameEquals(const txXPathNode& aNode,
                                  nsIAtom* aLocalName);
    static PRBool isRoot(const txXPathNode& aNode);
    static PRBool isElement(const txXPathNode& aNode);
    static PRBool isAttribute(const txXPathNode& aNode);
    static PRBool isProcessingInstruction(const txXPathNode& aNode);
    static PRBool isComment(const txXPathNode& aNode);
    static PRBool isText(const txXPathNode& aNode);

#ifdef TX_EXE
private:
    static void appendNodeValueHelper(NodeDefinition* aNode, nsAString& aResult);
#endif
};

#ifdef TX_EXE
class txXPathNativeNode
{
public:
    static txXPathNode* createXPathNode(Node* aNode);
    static nsresult getElement(const txXPathNode& aNode, Element** aResult);
    static nsresult getDocument(const txXPathNode& aNode, Document** aResult);
};
#else
class txXPathNativeNode
{
public:
    static txXPathNode* createXPathNode(nsIDOMNode* aNode);
    static txXPathNode* createXPathNode(nsIDOMDocument* aDocument);
    static nsresult getNode(const txXPathNode& aNode, nsIDOMNode** aResult);
    static nsIContent* getContent(const txXPathNode& aNode);
    static nsIDocument* getDocument(const txXPathNode& aNode);
    static void addRef(const txXPathNode& aNode)
    {
        // Hopefully it's ok to access mContent through mDocument.
        NS_ADDREF(aNode.mDocument);
    }
    static void release(const txXPathNode& aNode)
    {
        // Hopefully it's ok to access mContent through mDocument.
        nsISupports *node = aNode.mDocument;
        NS_RELEASE(node);
    }
};

#endif

inline const txXPathNode&
txXPathTreeWalker::getCurrentPosition() const
{
    return mPosition;
}

inline PRBool
txXPathTreeWalker::getAttr(nsIAtom* aLocalName, PRInt32 aNSID,
                           nsAString& aValue) const
{
    return txXPathNodeUtils::getAttr(mPosition, aLocalName, aNSID, aValue);
}

inline PRInt32
txXPathTreeWalker::getNamespaceID() const
{
    return txXPathNodeUtils::getNamespaceID(mPosition);
}

inline void
txXPathTreeWalker::appendNodeValue(nsAString& aResult) const
{
    txXPathNodeUtils::appendNodeValue(mPosition, aResult);
}

inline void
txXPathTreeWalker::getNodeName(nsAString& aName) const
{
    txXPathNodeUtils::getNodeName(mPosition, aName);
}

inline void
txXPathTreeWalker::moveTo(const txXPathTreeWalker& aWalker)
{
#ifdef TX_EXE
    mPosition.mInner = aWalker.mPosition.mInner;
#else
    mPosition.mIndex = aWalker.mPosition.mIndex;
    // Hopefully it's ok to access mContent through mDocument.
    mPosition.mDocument = aWalker.mPosition.mDocument;
    mCurrentIndex = aWalker.mCurrentIndex;
    mDescendants.Clear();
#endif
}

inline PRBool
txXPathTreeWalker::isOnNode(const txXPathNode& aNode) const
{
    return (mPosition == aNode);
}

/* static */
inline PRInt32
txXPathNodeUtils::getUniqueIdentifier(const txXPathNode& aNode)
{
#ifdef TX_EXE
    return NS_PTR_TO_INT32(aNode.mInner);
#else
    NS_PRECONDITION(aNode.mIndex == txXPathNode::eDocument,
                    "Only implemented for documents.");
    return NS_PTR_TO_INT32(aNode.mDocument);
#endif
}

/* static */
inline void
txXPathNodeUtils::release(txXPathNode* aNode)
{
#ifdef TX_EXE
    delete aNode->mInner;
#else
    NS_RELEASE(aNode->mDocument);
#endif
}

/* static */
inline PRBool
txXPathNodeUtils::localNameEquals(const txXPathNode& aNode,
                                  nsIAtom* aLocalName)
{
#ifdef TX_EXE
    nsCOMPtr<nsIAtom> localName;
    aNode.mInner->getLocalName(getter_AddRefs(localName));

    return localName == aLocalName;
#else
    if (aNode.isContent()) {
        nsINodeInfo *ni = aNode.mContent->GetNodeInfo();
        if (ni) {
            return ni->Equals(aLocalName);
        }
    }

    nsCOMPtr<nsIAtom> localName = txXPathNodeUtils::getLocalName(aNode);

    return localName == aLocalName;
#endif
}

/* static */
inline PRBool
txXPathNodeUtils::isRoot(const txXPathNode& aNode)
{
#ifdef TX_EXE
    return aNode.mInner->getNodeType() == Node::DOCUMENT_NODE;
#else
    return aNode.isDocument();
#endif
}

/* static */
inline PRBool
txXPathNodeUtils::isElement(const txXPathNode& aNode)
{
#ifdef TX_EXE
    return aNode.mInner->getNodeType() == Node::ELEMENT_NODE;
#else
    return aNode.isContent() &&
           aNode.mContent->IsContentOfType(nsIContent::eELEMENT);
#endif
}


/* static */
inline PRBool
txXPathNodeUtils::isAttribute(const txXPathNode& aNode)
{
#ifdef TX_EXE
    return aNode.mInner->getNodeType() == Node::ATTRIBUTE_NODE;
#else
    return aNode.isAttribute();
#endif
}

/* static */
inline PRBool
txXPathNodeUtils::isProcessingInstruction(const txXPathNode& aNode)
{
#ifdef TX_EXE
    return aNode.mInner->getNodeType() == Node::PROCESSING_INSTRUCTION_NODE;
#else
    return aNode.isContent() &&
           aNode.mContent->IsContentOfType(nsIContent::ePROCESSING_INSTRUCTION);
#endif
}

/* static */
inline PRBool
txXPathNodeUtils::isComment(const txXPathNode& aNode)
{
#ifdef TX_EXE
    return aNode.mInner->getNodeType() == Node::COMMENT_NODE;
#else
    return aNode.isContent() &&
           aNode.mContent->IsContentOfType(nsIContent::eCOMMENT);
#endif
}

/* static */
inline PRBool
txXPathNodeUtils::isText(const txXPathNode& aNode)
{
#ifdef TX_EXE
    return aNode.mInner->getNodeType() == Node::TEXT_NODE;
#else
    return aNode.isContent() &&
           aNode.mContent->IsContentOfType(nsIContent::eTEXT);
#endif
}

#endif /* txXPathTreeWalker_h__ */