applayerprotocols/wappushsupport/XmlLib/XmlLib.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 // XmlLib.h
       
    15 // 
       
    16 //
       
    17 
       
    18 // Local includes
       
    19 //
       
    20 #include "XmlPars.h"
       
    21 #include "XmlValid.h"
       
    22 
       
    23 //	System includes
       
    24 //
       
    25 #include <attrlut.h>
       
    26 #include <xmlelemt.h>
       
    27 #include <wapattrdf.h>
       
    28 #include <estatus.h>
       
    29 #include <mwappluginsp.h>
       
    30 #include <mframeworksp.h>
       
    31 #include <xmllib.h>
       
    32 
       
    33 //	METHOD IMPLEMENTATIONS
       
    34 //
       
    35 
       
    36 EXPORT_C CXmlLibrary::CXmlLibrary(MWapPluginSP& aPluginSP, CXmlElement* aRootNode)
       
    37 : iPluginSP(aPluginSP), iRootNode(aRootNode)
       
    38 	{
       
    39 	}
       
    40 
       
    41 EXPORT_C CXmlLibrary::~CXmlLibrary()
       
    42 	{
       
    43 	delete iParser;
       
    44 	}
       
    45 
       
    46 EXPORT_C CXmlLibrary* CXmlLibrary::NewL(MWapPluginSP& aPluginSP, CXmlElement* aRootNode)
       
    47 	{
       
    48 	CXmlLibrary* self = CXmlLibrary::NewLC(aPluginSP, aRootNode);
       
    49 	CleanupStack::Pop(self);
       
    50 	return(self);
       
    51 	}
       
    52 
       
    53 EXPORT_C CXmlLibrary* CXmlLibrary::NewLC(MWapPluginSP& aPluginSP, CXmlElement* aRootNode)
       
    54 	{
       
    55 	CXmlLibrary* self = new(ELeave) CXmlLibrary(aPluginSP, aRootNode);
       
    56 	CleanupStack::PushL(self);
       
    57 	self->ConstructL();
       
    58 	return(self);
       
    59 	}
       
    60 
       
    61 EXPORT_C void CXmlLibrary::ConstructL()
       
    62 	{
       
    63 	}
       
    64 
       
    65 EXPORT_C void CXmlLibrary::PrepareParserL()
       
    66 // A utlity method to set up the parser for parsing
       
    67 // Notice, that the iRootNode has to be set before this one is called!
       
    68 	{
       
    69 	iParser = CXmlParser::NewL(iPluginSP.AttributeLUT());
       
    70 	iParser->ResetL(iRootNode);
       
    71 	}
       
    72 
       
    73 EXPORT_C void CXmlLibrary::ResetL(CXmlElement* aRootNode)
       
    74 // Reset the library and parser for processing of new document
       
    75 	{
       
    76 	iRootNode = aRootNode;
       
    77 	iDTD = NULL;
       
    78 	if( iParser != NULL)
       
    79 		{
       
    80 		iParser->ResetL(iRootNode);
       
    81 		}
       
    82 	}
       
    83 
       
    84 // WAP 1.2 Addition!
       
    85 EXPORT_C TInt CXmlLibrary::ProcessDataL(TDesC8& aData)
       
    86 	{
       
    87 	HBufC8* newData = aData.AllocLC();
       
    88 	TInt ret = ProcessDataL(*newData);
       
    89 	CleanupStack::PopAndDestroy();	// newData
       
    90 	return (ret);
       
    91 	}
       
    92 
       
    93 EXPORT_C TInt CXmlLibrary::ProcessDataL(HBufC8& aData)
       
    94 // Method for processing the streaming data
       
    95 	{
       
    96 	if( iParser == NULL )
       
    97 		{
       
    98 		PrepareParserL();
       
    99 		}
       
   100 	iParser->ProcessDataL(aData);
       
   101 
       
   102 	return( ExecuteDataProcessingL() );
       
   103 	}
       
   104 
       
   105 EXPORT_C TInt CXmlLibrary::CommitL()
       
   106 // Method to inform the library that all the data has been passed in
       
   107 // Performs finishing tasks for the parsing, checks if all the data was parsed
       
   108 // correctly and if the DTD tree was available the validation is also performed!
       
   109 	{
       
   110 	TInt returnCode = CommitParserL();
       
   111 	if( returnCode != KErrNone )
       
   112 		{
       
   113 		return( returnCode );
       
   114 		}
       
   115 
       
   116 	if( iDTD != NULL )
       
   117 		{
       
   118 		// iDTD contains the correct dtdRoot, call the protected method
       
   119 		return( ExecuteValidateL(iDTD) );
       
   120 		}
       
   121 
       
   122 	return(KErrNone);
       
   123 	}
       
   124 
       
   125 EXPORT_C TInt CXmlLibrary::ValidateL(CBNFNode& aDTDRootNode)
       
   126 // Service method for validation
       
   127 	{
       
   128 	CBNFNode* dtdTree = ExtractDTDTree(&aDTDRootNode);
       
   129 	if( dtdTree == NULL )
       
   130 		{
       
   131 		// Check to see if we have buffered the data.
       
   132 		const HBufC* id = iPluginSP.AttributeLUT().Des2IDL(KXmlLibBufferedDocumentAttribute);
       
   133 		if (iRootNode->AttributeExists(id))
       
   134 			{
       
   135 			// Attribute exists - delete the data adn attribute
       
   136 			CDataNoDelete* attribute = (CDataNoDelete*)iRootNode->CNode::Attribute((TAny*)id);
       
   137 			HBufC* bufferedDocument = (HBufC*)attribute->Data();
       
   138 			delete bufferedDocument;
       
   139 			iRootNode->DeleteAttribute(id);
       
   140 			}
       
   141 		return( EWapErrXmlLibInvalidDTD );
       
   142 		}
       
   143 	return( ExecuteValidateL(dtdTree) );
       
   144 	}
       
   145 
       
   146 EXPORT_C TInt CXmlLibrary::ExecuteValidateL(CBNFNode* aDTDRootNode)
       
   147 // Actual execution of validation. This method can be overridden by
       
   148 // inheriting classes.
       
   149 // The aDTDRootNode MUST contain the actual DTD tree, not the engine Root.
       
   150 	{
       
   151 	TInt returnValue = KErrNone;
       
   152 
       
   153 	// Check if the document has already been validated
       
   154 	const HBufC* validAttrId = iPluginSP.AttributeLUT().Des2IDL(KWAPDocumentValidStatusAttributeName);
       
   155 	if( iRootNode->AttributeExists(validAttrId) )
       
   156 		{
       
   157 		return( EWapErrXmlLibDocumentAlreadyValid );
       
   158 		}
       
   159 
       
   160 	// If there was no DTD during the parsing the incoming data was buffered and
       
   161 	// attached as an attribute to the document root node. Now we have received all
       
   162 	// the data and have a DTD, so parse the document again!
       
   163 	const HBufC* id = iPluginSP.AttributeLUT().Des2IDL(KXmlLibBufferedDocumentAttribute);
       
   164 	if( iRootNode->AttributeExists(id) )
       
   165 		{
       
   166 		if( iDTD == NULL )
       
   167 			{
       
   168 			iDTD = aDTDRootNode;
       
   169 			}
       
   170 		if( iParser != NULL )
       
   171 			{
       
   172 			iParser->ResetL(iRootNode);
       
   173 			}
       
   174 		CDataNoDelete* attribute = (CDataNoDelete*)iRootNode->CNode::Attribute((TAny*)id);
       
   175 		HBufC* bufferedDocument = (HBufC*)attribute->Data();
       
   176 		iRootNode->DeleteAttribute(id);
       
   177 		returnValue = ProcessDataL(bufferedDocument);
       
   178 		if( returnValue == KErrNone )
       
   179 			{
       
   180 			returnValue = CommitParserL();
       
   181 			}
       
   182 		if(returnValue != KErrNone )
       
   183 			{
       
   184 			return(returnValue);
       
   185 			}
       
   186 		}
       
   187 	// Validation of the parsed document
       
   188 	CXmlValidator* validator = CXmlValidator::NewL(*aDTDRootNode, iPluginSP.AttributeLUT());
       
   189 	CleanupStack::PushL(validator);
       
   190 	returnValue = validator->ValidateTreeL(iRootNode);
       
   191 	CleanupStack::PopAndDestroy(validator);
       
   192 
       
   193 	// Should the validation finish without errors, mark the document validated 
       
   194 	// by attaching an attribute to the root node
       
   195 	if( returnValue == KErrNone )
       
   196 		{
       
   197 		iRootNode->SetAttributeL(KWAPDocumentValidStatusAttributeName, KNullDesC, iPluginSP.AttributeLUT());
       
   198 		}	
       
   199 	return( returnValue );
       
   200 	}
       
   201 
       
   202 TInt CXmlLibrary::ProcessDataL(HBufC16* aData)
       
   203 // Internal utility function. Used when reparsing the buffered document.
       
   204 // The attributes in the document nodes are 16-bit character data, hence we need a different interface.
       
   205 	{
       
   206 	if( iParser == NULL )
       
   207 		{
       
   208 		PrepareParserL();
       
   209 		}
       
   210 	iParser->ProcessDataL(aData);
       
   211 
       
   212 	return( ExecuteDataProcessingL() );
       
   213 	}
       
   214 
       
   215 TInt CXmlLibrary::CommitParserL()
       
   216 // Internal utility function for finishing off the parser and checking the errors, if any
       
   217 	{
       
   218 	// Finish off the parser
       
   219 	iParser->SetStatus(EPluginComplete);
       
   220 	iParser->CommitL();
       
   221 	if( iParser->ErrorCode() != KErrNone )
       
   222 		{
       
   223 		return( iParser->ErrorCode() );
       
   224 		}
       
   225 	if( !iParser->Valid() )
       
   226 		{
       
   227 		return( EWapErrXmlLibInvalidDocument );
       
   228 		}
       
   229 	return( KErrNone );
       
   230 	}
       
   231 
       
   232 TInt CXmlLibrary::ExecuteDataProcessingL()
       
   233 // A method to examine return codes from parser after a single
       
   234 // parsing pass. Here we also initiate DTD fetched if the parser
       
   235 // returns appropriate signals or we might activate the data gathering
       
   236 // in the parser, if no DTD is available.
       
   237 	{
       
   238 
       
   239 	if( iParser->ErrorCode() == CXmlParser::EDTDDefinitionFound ||
       
   240 		iParser->ErrorCode() == CXmlParser::ENoDTDDefined )
       
   241 		{
       
   242 		if( iDTD == NULL)
       
   243 			{
       
   244 			if( iParser->ErrorCode() == CXmlParser::EDTDDefinitionFound )
       
   245 				{
       
   246 				iDTD = (CBNFNode*)iPluginSP.PrepareDTDL(*iParser->DocType(), *iParser->DTDUrl(), (CDocumentNode*)iRootNode);  // DocType might be NULL!
       
   247 				}
       
   248 			else
       
   249 				{
       
   250 				iDTD = (CBNFNode*)iPluginSP.PrepareDTDL(*iDefaultDoctype, *iDefaultDTDUrl, (CDocumentNode*)iRootNode);
       
   251 				}
       
   252 
       
   253 			if( iDTD != NULL )
       
   254 				{
       
   255 				iDTD = ExtractDTDTree(iDTD);
       
   256 				if( iDTD == NULL )
       
   257 					{
       
   258 					// The engine returned us its root to DTD tree, but this node didn't
       
   259 					// contain the actual DTD tree, hence report an error
       
   260 					return( EWapErrXmlLibInvalidDTD );
       
   261 					}
       
   262 				}
       
   263 			}
       
   264 		if( iDTD == NULL )
       
   265 			{
       
   266 			iParser->SetDataGathering(ETrue);
       
   267 			}
       
   268 		else
       
   269 			{
       
   270 			iParser->SetDTD(iDTD);
       
   271 			}
       
   272 		iParser->ContinueL();
       
   273 		}
       
   274 	return( iParser->ErrorCode() );
       
   275 	}
       
   276 
       
   277 CBNFNode* CXmlLibrary::ExtractDTDTree(CBNFNode* aDTDRoot)
       
   278 // Internal utility function for extracting the actual DTD tree
       
   279 // from the document root node provided by the engine.
       
   280 	{
       
   281 	if( aDTDRoot != NULL )
       
   282 		return( (CBNFNode*)aDTDRoot->NextChild() );
       
   283 	else
       
   284 		return( NULL );
       
   285 	}
       
   286 
       
   287 // RESERVED FOR FUTURE USE
       
   288 EXPORT_C void CXmlLibrary::CXmlLibrary_Reserved1()
       
   289 	{
       
   290 	}
       
   291