author | Brendan Donegan <brendand@symbian.org> |
Fri, 28 May 2010 17:43:08 +0100 | |
branch | symbian1 |
changeset 145 | cc0182a5da39 |
parent 88 | f4b512d870e8 |
child 171 | cc1be3797632 |
child 228 | c553fa9dcbe5 |
child 242 | 64a2995a3e08 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* Copyright (c) 2007-2010 Sebastian Brannstrom, Lars Persson, EmbedDev AB |
|
3 |
* |
|
4 |
* All rights reserved. |
|
5 |
* This component and the accompanying materials are made available |
|
6 |
* under the terms of the License "Eclipse Public License v1.0" |
|
7 |
* which accompanies this distribution, and is available |
|
8 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
9 |
* |
|
10 |
* Initial Contributors: |
|
11 |
* EmbedDev AB - initial contribution. |
|
12 |
* |
|
13 |
* Contributors: |
|
14 |
* |
|
15 |
* Description: |
|
16 |
* |
|
17 |
*/ |
|
18 |
||
19 |
#include "FeedParser.h" |
|
20 |
#include <f32file.h> |
|
21 |
#include <bautils.h> |
|
22 |
#include <s32file.h> |
|
23 |
#include <charconv.h> |
|
24 |
#include <xml/stringdictionarycollection.h> |
|
25 |
#include <utf.h> |
|
26 |
#include <tinternetdate.h> |
|
27 |
#include "debug.h" |
|
60 | 28 |
#include "podcastutils.h" |
2 | 29 |
|
30 |
using namespace Xml; |
|
31 |
const TInt KMaxParseBuffer = 1024; |
|
32 |
const TInt KMaxStringBuffer = 100; |
|
33 |
||
34 |
CFeedParser::CFeedParser(MFeedParserObserver& aCallbacks, RFs& aFs) : iCallbacks(aCallbacks), iRfs(aFs) |
|
35 |
{ |
|
36 |
} |
|
37 |
||
38 |
CFeedParser::~CFeedParser() |
|
39 |
{ |
|
40 |
} |
|
41 |
||
42 |
void CFeedParser::ParseFeedL(const TFileName &feedFileName, CFeedInfo *info, TUint aMaxItems) |
|
43 |
{ |
|
44 |
//DP1("ParseFeedL BEGIN: %S", &feedFileName); |
|
45 |
||
46 |
_LIT8(KXmlMimeType, "text/xml"); |
|
47 |
// Contruct the parser object |
|
48 |
CParser* parser = CParser::NewLC(KXmlMimeType, *this); |
|
49 |
iActiveFeed = info; |
|
50 |
iFeedState = EStateRoot; |
|
51 |
iActiveShow = NULL; |
|
52 |
iItemsParsed = 0; |
|
53 |
iMaxItems = aMaxItems; |
|
54 |
iStoppedParsing = EFalse; |
|
55 |
iEncoding = ELatin1; |
|
56 |
||
57 |
ParseL(*parser, iRfs, feedFileName); |
|
58 |
||
59 |
CleanupStack::PopAndDestroy(parser); |
|
60 |
||
61 |
//DP("ParseFeedL END"); |
|
62 |
} |
|
63 |
||
64 |
// from MContentHandler |
|
65 |
void CFeedParser::OnStartDocumentL(const RDocumentParameters& aDocParam, TInt /*aErrorCode*/) |
|
66 |
{ |
|
67 |
DP("OnStartDocumentL()"); |
|
68 |
HBufC* charset = HBufC::NewLC(KMaxParseBuffer); |
|
69 |
charset->Des().Copy(aDocParam.CharacterSetName().DesC()); |
|
70 |
iEncoding = EUtf8; |
|
71 |
if (charset->CompareF(_L("utf-8")) == 0) { |
|
72 |
DP("setting UTF8"); |
|
73 |
iEncoding = EUtf8; |
|
74 |
} else if (charset->CompareF(_L("ISO-8859-1")) == 0) { |
|
75 |
iEncoding = EUtf8; //Latin1; |
|
76 |
} else { |
|
77 |
DP1("unknown charset: %S", &charset); |
|
78 |
} |
|
79 |
CleanupStack::PopAndDestroy(charset);//buffer |
|
80 |
} |
|
81 |
||
82 |
void CFeedParser::OnEndDocumentL(TInt /*aErrorCode*/) |
|
83 |
{ |
|
84 |
//DP("OnEndDocumentL()"); |
|
85 |
iCallbacks.ParsingCompleteL(iActiveFeed); |
|
86 |
} |
|
87 |
||
88 |
void CFeedParser::OnStartElementL(const RTagInfo& aElement, const RAttributeArray& aAttributes, TInt /*aErrorCode*/) |
|
89 |
{ |
|
90 |
if (iStoppedParsing) { |
|
91 |
iActiveShow = NULL; |
|
92 |
return; |
|
93 |
} |
|
94 |
||
95 |
TBuf<KMaxStringBuffer> str; |
|
96 |
str.Copy(aElement.LocalName().DesC()); |
|
97 |
//DP2("OnStartElementL START state=%d, element=%S", iFeedState, &str); |
|
98 |
iBuffer.Zero(); |
|
99 |
switch (iFeedState) { |
|
100 |
case EStateRoot: |
|
101 |
// <channel> |
|
102 |
if (str.CompareF(KTagChannel) == 0) { |
|
103 |
iFeedState = EStateChannel; |
|
104 |
} |
|
105 |
break; |
|
106 |
case EStateChannel: |
|
107 |
// <channel> <item> |
|
108 |
if(str.CompareF(KTagItem) == 0) { |
|
109 |
//DP("New item"); |
|
110 |
iFeedState=EStateItem; |
|
111 |
||
112 |
iActiveShow = NULL; |
|
113 |
iActiveShow = CShowInfo::NewL(); |
|
114 |
if (iActiveShow == NULL) { |
|
115 |
DP("Out of memory!"); |
|
116 |
iStoppedParsing = ETrue; |
|
117 |
return; |
|
118 |
} |
|
119 |
iActiveShow->SetFeedUid(iActiveFeed->Uid()); |
|
120 |
||
121 |
// <channel> <lastBuildDate> |
|
122 |
} else if (str.CompareF(KTagLastBuildDate) == 0) { |
|
123 |
DP("LastBuildDate BEGIN"); |
|
124 |
iFeedState=EStateChannelLastBuildDate; |
|
125 |
// <channel> <link> |
|
126 |
}else if (str.CompareF(KTagTitle) == 0) { |
|
127 |
iFeedState=EStateChannelTitle; |
|
128 |
// <channel> <link> |
|
129 |
} else if (str.CompareF(KTagLink) == 0) { |
|
130 |
iFeedState = EStateChannelLink; |
|
131 |
// <channel> <description> |
|
132 |
} else if (str.CompareF(KTagDescription) == 0) { |
|
133 |
iFeedState=EStateChannelDescription; |
|
134 |
// <channel> <image> |
|
135 |
} else if (str.CompareF(KTagImage) == 0) { |
|
136 |
for (int i=0;i<aAttributes.Count();i++) { |
|
137 |
RAttribute attr = aAttributes[i]; |
|
138 |
TBuf<KMaxStringBuffer> attr16; |
|
139 |
attr16.Copy(attr.Attribute().LocalName().DesC().Left(KMaxStringBuffer)); |
|
140 |
HBufC* val16 = CnvUtfConverter::ConvertToUnicodeFromUtf8L(attr.Value().DesC().Left(KMaxParseBuffer)); |
|
141 |
CleanupStack::PushL(val16); |
|
142 |
||
143 |
// href=... |
|
144 |
if (attr16.Compare(KTagHref) == 0) { |
|
145 |
iActiveFeed->SetImageUrlL(*val16); |
|
146 |
} |
|
147 |
CleanupStack::PopAndDestroy(val16); |
|
148 |
} |
|
149 |
||
150 |
iFeedState=EStateChannelImage; |
|
151 |
} |
|
152 |
break; |
|
153 |
case EStateChannelImage: |
|
154 |
// <channel> <image> <url> |
|
155 |
if (str.CompareF(KTagUrl) == 0) { |
|
156 |
iFeedState=EStateChannelImageUrl; |
|
157 |
} else { |
|
158 |
iFeedState=EStateChannelImage; |
|
159 |
} |
|
160 |
break; |
|
161 |
case EStateItem: |
|
162 |
// <channel> <item> <title> |
|
163 |
if (str.CompareF(KTagTitle) == 0) { |
|
164 |
iFeedState=EStateItemTitle; |
|
165 |
// <channel> <item> <link> |
|
166 |
} else if (str.CompareF(KTagLink) == 0) { |
|
167 |
iFeedState=EStateItemLink; |
|
168 |
// <channel> <item> <enclosure ...> |
|
169 |
} else if (str.CompareF(KTagEnclosure) == 0) { |
|
170 |
//DP("Enclosure START"); |
|
171 |
for (int i=0;i<aAttributes.Count();i++) { |
|
172 |
RAttribute attr = aAttributes[i]; |
|
173 |
TBuf<KMaxStringBuffer> attr16; |
|
174 |
attr16.Copy(attr.Attribute().LocalName().DesC()); |
|
175 |
// url=... |
|
176 |
if (attr16.Compare(KTagUrl) == 0) { |
|
177 |
HBufC* val16 = HBufC::NewLC(KMaxParseBuffer); |
|
178 |
val16->Des().Copy(attr.Value().DesC()); |
|
179 |
iActiveShow->SetUrlL(*val16); |
|
60 | 180 |
|
181 |
if (PodcastUtils::IsVideoShow(*val16)) { |
|
182 |
iActiveShow->SetShowType(EVideoPodcast); |
|
183 |
} |
|
2 | 184 |
CleanupStack::PopAndDestroy(val16); |
185 |
// length=... |
|
186 |
} else if (attr16.Compare(KTagLength) == 0) { |
|
187 |
TLex8 lex(attr.Value().DesC()); |
|
188 |
TUint size = 0; |
|
189 |
lex.Val(size, EDecimal); |
|
190 |
iActiveShow->SetShowSize(size); |
|
191 |
} |
|
192 |
} |
|
193 |
// <channel> <item> <description> |
|
194 |
} else if (str.CompareF(KTagDescription) == 0) { |
|
195 |
iFeedState=EStateItemDescription; |
|
196 |
// <channel> <item> <pubdate> |
|
197 |
} else if (str.CompareF(KTagPubDate) == 0) { |
|
198 |
//DP("LastBuildDate BEGIN"); |
|
199 |
iFeedState = EStateItemPubDate; |
|
200 |
} |
|
201 |
break; |
|
202 |
default: |
|
203 |
//DP2("Ignoring tag %S when in state %d", &str, iFeedState); |
|
204 |
break; |
|
205 |
} |
|
206 |
// DP1("OnStartElementL END state=%d", iFeedState); |
|
207 |
} |
|
208 |
||
209 |
void CFeedParser::OnEndElementL(const RTagInfo& aElement, TInt /*aErrorCode*/) |
|
210 |
{ |
|
211 |
||
212 |
if (iStoppedParsing) { |
|
213 |
return; |
|
214 |
} |
|
215 |
||
216 |
iBuffer.Trim(); |
|
217 |
||
218 |
TDesC8 lName = aElement.LocalName().DesC(); |
|
219 |
TBuf<KMaxStringBuffer> str; |
|
220 |
str.Copy(aElement.LocalName().DesC()); |
|
221 |
||
222 |
//DP2("OnEndElementL START state=%d, element=%S", iFeedState, &str); |
|
223 |
||
224 |
switch (iFeedState) { |
|
225 |
case EStateChannelTitle: |
|
226 |
if(str.CompareF(KTagTitle) == 0) { |
|
227 |
if (iActiveFeed->CustomTitle() == EFalse) { |
|
228 |
iActiveFeed->SetTitleL(iBuffer); |
|
229 |
} |
|
230 |
iFeedState = EStateChannel; |
|
231 |
} |
|
232 |
break; |
|
233 |
case EStateChannelLink: |
|
234 |
iActiveFeed->SetLinkL(iBuffer); |
|
235 |
iFeedState = EStateChannel; |
|
236 |
break; |
|
237 |
case EStateChannelDescription: |
|
238 |
iActiveFeed->SetDescriptionL(iBuffer); |
|
239 |
iFeedState = EStateChannel; |
|
240 |
break; |
|
241 |
case EStateChannelLastBuildDate: |
|
242 |
{ |
|
243 |
//DP("LastBuildDate END"); |
|
244 |
TInternetDate internetDate; |
|
245 |
TBuf8<128> temp; |
|
246 |
temp.Copy(iBuffer); |
|
247 |
||
248 |
TRAPD(parseError, internetDate.SetDateL(temp)); |
|
249 |
if(parseError == KErrNone) { |
|
250 |
if (TTime(internetDate.DateTime()) > iActiveFeed->BuildDate()) { |
|
251 |
DP("Successfully parsed build date"); |
|
252 |
iActiveFeed->SetBuildDate(TTime(internetDate.DateTime())); |
|
253 |
} else { |
|
254 |
DP("*** Nothing new, aborting parsing"); |
|
255 |
iStoppedParsing = ETrue; |
|
256 |
} |
|
257 |
} else { |
|
258 |
DP("Failed to parse last build date"); |
|
259 |
} |
|
260 |
iFeedState = EStateChannel; |
|
261 |
} |
|
262 |
break; |
|
263 |
case EStateChannelImageUrl: |
|
264 |
//DP1("Image url: %S", &iBuffer); |
|
265 |
iActiveFeed->SetImageUrlL(iBuffer); |
|
266 |
iFeedState = EStateChannelImage; |
|
267 |
break; |
|
268 |
case EStateChannelImage: |
|
269 |
if(str.CompareF(KTagImage) == 0) { |
|
270 |
iFeedState = EStateChannel; |
|
271 |
} |
|
272 |
break; |
|
273 |
case EStateItem: |
|
274 |
if (str.CompareF(KTagItem) == 0) |
|
275 |
{ |
|
276 |
iCallbacks.NewShowL(*iActiveShow); |
|
277 |
||
278 |
delete iActiveShow; |
|
279 |
||
280 |
// We should now be finished with the show. |
|
281 |
iActiveShow = NULL; |
|
282 |
||
283 |
iItemsParsed++; |
|
88
f4b512d870e8
Moved call to DeleteOldShowsByFeedL to when a feed is listed. This prevents a race condition which likely caused
teknolog
parents:
60
diff
changeset
|
284 |
DP2("iItemsParsed: %d, iMaxItems: %d", iItemsParsed, iMaxItems); |
f4b512d870e8
Moved call to DeleteOldShowsByFeedL to when a feed is listed. This prevents a race condition which likely caused
teknolog
parents:
60
diff
changeset
|
285 |
if (iItemsParsed >= iMaxItems) |
2 | 286 |
{ |
287 |
iStoppedParsing = ETrue; |
|
288 |
DP("*** Too many items, aborting parsing"); |
|
289 |
} |
|
290 |
||
291 |
iFeedState=EStateChannel; |
|
292 |
} |
|
293 |
break; |
|
294 |
case EStateItemPubDate: |
|
295 |
DP1("PubDate END: iBuffer='%S'", &iBuffer); |
|
296 |
if (str.CompareF(KTagPubDate) == 0) { |
|
297 |
// hack for feeds that don't always write day as two digits |
|
298 |
TChar five(iBuffer[5]); |
|
299 |
TChar six(iBuffer[6]); |
|
300 |
||
301 |
if (five.IsDigit() && !six.IsDigit()) { |
|
302 |
TBuf<KMaxStringBuffer> fix; |
|
303 |
fix.Copy(iBuffer.Left(4)); |
|
304 |
fix.Append(_L(" 0")); |
|
305 |
fix.Append(iBuffer.Mid(5)); |
|
306 |
iBuffer.Copy(fix); |
|
307 |
} |
|
308 |
// end hack |
|
309 |
||
310 |
// hack for feeds that write out months in full |
|
311 |
||
312 |
if (iBuffer[11] != ' ') { |
|
313 |
TPtrC midPtr = iBuffer.Mid(8); |
|
314 |
||
315 |
int spacePos = midPtr.Find(_L(" ")); |
|
316 |
||
317 |
if (spacePos != KErrNotFound) { |
|
318 |
//DP1("Month: %S", &midPtr.Left(spacePos)); |
|
319 |
||
320 |
TBuf16<KBufferLength> newBuffer; |
|
321 |
newBuffer.Copy(iBuffer.Left(11)); |
|
322 |
newBuffer.Append(_L(" ")); |
|
323 |
newBuffer.Append(iBuffer.Mid(11+spacePos)); |
|
324 |
//DP1("newBuffer: %S", &newBuffer); |
|
325 |
iBuffer.Copy(newBuffer); |
|
326 |
} |
|
327 |
} |
|
328 |
||
329 |
// hack for feeds that write days and months as UPPERCASE |
|
330 |
TChar one(iBuffer[1]); |
|
331 |
TChar two(iBuffer[2]); |
|
332 |
TChar nine(iBuffer[9]); |
|
333 |
TChar ten(iBuffer[10]); |
|
334 |
||
335 |
one.LowerCase(); |
|
336 |
two.LowerCase(); |
|
337 |
nine.LowerCase(); |
|
338 |
ten.LowerCase(); |
|
339 |
||
340 |
iBuffer[1] = one; |
|
341 |
iBuffer[2] = two; |
|
342 |
iBuffer[9] = nine; |
|
343 |
iBuffer[10] = ten; |
|
344 |
||
345 |
TBuf8<128> temp; |
|
346 |
temp.Copy(iBuffer); |
|
347 |
||
348 |
TInternetDate internetDate; |
|
349 |
TRAPD(parseError, internetDate.SetDateL(temp)); |
|
350 |
if(parseError == KErrNone) { |
|
351 |
//DP1("PubDate parse success: '%S'", &iBuffer); |
|
352 |
iActiveShow->SetPubDate(TTime(internetDate.DateTime())); |
|
353 |
||
354 |
||
355 |
DP6("Successfully parsed pubdate %d/%d/%d %d:%d:%d", |
|
356 |
iActiveShow->PubDate().DateTime().Year(), |
|
357 |
iActiveShow->PubDate().DateTime().Month(), |
|
358 |
iActiveShow->PubDate().DateTime().Day(), |
|
359 |
iActiveShow->PubDate().DateTime().Hour(), |
|
360 |
iActiveShow->PubDate().DateTime().Minute(), |
|
361 |
iActiveShow->PubDate().DateTime().Second()); |
|
362 |
||
363 |
} else { |
|
364 |
DP2("Pubdate parse error: '%S', error=%d", &iBuffer, parseError); |
|
365 |
} |
|
366 |
} |
|
367 |
iFeedState=EStateItem; |
|
368 |
break; |
|
369 |
case EStateItemTitle: |
|
370 |
//DP1("title: %S", &iBuffer); |
|
371 |
iActiveShow->SetTitleL(iBuffer); |
|
372 |
iFeedState = EStateItem; |
|
373 |
break; |
|
374 |
case EStateItemLink: |
|
375 |
if (iActiveShow->Url().Length() == 0) { |
|
376 |
iActiveShow->SetUrlL(iBuffer); |
|
60 | 377 |
|
378 |
if (PodcastUtils::IsVideoShow(iBuffer)) { |
|
379 |
iActiveShow->SetShowType(EVideoPodcast); |
|
380 |
} |
|
2 | 381 |
} |
382 |
iFeedState = EStateItem; |
|
383 |
break; |
|
384 |
case EStateItemDescription: |
|
385 |
iActiveShow->SetDescriptionL(iBuffer); |
|
386 |
iFeedState = EStateItem; |
|
387 |
break; |
|
388 |
default: |
|
389 |
// fall back to channel level when in doubt |
|
390 |
iFeedState = EStateChannel; |
|
391 |
//DP2("Don't know how to handle end tag %S when in state %d", &str, iFeedState); |
|
392 |
break; |
|
393 |
} |
|
394 |
||
395 |
//DP1("OnEndElementL END state=%d", iFeedState); |
|
396 |
} |
|
397 |
||
398 |
void CFeedParser::OnContentL(const TDesC8& aBytes, TInt /*aErrorCode*/) |
|
399 |
{ |
|
400 |
TBuf<KBufferLength> temp; |
|
401 |
if (iEncoding == EUtf8) { |
|
402 |
CnvUtfConverter::ConvertToUnicodeFromUtf8(temp, aBytes); |
|
403 |
} else { |
|
404 |
temp.Copy(aBytes); |
|
405 |
} |
|
406 |
||
407 |
if(temp.Length() + iBuffer.Length() < KBufferLength) { |
|
408 |
iBuffer.Append(temp); |
|
409 |
} |
|
410 |
} |
|
411 |
||
412 |
void CFeedParser::OnStartPrefixMappingL(const RString& /*aPrefix*/, const RString& /*aUri*/, TInt /*aErrorCode*/) |
|
413 |
{ |
|
414 |
DP("OnStartPrefixMappingL()"); |
|
415 |
} |
|
416 |
||
417 |
void CFeedParser::OnEndPrefixMappingL(const RString& /*aPrefix*/, TInt /*aErrorCode*/) |
|
418 |
{ |
|
419 |
DP("OnEndPrefixMappingL()"); |
|
420 |
} |
|
421 |
||
422 |
void CFeedParser::OnIgnorableWhiteSpaceL(const TDesC8& /*aBytes*/, TInt /*aErrorCode*/) |
|
423 |
{ |
|
424 |
DP("OnIgnorableWhiteSpaceL()"); |
|
425 |
} |
|
426 |
||
427 |
void CFeedParser::OnSkippedEntityL(const RString& /*aName*/, TInt /*aErrorCode*/) |
|
428 |
{ |
|
429 |
DP("OnSkippedEntityL()"); |
|
430 |
} |
|
431 |
||
432 |
void CFeedParser::OnProcessingInstructionL(const TDesC8& /*aTarget*/, const TDesC8& /*aData*/, TInt /*aErrorCode*/) |
|
433 |
{ |
|
434 |
DP("OnProcessingInstructionL()"); |
|
435 |
} |
|
436 |
||
437 |
void CFeedParser::OnError(TInt aErrorCode) |
|
438 |
{ |
|
439 |
DP1("CFeedParser::OnError %d", aErrorCode); |
|
440 |
} |
|
441 |
||
442 |
TAny* CFeedParser::GetExtendedInterface(const TInt32 /*aUid*/) |
|
443 |
{ |
|
444 |
DP("GetExtendedInterface()"); |
|
445 |
return NULL; |
|
446 |
} |
|
447 |
||
448 |
CFeedInfo& CFeedParser::ActiveFeed() |
|
449 |
{ |
|
450 |
return *iActiveFeed; |
|
451 |
} |