os/security/contentmgmt/referencedrmagent/RefTestAgent/RTAParser/contentparser.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2004-2009 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: 
    15 *
    16 */
    17 
    18 
    19 #include "contentparser.h"
    20 #include "attributeparser.h"
    21 #include "stringattributeparser.h"
    22 #include "stringparser.h"
    23 #include "drmfilecontent.h"
    24 #include "drmfilecontainer.h"
    25 #include "tags.h"
    26 #include "rtapanic.h"
    27 #include <caf/caftypes.h>
    28 #include <caf/caferr.h>
    29 
    30 using namespace ContentAccess;
    31 using namespace ReferenceTestAgent;
    32 using namespace Xml;
    33 
    34 
    35 CContentParser* CContentParser::NewL(CDrmFileContainer& aCurrentContainer)
    36 	{
    37 	CContentParser* self = new (ELeave) CContentParser(aCurrentContainer);
    38 	return self;
    39 	}
    40 
    41 CContentParser::CContentParser(CDrmFileContainer& aCurrentContainer) : iCurrentContainer(aCurrentContainer)
    42 	{	
    43 	}
    44 
    45 CContentParser::~CContentParser()
    46 	{
    47 	delete iCid;
    48 	delete iName;
    49 	delete iUniqueId;
    50 	delete iFileName;
    51 	
    52 	iAttributeSet.Close();
    53 	iStringAttributeSet.Close();
    54 	}
    55 
    56 void CContentParser::ParseStartElementL(const RTagInfo& aElement, const RAttributeArray& aAttributes)
    57 	{
    58 	TPtrC8 tag(aElement.LocalName().DesC());
    59 
    60 	if(KContentTag().CompareF(tag) == 0)
    61 		{
    62 		if(iCid)
    63 			{
    64 			#ifdef _DEBUG
    65 			LogL(_L("ERROR: <content> found within a <content>"));	
    66 			#endif
    67 
    68 			// Should not find a content tag within a content tag
    69 			User::Leave(KErrCorrupt);
    70 			}
    71 		// content tag has the form <content cid="something">
    72 		TRAPD(err, iCid = GetTagAttributeL(aAttributes, KCidTag()).AllocL());
    73 		if(err != KErrNone)
    74 			{
    75 			#ifdef _DEBUG
    76 			LogL(_L("ERROR: Start Element <content> missing cid attribute"));	
    77 			#endif
    78 
    79 			User::Leave(err);
    80 			}
    81 
    82 		#ifdef _DEBUG
    83 		LogL(_L("Start Element <content cid=\"%S\">"),*iCid);
    84 		#endif
    85 
    86 		}
    87 	else if(KNameTag().CompareF(tag) == 0)
    88 		{
    89 		if(iName)
    90 			{
    91 			#ifdef _DEBUG
    92 			LogL(_L("ERROR: Second <name> tag found for content"));
    93 			#endif
    94 
    95 			// Should not find two name tags within a content tag
    96 			User::Leave(KErrCorrupt);
    97 			}
    98 
    99 		#ifdef _DEBUG
   100 		LogL(_L("Start element <name>"));
   101 		#endif
   102 
   103 		iWaitingForTag = ENameTag;
   104 		// This will parse the name and call StringParseCompleteL() when done
   105 		StartChildParserL(CStringParser::NewL(*this), aElement, aAttributes);
   106 		}	
   107 	else if(KUniqueIdTag().CompareF(tag) == 0)
   108 		{
   109 		if(iUniqueId)
   110 			{
   111 			#ifdef _DEBUG
   112 			LogL(_L("ERROR: Second <uniqueid> tag found for content"));
   113 			#endif
   114 
   115 			// Should not find two UniqueId tags within a content tag
   116 			User::Leave(KErrCorrupt);
   117 			}
   118 
   119 		#ifdef _DEBUG
   120 		LogL(_L("Start element <uniqueid>"));
   121 		#endif
   122 		
   123 		iWaitingForTag = EUniqueIdTag;
   124 		StartChildParserL(CStringParser::NewL(*this), aElement, aAttributes);
   125 		}	
   126 	else if(KFileNameTag().CompareF(tag) == 0)
   127 		{
   128 		if(iFileName)
   129 			{
   130 			#ifdef _DEBUG
   131 			LogL(_L("ERROR: Second <filename> tag found for content"));
   132 			#endif
   133 
   134 			// Should not find two filename tags within a content tag
   135 			User::Leave(KErrCorrupt);
   136 			}
   137 
   138 		#ifdef _DEBUG
   139 		LogL(_L("Start element <filename>"));
   140 		#endif
   141 
   142 		iWaitingForTag = EFileNameTag;
   143 		StartChildParserL(CStringParser::NewL(*this), aElement, aAttributes);
   144 		}
   145 	else if(KAttributeTag().CompareF(tag) == 0)
   146 		{
   147 		// Set an attribute
   148 		StartChildParserL(CAttributeParser::NewL(*this), aElement, aAttributes);
   149 		}
   150 	else if(KStringAttributeTag().CompareF(tag) == 0)
   151 		{
   152 		// Set a string attribute
   153 		StartChildParserL(CStringAttributeParser::NewL(*this), aElement, aAttributes);		
   154 		}
   155 	else
   156 		{
   157 		#ifdef _DEBUG
   158 		LogL(_L("ERROR: Unexpected tag <%S> found"),tag);
   159 		#endif
   160 
   161 		// unexpected start tag
   162 		User::Leave(KErrCorrupt);	
   163 		}
   164 	}
   165 	
   166 void CContentParser::ParseEndElementL(const RTagInfo& aElement)
   167 	{
   168 	TInt err = KErrNone;
   169 	TBuf <KMaxDataTypeLength> mimeType;
   170 	if(KContentTag().CompareF(aElement.LocalName().DesC()) == 0)
   171 		{
   172 		// Validate and add to iCurrentContainer
   173 		// iCurrentContainer has ownership of the content
   174 		if(iCid && iName && iUniqueId && iFileName)
   175 			{
   176 			#ifdef _DEBUG
   177 			LogL(_L("  End Element </content>"));
   178 			#endif
   179 
   180 			err = iStringAttributeSet.GetValue(EMimeType, mimeType);
   181 			if(err != KErrNone || mimeType.Length() == 0)
   182 				{
   183 				#ifdef _DEBUG
   184 				LogL(_L("ERROR: A string attribute \"mimetype\" not specified for content with cid: %S"), *iCid);
   185 				#endif
   186 
   187 				User::Leave(KErrCorrupt);
   188 				}
   189 			else
   190 				{
   191 				iCurrentContainer.AddContentL(*iCid, *iName, *iUniqueId, *iFileName, iAttributeSet, iStringAttributeSet);
   192 				}
   193 			}
   194 		else
   195 			{
   196 			#ifdef _DEBUG
   197 			LogL(_L("ERROR: </content> tag not expected yet, name, uniqueid, filename and the mimetype stringattribute must all be specified first"));
   198 			#endif
   199 
   200 			User::Leave(KErrCorrupt);	
   201 			}
   202 		}
   203 	else
   204 		{
   205 		#ifdef _DEBUG
   206 		LogL(_L("ERROR: Unexpected end element </%S>, expected </content>"),aElement.LocalName().DesC());
   207 		#endif
   208 
   209 		// We were expecting the <\content> tag
   210 		User::Leave(KErrCorrupt);	
   211 		}
   212 	}
   213 
   214 void CContentParser::StringParseCompleteL(const TDesC& aString)
   215 	{
   216 	switch(iWaitingForTag)
   217 		{
   218 	case ENameTag:
   219 		iName = aString.AllocL();
   220 		if(iName->Length() > KMaxCafContentName)
   221 		{
   222 			#ifdef _DEBUG
   223 			LogL(_L("ERROR: content of <name> tag is too large"));
   224 			#endif
   225 
   226 			// enforce from boundary check
   227 			User::Leave(KErrCAOutOfRange);
   228 		}
   229 		break;
   230 	case EUniqueIdTag:
   231 		iUniqueId = aString.AllocL();
   232 		if(iUniqueId->Length() > KMaxCafUniqueId)
   233 			{
   234 			#ifdef _DEBUG
   235 			LogL(_L("ERROR: content of <uniqueid> tag is too large"));
   236 			#endif
   237 
   238 			// enforce from boundary check
   239 			User::Leave(KErrCAOutOfRange);
   240 			}
   241 		break;
   242 	case EFileNameTag:
   243 		iFileName = aString.AllocL();
   244 		break;
   245 	default:
   246 		User::Panic(KRtaPanicString(), ERtaPanicParserInvalidState);
   247 		break;
   248 		}
   249 	}
   250 
   251 
   252 void CContentParser::SetAttributeL(TInt aAttribute, TInt aValue)
   253 	{
   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));
   258 	}
   259 	
   260 void CContentParser::SetStringAttributeL(TInt aStringAttribute, const TDesC& aValue)
   261 	{
   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));
   266 	}