| author | teknolog | 
| Sat, 03 Apr 2010 12:02:32 +0100 | |
| changeset 103 | 520291e28460 | 
| parent 35 | 66c5303f3610 | 
| child 109 | 223f270fa7ff | 
| 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" | |
| 20 | #include "debug.h" | |
| 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: 
20diff
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; | |
| 133 | // text=... | |
| 134 | 				} else if (attr16.Compare(KTagTitle) == 0) {
 | |
| 135 | newFeed->SetTitleL(*val16); | |
| 136 | newFeed->SetCustomTitle(); | |
| 137 | hasTitle = ETrue; | |
| 138 | // description= | |
| 139 | 				} else if (attr16.Compare(KTagDescription) == 0) {
 | |
| 140 | newFeed->SetDescriptionL(*val16); | |
| 141 | 				} else if (attr16.Compare(KTagText) == 0) {
 | |
| 142 | 					if (!hasTitle) {
 | |
| 143 | newFeed->SetTitleL(*val16); | |
| 144 | newFeed->SetCustomTitle(); | |
| 145 | hasTitle = ETrue; | |
| 146 | } | |
| 147 | } | |
| 148 | CleanupStack::PopAndDestroy(val16); | |
| 149 | } | |
| 150 | ||
| 151 | 			if (!hasUrl) {
 | |
| 152 | break; | |
| 153 | } | |
| 154 | ||
| 155 | 			if (!hasTitle) {
 | |
| 156 | newFeed->SetTitleL(newFeed->Url()); | |
| 157 | } | |
| 158 | ||
| 159 | 			if (iSearching) {
 | |
| 160 | iFeedEngine.AddSearchResultL(newFeed); | |
| 161 | CleanupStack::Pop(newFeed); | |
| 162 | 			} else {
 | |
| 35 
66c5303f3610
A ton of CodeScanner fixes (high issues) - but not all
 Brendan Donegan <brendand@symbian.org> parents: 
20diff
changeset | 163 | TRAPD(err, iFeedEngine.AddFeedL(*newFeed)) | 
| 
66c5303f3610
A ton of CodeScanner fixes (high issues) - but not all
 Brendan Donegan <brendand@symbian.org> parents: 
20diff
changeset | 164 | if (err == KErrNone) | 
| 20 
c92d8384952c
Fix so only actually added feeds are counted during import
 teknolog parents: 
7diff
changeset | 165 | 					{
 | 
| 
c92d8384952c
Fix so only actually added feeds are counted during import
 teknolog parents: 
7diff
changeset | 166 | iNumFeedsAdded++; | 
| 
c92d8384952c
Fix so only actually added feeds are counted during import
 teknolog parents: 
7diff
changeset | 167 | } | 
| 2 | 168 | CleanupStack::PopAndDestroy(newFeed); | 
| 169 | } | |
| 170 | } | |
| 171 | break; | |
| 172 | default: | |
| 173 | 		DP2("Ignoring tag %S when in state %d", &str, iOpmlState);
 | |
| 174 | break; | |
| 175 | } | |
| 176 | 	DP1("OnStartElementL END state=%d", iOpmlState);
 | |
| 177 | } | |
| 178 | ||
| 179 | void COpmlParser::OnEndElementL(const RTagInfo& aElement, TInt /*aErrorCode*/) | |
| 180 | 	{
 | |
| 181 | ||
| 182 | TDesC8 lName = aElement.LocalName().DesC(); | |
| 183 | TBuf<KMaxStringBuffer> str; | |
| 184 | str.Copy(aElement.LocalName().DesC()); | |
| 185 | ||
| 186 | 	DP2("OnEndElementL START state=%d, element=%S", iOpmlState, &str);
 | |
| 187 | ||
| 188 | 	switch (iOpmlState) {
 | |
| 189 | case EStateOpmlOutlineOutline: | |
| 190 | iOpmlState=EStateOpmlOutline; | |
| 191 | break; | |
| 192 | case EStateOpmlOutline: | |
| 193 | iOpmlState=EStateOpmlBody; | |
| 194 | break; | |
| 195 | case EStateOpmlBody: | |
| 196 | iOpmlState = EStateOpmlRoot; | |
| 197 | break; | |
| 198 | default: | |
| 199 | // fall back to body level when in doubt | |
| 200 | iOpmlState = EStateOpmlBody; | |
| 201 | 			//DP("Don't know how to handle end tag %S when in state %d"), &str, iFeedState);
 | |
| 202 | break; | |
| 203 | } | |
| 204 | ||
| 205 | 	DP1("OnEndElementL END state=%d", iOpmlState);	
 | |
| 206 | } | |
| 207 | ||
| 208 | void COpmlParser::OnContentL(const TDesC8& /*aBytes*/, TInt /*aErrorCode*/) | |
| 209 | 	{
 | |
| 210 | 	//DP("OnContentL()");
 | |
| 211 | } | |
| 212 | ||
| 213 | void COpmlParser::OnStartPrefixMappingL(const RString& /*aPrefix*/, const RString& /*aUri*/, TInt /*aErrorCode*/) | |
| 214 | 	{
 | |
| 215 | 	//DP("OnStartPrefixMappingL()");
 | |
| 216 | } | |
| 217 | ||
| 218 | void COpmlParser::OnEndPrefixMappingL(const RString& /*aPrefix*/, TInt /*aErrorCode*/) | |
| 219 | 	{
 | |
| 220 | 	//DP("OnEndPrefixMappingL()");
 | |
| 221 | } | |
| 222 | ||
| 223 | void COpmlParser::OnIgnorableWhiteSpaceL(const TDesC8& /*aBytes*/, TInt /*aErrorCode*/) | |
| 224 | 	{
 | |
| 225 | 	//DP("OnIgnorableWhiteSpaceL()");
 | |
| 226 | } | |
| 227 | ||
| 228 | void COpmlParser::OnSkippedEntityL(const RString& /*aName*/, TInt /*aErrorCode*/) | |
| 229 | 	{
 | |
| 230 | 	//DP("OnSkippedEntityL()");
 | |
| 231 | } | |
| 232 | ||
| 233 | void COpmlParser::OnProcessingInstructionL(const TDesC8& /*aTarget*/, const TDesC8& /*aData*/, TInt /*aErrorCode*/) | |
| 234 | 	{
 | |
| 235 | 	//DP("OnProcessingInstructionL()");
 | |
| 236 | } | |
| 237 | ||
| 238 | void COpmlParser::OnError(TInt aErrorCode) | |
| 239 | 	{
 | |
| 240 | 	DP1("COpmlParser::OnError %d", aErrorCode);
 | |
| 35 
66c5303f3610
A ton of CodeScanner fixes (high issues) - but not all
 Brendan Donegan <brendand@symbian.org> parents: 
20diff
changeset | 241 | iFeedEngine.OpmlParsingCompleteL(aErrorCode, iNumFeedsAdded); | 
| 2 | 242 | } | 
| 243 | ||
| 244 | TAny* COpmlParser::GetExtendedInterface(const TInt32 /*aUid*/) | |
| 245 | 	{
 | |
| 246 | 	//DP("GetExtendedInterface()");
 | |
| 247 | return NULL; | |
| 248 | } |