|
1 /* |
|
2 * Copyright (c) 2005 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 the License "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: Opml parser. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "OpmlParser.h" |
|
20 #include "CleanupLibXml2.h" |
|
21 #include "LeakTracker.h" |
|
22 #include "PackedFolder.h" |
|
23 #include "XmlUtils.h" |
|
24 |
|
25 |
|
26 // Element and attribute names used by this parser. |
|
27 _LIT8(KBody, "body"); |
|
28 _LIT8(KHead, "head"); |
|
29 _LIT8(KTitle, "title"); |
|
30 _LIT8(KIsComment, "isComment"); |
|
31 _LIT8(KOml, "oml"); |
|
32 _LIT8(KOpml, "opml"); |
|
33 _LIT8(KOutlineDocument, "outlineDocument"); |
|
34 _LIT8(KOutline, "outline"); |
|
35 _LIT8(KText, "text"); |
|
36 _LIT8(KUrl, "url"); |
|
37 _LIT8(KXmlUrl, "xmlUrl"); |
|
38 |
|
39 _LIT(KRootFolder, "Root Folder"); |
|
40 |
|
41 _LIT(KComma, " , "); |
|
42 _LIT(KBackQuote, " ` "); |
|
43 |
|
44 |
|
45 |
|
46 const TInt KMaxURLLength = 256; |
|
47 |
|
48 // ----------------------------------------------------------------------------- |
|
49 // RssFeedParser::NewL |
|
50 // |
|
51 // Two-phased constructor. |
|
52 // ----------------------------------------------------------------------------- |
|
53 // |
|
54 COpmlParser* COpmlParser::NewL(CXmlUtils& aXmlUtils) |
|
55 { |
|
56 COpmlParser* self = new (ELeave) COpmlParser(aXmlUtils); |
|
57 |
|
58 CleanupStack::PushL(self); |
|
59 self->ConstructL(); |
|
60 CleanupStack::Pop(); |
|
61 |
|
62 return self; |
|
63 } |
|
64 |
|
65 |
|
66 // ----------------------------------------------------------------------------- |
|
67 // COpmlParser::COpmlParser |
|
68 // |
|
69 // C++ default constructor can NOT contain any code, that |
|
70 // might leave. |
|
71 // ----------------------------------------------------------------------------- |
|
72 // |
|
73 COpmlParser::COpmlParser(CXmlUtils& aXmlUtils): |
|
74 iLeakTracker(CLeakTracker::EOpmlParser), iXmlUtils(aXmlUtils) |
|
75 { |
|
76 } |
|
77 |
|
78 |
|
79 // ----------------------------------------------------------------------------- |
|
80 // COpmlParser::ConstructL |
|
81 // |
|
82 // Symbian 2nd phase constructor can leave. |
|
83 // ----------------------------------------------------------------------------- |
|
84 // |
|
85 void COpmlParser::ConstructL() |
|
86 { |
|
87 } |
|
88 |
|
89 |
|
90 // ----------------------------------------------------------------------------- |
|
91 // COpmlParser::~COpmlParser |
|
92 // |
|
93 // Deconstructor. |
|
94 // ----------------------------------------------------------------------------- |
|
95 // |
|
96 COpmlParser::~COpmlParser() |
|
97 { |
|
98 } |
|
99 |
|
100 |
|
101 // ----------------------------------------------------------------------------- |
|
102 // COpmlParser::IsSupported |
|
103 // |
|
104 // Returns true if this parser can process the given document. |
|
105 // ----------------------------------------------------------------------------- |
|
106 // |
|
107 TBool COpmlParser::IsSupported(RXmlEngDocument aDocument, const TDesC& /*aContentType*/) const |
|
108 { |
|
109 TXmlEngElement node = NULL; |
|
110 |
|
111 // Get the root element. |
|
112 node = iXmlUtils.GetDocumentFirstElement(aDocument); |
|
113 |
|
114 // If the root node is missing the feed isn't supported. |
|
115 if (node.IsNull()) |
|
116 { |
|
117 return EFalse; |
|
118 } |
|
119 |
|
120 // If the root node is not HEAD_STR the feed isn't supported. |
|
121 else if (!iXmlUtils.IsNamed(node, KOpml) && |
|
122 !iXmlUtils.IsNamed(node, KOml) && |
|
123 !iXmlUtils.IsNamed(node, KOutlineDocument)) |
|
124 { |
|
125 return EFalse; |
|
126 } |
|
127 |
|
128 return ETrue; |
|
129 } |
|
130 |
|
131 |
|
132 // ----------------------------------------------------------------------------- |
|
133 // COpmlParser::ParseL |
|
134 // |
|
135 // Creates a FolderItem instance from the given document. |
|
136 // ----------------------------------------------------------------------------- |
|
137 // |
|
138 CPackedFolder* COpmlParser::ParseL(TDesC8& aBuffer, const TDesC& aContentType, |
|
139 const TDesC& aCharSet, const TDesC& aImportRootFolder) const |
|
140 { |
|
141 RXmlEngDocument document; |
|
142 TXmlEngElement rootNode; |
|
143 TXmlEngElement bodyNode; |
|
144 CPackedFolder* packedFolder = NULL; |
|
145 |
|
146 // Parse the buffer. |
|
147 document = iXmlUtils.ParseBufferL(aBuffer, aCharSet); |
|
148 CleanupClosePushL(document); |
|
149 |
|
150 if (!IsSupported(document, aContentType)) |
|
151 { |
|
152 User::Leave(KErrNotSupported); |
|
153 } |
|
154 |
|
155 // Get the root node. |
|
156 rootNode = iXmlUtils.GetDocumentFirstElement(document); |
|
157 |
|
158 // Find the body element. |
|
159 bodyNode = iXmlUtils.GetFirstNamedChild(rootNode, KBody()); |
|
160 |
|
161 if (bodyNode.IsNull()) |
|
162 { |
|
163 User::Leave(KErrCorrupt); |
|
164 } |
|
165 |
|
166 // Process the body. |
|
167 packedFolder = CPackedFolder::NewL(); |
|
168 CleanupStack::PushL(packedFolder); |
|
169 |
|
170 TBuf<KMaxFileName> OPMLRootFolderName; |
|
171 |
|
172 if(aImportRootFolder.Length()<=0) |
|
173 { |
|
174 // This will happen only while importing default OPML file - Do nothing |
|
175 } |
|
176 else |
|
177 { |
|
178 // Query for the opml/head/title |
|
179 |
|
180 // Find the head/title element. |
|
181 TXmlEngElement headNode; |
|
182 HBufC* Title = NULL; |
|
183 headNode = iXmlUtils.GetFirstNamedChild(rootNode, KHead()); |
|
184 |
|
185 if(!headNode.IsNull()) |
|
186 { |
|
187 TXmlEngElement titleNode; |
|
188 titleNode = iXmlUtils.GetFirstNamedChild(headNode, KTitle()); |
|
189 |
|
190 if(!titleNode.IsNull()) |
|
191 Title = iXmlUtils.ExtractTextL(titleNode); |
|
192 } |
|
193 |
|
194 CleanupStack::PushL(Title); |
|
195 |
|
196 if(Title == NULL) |
|
197 OPMLRootFolderName.Append(aImportRootFolder); |
|
198 else |
|
199 OPMLRootFolderName.Append(Title->Des()); |
|
200 |
|
201 CleanupStack::PopAndDestroy(/*Title*/); |
|
202 } |
|
203 |
|
204 packedFolder->FolderBeginsL(KRootFolder); |
|
205 |
|
206 if(OPMLRootFolderName.Length() > 0) |
|
207 packedFolder->FolderBeginsL(OPMLRootFolderName); |
|
208 |
|
209 ProcessFolderL(bodyNode, *packedFolder); |
|
210 |
|
211 if(OPMLRootFolderName.Length() > 0) |
|
212 packedFolder->FolderEndsL(); |
|
213 |
|
214 packedFolder->FolderEndsL(); |
|
215 packedFolder->Trim(); |
|
216 packedFolder->DoneL(); |
|
217 |
|
218 CleanupStack::Pop(packedFolder); |
|
219 CleanupStack::PopAndDestroy(/*document*/); |
|
220 |
|
221 return packedFolder; |
|
222 } |
|
223 |
|
224 |
|
225 // ----------------------------------------------------------------------------- |
|
226 // COpmlParser::ProcessFolderL |
|
227 // |
|
228 // Populates the FolderItem instance with the values from the given node. |
|
229 // |
|
230 // TODO: Factor out the recursion. |
|
231 // ----------------------------------------------------------------------------- |
|
232 // |
|
233 void COpmlParser::ProcessFolderL(TXmlEngElement aNode, CPackedFolder& aPackedFolder) const |
|
234 { |
|
235 _LIT(KTrue, "true"); |
|
236 const TTime KTimeZero(0); |
|
237 |
|
238 TXmlEngElement node; |
|
239 TBool isFolder; |
|
240 HBufC* itemName = NULL; |
|
241 HBufC* itemUrl = NULL; |
|
242 HBufC* isComment = NULL; |
|
243 RPointerArray<HBufC> titleArray; |
|
244 const TInt KAutoUpdatingOff = 0; |
|
245 |
|
246 HBufC* FeedUrlsInCurrentFolder = NULL; |
|
247 |
|
248 // Process the body's children. |
|
249 node = iXmlUtils.GetFirstElementChild(aNode); |
|
250 |
|
251 while (node.NotNull()) |
|
252 { |
|
253 isFolder = EFalse; |
|
254 |
|
255 // Process an outline element. |
|
256 if (iXmlUtils.IsNamed(node, KOutline)) |
|
257 { |
|
258 // Skip the element and any children if its a comment. |
|
259 if ((isComment = iXmlUtils.AttributeL(node, KIsComment)) != NULL) |
|
260 { |
|
261 if (isComment->CompareF(KTrue()) == 0) |
|
262 { |
|
263 delete isComment; |
|
264 |
|
265 // Skip the element. |
|
266 node = iXmlUtils.GetNextSiblingElement(node); |
|
267 continue; |
|
268 } |
|
269 |
|
270 delete isComment; |
|
271 } |
|
272 |
|
273 // If the node has a outline child its a folder. |
|
274 if (iXmlUtils.GetFirstNamedChild(node, KOutline).NotNull()) |
|
275 { |
|
276 isFolder = ETrue; |
|
277 } |
|
278 |
|
279 // Extract the name. |
|
280 itemName = iXmlUtils.AttributeL(node, KText); |
|
281 if (itemName == NULL) |
|
282 { |
|
283 itemName = KNullDesC().AllocL(); |
|
284 } |
|
285 itemName->Des().Trim(); |
|
286 CleanupStack::PushL(itemName); |
|
287 |
|
288 // If the node isn't a folder then add a feed item |
|
289 if (!isFolder) |
|
290 { |
|
291 TBool found = EFalse; |
|
292 titleArray.AppendL(itemName); |
|
293 CleanupStack::Pop(itemName); |
|
294 // Extract the url. |
|
295 itemUrl = iXmlUtils.AttributeL(node, KUrl); |
|
296 if (itemUrl == NULL) |
|
297 { |
|
298 itemUrl = iXmlUtils.AttributeL(node, KXmlUrl); |
|
299 if (itemUrl == NULL) |
|
300 { |
|
301 itemUrl = KNullDesC().AllocL(); |
|
302 } |
|
303 } |
|
304 |
|
305 CleanupStack::PushL(itemUrl); |
|
306 |
|
307 // Clean up the url. |
|
308 TPtr ptr(itemUrl->Des()); |
|
309 iXmlUtils.CleanupUrlL(ptr); |
|
310 |
|
311 itemUrl->Des().Trim(); |
|
312 |
|
313 for(TInt i = 0;i<titleArray.Count()-1;i++) |
|
314 { |
|
315 if(itemName->Des().Compare(titleArray[i]->Des())==0) |
|
316 found = ETrue; |
|
317 } |
|
318 |
|
319 HBufC* titleAndUrl = HBufC::NewL(itemUrl->Length()+itemName->Length()+KBackQuote().Length()); |
|
320 CleanupStack::PushL(titleAndUrl); |
|
321 titleAndUrl->Des().Copy(itemName->Des()); |
|
322 titleAndUrl->Des().Append(KBackQuote()); |
|
323 titleAndUrl->Des().Append(itemUrl->Des()); |
|
324 if(found && itemUrl->Length() <= KMaxURLLength) |
|
325 { |
|
326 found = EFalse; |
|
327 SearchURLInCurrentFolderL(FeedUrlsInCurrentFolder,titleAndUrl,found); |
|
328 if(!found) |
|
329 { |
|
330 aPackedFolder.AddFeedL(*itemName, *itemUrl, KTimeZero, KAutoUpdatingOff, KErrNotFound); |
|
331 } |
|
332 } |
|
333 else if(itemUrl->Length() <= KMaxURLLength) |
|
334 { |
|
335 aPackedFolder.AddFeedL(*itemName, *itemUrl, KTimeZero, KAutoUpdatingOff, KErrNotFound); |
|
336 } |
|
337 if(!FeedUrlsInCurrentFolder) |
|
338 { |
|
339 FeedUrlsInCurrentFolder = HBufC::NewL(itemUrl->Length()+itemName->Length()+KBackQuote().Length()); |
|
340 /* |
|
341 Cleanupstack manipulation, since we are popping itemUrl & itemName before FeedUrlsInCurrentFolder |
|
342 */ |
|
343 CleanupStack::Pop(2); |
|
344 CleanupStack::PushL(FeedUrlsInCurrentFolder); |
|
345 CleanupStack::PushL(itemUrl); |
|
346 CleanupStack::PushL(titleAndUrl); |
|
347 } |
|
348 else |
|
349 { |
|
350 /* |
|
351 Cleanupstack manipulation, since we are popping itemUrl & itemName before FeedUrlsInCurrentFolder |
|
352 */ |
|
353 |
|
354 HBufC* temp = FeedUrlsInCurrentFolder->ReAllocL(FeedUrlsInCurrentFolder->Length() + itemUrl->Length()+itemName->Length()+KBackQuote().Length() + KComma().Length()); |
|
355 CleanupStack::Pop(3); |
|
356 FeedUrlsInCurrentFolder = temp; |
|
357 CleanupStack::PushL(FeedUrlsInCurrentFolder); |
|
358 CleanupStack::PushL(itemUrl); |
|
359 CleanupStack::PushL(titleAndUrl); |
|
360 FeedUrlsInCurrentFolder->Des().Append(KComma()); |
|
361 } |
|
362 FeedUrlsInCurrentFolder->Des().Append(titleAndUrl->Des()); |
|
363 CleanupStack::PopAndDestroy(titleAndUrl); |
|
364 CleanupStack::PopAndDestroy(itemUrl); |
|
365 |
|
366 } |
|
367 |
|
368 // Otherwise add a folder item. |
|
369 else |
|
370 { |
|
371 aPackedFolder.FolderBeginsL(*itemName); |
|
372 CleanupStack::PopAndDestroy(itemName); |
|
373 ProcessFolderL(node, aPackedFolder); |
|
374 aPackedFolder.FolderEndsL(); |
|
375 } |
|
376 } |
|
377 |
|
378 // Get the next element. |
|
379 node = iXmlUtils.GetNextSiblingElement(node); |
|
380 } |
|
381 |
|
382 if(FeedUrlsInCurrentFolder) |
|
383 { |
|
384 CleanupStack::PopAndDestroy(FeedUrlsInCurrentFolder); |
|
385 } |
|
386 titleArray.ResetAndDestroy(); |
|
387 } |
|
388 |
|
389 // ----------------------------------------------------------------------------- |
|
390 // COpmlParser::SearchURLInCurrentFolderL |
|
391 // |
|
392 // Search for the feed URL from a set of feed URLs present in the current folder seperated by commas |
|
393 // |
|
394 // ----------------------------------------------------------------------------- |
|
395 // |
|
396 void COpmlParser::SearchURLInCurrentFolderL(HBufC* aURLsInCurFolder, HBufC* aFeedURL, TBool& aFound) const |
|
397 { |
|
398 |
|
399 aFound = EFalse; |
|
400 |
|
401 if(!aURLsInCurFolder) |
|
402 { |
|
403 aFound = EFalse; |
|
404 return; |
|
405 } |
|
406 |
|
407 HBufC *temp = HBufC::NewLC(aURLsInCurFolder->Length()); |
|
408 temp->Des().Copy(aURLsInCurFolder->Des()); |
|
409 |
|
410 TInt curPos = 0; |
|
411 TInt commaPos = 0; |
|
412 while(ETrue) |
|
413 { |
|
414 commaPos = temp->Find(KComma()); |
|
415 if(commaPos == KErrNotFound) |
|
416 { |
|
417 aFound = !temp->Compare(aFeedURL->Des()); |
|
418 break; |
|
419 } |
|
420 TPtrC curURL = temp->Mid(curPos, commaPos); |
|
421 if(curURL == aFeedURL->Des()) |
|
422 { |
|
423 aFound = ETrue; |
|
424 break; |
|
425 } |
|
426 |
|
427 temp->Des().Copy(temp->Mid(commaPos+KComma().Length())); |
|
428 } |
|
429 CleanupStack::PopAndDestroy(/*temp*/); |
|
430 } |