servicediscoveryandcontrol/pnp/test/upnp/upnpdescription/src/cupnpdevicecomposer.cpp
changeset 0 f5a58ecadc66
equal deleted inserted replaced
-1:000000000000 0:f5a58ecadc66
       
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "cupnpdevicecomposer.h"
       
    17 #include <upnpdescriptionschema.h>
       
    18 #include "inetprottextutils.h"
       
    19 #include "tattributeiter.h"
       
    20 #include "cattribute.h"
       
    21 
       
    22 #define COMPOSE_BUFLENGTH 1024
       
    23 
       
    24 _LIT8(KStringXmlStart,"<?xml version=\"1.0\" encoding=\"utf-8\"?>");
       
    25 // Below hard coding should be removed
       
    26 _LIT8(KStringRootStart,"\n<root xmlns=\"urn:schemas-upnp-org:device-1-0\">");
       
    27 _LIT8(KDlnaNameSpace," xmlns:dlna=\"urn:schemas-dlna-org:device-1-0\">");
       
    28 _LIT8(KDlna,"dlna:");
       
    29 
       
    30 _LIT8(KBeginOpen,"\n<");
       
    31 _LIT8(KBeginClose,"</"); 
       
    32 _LIT8(KEnd,">");
       
    33 
       
    34 
       
    35 const TStringTable& CUPnPDeviceXmlComposer::GetTable()
       
    36 	{
       
    37 	return UPNPDESCRIPTIONXMLTAGS::Table;
       
    38 	}
       
    39 
       
    40 void CUPnPDeviceXmlComposer::AppendLiteralL(TInt aTag, RBuf8& aBuf, TInt& count, TBool aIsStart )
       
    41 	{
       
    42 	
       
    43     RString  str = iStringPool.String(aTag,UPNPDESCRIPTIONXMLTAGS::Table );
       
    44     TInt litSize = str.DesC().Size();
       
    45     
       
    46     if(aIsStart)
       
    47     	{
       
    48     	litSize+= KBeginOpen.iTypeLength + KEnd.iTypeLength;
       
    49     	}
       
    50     else
       
    51     	{
       
    52     	litSize+= KBeginClose.iTypeLength + KEnd.iTypeLength;
       
    53     	}
       
    54 	
       
    55 	if((count+litSize)>= aBuf.MaxSize()) 
       
    56 		{ 
       
    57 		aBuf.ReAllocL(((aBuf.MaxSize()+COMPOSE_BUFLENGTH)<(aBuf.MaxSize()+litSize))?(aBuf.MaxSize()+litSize):(aBuf.MaxSize()+COMPOSE_BUFLENGTH));
       
    58 		}
       
    59 
       
    60 		if(aIsStart)
       
    61 			{
       
    62 			aBuf.Append(KBeginOpen);
       
    63 			}
       
    64 		else
       
    65 			{
       
    66 			aBuf.Append(KBeginClose);
       
    67 			}
       
    68 		aBuf.Append(str.DesC()); 
       
    69 		aBuf.Append(KEnd);
       
    70 		count+=litSize;
       
    71 	}
       
    72 void CUPnPDeviceXmlComposer::AppendLiteralL(const RString& aString, RBuf8& aBuf, TInt& count, TBool aIsStart )
       
    73 	{
       
    74 	TInt litSize = aString.DesC().Size();
       
    75     if(aIsStart)
       
    76     	{
       
    77     	litSize+= KBeginOpen.iTypeLength + KEnd.iTypeLength;
       
    78     	}
       
    79     else
       
    80     	{
       
    81     	litSize+= KBeginClose.iTypeLength + KEnd.iTypeLength;
       
    82     	}
       
    83 	
       
    84 	if((count+litSize)>= aBuf.MaxSize()) 
       
    85 		{ 
       
    86 		aBuf.ReAllocL(((aBuf.MaxSize()+COMPOSE_BUFLENGTH)<(aBuf.MaxSize()+litSize))?(aBuf.MaxSize()+litSize):(aBuf.MaxSize()+COMPOSE_BUFLENGTH));
       
    87 		}
       
    88 
       
    89 		if(aIsStart)
       
    90 			{
       
    91 			aBuf.Append(KBeginOpen);
       
    92 			}
       
    93 		else
       
    94 			{
       
    95 			aBuf.Append(KBeginClose);
       
    96 			}
       
    97 		aBuf.Append(aString.DesC()); 
       
    98 		aBuf.Append(KEnd);
       
    99 		count+=litSize;
       
   100 	}
       
   101 
       
   102 void CUPnPDeviceXmlComposer::AppendLiteralL(const TDesC8& aLiteral, RBuf8& aBuf, TInt& count)
       
   103 	{
       
   104     TInt litSize = aLiteral.Length();
       
   105     
       
   106 	if((count+litSize)>= aBuf.MaxSize()) 
       
   107 		{ 
       
   108 		aBuf.ReAllocL(((aBuf.MaxSize()+COMPOSE_BUFLENGTH)<(aBuf.MaxSize()+litSize))?(aBuf.MaxSize()+litSize):(aBuf.MaxSize()+COMPOSE_BUFLENGTH));
       
   109 		}
       
   110 
       
   111 		aBuf.Append(aLiteral); 
       
   112 		count+=litSize;
       
   113 	}
       
   114 
       
   115 /**
       
   116 Allocates and constructs a CUPnPDeviceXmlComposer object.
       
   117 Initialises all member data to their default values.
       
   118 */
       
   119 CUPnPDeviceXmlComposer* CUPnPDeviceXmlComposer::NewL( const RStringPool& aStringPool)
       
   120     {
       
   121     CUPnPDeviceXmlComposer* self = new (ELeave) CUPnPDeviceXmlComposer( aStringPool );
       
   122     return self;
       
   123     }
       
   124 
       
   125 CUPnPDeviceXmlComposer::CUPnPDeviceXmlComposer( const RStringPool& aStringPool): iStringPool (aStringPool)
       
   126     {
       
   127     iError = KErrNone;
       
   128     }
       
   129 /**
       
   130 Destructor
       
   131 */
       
   132 CUPnPDeviceXmlComposer::~CUPnPDeviceXmlComposer()
       
   133     {
       
   134     }
       
   135 
       
   136 void CUPnPDeviceXmlComposer::AppendDeviceAttributesL(const CUPnPDevice*	aDeviceAttribute, RBuf8 &aXmlData, TInt &count)
       
   137 	{
       
   138 	TAttributeIter it(aDeviceAttribute);
       
   139 	const CAttribute* nextParam = NULL;
       
   140 	if ( aDeviceAttribute->Dlna())
       
   141 		{
       
   142 		if (aDeviceAttribute->Property(iStringPool.String(UPNPDESCRIPTIONXMLTAGS::EDlnaDoc, GetTable()))!= KNullDesC8 )
       
   143 			{
       
   144 			RBuf8 dlnaValue;
       
   145 			dlnaValue.CreateL( 100 );
       
   146 			dlnaValue.Append(KBeginOpen);
       
   147 			dlnaValue.Append(KDlna);
       
   148 			dlnaValue.Append(iStringPool.String(UPNPDESCRIPTIONXMLTAGS::EDlnaDoc, GetTable()).DesC());
       
   149 			dlnaValue.Append( KDlnaNameSpace );
       
   150 			AppendLiteralL(dlnaValue, aXmlData,count );
       
   151 			dlnaValue.Close();
       
   152 			AppendLiteralL(aDeviceAttribute->Property(iStringPool.String(UPNPDESCRIPTIONXMLTAGS::EDlnaDoc, GetTable())),aXmlData,count);
       
   153 			dlnaValue.CreateL(50);
       
   154 			dlnaValue.Append(KBeginClose);
       
   155 			dlnaValue.Append(KDlna);
       
   156 			dlnaValue.Append(iStringPool.String(UPNPDESCRIPTIONXMLTAGS::EDlnaDoc, GetTable()).DesC());
       
   157 			dlnaValue.Append( KEnd );	
       
   158 			AppendLiteralL(dlnaValue, aXmlData,count );
       
   159 			dlnaValue.Close();
       
   160 			}
       
   161 		if (aDeviceAttribute->Property(iStringPool.String(UPNPDESCRIPTIONXMLTAGS::EDlnaCap, GetTable()))!= KNullDesC8 )
       
   162 			{
       
   163 			RBuf8 dlnaValue;
       
   164 			dlnaValue.CreateL( 100 );
       
   165 			dlnaValue.Append(KBeginOpen);
       
   166 			dlnaValue.Append(KDlna);
       
   167 			dlnaValue.Append(iStringPool.String(UPNPDESCRIPTIONXMLTAGS::EDlnaCap, GetTable()).DesC());
       
   168 			dlnaValue.Append( KDlnaNameSpace );
       
   169 			dlnaValue.Append( KEnd );
       
   170 			AppendLiteralL(dlnaValue, aXmlData,count );
       
   171 			dlnaValue.Close();
       
   172 			AppendLiteralL(aDeviceAttribute->Property(iStringPool.String(UPNPDESCRIPTIONXMLTAGS::EDlnaCap, GetTable())),aXmlData,count);
       
   173 			dlnaValue.CreateL(50);
       
   174 			dlnaValue.Append(KBeginClose);
       
   175 			dlnaValue.Append(KDlna);
       
   176 			dlnaValue.Append(iStringPool.String(UPNPDESCRIPTIONXMLTAGS::EDlnaCap, GetTable()).DesC());
       
   177 			dlnaValue.Append( KEnd );	
       
   178 			AppendLiteralL(dlnaValue, aXmlData,count );
       
   179 			dlnaValue.Close();
       
   180 			}
       
   181 		
       
   182 		}
       
   183 	while (!it.AtEnd())
       
   184 		{
       
   185 		nextParam = it();
       
   186 		TInt index = nextParam->AttributeName().Index(GetTable());
       
   187 		if (index != UPNPDESCRIPTIONXMLTAGS::EDlnaDoc && index != UPNPDESCRIPTIONXMLTAGS::EDlnaCap )
       
   188 			{
       
   189 			AppendLiteralL(nextParam->AttributeName(),aXmlData,count,ETrue);
       
   190 			AppendLiteralL(nextParam->AttributeValue(),aXmlData,count);
       
   191 			AppendLiteralL(nextParam->AttributeName(),aXmlData,count,EFalse);
       
   192 			++it;
       
   193 			}
       
   194 		else
       
   195 			++it;
       
   196 		}
       
   197 	}
       
   198 
       
   199 void CUPnPDeviceXmlComposer::AppendDeviceIconL(const CUPnPIconInfo* aIconInfo, RBuf8 &aXmlData, TInt &count)
       
   200 	{
       
   201 	TAttributeIter it(aIconInfo);
       
   202 	const CAttribute* nextParam = NULL;
       
   203 	while (!it.AtEnd())
       
   204 		{
       
   205 		nextParam = it();
       
   206 		AppendLiteralL(nextParam->AttributeName(),aXmlData,count,ETrue);
       
   207 		AppendLiteralL(nextParam->AttributeValue(),aXmlData,count);
       
   208 		AppendLiteralL(nextParam->AttributeName(),aXmlData,count,EFalse);
       
   209 		++it;
       
   210 		}
       
   211 	}
       
   212 	
       
   213 void CUPnPDeviceXmlComposer::AppendServiceL(const CUPnPServiceInfo* aServInfo, RBuf8 &aXmlData, TInt &count)
       
   214 	{
       
   215 	TAttributeIter it(aServInfo);
       
   216 	const CAttribute* nextParam = NULL;
       
   217 	while (!it.AtEnd())
       
   218 		{
       
   219 		nextParam = it();
       
   220 		AppendLiteralL(nextParam->AttributeName(),aXmlData,count,ETrue);
       
   221 		AppendLiteralL(nextParam->AttributeValue(),aXmlData,count);
       
   222 		AppendLiteralL(nextParam->AttributeName(),aXmlData,count,EFalse);
       
   223 		++it;
       
   224 		}
       
   225 	}
       
   226 
       
   227 void CUPnPDeviceXmlComposer::AppendDeviceXmlL(const CUPnPDevice* devObj, RBuf8 &aXmlData, TInt &count)
       
   228 	{
       
   229 
       
   230     AppendLiteralL(UPNPDESCRIPTIONXMLTAGS::EDevice,aXmlData,count,ETrue);
       
   231 
       
   232 	if(devObj->Property(iStringPool.String(UPNPDESCRIPTIONXMLTAGS::EPresentationUrl, GetTable())).Length()!=0)
       
   233 		{
       
   234         AppendLiteralL(UPNPDESCRIPTIONXMLTAGS::EPresentationUrl,aXmlData,count,ETrue);
       
   235 	    AppendLiteralL(devObj->Property(iStringPool.String(UPNPDESCRIPTIONXMLTAGS::EPresentationUrl, GetTable())),aXmlData,count);
       
   236         AppendLiteralL(UPNPDESCRIPTIONXMLTAGS::EPresentationUrl,aXmlData,count,EFalse);
       
   237 		}
       
   238 
       
   239     AppendDeviceAttributesL(devObj,aXmlData,count);
       
   240     if(devObj->CountOfIconInfoTable()!=0)
       
   241     	{
       
   242     	const CUPnPIconInfo* aIconInfo = NULL;
       
   243     	AppendLiteralL(UPNPDESCRIPTIONXMLTAGS::EIconList,aXmlData,count,ETrue);
       
   244         for(TInt k=0;k<devObj->CountOfIconInfoTable();k++)
       
   245         	{
       
   246             AppendLiteralL(UPNPDESCRIPTIONXMLTAGS::EIcon,aXmlData,count,ETrue);
       
   247             aIconInfo = devObj->AtIconInfoTable(k);
       
   248             AppendDeviceIconL(aIconInfo, aXmlData, count);
       
   249             AppendLiteralL(UPNPDESCRIPTIONXMLTAGS::EIcon,aXmlData,count,EFalse);
       
   250         	}
       
   251     	AppendLiteralL(UPNPDESCRIPTIONXMLTAGS::EIconList,aXmlData,count,EFalse);
       
   252     	}
       
   253     
       
   254     // service info goes here
       
   255     
       
   256     if(devObj->CountOfServiceInfoTable()!=0)
       
   257     	{
       
   258     	const CUPnPServiceInfo* aServInfo = NULL;
       
   259     	AppendLiteralL(UPNPDESCRIPTIONXMLTAGS::EServiceList,aXmlData,count,ETrue);
       
   260     	
       
   261     	for(TInt m=0;m<devObj->CountOfServiceInfoTable();m++)
       
   262     		{
       
   263     		AppendLiteralL(UPNPDESCRIPTIONXMLTAGS::EService,aXmlData,count,ETrue);
       
   264     		aServInfo = devObj->AtServiceInfoTable(m);
       
   265     		AppendServiceL(aServInfo,aXmlData, count);
       
   266     		AppendLiteralL(UPNPDESCRIPTIONXMLTAGS::EService,aXmlData,count,EFalse);
       
   267     		}
       
   268     	
       
   269     	AppendLiteralL(UPNPDESCRIPTIONXMLTAGS::EServiceList,aXmlData,count,EFalse);
       
   270     	}
       
   271 
       
   272     
       
   273     if(devObj->CountOfEmbeddedDeviceInfoTable()!= 0 )
       
   274     	{
       
   275     	AppendLiteralL(UPNPDESCRIPTIONXMLTAGS::EDeviceList,aXmlData,count,ETrue);
       
   276     	
       
   277     	for(TInt n = 0; n < devObj->CountOfEmbeddedDeviceInfoTable(); n++)
       
   278     		AppendDeviceXmlL(devObj->AtEmbeddedDeviceInfoTable(n),aXmlData,count);
       
   279     	
       
   280     	AppendLiteralL(UPNPDESCRIPTIONXMLTAGS::EDeviceList,aXmlData,count,EFalse);
       
   281     	}
       
   282 
       
   283     
       
   284     AppendLiteralL(UPNPDESCRIPTIONXMLTAGS::EDevice,aXmlData,count,EFalse);
       
   285 
       
   286 	}
       
   287 
       
   288 void CUPnPDeviceXmlComposer::ComposeDeviceXmlL(const CUPnPDeviceDescription *aDeviceDescObj, RBuf8 &aXmlData)
       
   289 	{
       
   290 	RBuf	unicode;
       
   291     TInt	count = 0;
       
   292  
       
   293 
       
   294     if(!(aDeviceDescObj->Validate(iStringPool,GetTable())))
       
   295     	User::Leave(KErrCorrupt);
       
   296  
       
   297     User::LeaveIfError(aXmlData.Create(COMPOSE_BUFLENGTH));
       
   298         
       
   299     AppendLiteralL(KStringXmlStart,aXmlData,count);
       
   300     AppendLiteralL(KStringRootStart,aXmlData,count);
       
   301 
       
   302     AppendLiteralL(UPNPDESCRIPTIONXMLTAGS::ESpecVersion,aXmlData,count,ETrue);
       
   303 
       
   304     HBufC8* versionMajor = NULL;
       
   305     HBufC8* versionMinor = NULL;
       
   306     InetProtTextUtils::ConvertIntToDescriptorL(aDeviceDescObj->MajorNumber(), versionMajor);
       
   307     InetProtTextUtils::ConvertIntToDescriptorL(aDeviceDescObj->MinorNumber(), versionMinor);
       
   308     CleanupStack::PushL(versionMajor);
       
   309     CleanupStack::PushL(versionMinor);
       
   310     
       
   311     AppendLiteralL(UPNPDESCRIPTIONXMLTAGS::EMajorNumber,aXmlData,count,ETrue);
       
   312     AppendLiteralL(versionMajor->Des(),aXmlData,count);
       
   313     AppendLiteralL(UPNPDESCRIPTIONXMLTAGS::EMajorNumber,aXmlData,count,EFalse);
       
   314     AppendLiteralL(UPNPDESCRIPTIONXMLTAGS::EMinorNumber,aXmlData,count,ETrue);
       
   315     AppendLiteralL(versionMinor->Des(),aXmlData,count);
       
   316     AppendLiteralL(UPNPDESCRIPTIONXMLTAGS::EMinorNumber,aXmlData,count,EFalse);
       
   317     
       
   318     CleanupStack::PopAndDestroy(versionMinor);
       
   319     CleanupStack::PopAndDestroy(versionMajor);
       
   320 
       
   321     
       
   322     AppendLiteralL(UPNPDESCRIPTIONXMLTAGS::ESpecVersion,aXmlData,count,EFalse);
       
   323     
       
   324     CUPnPDevice* devObj =  aDeviceDescObj->DeviceObject();
       
   325     if(aDeviceDescObj->Property(iStringPool.String(UPNPDESCRIPTIONXMLTAGS::EUrlBase, GetTable())).Length()!=0)
       
   326     	{
       
   327         AppendLiteralL(UPNPDESCRIPTIONXMLTAGS::EUrlBase,aXmlData,count,ETrue);
       
   328         AppendLiteralL(aDeviceDescObj->Property(iStringPool.String(UPNPDESCRIPTIONXMLTAGS::EUrlBase, GetTable())),aXmlData,count);
       
   329         AppendLiteralL(UPNPDESCRIPTIONXMLTAGS::EUrlBase,aXmlData,count,EFalse);
       
   330     	}
       
   331     
       
   332     // Add device specific info to the xml buffer.
       
   333     AppendDeviceXmlL(devObj,aXmlData,count);
       
   334 
       
   335     AppendLiteralL(UPNPDESCRIPTIONXMLTAGS::ERoot,aXmlData,count,EFalse);
       
   336 	
       
   337  	}
       
   338