secureswitools/swisistools/source/interpretsislib/xmlgenerator.cpp
changeset 0 ba25891c3a9e
child 25 98b66e4fb0be
child 65 7333d7932ef7
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     1 /*
       
     2 * Copyright (c) 2008-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 * CXmlGenerator - Used to generate the xml file containing registry information.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 /**
       
    21  @file 
       
    22  @released
       
    23  @internalTechnology
       
    24 */
       
    25 
       
    26 #include "xmlgenerator.h"
       
    27 #include "../common/exception.h"
       
    28 #include "is_utils.h"
       
    29 
       
    30 #include <xercesc/dom/DOM.hpp>
       
    31 #include <xercesc/util/XMLString.hpp>
       
    32 #include <xercesc/util/PlatformUtils.hpp>
       
    33 #include <xercesc/framework/StdOutFormatTarget.hpp>
       
    34 #include <xercesc/framework/LocalFileFormatTarget.hpp>
       
    35 #include <xercesc/framework/XMLFormatter.hpp>
       
    36 #include <xercesc/framework/MemBufFormatTarget.hpp>
       
    37 #include <xercesc/util/XercesVersion.hpp>
       
    38 
       
    39 #include <exception>
       
    40 #include <string>
       
    41 #include <vector>
       
    42 
       
    43 #include <memory>
       
    44 #include <cstdio>
       
    45 #include <stdexcept>
       
    46 
       
    47 #include "toolsconf.h"
       
    48 
       
    49 XERCES_CPP_NAMESPACE_USE
       
    50 
       
    51 // these function pointers is used to call appropriate release methods of 
       
    52 // which are member function of a class. 
       
    53 typedef void (XERCES_CPP_NAMESPACE::DOMDocument::*releaseDOMDoc) ();
       
    54 typedef void (XERCES_CPP_NAMESPACE::DOMWriter::*releaseDOMWriter) ();
       
    55 typedef void (*xmlPlatform) ();
       
    56 typedef void (*releaseXmlChPtr) (XMLCh** buf);
       
    57 
       
    58 // constants used for writing to the xml file
       
    59 std::wstring	PreProvisionInformation(L"PreProvisionInformation");
       
    60 std::wstring	SoftwareTypeName(L"SoftwareTypeName");
       
    61 
       
    62 
       
    63 CXmlGenerator::CXmlGenerator()
       
    64 	{}
       
    65 
       
    66 CXmlGenerator::~CXmlGenerator()
       
    67 	{}
       
    68 
       
    69 
       
    70 void CXmlGenerator::WritePreProvisionDetails(const std::wstring aXmlFileName, const XmlDetails::TScrPreProvisionDetail& aPreProvisionDetail)
       
    71 	{
       
    72 	try 
       
    73 		{
       
    74 		XMLPlatformUtils::Initialize();
       
    75 		static_fn_auto_ptr<xmlPlatform> xmlPlatformTerm(&XMLPlatformUtils::Terminate);
       
    76 
       
    77 		DOMImplementation* domImpl = DOMImplementation::getImplementation() ;
       
    78 
       
    79 		mem_fn_auto_ptr<releaseDOMWriter, DOMWriter*> domWriter( &XERCES_CPP_NAMESPACE::DOMWriter::release, ((DOMImplementationLS*)domImpl)->createDOMWriter() );
       
    80 				
       
    81 		SetWriterFeatures(domWriter.get());
       
    82 		
       
    83 		std::auto_ptr < SchemaDomErrorHandler > errHandler(new SchemaDomErrorHandler());
       
    84 		domWriter->setErrorHandler(errHandler.get());
       
    85 
       
    86 		std::auto_ptr < XMLFormatTarget > outputFile( new LocalFileFormatTarget( aXmlFileName.c_str() ) );	
       
    87 		
       
    88 		const char* epocRoot = getenv("EPOCROOT");
       
    89 		if(NULL == epocRoot)
       
    90 			{
       
    91 			throw std::runtime_error("EPOCROOT environment variable not specified.");
       
    92 			}
       
    93 		
       
    94 		std::string epocRootStr(epocRoot); 
       
    95 
       
    96 		std::string dtdFilePath = epocRootStr + "epoc32\\tools\\preprovision.dtd";
       
    97 		
       
    98 		fn_auto_ptr<releaseXmlChPtr, XMLCh> dtdPath( &XMLString::release, XMLString::transcode(dtdFilePath.c_str()) );
       
    99 		DOMDocumentType* documentType = domImpl->createDocumentType(L"PreProvisionInformation",NULL, dtdPath.get());
       
   100 		
       
   101 		
       
   102 		mem_fn_auto_ptr< releaseDOMDoc, DOMDocument* > domDocument( &XERCES_CPP_NAMESPACE::DOMDocument::release, domImpl->createDocument(	0, PreProvisionInformation.c_str(), documentType) );
       
   103 
       
   104 		DOMElement* rootElement = domDocument->getDocumentElement();
       
   105 		
       
   106 		// SoftwareTypeName
       
   107 		AddChildElement(rootElement, domDocument.get(), SoftwareTypeName.c_str(), aPreProvisionDetail.iSoftwareTypeName.c_str() );
       
   108 
       
   109 		std::vector<XmlDetails::TScrPreProvisionDetail::TComponent>::const_iterator compIter;
       
   110 		for(compIter = aPreProvisionDetail.iComponents.begin(); compIter != aPreProvisionDetail.iComponents.end() ; ++compIter)
       
   111 			{
       
   112 			DOMElement*  component = domDocument->createElement(L"Component");
       
   113 			rootElement->appendChild(component);
       
   114 
       
   115 			WriteComponent(component,domDocument.get(), *compIter);
       
   116 			}
       
   117 		
       
   118 		// do the serialization through DOMWriter::writeNode();
       
   119 		domWriter->writeNode(outputFile.get(), *domDocument.get());
       
   120 		
       
   121 		}
       
   122         catch (const XMLException& toCatch) 
       
   123 			{
       
   124             char* message = XMLString::transcode(toCatch.getMessage());
       
   125             XMLString::release(&message);
       
   126 			}
       
   127         catch (const DOMException& toCatch) 
       
   128 			{
       
   129             char* message = XMLString::transcode(toCatch.msg);
       
   130             XMLString::release(&message);
       
   131 			}
       
   132         catch (...) 
       
   133 			{
       
   134 			} 
       
   135 	}
       
   136 
       
   137 void CXmlGenerator::SetWriterFeatures(DOMWriter* aDomWriter)
       
   138 	{
       
   139 	if (aDomWriter->canSetFeature(XMLUni::fgDOMWRTEntities, true))
       
   140 		aDomWriter->setFeature(XMLUni::fgDOMWRTEntities, true);
       
   141 
       
   142 	if (aDomWriter->canSetFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true))
       
   143 		 aDomWriter->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true);
       
   144 	
       
   145 	if (aDomWriter->canSetFeature(XMLUni::fgDOMWRTNormalizeCharacters, false))
       
   146 		aDomWriter->setFeature(XMLUni::fgDOMWRTNormalizeCharacters, false);
       
   147 
       
   148 	if (aDomWriter->canSetFeature(XMLUni::fgDOMWRTSplitCdataSections, true))
       
   149 		 aDomWriter->setFeature(XMLUni::fgDOMWRTSplitCdataSections, true);
       
   150 	
       
   151 	if (aDomWriter->canSetFeature(XMLUni::fgDOMWRTValidation, false))
       
   152 		aDomWriter->setFeature(XMLUni::fgDOMWRTValidation, false);
       
   153 
       
   154 	if (aDomWriter->canSetFeature(XMLUni::fgDOMWRTWhitespaceInElementContent, true))
       
   155 		 aDomWriter->setFeature(XMLUni::fgDOMWRTWhitespaceInElementContent, true);
       
   156 	
       
   157 	if (aDomWriter->canSetFeature(XMLUni::fgDOMWRTBOM, true))
       
   158 		aDomWriter->setFeature(XMLUni::fgDOMWRTBOM, true);
       
   159 
       
   160 	if (aDomWriter->canSetFeature(XMLUni::fgDOMXMLDeclaration, true))
       
   161 		 aDomWriter->setFeature(XMLUni::fgDOMXMLDeclaration, true);
       
   162 	
       
   163 	aDomWriter->setEncoding(L"UTF-16");
       
   164 	}
       
   165 
       
   166 void CXmlGenerator::WriteComponent( DOMElement* aRootElement, DOMDocument* aDocument, const XmlDetails::TScrPreProvisionDetail::TComponent& aComponent)
       
   167 	{
       
   168 	std::wstring isRemovable = Utils::IntegerToWideString(aComponent.iComponentDetails.iIsRemovable);
       
   169 	AddChildElement(aRootElement, aDocument, L"Removable", isRemovable.c_str() );
       
   170 	
       
   171 	std::wstring size = Utils::Int64ToWideString(aComponent.iComponentDetails.iSize);
       
   172 	AddChildElement(aRootElement, aDocument, L"Size", size.c_str() );
       
   173 	
       
   174 	std::wstring scomoState = Utils::IntegerToWideString(aComponent.iComponentDetails.iScomoState);
       
   175 	AddChildElement(aRootElement, aDocument, L"ScomoState", scomoState.c_str() );
       
   176 
       
   177 	AddChildElement(aRootElement, aDocument, L"GlobalId", aComponent.iComponentDetails.iGlobalId.c_str() );
       
   178 
       
   179 	WriteComponentVersion(aRootElement, aDocument, aComponent.iComponentDetails.iVersion);
       
   180 	
       
   181 	std::wstring isOriginVerified = Utils::IntegerToWideString(aComponent.iComponentDetails.iOriginVerified);
       
   182 	AddChildElement(aRootElement, aDocument, L"OriginVerified", isOriginVerified.c_str() );
       
   183 
       
   184 	std::wstring isHidden = Utils::IntegerToWideString(aComponent.iComponentDetails.iIsHidden);
       
   185 	AddChildElement(aRootElement, aDocument, L"Hidden", isHidden.c_str() );
       
   186 
       
   187 	WriteComponentLocalizables(aRootElement, aDocument, aComponent.iComponentLocalizables);
       
   188 
       
   189 	WriteComponentProperties(aRootElement, aDocument, aComponent.iComponentProperties);
       
   190 
       
   191 	WriteComponentFiles(aRootElement, aDocument, aComponent.iComponentFiles);
       
   192 
       
   193 	WriteComponentDependencies(aRootElement, aDocument, aComponent.iComponentDependency);
       
   194 	}
       
   195 
       
   196 void CXmlGenerator::WriteComponentVersion
       
   197 				(
       
   198 				DOMElement* aRootElement, 
       
   199 				DOMDocument* aDocument, 
       
   200 				XmlDetails::TScrPreProvisionDetail::TComponentDetails::TVersion aVersion
       
   201 				)
       
   202 	{
       
   203 	DOMElement*  version = aDocument->createElement(L"Version");
       
   204 	aRootElement->appendChild(version);
       
   205 	version->setAttribute( L"Major", aVersion.iMajor.c_str() );
       
   206 	version->setAttribute( L"Minor", aVersion.iMinor.c_str() );
       
   207 	version->setAttribute( L"Build", aVersion.iBuild.c_str() );
       
   208 	}
       
   209 
       
   210 void CXmlGenerator::WriteComponentLocalizables
       
   211 					(	
       
   212 						DOMElement* aRootElement, DOMDocument* aDocument, 
       
   213 						const std::vector<ComponentLocalizable>& aComponentLocalizable 
       
   214 					)
       
   215 	{
       
   216 	std::vector<ComponentLocalizable>::const_iterator compLocIter;
       
   217 	for( compLocIter = aComponentLocalizable.begin() ; compLocIter != aComponentLocalizable.end() ; ++compLocIter)
       
   218 		{
       
   219 		DOMElement* newRoot = AddTag(aRootElement, aDocument, L"ComponentLocalizable");
       
   220 		std::wstring locale = Utils::IntegerToWideString(compLocIter->iLocale);
       
   221 		AddChildElement(newRoot,aDocument, L"ComponentLocalizable_Locale", locale.c_str());
       
   222 		AddChildElement(newRoot,aDocument, L"ComponentLocalizable_Name", compLocIter->iName.c_str());
       
   223 		AddChildElement(newRoot,aDocument, L"ComponentLocalizable_Vendor", compLocIter->iVendor.c_str());
       
   224 		}
       
   225 	}
       
   226 
       
   227 void CXmlGenerator::WriteComponentProperties	
       
   228 					( 
       
   229 						DOMElement* aRootElement, DOMDocument* aDocument, 
       
   230 						const std::vector<ComponentProperty>& aComponentProperties 
       
   231 					)
       
   232 	{
       
   233 	std::vector<ComponentProperty>::const_iterator compPropIter;
       
   234 	for( compPropIter = aComponentProperties.begin() ; compPropIter != aComponentProperties.end() ; ++compPropIter)
       
   235 		{
       
   236 		
       
   237 		DOMElement* compPropRoot = AddTag(aRootElement, aDocument, L"ComponentProperty");
       
   238 		
       
   239 		std::wstring locale = Utils::IntegerToWideString(compPropIter->iLocale);
       
   240 		AddChildElement(compPropRoot,aDocument, L"ComponentProperty_Locale", locale.c_str());
       
   241 		
       
   242 		DOMElement* compPropValueRoot = AddTag(compPropRoot, aDocument, L"ComponentProperty_Value");
       
   243 		
       
   244 		std::wstring isBinary = Utils::IntegerToWideString(compPropIter->iIsStr8Bit);
       
   245 
       
   246 		if(compPropIter->iIsIntValue)
       
   247 			{
       
   248 			AddChildElement(compPropValueRoot,aDocument, L"ComponentProperty_IntValue", compPropIter->iValue.c_str());
       
   249 			}
       
   250 		else
       
   251 			{
       
   252 			AddChildElement(compPropValueRoot,aDocument, L"ComponentProperty_StrValue", compPropIter->iValue.c_str());
       
   253 			}
       
   254 		
       
   255 		
       
   256 		AddChildElement(compPropRoot,aDocument, L"ComponentProperty_IsBinary", isBinary.c_str());
       
   257 
       
   258 		compPropRoot->setAttribute(L"Name", compPropIter->iName.c_str());
       
   259 		
       
   260 		}
       
   261 	}
       
   262 
       
   263 void CXmlGenerator::WriteComponentFiles	
       
   264 					( 
       
   265 						DOMElement* aRootElement, DOMDocument* aDocument, 
       
   266 						const std::vector<ComponentFile>& aComponentFiles 
       
   267 					)
       
   268 	{
       
   269 	std::vector<ComponentFile>::const_iterator compFileIter;
       
   270 	for( compFileIter = aComponentFiles.begin() ; compFileIter != aComponentFiles.end() ; ++compFileIter)
       
   271 		{
       
   272 		DOMElement* compFileRoot = AddTag(aRootElement, aDocument, L"ComponentFile");
       
   273 
       
   274 		WriteFileProperties(compFileRoot, aDocument, compFileIter->iFileProperties);
       
   275 		
       
   276 		compFileRoot->setAttribute(L"Location", compFileIter->iLocation.c_str());
       
   277 		}
       
   278 	}
       
   279 
       
   280 void CXmlGenerator::WriteFileProperties	
       
   281 					( 
       
   282 						DOMElement* aRootElement, DOMDocument* aDocument, 
       
   283 						const std::vector<FileProperty>& aFileProperties 
       
   284 					)
       
   285 	{
       
   286 	std::vector<FileProperty>::const_iterator filePropIter;
       
   287 	for( filePropIter = aFileProperties.begin() ; filePropIter != aFileProperties.end() ; ++filePropIter)
       
   288 		{
       
   289 		DOMElement* filePropRoot = AddTag(aRootElement, aDocument, L"FileProperty");
       
   290 		DOMElement* filePropValueRoot = AddTag(filePropRoot, aDocument, L"FileProperty_Value");
       
   291 
       
   292 		if(filePropIter->iIsIntValue)
       
   293 			{
       
   294 			AddChildElement(filePropValueRoot, aDocument, L"FileProperty_IntValue", filePropIter->iValue.c_str());
       
   295 			}
       
   296 		else
       
   297 			{
       
   298 			AddChildElement(filePropValueRoot, aDocument, L"FileProperty_StrValue", filePropIter->iValue.c_str());
       
   299 			}
       
   300 		filePropRoot->setAttribute(L"Name", filePropIter->iName.c_str());
       
   301 		}
       
   302 	}
       
   303 
       
   304 
       
   305 void CXmlGenerator::WriteComponentDependencies	
       
   306 					( 
       
   307 						DOMElement* aRootElement, 
       
   308 						DOMDocument* aDocument, 
       
   309 						const ComponentDependency& aComponentDependency 
       
   310 					)
       
   311 	{
       
   312 	if( aComponentDependency.iDependentId.empty())
       
   313 		return;
       
   314 
       
   315 	std::vector<ComponentDependencyDetail> componentDependencyList = aComponentDependency.iComponentDependencyList;
       
   316 	std::vector<ComponentDependencyDetail>::const_iterator compDepIter;
       
   317 
       
   318 	DOMElement* compDepRoot = AddTag(aRootElement, aDocument, L"ComponentDependency");
       
   319 
       
   320 	for( compDepIter = componentDependencyList.begin() ; compDepIter != componentDependencyList.end() ; ++compDepIter)
       
   321 		{
       
   322 		DOMElement* compDepListRoot = AddTag( compDepRoot, aDocument, L"DependencyList" );
       
   323 		AddChildElement( compDepListRoot, aDocument, L"SupplierId", compDepIter->iSupplierId.c_str() );
       
   324 		AddChildElement( compDepListRoot, aDocument, L"FromVersion", compDepIter->iFromVersion.c_str() );
       
   325 		AddChildElement( compDepListRoot, aDocument, L"ToVersion", compDepIter->iToVersion.c_str() );
       
   326 		}
       
   327 	compDepRoot->setAttribute(L"DependentId", aComponentDependency.iDependentId.c_str());
       
   328 	}
       
   329 
       
   330 
       
   331 void CXmlGenerator::AddChildElement( DOMElement* aRootElement, DOMDocument* aDocument, const wchar_t* aElementName, const wchar_t* aTextValue )
       
   332 	{
       
   333 	DOMElement*  element = aDocument->createElement(aElementName);
       
   334 	aRootElement->appendChild(element);
       
   335 
       
   336 	DOMText* textValue = aDocument->createTextNode(aTextValue);
       
   337 	element->appendChild(textValue);
       
   338 	}
       
   339 
       
   340 DOMElement* CXmlGenerator::AddTag( DOMElement* aRootElement, DOMDocument* aDocument, const wchar_t* aTagName)
       
   341 	{
       
   342 	DOMElement*  tagName = aDocument->createElement(aTagName);
       
   343 	aRootElement->appendChild(tagName);
       
   344 
       
   345 	return tagName;
       
   346 	}
       
   347 
       
   348 
       
   349 /**
       
   350  * Handles all warnings received while xml parsing. 
       
   351  */
       
   352 bool SchemaDomErrorHandler::handleError(const DOMError& domError)
       
   353 	{
       
   354 	const XMLCh* message = domError.getMessage();
       
   355 	return false;
       
   356 	}