applayerprotocols/wappushsupport/XmlElement/XMLELEMT.CPP
changeset 0 b16258d2340f
equal deleted inserted replaced
-1:000000000000 0:b16258d2340f
       
     1 // Copyright (c) 1999-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 // XmlElement.cpp
       
    15 // XML Element class
       
    16 // Derived from CTypedNode. Adds functionality
       
    17 // to make the use of the node simpler
       
    18 // 
       
    19 //
       
    20 
       
    21 #include <f32file.h>
       
    22 #include <attrlut.h> 
       
    23 
       
    24 #include <xmlelemt.h>
       
    25 
       
    26 LOCAL_C TBool Compare(TInt aLength,const TText* aStringA,const TDesC& aStringB)
       
    27 	{
       
    28 	if(aLength==aStringB.Length())
       
    29 		{
       
    30 		const TText* stringB=aStringB.Ptr();
       
    31 		while( STATIC_CAST(TUint, aLength) >= (sizeof(TInt32) / sizeof(TText)) )
       
    32 			{
       
    33 			if(*(TInt32*)stringB!=*(TInt32*)aStringA)
       
    34 				return EFalse;
       
    35 			stringB+=(sizeof(TInt32)/sizeof(TText));
       
    36 			aStringA+=(sizeof(TInt32)/sizeof(TText));
       
    37 			aLength-=(sizeof(TInt32)/sizeof(TText));
       
    38 			}
       
    39 		while(aLength>0)
       
    40 			{
       
    41 			if(*stringB++!=*aStringA++)
       
    42 				return EFalse;
       
    43 			aLength--;
       
    44 			}
       
    45 		return ETrue;
       
    46 		}
       
    47 	return EFalse;
       
    48 	}
       
    49 
       
    50 
       
    51 //-------------------------------------------------------------------------
       
    52 
       
    53 EXPORT_C void CXmlElement::InsertChildL(TInt aIndexToInsertBefore, CXmlElement* aElement)
       
    54 {
       
    55 	if(!iChildList)
       
    56 	{
       
    57 		iChildList = new (ELeave) CArrayPtrFlat<CNode>(KGranularity);
       
    58 	}
       
    59 	iChildList->InsertL(aIndexToInsertBefore, aElement);
       
    60 }
       
    61 
       
    62 //-------------------------------------------------------------------------
       
    63 
       
    64 EXPORT_C const TDesC* CXmlElement::Attribute(const TDesC& aAttributeName) const
       
    65 {
       
    66 	const TInt count = AttributeCount();
       
    67 	TInt length=aAttributeName.Length();
       
    68 	const TText* stringPtr=aAttributeName.Ptr();
       
    69 	for (TInt i = 0; i < count; i++)
       
    70 	{ 
       
    71 		TXmlAttributeType name;
       
    72 		CBase* att=AttributeByIndex(i,name);
       
    73 		if (Compare(length,stringPtr,*name))
       
    74 			return (TDesC*)((CDataDelete*)att)->Data();
       
    75 	}  
       
    76  
       
    77 	return NULL;
       
    78 }
       
    79 
       
    80 //-------------------------------------------------------------------------
       
    81 
       
    82 EXPORT_C void CXmlElement::SetAttributeL(const TDesC& aAttributeName, 
       
    83 										 const TDesC& aAttributeValue, 
       
    84 										 CAttributeLookupTable& aAttributeLUT)
       
    85 {
       
    86 	TXmlAttributeType type = aAttributeLUT.Des2IDL(aAttributeName);
       
    87 
       
    88 	HBufC* attrValue = aAttributeValue.AllocLC();
       
    89 	CDataDelete* attrib = new (ELeave) CDataDelete((HBufC16*)attrValue);
       
    90 	CleanupStack::Pop(); // attrValue
       
    91 	CleanupStack::PushL(attrib);
       
    92 	AddAttributeL(type, attrib);
       
    93 	CleanupStack::Pop(); // attrib
       
    94 }
       
    95 
       
    96 //-------------------------------------------------------------------------
       
    97 
       
    98 EXPORT_C void CXmlElement::SetTextL(const TDesC16& aData)
       
    99 	{
       
   100 	HBufC* data = aData.AllocLC();
       
   101 	CTypedNode<TXmlElementType, TXmlAttributeType>::SetDataL(data);
       
   102 	CleanupStack::Pop(data);
       
   103 	}
       
   104 
       
   105 //-------------------------------------------------------------------------
       
   106 
       
   107 EXPORT_C void CXmlElement::SetDataItemL(CBase* aDataItem, CAttributeLookupTable& aAttributeLUT)
       
   108 {
       
   109 	__ASSERT_DEBUG(aDataItem, Panic(EXmlElPanicInvalidDataItem));
       
   110 
       
   111 	TXmlAttributeType type = aAttributeLUT.Des2IDL(KXmlElementDataItemAttributeName);
       
   112 	CDataItemAttribute* attrib = new (ELeave) CDataItemAttribute;
       
   113 
       
   114 	CleanupStack::PushL(attrib);
       
   115 	AddAttributeL(type, attrib);
       
   116 	CleanupStack::Pop(); // attrib
       
   117 
       
   118 	// Take ownership last
       
   119 	attrib->iDataItem = aDataItem;
       
   120 }
       
   121 
       
   122 //-------------------------------------------------------------------------
       
   123 
       
   124 EXPORT_C void CXmlElement::DeleteDataItem(CAttributeLookupTable& aAttributeLUT)
       
   125 {
       
   126 	TXmlAttributeType type = NULL;
       
   127 	TRAPD(err, type = aAttributeLUT.Des2IDL(KXmlElementDataItemAttributeName));
       
   128 	__ASSERT_DEBUG(err==KErrNone, Panic(EXmlElPanicInvalidDataItem));
       
   129 	if(err==KErrNone)
       
   130 		{
       
   131 		// Delete data item
       
   132 		DeleteAttribute(type);
       
   133 		}
       
   134 }
       
   135 
       
   136 //-------------------------------------------------------------------------
       
   137 
       
   138 EXPORT_C CBase* CXmlElement::DataItem() const
       
   139 {
       
   140 	const TInt count = AttributeCount();
       
   141 	TPtrC attName(KXmlElementDataItemAttributeName);
       
   142 	TInt length=attName.Length();
       
   143 	const TText* stringPtr=attName.Ptr();
       
   144 	for (TInt i = 0; i < count; i++)
       
   145 	{
       
   146 		TXmlAttributeType name;
       
   147 		CBase* att=AttributeByIndex(i,name);
       
   148 		if (Compare(length,stringPtr,*name))
       
   149 			return ((CDataItemAttribute*)att)->iDataItem;
       
   150 	}
       
   151 	return NULL;
       
   152 }
       
   153 
       
   154 //-------------------------------------------------------------------------
       
   155 
       
   156 EXPORT_C TInt CXmlElement::ChildIndex(CXmlElement* aChild) const
       
   157 {
       
   158 	TInt i;
       
   159 	for( i=0; i < Count(); i++)
       
   160 	{
       
   161 		if( Child(i) == aChild )
       
   162 			return(i);
       
   163 	}
       
   164 	return( -1 );
       
   165 }
       
   166 
       
   167 //#ifdef _DEBUG
       
   168 // Write the name of the class into a debug file
       
   169 void CXmlElement::WriteNameL(RFile& aFile, TInt aIndentation)
       
   170 {
       
   171 	// Write line feed
       
   172 	aFile.Write(_L8("\r\n"));
       
   173 
       
   174 	// Write as many tabs as the current indentation level
       
   175 	for (TInt i = 0; i < aIndentation; i++)
       
   176 	{
       
   177 		aFile.Write(_L8("\t"));
       
   178 	}
       
   179  
       
   180 	// Write the name of element
       
   181 	TXmlElementType type = Type();
       
   182 
       
   183 	if (type)
       
   184 	{
       
   185 		// Convert to 8 bit
       
   186 		HBufC8* type8 = HBufC8::NewL(type->Length());
       
   187 		type8->Des().Copy(*type);
       
   188 		aFile.Write(*type8);
       
   189 		delete type8;
       
   190 	}
       
   191 	else
       
   192 		aFile.Write(_L8("Error: Type = NULL"));
       
   193 
       
   194 
       
   195 	// Write attributes
       
   196 	const TInt count = AttributeCount();
       
   197 	if(count > 0 || Data())
       
   198 	{
       
   199 		aFile.Write(_L8(" ("));
       
   200 	}
       
   201 
       
   202 	for (TInt a = 0; a < count; a++)
       
   203 	{
       
   204 		if(a>0 && count>1)
       
   205 		{
       
   206 			aFile.Write(_L8("\", "));
       
   207 		}
       
   208 		TXmlAttributeType type = AttributeTypeByIndex(a);
       
   209 		// Convert to 8 bit
       
   210 		HBufC8* type8 = HBufC8::NewL(type->Length());
       
   211 		type8->Des().Copy(*type);
       
   212 		aFile.Write(*type8);
       
   213 		delete type8;
       
   214 
       
   215 		aFile.Write(_L8(" = \""));
       
   216 
       
   217 		const TDesC* attrib = Attribute(a);
       
   218 		HBufC8* attrib8 = HBufC8::NewL(attrib->Length());
       
   219 		attrib8->Des().Copy(*attrib);
       
   220 		aFile.Write(*attrib8);
       
   221 		delete attrib8;
       
   222 	}
       
   223  
       
   224 	// Write data
       
   225 	if (Data())
       
   226 	{
       
   227 		// Data is expected to be a text (for now)
       
   228 		aFile.Write(_L8("\""));
       
   229 		const TDesC* data = (TDesC*)Data();
       
   230 		HBufC8* data8 = HBufC8::NewL(data->Length());
       
   231 		data8->Des().Copy(*data);
       
   232 		aFile.Write(*data8);
       
   233 		delete data8;
       
   234 		aFile.Write(_L8("\""));
       
   235 	}
       
   236 
       
   237 	if(count>0)
       
   238 	{
       
   239 		// Write ")
       
   240 		aFile.Write(_L8("\")"));
       
   241 	}
       
   242 	else if(Data())
       
   243 	{
       
   244 		// Write )
       
   245 		aFile.Write(_L8(")"));
       
   246 	}
       
   247 }
       
   248 
       
   249 //-------------------------------------------------------------------------
       
   250 // Write name of class in debug file
       
   251 void CXmlElement::StartWriteL(RFile& aFile, TInt aIndentation)
       
   252 {
       
   253 	// Write itself
       
   254 	WriteNameL(aFile, aIndentation);
       
   255 
       
   256 	// Write children with one tab stop more
       
   257 	aIndentation++;
       
   258 	const TInt count = Count();
       
   259 	for (TInt i = 0; i < count; i++)
       
   260 	{
       
   261 		Child(i)->StartWriteL(aFile, aIndentation);
       
   262 	}
       
   263 }
       
   264 
       
   265 EXPORT_C void CXmlElement::WriteIntoFileL() 
       
   266 { 
       
   267 	RFs fileSession;
       
   268 	User::LeaveIfError(fileSession.Connect());
       
   269 	CleanupClosePushL(fileSession);
       
   270 	RFile file2;
       
   271 	TPtrC filePath;
       
   272 	filePath.Set( _L("c:\\tree.txt"));
       
   273 	User::LeaveIfError(file2.Replace( fileSession, filePath , EFileWrite ));
       
   274 	CleanupClosePushL(file2);
       
   275 
       
   276 	// Write contents
       
   277 	StartWriteL(file2, 0);
       
   278 	CleanupStack::PopAndDestroy(2);	// fileSession, file2
       
   279 }
       
   280 
       
   281 //#endif
       
   282 #ifndef __WAP_MONOLITHIC__
       
   283 // Its a standalone DLL
       
   284 //-------------------------------------------------------------------------
       
   285 
       
   286 GLDEF_C void Panic(TXmlElementPanic aPanic)
       
   287 {
       
   288 	User::Panic(_L("XML-ELM"),aPanic);	
       
   289 }
       
   290 
       
   291 //-----------------------------------------------------------------------------
       
   292 //    	Dlls entry point
       
   293 //-----------------------------------------------------------------------------
       
   294 
       
   295 #ifndef EKA2
       
   296 GLDEF_C TInt E32Dll (TDllReason)
       
   297 {
       
   298 	return KErrNone;
       
   299 }
       
   300 #endif // EKA2
       
   301 #endif // __WAP_MONOLITHIC__
       
   302