|
1 // Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // MDXMLNode.CPP |
|
15 // @file |
|
16 // This file contains the implementation of the CMDXMLNode class which |
|
17 // is the base class for the Mini-DOM. |
|
18 // This class forms the basis for the Mini-DOM. It is a |
|
19 // pure abstract class and it's sole concern is the node |
|
20 // type and name and the management of the list of child |
|
21 // nodes. |
|
22 // The node name and type are set in the constructor and |
|
23 // cannot be changed thereafter. |
|
24 // The list of child nodes is managed as a doubly-linked |
|
25 // list, not an array or similar structure. Derived |
|
26 // classes which are not allowed children will override the |
|
27 // operations to manipulate the child list. |
|
28 // |
|
29 // |
|
30 |
|
31 #include <gmxmlnode.h> |
|
32 #include <gmxmldomconstants.h> |
|
33 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS |
|
34 #include "gmxmldummydtd.h" |
|
35 #endif |
|
36 |
|
37 CMDXMLNode::CMDXMLNode( TDOMNodeType aNodeType, TBool aCanHaveChildren, CMDXMLDocument* aOwnerDocument ): |
|
38 iElementType(KXMLUndefinedElement), |
|
39 iNodeType(aNodeType), |
|
40 iCanHaveChildren(aCanHaveChildren) |
|
41 // |
|
42 // The node type will normally be hard-wired in any derived class constructor. |
|
43 // The node name and HaveChildren flag will normally be hard-wired in a derived class constructor unless it |
|
44 // covers a generic element. |
|
45 // @param aNodeType The tpe of node to be created |
|
46 // @param aCanHaveChildren Flag to indicate if the node represents a node which is allowed children |
|
47 // @param aOwnerDocument Pointer to the document at the root of the DOM tree |
|
48 // |
|
49 { |
|
50 iOwnerDocument = aOwnerDocument; |
|
51 } |
|
52 |
|
53 EXPORT_C CMDXMLNode::~CMDXMLNode() |
|
54 // |
|
55 // When this node is deleted it will links its previous and next siblings across the gap, will |
|
56 // delete all of its children and will correct its parents first and last child pointers if necessary. |
|
57 // |
|
58 { |
|
59 // Perform housekeeping on parent and siblings - shouldn't be necessary as the links to |
|
60 // this element should have been removed first or we are in a tree delete but |
|
61 // let's be helpful anyway. |
|
62 if((iNextSiblingPtr != NULL) && (iNextSiblingPtr->iPrevSiblingPtr == this)) |
|
63 { |
|
64 iNextSiblingPtr->iPrevSiblingPtr = iPrevSiblingPtr; |
|
65 } |
|
66 if((iPrevSiblingPtr != NULL) && (iPrevSiblingPtr->iNextSiblingPtr == this)) |
|
67 { |
|
68 iPrevSiblingPtr->iNextSiblingPtr = iNextSiblingPtr; |
|
69 } |
|
70 if( iParentPtr != NULL ) |
|
71 { |
|
72 if(iParentPtr->iFirstChildPtr == this) |
|
73 { |
|
74 iParentPtr->iFirstChildPtr = iNextSiblingPtr; |
|
75 } |
|
76 if(iParentPtr->iLastChildPtr == this) |
|
77 { |
|
78 iParentPtr->iLastChildPtr = iPrevSiblingPtr; |
|
79 } |
|
80 } |
|
81 |
|
82 // Delete all children |
|
83 CMDXMLNode* childToDelete; |
|
84 CMDXMLNode* nextChild; |
|
85 childToDelete = iFirstChildPtr; |
|
86 while( childToDelete != NULL ) |
|
87 { |
|
88 nextChild = childToDelete->iNextSiblingPtr; |
|
89 delete childToDelete; |
|
90 childToDelete = nextChild; |
|
91 } |
|
92 |
|
93 delete iNodeName; |
|
94 iNodeName = NULL; |
|
95 } |
|
96 |
|
97 EXPORT_C void CMDXMLNode::SetNodeNameL( const TDesC& aNodeName) |
|
98 // |
|
99 // @param aNodeName The name of the node. |
|
100 // @leave Can Leave due to OOM |
|
101 // |
|
102 { |
|
103 iNodeName = HBufC::NewMaxL(aNodeName.Length()); |
|
104 TPtr nodeAccess = iNodeName->Des(); |
|
105 nodeAccess = aNodeName; |
|
106 } |
|
107 |
|
108 EXPORT_C TPtrC CMDXMLNode::NodeName() |
|
109 // |
|
110 // @return Returns the name of the node |
|
111 // |
|
112 { |
|
113 TPtrC retVal = iNodeName->Des(); |
|
114 return retVal; |
|
115 } |
|
116 |
|
117 |
|
118 EXPORT_C CMDXMLNode* CMDXMLNode::PreviousSameTypeSibling() |
|
119 // |
|
120 // @return Returns a pointer to the previous sibling node of the same type if any, |
|
121 // oherwise returns NULL |
|
122 // |
|
123 { |
|
124 CMDXMLNode* nodePtr = iPrevSiblingPtr; |
|
125 while((nodePtr != NULL) && nodePtr->iElementType != iElementType) |
|
126 { |
|
127 nodePtr = nodePtr->iPrevSiblingPtr; |
|
128 } |
|
129 return nodePtr; |
|
130 } |
|
131 |
|
132 EXPORT_C CMDXMLNode* CMDXMLNode::NextSameTypeSibling() |
|
133 // |
|
134 // @return Returns a pointer to the next sibling node of the same type if any, |
|
135 // oherwise returns NULL |
|
136 // |
|
137 { |
|
138 CMDXMLNode* nodePtr = iNextSiblingPtr; |
|
139 while((nodePtr != NULL) && nodePtr->iElementType != iElementType) |
|
140 { |
|
141 nodePtr = nodePtr->iNextSiblingPtr; |
|
142 } |
|
143 return nodePtr; |
|
144 } |
|
145 |
|
146 EXPORT_C TInt CMDXMLNode::RemoveChild(CMDXMLNode* aChildToRemove) |
|
147 // |
|
148 // Removes a child from the list of child nodes. The child node is not deleted - that is the responsibility of the caller |
|
149 // @param aChildToRemove Pointer to child to remove from the list |
|
150 // @return Returns KERRNone if sucessful or KErrNotFound if the referenced child node is not found |
|
151 // |
|
152 { |
|
153 TInt retVal=KErrNone; |
|
154 |
|
155 if(aChildToRemove == NULL) |
|
156 { |
|
157 retVal = KErrNotFound; |
|
158 } |
|
159 else if( !iCanHaveChildren ) |
|
160 { |
|
161 retVal = KErrXMLInvalidChild; |
|
162 } |
|
163 else if( aChildToRemove->iParentPtr != this ) |
|
164 { |
|
165 retVal = KErrNotFound; |
|
166 } |
|
167 else |
|
168 { |
|
169 if(iFirstChildPtr == NULL) |
|
170 { |
|
171 retVal = KErrNotFound; |
|
172 } |
|
173 else |
|
174 { |
|
175 if( aChildToRemove == iFirstChildPtr) |
|
176 { |
|
177 if( aChildToRemove == iLastChildPtr ) |
|
178 { // Only child so just zero pointers |
|
179 iFirstChildPtr = NULL; |
|
180 iLastChildPtr = NULL; |
|
181 } |
|
182 else |
|
183 { // First but not only child |
|
184 iFirstChildPtr->iNextSiblingPtr->iPrevSiblingPtr = NULL; |
|
185 iFirstChildPtr = iFirstChildPtr->iNextSiblingPtr; |
|
186 } |
|
187 } |
|
188 else if( aChildToRemove == iLastChildPtr ) |
|
189 { // Last but not only child |
|
190 iLastChildPtr->iPrevSiblingPtr->iNextSiblingPtr = NULL; |
|
191 iLastChildPtr = iLastChildPtr->iPrevSiblingPtr; |
|
192 } |
|
193 else |
|
194 { // Not first or last so somewhere in the list |
|
195 CMDXMLNode* nodeBefore = iFirstChildPtr; |
|
196 while((nodeBefore->iNextSiblingPtr != NULL) && |
|
197 (nodeBefore->iNextSiblingPtr != aChildToRemove)) |
|
198 { |
|
199 nodeBefore = nodeBefore->iNextSiblingPtr; |
|
200 } |
|
201 if( nodeBefore->iNextSiblingPtr != aChildToRemove ) |
|
202 { |
|
203 retVal = KErrNotFound; |
|
204 } |
|
205 else |
|
206 { |
|
207 nodeBefore->iNextSiblingPtr = aChildToRemove->iNextSiblingPtr; |
|
208 aChildToRemove->iNextSiblingPtr->iPrevSiblingPtr = nodeBefore; |
|
209 } |
|
210 } |
|
211 } |
|
212 }//end if |
|
213 |
|
214 if( retVal == KErrNone ) |
|
215 { // Having cleared this node off the list, clear its own pointers |
|
216 aChildToRemove->iPrevSiblingPtr = NULL; |
|
217 aChildToRemove->iNextSiblingPtr = NULL; |
|
218 aChildToRemove->iParentPtr = NULL; |
|
219 } |
|
220 return retVal; |
|
221 } |
|
222 |
|
223 EXPORT_C TInt CMDXMLNode::InsertBefore(CMDXMLNode* aInsertBeforeChild, CMDXMLNode* aChildToInsert) |
|
224 // |
|
225 // Inserts a new child node at a specific point in the child list. |
|
226 // @param aInsertBeforeChild Pointer to the element before which the new child should be inserted (NULL to insert at the start of the list) |
|
227 // @param aChildToInsert Pointer to the new child node to insert. |
|
228 // @return Returns KErrNone if successful, KErrXMLInvalidChild if the node cannot have children or KErrNotFound if it cannot fnd the child before which to insert |
|
229 // |
|
230 { |
|
231 TInt retVal = KErrNone; |
|
232 |
|
233 if(aChildToInsert == NULL) |
|
234 { |
|
235 retVal = KErrNotSupported; |
|
236 } |
|
237 else if( !iCanHaveChildren ) |
|
238 { |
|
239 retVal = KErrXMLInvalidChild ; |
|
240 } |
|
241 else if(iFirstChildPtr == NULL && aInsertBeforeChild != NULL) |
|
242 { |
|
243 retVal = KErrNotFound; |
|
244 } |
|
245 else if((aInsertBeforeChild == NULL) || (aInsertBeforeChild == iFirstChildPtr)) |
|
246 { // Insert at start of list |
|
247 aChildToInsert->iParentPtr = this; |
|
248 if(iFirstChildPtr != NULL) |
|
249 { |
|
250 iFirstChildPtr->iPrevSiblingPtr = aChildToInsert; |
|
251 aChildToInsert->iNextSiblingPtr = iFirstChildPtr; |
|
252 } |
|
253 else |
|
254 { |
|
255 iLastChildPtr = aChildToInsert; |
|
256 aChildToInsert->iNextSiblingPtr = NULL; |
|
257 } |
|
258 iFirstChildPtr = aChildToInsert; |
|
259 } |
|
260 else |
|
261 { // Find the insert before child as a check. |
|
262 CMDXMLNode* nodeBefore = iFirstChildPtr; |
|
263 while((nodeBefore->iNextSiblingPtr != NULL) && |
|
264 (nodeBefore->iNextSiblingPtr != aInsertBeforeChild)) |
|
265 { |
|
266 nodeBefore = nodeBefore->iNextSiblingPtr; |
|
267 } |
|
268 if( nodeBefore->iNextSiblingPtr != aInsertBeforeChild ) |
|
269 { |
|
270 retVal = KErrNotFound; |
|
271 } |
|
272 else |
|
273 { |
|
274 aChildToInsert->iParentPtr = this; |
|
275 aChildToInsert->iNextSiblingPtr = nodeBefore->iNextSiblingPtr; |
|
276 aChildToInsert->iPrevSiblingPtr = nodeBefore; |
|
277 nodeBefore->iNextSiblingPtr = aChildToInsert; |
|
278 aChildToInsert->iNextSiblingPtr->iPrevSiblingPtr = aChildToInsert; |
|
279 } |
|
280 } |
|
281 return retVal; |
|
282 } |
|
283 |
|
284 EXPORT_C TInt CMDXMLNode::AppendChild(CMDXMLNode* aChildToInsert) |
|
285 // |
|
286 // Appends a new child at the end of the child list. |
|
287 // @param aChildToInsert Pointer to the new child node to append. |
|
288 // @return Returns KErrNone if successful or KErrXMLInvalidChild if the node cannot have children |
|
289 // |
|
290 { |
|
291 TInt retVal = KErrNone; |
|
292 |
|
293 if(aChildToInsert == NULL) |
|
294 { |
|
295 retVal = KErrNotSupported; |
|
296 } |
|
297 else if( !iCanHaveChildren ) |
|
298 { |
|
299 retVal = KErrXMLInvalidChild; |
|
300 } |
|
301 else |
|
302 { |
|
303 if(iFirstChildPtr == NULL) |
|
304 { |
|
305 iFirstChildPtr = aChildToInsert; |
|
306 } |
|
307 |
|
308 if(iLastChildPtr != NULL) |
|
309 { |
|
310 iLastChildPtr->iNextSiblingPtr = aChildToInsert; |
|
311 } |
|
312 |
|
313 aChildToInsert->iPrevSiblingPtr = iLastChildPtr; |
|
314 iLastChildPtr = aChildToInsert; |
|
315 aChildToInsert->iParentPtr = this; |
|
316 } |
|
317 return retVal; |
|
318 } |
|
319 |
|
320 EXPORT_C TInt CMDXMLNode::ReplaceChild(CMDXMLNode* aChildToInsert, CMDXMLNode* aChildToReplace) |
|
321 // |
|
322 // Inserts a new child node while removing an existing one. The old child is not deleted. |
|
323 // @param aChildToInsert Pointer to the new child node to insert. |
|
324 // @param aChildToReplace Pointer to the child node to be replaced. |
|
325 // @return Returns KErrNone if successful, KErrXMLInvalidChild if the node cannot have children or KErrNotFound if it cannot fnd the child to replace. |
|
326 // |
|
327 { |
|
328 TInt retVal = KErrNone; |
|
329 |
|
330 if(aChildToInsert == NULL) |
|
331 { |
|
332 retVal = KErrNotSupported; |
|
333 } |
|
334 else if(aChildToReplace == NULL) |
|
335 { |
|
336 retVal = KErrNotFound; |
|
337 } |
|
338 else if( !iCanHaveChildren ) |
|
339 { |
|
340 retVal = KErrXMLInvalidChild ; |
|
341 } |
|
342 else |
|
343 { |
|
344 if((aChildToReplace == iFirstChildPtr) && (aChildToReplace == iLastChildPtr)) |
|
345 { // Replace only child |
|
346 iFirstChildPtr = aChildToInsert; |
|
347 iLastChildPtr = aChildToInsert; |
|
348 } |
|
349 else if(aChildToReplace == iFirstChildPtr) |
|
350 { // Replace first but not only |
|
351 aChildToInsert->iNextSiblingPtr = iFirstChildPtr->iNextSiblingPtr; |
|
352 aChildToInsert->iNextSiblingPtr->iPrevSiblingPtr = aChildToInsert; |
|
353 iFirstChildPtr = aChildToInsert; |
|
354 } |
|
355 else if(aChildToReplace == iLastChildPtr) |
|
356 { // Replace Last but not only |
|
357 aChildToInsert->iPrevSiblingPtr = iLastChildPtr->iPrevSiblingPtr; |
|
358 aChildToInsert->iPrevSiblingPtr->iNextSiblingPtr = aChildToInsert; |
|
359 iLastChildPtr = aChildToInsert; |
|
360 } |
|
361 else if(iFirstChildPtr != NULL) |
|
362 { // Replace in the middle of the list |
|
363 CMDXMLNode* nodeBefore = iFirstChildPtr; |
|
364 while((nodeBefore->iNextSiblingPtr != NULL) && |
|
365 (nodeBefore->iNextSiblingPtr != aChildToReplace)) |
|
366 { |
|
367 nodeBefore = nodeBefore->iNextSiblingPtr; |
|
368 } |
|
369 if( nodeBefore->iNextSiblingPtr != aChildToReplace ) |
|
370 { |
|
371 retVal = KErrNotFound; |
|
372 } |
|
373 else |
|
374 { // found so just cross link |
|
375 aChildToInsert->iNextSiblingPtr = aChildToReplace->iNextSiblingPtr; |
|
376 aChildToInsert->iPrevSiblingPtr = aChildToReplace->iPrevSiblingPtr; |
|
377 aChildToInsert->iPrevSiblingPtr->iNextSiblingPtr = aChildToInsert; |
|
378 aChildToInsert->iNextSiblingPtr->iPrevSiblingPtr = aChildToInsert; |
|
379 } |
|
380 } |
|
381 else |
|
382 { |
|
383 retVal = KErrNotFound; |
|
384 } |
|
385 } |
|
386 |
|
387 if( retVal == KErrNone ) |
|
388 { // Clear out the replaced node |
|
389 aChildToReplace->iPrevSiblingPtr = NULL; |
|
390 aChildToReplace->iNextSiblingPtr = NULL; |
|
391 aChildToReplace->iParentPtr = NULL; |
|
392 // Point the new child at its parent |
|
393 aChildToInsert->iParentPtr = this; |
|
394 } |
|
395 return retVal; |
|
396 } |
|
397 |
|
398 // End Of File |
|
399 |