|
1 /* |
|
2 * Copyright (c) 2005,2006 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Depth iterator walks the dom tree using depth first principle. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 // INCLUDE FILES |
|
21 #include "hspsdomdepthiterator.h" |
|
22 #include "hspsdomlist.h" |
|
23 #include "hspsdomnode.h" |
|
24 |
|
25 // ============================ LOCAL FUNCTIONS =============================== |
|
26 |
|
27 |
|
28 // ============================ MEMBER FUNCTIONS =============================== |
|
29 |
|
30 // ----------------------------------------------------------------------------- |
|
31 // ChspsDomDepthIterator::ChspsDomDepthIterator |
|
32 // C++ default constructor can NOT contain any code, that |
|
33 // might leave. |
|
34 // ----------------------------------------------------------------------------- |
|
35 // |
|
36 ChspsDomDepthIterator::ChspsDomDepthIterator( ChspsDomNode& aRootNode ): |
|
37 iFirst(&aRootNode), iCurrent(&aRootNode) |
|
38 { |
|
39 } |
|
40 |
|
41 // ----------------------------------------------------------------------------- |
|
42 // ChspsDomDepthIterator::ConstructL |
|
43 // Symbian 2nd phase constructor can leave. |
|
44 // ----------------------------------------------------------------------------- |
|
45 // |
|
46 void ChspsDomDepthIterator::ConstructL() |
|
47 { |
|
48 } |
|
49 |
|
50 // ----------------------------------------------------------------------------- |
|
51 // ChspsDomDepthIterator::NewL |
|
52 // Two-phased constructor. |
|
53 // ----------------------------------------------------------------------------- |
|
54 // |
|
55 EXPORT_C ChspsDomDepthIterator* ChspsDomDepthIterator::NewL( ChspsDomNode& aRootNode ) |
|
56 { |
|
57 ChspsDomDepthIterator* self = new( ELeave ) ChspsDomDepthIterator( aRootNode ); |
|
58 |
|
59 CleanupStack::PushL( self ); |
|
60 self->ConstructL(); |
|
61 CleanupStack::Pop( self ); |
|
62 |
|
63 return self; |
|
64 } |
|
65 |
|
66 |
|
67 // Destructor |
|
68 EXPORT_C ChspsDomDepthIterator::~ChspsDomDepthIterator() |
|
69 { |
|
70 iDepthLevel.Close(); |
|
71 } |
|
72 |
|
73 // ----------------------------------------------------------------------------- |
|
74 // ChspsDomDepthIterator::First |
|
75 // ----------------------------------------------------------------------------- |
|
76 // |
|
77 EXPORT_C ChspsDomNode* ChspsDomDepthIterator::First() |
|
78 { |
|
79 return iFirst; |
|
80 } |
|
81 |
|
82 // ----------------------------------------------------------------------------- |
|
83 // ChspsDomDepthIterator::Next |
|
84 // ----------------------------------------------------------------------------- |
|
85 // |
|
86 EXPORT_C ChspsDomNode* ChspsDomDepthIterator::NextL() |
|
87 { |
|
88 ChspsDomNode* sibling = NULL; |
|
89 ChspsDomNode* firstChild = NULL; |
|
90 |
|
91 //Initialise iCurrent if the whole tree has been walked through |
|
92 if (!iCurrent) |
|
93 { |
|
94 iCurrent = iFirst; |
|
95 } |
|
96 |
|
97 firstChild = static_cast<ChspsDomNode*>( iCurrent->ChildNodes().First() ); |
|
98 |
|
99 // Current node has childs left |
|
100 if (firstChild) |
|
101 { |
|
102 //Keep count of depth level |
|
103 PushL( *iCurrent ); |
|
104 iCurrent = firstChild; |
|
105 } |
|
106 else //If current node has siblings left |
|
107 { |
|
108 sibling = NextSibling(*iCurrent); |
|
109 if(sibling) |
|
110 { |
|
111 iCurrent = sibling; |
|
112 } |
|
113 else //Current node don't have any childs or siblings left |
|
114 { |
|
115 // Reverse the tree by moving up in hierarchy |
|
116 // Move up one level in hierarchy and check if siblings exists or we are in a |
|
117 // root level |
|
118 TBool stop( EFalse ); |
|
119 while( !stop && iDepthLevel.Count() ) |
|
120 { |
|
121 iCurrent = Pop(); //Reach the previous level |
|
122 sibling = NextSibling( *iCurrent ); //Check if siblings exist |
|
123 if ( sibling && iDepthLevel.Count() ) |
|
124 { |
|
125 iCurrent = sibling; |
|
126 stop = ETrue; |
|
127 } |
|
128 else //We didn't find any siblings |
|
129 { |
|
130 iCurrent = NULL; |
|
131 } |
|
132 } |
|
133 } |
|
134 } |
|
135 return iCurrent; |
|
136 } |
|
137 |
|
138 // ----------------------------------------------------------------------------- |
|
139 // NextSibling Find next sibling. |
|
140 // Returns NULL if there is no siblings left or there is no parent i.e this is a |
|
141 // root node |
|
142 // Returns: ChspsDomNode* Pointer to the node or NULL |
|
143 // |
|
144 // ----------------------------------------------------------------------------- |
|
145 // |
|
146 |
|
147 EXPORT_C ChspsDomNode* ChspsDomDepthIterator::NextSibling( ChspsDomNode& aNode ) |
|
148 { |
|
149 ChspsDomNode* left = NULL; |
|
150 ChspsDomNode* parent = aNode.Parent(); |
|
151 if ( parent ) |
|
152 { |
|
153 ChspsDomList& nodeList = parent->ChildNodes(); |
|
154 TInt currentIndex( nodeList.ItemIndex( aNode ) ); |
|
155 TInt nextIndex( ++currentIndex ); |
|
156 TInt length( nodeList.Length() ); |
|
157 if ( nextIndex < length ) |
|
158 { |
|
159 left = static_cast<ChspsDomNode*>( nodeList.Item( nextIndex ) ); |
|
160 } |
|
161 } |
|
162 return left; |
|
163 } |
|
164 |
|
165 // ----------------------------------------------------------------------------- |
|
166 // ChspsDomDepthIterator::PushL |
|
167 // ----------------------------------------------------------------------------- |
|
168 // |
|
169 void ChspsDomDepthIterator::PushL( ChspsDomNode& aNode ) |
|
170 { |
|
171 iDepthLevel.AppendL( &aNode ); |
|
172 } |
|
173 |
|
174 // ----------------------------------------------------------------------------- |
|
175 // ChspsDomDepthIterator::Pop |
|
176 // ----------------------------------------------------------------------------- |
|
177 // |
|
178 ChspsDomNode* ChspsDomDepthIterator::Pop() |
|
179 { |
|
180 ChspsDomNode* pop = NULL; |
|
181 TInt count( iDepthLevel.Count() ); |
|
182 if ( count ) |
|
183 { |
|
184 pop = iDepthLevel[ count-1 ]; |
|
185 iDepthLevel.Remove( count-1 ); |
|
186 } |
|
187 return pop; |
|
188 } |
|
189 |
|
190 // End of File |