os/security/contentmgmt/referencedrmagent/RefTestAgent/RTAParser/contentparser.cpp
Update contrib.
2 * Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
19 #include "contentparser.h"
20 #include "attributeparser.h"
21 #include "stringattributeparser.h"
22 #include "stringparser.h"
23 #include "drmfilecontent.h"
24 #include "drmfilecontainer.h"
27 #include <caf/caftypes.h>
28 #include <caf/caferr.h>
30 using namespace ContentAccess;
31 using namespace ReferenceTestAgent;
35 CContentParser* CContentParser::NewL(CDrmFileContainer& aCurrentContainer)
37 CContentParser* self = new (ELeave) CContentParser(aCurrentContainer);
41 CContentParser::CContentParser(CDrmFileContainer& aCurrentContainer) : iCurrentContainer(aCurrentContainer)
45 CContentParser::~CContentParser()
52 iAttributeSet.Close();
53 iStringAttributeSet.Close();
56 void CContentParser::ParseStartElementL(const RTagInfo& aElement, const RAttributeArray& aAttributes)
58 TPtrC8 tag(aElement.LocalName().DesC());
60 if(KContentTag().CompareF(tag) == 0)
65 LogL(_L("ERROR: <content> found within a <content>"));
68 // Should not find a content tag within a content tag
69 User::Leave(KErrCorrupt);
71 // content tag has the form <content cid="something">
72 TRAPD(err, iCid = GetTagAttributeL(aAttributes, KCidTag()).AllocL());
76 LogL(_L("ERROR: Start Element <content> missing cid attribute"));
83 LogL(_L("Start Element <content cid=\"%S\">"),*iCid);
87 else if(KNameTag().CompareF(tag) == 0)
92 LogL(_L("ERROR: Second <name> tag found for content"));
95 // Should not find two name tags within a content tag
96 User::Leave(KErrCorrupt);
100 LogL(_L("Start element <name>"));
103 iWaitingForTag = ENameTag;
104 // This will parse the name and call StringParseCompleteL() when done
105 StartChildParserL(CStringParser::NewL(*this), aElement, aAttributes);
107 else if(KUniqueIdTag().CompareF(tag) == 0)
112 LogL(_L("ERROR: Second <uniqueid> tag found for content"));
115 // Should not find two UniqueId tags within a content tag
116 User::Leave(KErrCorrupt);
120 LogL(_L("Start element <uniqueid>"));
123 iWaitingForTag = EUniqueIdTag;
124 StartChildParserL(CStringParser::NewL(*this), aElement, aAttributes);
126 else if(KFileNameTag().CompareF(tag) == 0)
131 LogL(_L("ERROR: Second <filename> tag found for content"));
134 // Should not find two filename tags within a content tag
135 User::Leave(KErrCorrupt);
139 LogL(_L("Start element <filename>"));
142 iWaitingForTag = EFileNameTag;
143 StartChildParserL(CStringParser::NewL(*this), aElement, aAttributes);
145 else if(KAttributeTag().CompareF(tag) == 0)
148 StartChildParserL(CAttributeParser::NewL(*this), aElement, aAttributes);
150 else if(KStringAttributeTag().CompareF(tag) == 0)
152 // Set a string attribute
153 StartChildParserL(CStringAttributeParser::NewL(*this), aElement, aAttributes);
158 LogL(_L("ERROR: Unexpected tag <%S> found"),tag);
161 // unexpected start tag
162 User::Leave(KErrCorrupt);
166 void CContentParser::ParseEndElementL(const RTagInfo& aElement)
169 TBuf <KMaxDataTypeLength> mimeType;
170 if(KContentTag().CompareF(aElement.LocalName().DesC()) == 0)
172 // Validate and add to iCurrentContainer
173 // iCurrentContainer has ownership of the content
174 if(iCid && iName && iUniqueId && iFileName)
177 LogL(_L(" End Element </content>"));
180 err = iStringAttributeSet.GetValue(EMimeType, mimeType);
181 if(err != KErrNone || mimeType.Length() == 0)
184 LogL(_L("ERROR: A string attribute \"mimetype\" not specified for content with cid: %S"), *iCid);
187 User::Leave(KErrCorrupt);
191 iCurrentContainer.AddContentL(*iCid, *iName, *iUniqueId, *iFileName, iAttributeSet, iStringAttributeSet);
197 LogL(_L("ERROR: </content> tag not expected yet, name, uniqueid, filename and the mimetype stringattribute must all be specified first"));
200 User::Leave(KErrCorrupt);
206 LogL(_L("ERROR: Unexpected end element </%S>, expected </content>"),aElement.LocalName().DesC());
209 // We were expecting the <\content> tag
210 User::Leave(KErrCorrupt);
214 void CContentParser::StringParseCompleteL(const TDesC& aString)
216 switch(iWaitingForTag)
219 iName = aString.AllocL();
220 if(iName->Length() > KMaxCafContentName)
223 LogL(_L("ERROR: content of <name> tag is too large"));
226 // enforce from boundary check
227 User::Leave(KErrCAOutOfRange);
231 iUniqueId = aString.AllocL();
232 if(iUniqueId->Length() > KMaxCafUniqueId)
235 LogL(_L("ERROR: content of <uniqueid> tag is too large"));
238 // enforce from boundary check
239 User::Leave(KErrCAOutOfRange);
243 iFileName = aString.AllocL();
246 User::Panic(KRtaPanicString(), ERtaPanicParserInvalidState);
252 void CContentParser::SetAttributeL(TInt aAttribute, TInt aValue)
254 // Called by CAttributeParser
255 // This will leave if the same attribute tag appears twice
256 iAttributeSet.AddL(aAttribute);
257 User::LeaveIfError(iAttributeSet.SetValue(aAttribute, aValue, KErrNone));
260 void CContentParser::SetStringAttributeL(TInt aStringAttribute, const TDesC& aValue)
262 // Called by CStringAttributeParser
263 // This will leave if the same attribute tag appears twice
264 iStringAttributeSet.AddL(aStringAttribute);
265 User::LeaveIfError(iStringAttributeSet.SetValue(aStringAttribute, aValue, KErrNone));