languageinterworkingfw/servicehandler/src/liwxmlhandler.cpp
changeset 57 61b27eec6533
parent 45 7aa6007702af
equal deleted inserted replaced
45:7aa6007702af 57:61b27eec6533
     1 /*
       
     2 * Copyright (c) 2003-2005 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:       Wrapper to XML parser interface. Loads the inline and external XML metadata
       
    15 *				 and populates the service provider metadata information. This class
       
    16 *				 uses Symbian SAX parser to load the metadata information.
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 
       
    24 
       
    25 
       
    26 #include "liwxmlhandler.h"
       
    27 #include <f32file.h>
       
    28 #include "data_caging_path_literals.hrh"
       
    29 #include <e32capability.h>
       
    30 
       
    31 #include "liwservicedata.h"
       
    32 #include <liwvariant.h>
       
    33 
       
    34 _LIT8(KMetaDataName, "meta");
       
    35 
       
    36 
       
    37 /*
       
    38  * Creates and returns an instance of \c CLiwXmlHandler
       
    39  */
       
    40 CLiwXmlHandler* CLiwXmlHandler::NewL()
       
    41 {
       
    42 	CLiwXmlHandler* ptrToThis = CLiwXmlHandler::NewLC();
       
    43 	CleanupStack::Pop(ptrToThis);
       
    44 	
       
    45 	return ptrToThis;
       
    46 }
       
    47 
       
    48 /*
       
    49  * Creates and returns an instance of \c CLiwXmlHandler
       
    50  * Leaves the created instance in the cleanupstack.
       
    51  *
       
    52  * @return an instance of \c CLiwXmlHandler
       
    53  */
       
    54 CLiwXmlHandler* CLiwXmlHandler::NewLC()
       
    55 {
       
    56 	CLiwXmlHandler* ptrToThis = new (ELeave) CLiwXmlHandler();
       
    57 	CleanupStack::PushL(ptrToThis);
       
    58 	
       
    59 	ptrToThis->ConstructL();
       
    60 	
       
    61 	return ptrToThis;
       
    62 }
       
    63 
       
    64 
       
    65 /*
       
    66  * Destructor. Deletes the handler to XML parser
       
    67  */	
       
    68 CLiwXmlHandler::~CLiwXmlHandler()
       
    69 {
       
    70 	if(iXmlHandler)
       
    71 		delete iXmlHandler;
       
    72 	
       
    73 	if(iBuffer)
       
    74 		delete iBuffer;
       
    75 }
       
    76 
       
    77 /*
       
    78  * Default constructor.
       
    79  */	
       
    80 CLiwXmlHandler::CLiwXmlHandler()
       
    81 {
       
    82 	
       
    83 }
       
    84 
       
    85 /*
       
    86  * Instantiates the SAX parser instance and registers itself
       
    87  * as the callback to handle SAX events.
       
    88  */
       
    89 void CLiwXmlHandler::ConstructL()
       
    90 {
       
    91 	TInt parserFeature = EErrorOnUnrecognisedTags | EXmlVersion_1_1;
       
    92 	iXmlHandler = CSenXmlReader::NewL(parserFeature);
       
    93 	iXmlHandler->SetContentHandler(*this);
       
    94 }
       
    95 
       
    96 /*
       
    97  * Parses the inline XML content passed in the buffer. The parser
       
    98  * is kick started to recevie SAX events. This method also accepts the
       
    99  * reference to \c CLiwServiceData to store the parsed metadata name-value
       
   100  * pairs.
       
   101  */
       
   102 TInt CLiwXmlHandler::LoadServiceData(const TDesC8& aXmlBuffer,CLiwServiceData* aServiceData)
       
   103 {
       
   104 	iServiceData = aServiceData;
       
   105 	
       
   106 	TRAPD(err,iXmlHandler->ParseL(aXmlBuffer));
       
   107 	
       
   108 	TInt retVal = ESrvDataLoadFailed;
       
   109 		
       
   110 	if(!err)
       
   111 	{
       
   112 		retVal = ESrvDataLoadSuccess;
       
   113 	}
       
   114 	
       
   115 	return retVal;
       
   116 }
       
   117 
       
   118 /*
       
   119  * Parses the XML content present in an external file. The parser
       
   120  * is kick started to recevie SAX events. This method also accepts the
       
   121  * reference to \c CLiwServiceData to store the parsed metadata name-value
       
   122  * pairs.
       
   123  */
       
   124  #include <bautils.h>
       
   125 TInt CLiwXmlHandler::LoadServiceData(const TDesC& aFileToParse,CLiwServiceData* aServiceData)
       
   126 {
       
   127 	TInt retVal = ESrvDataLoadFailed;
       
   128 	
       
   129 	iServiceData = aServiceData;
       
   130 	
       
   131 	RFs fsSession;
       
   132     User::LeaveIfError( fsSession.Connect() );
       
   133     
       
   134     TFileName resFile;
       
   135     resFile.Append(aFileToParse);
       
   136     
       
   137     if(!BaflUtils::FileExists(fsSession,resFile))
       
   138     {
       
   139     	fsSession.Close();
       
   140     	return ESrvDataFileNotFnd;
       
   141     }
       
   142     	
       
   143     
       
   144 	TRAPD(err,iXmlHandler->ParseL(fsSession,resFile));
       
   145 	
       
   146 	fsSession.Close();
       
   147 	if(!err)
       
   148 	{
       
   149 		retVal = ESrvDataLoadSuccess;
       
   150 	}
       
   151 	
       
   152 	return retVal;	
       
   153 }
       
   154 
       
   155 /**
       
   156 * Receive notification of the beginning of a document.
       
   157 * @return KErrNone or some of the system-wide Symbian error codes.
       
   158 */		
       
   159 TInt CLiwXmlHandler::StartDocument()
       
   160 {
       
   161 	return KErrNone;
       
   162 }
       
   163 
       
   164 /**
       
   165 * Receive notification of the end of a document.
       
   166 * @return KErrNone or some of the system-wide Symbian error codes.
       
   167 */
       
   168 TInt CLiwXmlHandler::EndDocument()
       
   169 {
       
   170 	return KErrNone;
       
   171 }
       
   172 
       
   173 /**
       
   174 * The important parsed elements are <metadata/> and its sub-element
       
   175 * <capability/>.
       
   176 * The other sub-elements (other than <capability/>) are considered
       
   177 * as service provider specific metadata name-value pairs.
       
   178 *
       
   179 * @return KErrNone or some of the system-wide Symbian error codes.
       
   180 */
       
   181 TInt CLiwXmlHandler::StartElement(	const TDesC8& /*aURI*/, 
       
   182 									const TDesC8& aLocalName, 
       
   183 									const TDesC8& /*aName*/, 
       
   184 									const RAttributeArray& /* apAttrs */)
       
   185 {
       
   186 	if(iBuffer)
       
   187 	{
       
   188 		delete iBuffer;
       
   189 		iBuffer=NULL;
       
   190 	}
       
   191 		
       
   192 	startBuf = aLocalName;
       
   193 	return KErrNone;
       
   194 }
       
   195 
       
   196 
       
   197 /**
       
   198 * The end of the elements such as <metadata/> and its sub-element
       
   199 * <capability/> are processed. The flags set for the processing of
       
   200 * <metadata> and it sub-elements like <capability/> are toggled off.
       
   201 *
       
   202 * @return KErrNone or some of the system-wide Symbian error codes.
       
   203 */
       
   204 TInt CLiwXmlHandler::EndElement(	const TDesC8& /*aURI*/, 
       
   205 								const TDesC8& aLocalName, 
       
   206 								const TDesC8& /*aName*/)
       
   207 {
       
   208 	if(0!=aLocalName.CompareF(KMetaDataName) && 0==aLocalName.CompareF(startBuf))
       
   209 		iServiceData->AddMetaDataL(aLocalName,*iBuffer);
       
   210 	
       
   211 	delete iBuffer;
       
   212 	iBuffer = NULL;
       
   213 
       
   214 	return KErrNone;
       
   215 }
       
   216 
       
   217 /**
       
   218 * The metadata can be defined in the following XML format:
       
   219 *
       
   220 *
       
   221 * <metadata>
       
   222 *	<metadata_keyname1>metadata_keyvalue1</metadata_keyname1>
       
   223 *	<metadata_keyname2>metadata_keyvalue2</metadata_keyname2>
       
   224 * </metadata>
       
   225 *
       
   226 * A realistic example below
       
   227 *
       
   228 *
       
   229 * <metadata>
       
   230 * 	<capability>CapabilityReadDeviceData</capability>
       
   231 * 	<capability>CapabilityWriteDeviceData</capability>
       
   232 * 	<capability>CapabilityLocation</capability>
       
   233 * </metadata>
       
   234 *
       
   235 * This function obtains the character data defined within the
       
   236 * child elements of <metadata>. The child element name under <metadata>
       
   237 * is taken as metadata key and the character data under it as metadata
       
   238 * value and added to the internal metadata key-value pair entries.
       
   239 *
       
   240 * There could be multiple metadata keys having different values as
       
   241 * shown above
       
   242 *
       
   243 * Right now it supports only capability metadata information
       
   244 *
       
   245 */										
       
   246 TInt CLiwXmlHandler::Characters(const TDesC8& aBuf, 
       
   247 		   							   const TInt /* aStart */, 
       
   248 									   const TInt /* aLength */)
       
   249 {
       
   250 	if(iBuffer)
       
   251 	{		
       
   252 		delete iBuffer;
       
   253 		iBuffer=NULL;
       
   254 	}
       
   255 		
       
   256 	
       
   257 	iBuffer = aBuf.Alloc();
       
   258 	
       
   259 	return KErrNone;
       
   260 }
       
   261 
       
   262 /**
       
   263 * In case of parsing errors due to non-well formed XML content,
       
   264 * file not being present etc are handled here. In case of XML content
       
   265 * error, the metadata stored so far will be cleaned up.
       
   266 *
       
   267 * @return KErrNone or some of the system-wide Symbian error codes.
       
   268 */
       
   269 
       
   270 TInt CLiwXmlHandler::Error(TInt /*aErrorCode*/)
       
   271 {
       
   272 	return ESrvDataLoadFailed;
       
   273 }