author | larspson |
Wed, 13 Oct 2010 20:14:00 +0200 | |
branch | podcatcher_qt_symbian4 |
changeset 234 | 05075131dd6a |
parent 228 | c553fa9dcbe5 |
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 "OpmlParser.h" |
|
228 | 20 |
#include "podcatcher_debug.h" |
2 | 21 |
#include <f32file.h> |
22 |
#include <bautils.h> |
|
23 |
#include <s32file.h> |
|
24 |
#include <charconv.h> |
|
25 |
#include <xml/stringdictionarycollection.h> |
|
26 |
#include <utf.h> |
|
27 |
||
28 |
using namespace Xml; |
|
29 |
const TInt KMaxParseBuffer = 2048; |
|
30 |
const TInt KMaxStringBuffer = 1024; |
|
31 |
COpmlParser::COpmlParser(CFeedEngine& aFeedEngine, RFs& aFs) : iFeedEngine(aFeedEngine),iFs(aFs) |
|
32 |
{ |
|
33 |
} |
|
34 |
||
35 |
COpmlParser::~COpmlParser() |
|
36 |
{ |
|
37 |
} |
|
38 |
||
39 |
void COpmlParser::ParseOpmlL(const TFileName &feedFileName, TBool aSearching) |
|
40 |
{ |
|
41 |
DP1("ParseOpmlL BEGIN: %S", &feedFileName); |
|
42 |
||
43 |
iSearching = aSearching; |
|
44 |
_LIT8(KXmlMimeType, "text/xml"); |
|
45 |
// Contruct the parser object |
|
46 |
CParser* parser = CParser::NewLC(KXmlMimeType, *this); |
|
47 |
iOpmlState = EStateOpmlRoot; |
|
48 |
iEncoding = EUtf8; |
|
49 |
iNumFeedsAdded = 0; |
|
50 |
ParseL(*parser, iFs, feedFileName); |
|
51 |
||
52 |
CleanupStack::PopAndDestroy(parser); |
|
53 |
//DP("ParseFeedL END"); |
|
54 |
} |
|
55 |
||
56 |
// from MContentHandler |
|
57 |
void COpmlParser::OnStartDocumentL(const RDocumentParameters& aDocParam, TInt /*aErrorCode*/) |
|
58 |
{ |
|
59 |
DP("OnStartDocumentL()"); |
|
60 |
HBufC* charset = HBufC::NewLC(KMaxParseBuffer); |
|
61 |
charset->Des().Copy(aDocParam.CharacterSetName().DesC()); |
|
62 |
iEncoding = EUtf8; |
|
63 |
if (charset->CompareF(_L("utf-8")) == 0) { |
|
64 |
DP("setting UTF8"); |
|
65 |
iEncoding = EUtf8; |
|
66 |
} else if (charset->CompareF(_L("ISO-8859-1")) == 0) { |
|
67 |
iEncoding = EUtf8; //Latin1; |
|
68 |
} else { |
|
69 |
DP1("unknown charset: %S", &charset); |
|
70 |
} |
|
71 |
CleanupStack::PopAndDestroy(charset); |
|
72 |
} |
|
73 |
||
7 | 74 |
void COpmlParser::OnEndDocumentL(TInt aErrorCode) |
2 | 75 |
{ |
35
66c5303f3610
A ton of CodeScanner fixes (high issues) - but not all
Brendan Donegan <brendand@symbian.org>
parents:
20
diff
changeset
|
76 |
iFeedEngine.OpmlParsingCompleteL(aErrorCode, iNumFeedsAdded); |
2 | 77 |
//DP("OnEndDocumentL()"); |
78 |
} |
|
79 |
||
80 |
void COpmlParser::OnStartElementL(const RTagInfo& aElement, const RAttributeArray& aAttributes, TInt /*aErrorCode*/) |
|
81 |
{ |
|
82 |
TBuf<KMaxStringBuffer> str; |
|
83 |
str.Copy(aElement.LocalName().DesC()); |
|
84 |
DP2("OnStartElementL START state=%d, element=%S", iOpmlState, &str); |
|
85 |
iBuffer.Zero(); |
|
86 |
switch (iOpmlState) { |
|
87 |
case EStateOpmlRoot: |
|
88 |
// <body> |
|
89 |
if (str.CompareF(KTagBody) == 0) { |
|
90 |
iOpmlState = EStateOpmlBody; |
|
91 |
} |
|
92 |
break; |
|
93 |
case EStateOpmlBody: |
|
94 |
// <body> <outline> |
|
95 |
||
96 |
if(str.CompareF(KTagOutline) == 0) { |
|
97 |
iOpmlState = EStateOpmlOutline; |
|
98 |
} |
|
103 | 99 |
|
100 |
// there are two variations on OPML, where the <outline> tags sit either |
|
101 |
// directly below <body>, or inside a collective <body> <outline> |
|
102 |
// by checking if the <body> <outline> has arguments, we can support both |
|
103 |
// by falling through to the nextstate |
|
104 |
if (aAttributes.Count() == 0) |
|
105 |
{ |
|
106 |
break; |
|
107 |
} |
|
2 | 108 |
case EStateOpmlOutline: |
109 |
// <body> <outline> <outline... |
|
110 |
if(str.CompareF(KTagOutline) == 0) { |
|
111 |
iOpmlState=EStateOpmlOutlineOutline; |
|
112 |
CFeedInfo* newFeed = CFeedInfo::NewLC(); |
|
113 |
||
114 |
TBool hasTitle = EFalse; |
|
115 |
TBool hasUrl = EFalse; |
|
116 |
||
117 |
for (int i=0;i<aAttributes.Count();i++) { |
|
118 |
RAttribute attr = aAttributes[i]; |
|
119 |
TBuf<KMaxStringBuffer> attr16; |
|
120 |
attr16.Copy(attr.Attribute().LocalName().DesC().Left(KMaxStringBuffer)); |
|
121 |
HBufC* val16 = CnvUtfConverter::ConvertToUnicodeFromUtf8L( |
|
122 |
attr.Value().DesC().Left(KMaxParseBuffer)); |
|
123 |
CleanupStack::PushL(val16); |
|
124 |
||
125 |
// xmlUrl=... |
|
126 |
if (attr16.Compare(KTagXmlUrl) == 0 || attr16.Compare(KTagUrl) == 0) { |
|
127 |
newFeed->SetUrlL(*val16); |
|
128 |
hasUrl = ETrue; |
|
129 |
// htmlUrl |
|
130 |
} else if (attr16.Compare(KTagHtmlUrl) == 0) { |
|
131 |
newFeed->SetLinkL(*val16); |
|
132 |
hasUrl = ETrue; |
|
109 | 133 |
// title=... |
2 | 134 |
} else if (attr16.Compare(KTagTitle) == 0) { |
135 |
newFeed->SetTitleL(*val16); |
|
136 |
hasTitle = ETrue; |
|
137 |
// description= |
|
138 |
} else if (attr16.Compare(KTagDescription) == 0) { |
|
139 |
newFeed->SetDescriptionL(*val16); |
|
109 | 140 |
// text= |
2 | 141 |
} else if (attr16.Compare(KTagText) == 0) { |
142 |
if (!hasTitle) { |
|
143 |
newFeed->SetTitleL(*val16); |
|
144 |
hasTitle = ETrue; |
|
145 |
} |
|
146 |
} |
|
147 |
CleanupStack::PopAndDestroy(val16); |
|
148 |
} |
|
149 |
||
150 |
if (!hasUrl) { |
|
180
74d497f911e0
Fix for bug 3244 - new feed not removed from cleanupstack when parsing OPML
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
117
diff
changeset
|
151 |
CleanupStack::PopAndDestroy(newFeed); |
2 | 152 |
break; |
153 |
} |
|
154 |
||
155 |
if (!hasTitle) { |
|
156 |
newFeed->SetTitleL(newFeed->Url()); |
|
157 |
} |
|
158 |
||
109 | 159 |
// if the title is the same as the URL, it is hardly a custom |
160 |
// title, so let's replace it on update |
|
161 |
if (newFeed->Title().Length() && |
|
162 |
newFeed->Url().Length() && |
|
163 |
newFeed->Title().Compare(newFeed->Url()) != 0) { |
|
164 |
newFeed->SetCustomTitle(); |
|
165 |
} |
|
166 |
||
2 | 167 |
if (iSearching) { |
168 |
iFeedEngine.AddSearchResultL(newFeed); |
|
169 |
CleanupStack::Pop(newFeed); |
|
170 |
} else { |
|
35
66c5303f3610
A ton of CodeScanner fixes (high issues) - but not all
Brendan Donegan <brendand@symbian.org>
parents:
20
diff
changeset
|
171 |
TRAPD(err, iFeedEngine.AddFeedL(*newFeed)) |
66c5303f3610
A ton of CodeScanner fixes (high issues) - but not all
Brendan Donegan <brendand@symbian.org>
parents:
20
diff
changeset
|
172 |
if (err == KErrNone) |
20
c92d8384952c
Fix so only actually added feeds are counted during import
teknolog
parents:
7
diff
changeset
|
173 |
{ |
c92d8384952c
Fix so only actually added feeds are counted during import
teknolog
parents:
7
diff
changeset
|
174 |
iNumFeedsAdded++; |
c92d8384952c
Fix so only actually added feeds are counted during import
teknolog
parents:
7
diff
changeset
|
175 |
} |
2 | 176 |
CleanupStack::PopAndDestroy(newFeed); |
177 |
} |
|
178 |
} |
|
179 |
break; |
|
180 |
default: |
|
181 |
DP2("Ignoring tag %S when in state %d", &str, iOpmlState); |
|
182 |
break; |
|
183 |
} |
|
184 |
DP1("OnStartElementL END state=%d", iOpmlState); |
|
185 |
} |
|
186 |
||
187 |
void COpmlParser::OnEndElementL(const RTagInfo& aElement, TInt /*aErrorCode*/) |
|
188 |
{ |
|
189 |
||
190 |
TDesC8 lName = aElement.LocalName().DesC(); |
|
191 |
TBuf<KMaxStringBuffer> str; |
|
192 |
str.Copy(aElement.LocalName().DesC()); |
|
193 |
||
194 |
DP2("OnEndElementL START state=%d, element=%S", iOpmlState, &str); |
|
195 |
||
196 |
switch (iOpmlState) { |
|
197 |
case EStateOpmlOutlineOutline: |
|
198 |
iOpmlState=EStateOpmlOutline; |
|
199 |
break; |
|
200 |
case EStateOpmlOutline: |
|
201 |
iOpmlState=EStateOpmlBody; |
|
202 |
break; |
|
203 |
case EStateOpmlBody: |
|
204 |
iOpmlState = EStateOpmlRoot; |
|
205 |
break; |
|
206 |
default: |
|
207 |
// fall back to body level when in doubt |
|
208 |
iOpmlState = EStateOpmlBody; |
|
209 |
//DP("Don't know how to handle end tag %S when in state %d"), &str, iFeedState); |
|
210 |
break; |
|
211 |
} |
|
212 |
||
213 |
DP1("OnEndElementL END state=%d", iOpmlState); |
|
214 |
} |
|
215 |
||
216 |
void COpmlParser::OnContentL(const TDesC8& /*aBytes*/, TInt /*aErrorCode*/) |
|
217 |
{ |
|
218 |
//DP("OnContentL()"); |
|
219 |
} |
|
220 |
||
221 |
void COpmlParser::OnStartPrefixMappingL(const RString& /*aPrefix*/, const RString& /*aUri*/, TInt /*aErrorCode*/) |
|
222 |
{ |
|
223 |
//DP("OnStartPrefixMappingL()"); |
|
224 |
} |
|
225 |
||
226 |
void COpmlParser::OnEndPrefixMappingL(const RString& /*aPrefix*/, TInt /*aErrorCode*/) |
|
227 |
{ |
|
228 |
//DP("OnEndPrefixMappingL()"); |
|
229 |
} |
|
230 |
||
231 |
void COpmlParser::OnIgnorableWhiteSpaceL(const TDesC8& /*aBytes*/, TInt /*aErrorCode*/) |
|
232 |
{ |
|
233 |
//DP("OnIgnorableWhiteSpaceL()"); |
|
234 |
} |
|
235 |
||
236 |
void COpmlParser::OnSkippedEntityL(const RString& /*aName*/, TInt /*aErrorCode*/) |
|
237 |
{ |
|
238 |
//DP("OnSkippedEntityL()"); |
|
239 |
} |
|
240 |
||
241 |
void COpmlParser::OnProcessingInstructionL(const TDesC8& /*aTarget*/, const TDesC8& /*aData*/, TInt /*aErrorCode*/) |
|
242 |
{ |
|
243 |
//DP("OnProcessingInstructionL()"); |
|
244 |
} |
|
245 |
||
246 |
void COpmlParser::OnError(TInt aErrorCode) |
|
247 |
{ |
|
248 |
DP1("COpmlParser::OnError %d", aErrorCode); |
|
117
3b59b88b089e
Fixed Code Scanner L-issues; Further improvements to HTTP robustness
teknolog
parents:
109
diff
changeset
|
249 |
TRAP_IGNORE(iFeedEngine.OpmlParsingCompleteL(aErrorCode, iNumFeedsAdded)); |
2 | 250 |
} |
251 |
||
252 |
TAny* COpmlParser::GetExtendedInterface(const TInt32 /*aUid*/) |
|
253 |
{ |
|
254 |
//DP("GetExtendedInterface()"); |
|
255 |
return NULL; |
|
256 |
} |