imgtools/imgcheck/src/xmlwriter.cpp
changeset 0 044383f39525
child 590 360bd6b35136
equal deleted inserted replaced
-1:000000000000 0:044383f39525
       
     1 /*
       
     2 * Copyright (c) 2007-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 * Writes XML elements into output stream to generate a XML file
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 /**
       
    21  @file
       
    22  @internalComponent
       
    23  @released
       
    24 */
       
    25 
       
    26 #include "xmlwriter.h"
       
    27 
       
    28 #ifndef __LINUX__		
       
    29 	#include "/epoc32/gcc_mingw/include/windows.h"	
       
    30 #endif //__LINUX__
       
    31 
       
    32 /**
       
    33 Constructor: XmlWriter class
       
    34 
       
    35 @internalComponent
       
    36 @released
       
    37 
       
    38 @param aXmlfileName - Reference to xml filename.
       
    39 @param iInputCommnd - Reference to the input options
       
    40 */
       
    41 XmlWriter::XmlWriter(const String& aXmlFileName, const String& aInputCommand)
       
    42 : iXmlFileName(aXmlFileName), iXmlBufPtr(0) , iXmlTextWriter(0) , iInputCommnd(aInputCommand), iRptType(KXml)
       
    43 {
       
    44 }
       
    45 
       
    46 
       
    47 /**
       
    48 Destructor : XmlWriter class
       
    49 Closes the Xml output file.
       
    50 
       
    51 @internalComponent
       
    52 @released
       
    53 */
       
    54 XmlWriter::~XmlWriter(void)
       
    55 {
       
    56 	if(iXmlFile.is_open())
       
    57 	{
       
    58 		iXmlFile.close();
       
    59 	}
       
    60 }
       
    61 
       
    62 
       
    63 /**
       
    64 Create the XSL file, OverWrites if exist.
       
    65 Get the Xsl Source Path.
       
    66 
       
    67 @internalComponent
       
    68 @released
       
    69 
       
    70 @returns : 'True' for Success or 'False'.
       
    71 */		   
       
    72 int XmlWriter::CreateXslFile(void)
       
    73 {
       
    74 	// Validate the user entered xml path.
       
    75 	char* xslFileName = (char*)iXmlFileName.c_str();
       
    76 	String xslSourcePath;
       
    77 
       
    78 	while(*xslFileName)
       
    79 	{
       
    80 		if(*xslFileName++ == '\\')
       
    81 			*(--xslFileName) = '/';
       
    82 	}
       
    83 	String xslDestPath(iXmlFileName);
       
    84 	unsigned int position = xslDestPath.rfind('/');
       
    85 	if(position != String::npos)
       
    86 	{
       
    87 		xslDestPath.erase(position+1);
       
    88 		xslDestPath.append(KXslFileName);
       
    89 	}
       
    90 	else
       
    91 	{
       
    92 		xslDestPath.erase();
       
    93 		xslDestPath.assign(KXslFileName);
       
    94 	}
       
    95 
       
    96 	if(!(GetXslSourcePath(xslSourcePath)))
       
    97 	{
       
    98 		return false;
       
    99 	}
       
   100 	xslSourcePath.append(KXslFileName.c_str());
       
   101 
       
   102 	ifstream xslSourceHandle;
       
   103 	xslSourceHandle.open(xslSourcePath.c_str(), Ios::binary);
       
   104 	if(!xslSourceHandle)
       
   105 	{
       
   106 		return false;
       
   107 	}
       
   108 	xslSourceHandle.seekg(0, Ios::end);		
       
   109 	int fileSize = xslSourceHandle.tellg();
       
   110 	xslSourceHandle.seekg(0, Ios::beg);
       
   111 	char* filetocopy = new char[fileSize];
       
   112 	if (!filetocopy)
       
   113 	{
       
   114 		throw ExceptionReporter(NOMEMORY, __FILE__, __LINE__);
       
   115 	}
       
   116 	xslSourceHandle.read(filetocopy,fileSize);
       
   117 	xslSourceHandle.close();
       
   118 
       
   119 	ofstream xslDestHandle(xslDestPath.c_str(), Ios::binary | Ios::out);
       
   120 	if(!xslDestHandle)
       
   121 	{
       
   122 		delete [] filetocopy;
       
   123 		return false;
       
   124 	}
       
   125 	xslDestHandle.write(filetocopy,fileSize);			
       
   126 	xslDestHandle.close();
       
   127 	delete [] filetocopy;
       
   128 	return true;
       
   129 }
       
   130 	
       
   131 
       
   132 /**
       
   133 Get Xsl file path (imagecheck.xsl).
       
   134 
       
   135 @internalComponent
       
   136 @released
       
   137 
       
   138 @param aExePath - Reference to Xsl file Path.
       
   139 @return - 'true' for success.
       
   140 */
       
   141 bool XmlWriter::GetXslSourcePath(String& aExePath)
       
   142 {
       
   143 #ifdef __LINUX__
       
   144 	aExePath.assign("/");
       
   145 	return true;
       
   146 #else
       
   147 
       
   148 	char* size = new char[KXmlGenBuffer];
       
   149 	if (!size)
       
   150 	{
       
   151 		throw ExceptionReporter(NOMEMORY, __FILE__, __LINE__);
       
   152 	}
       
   153 	if(!(GetModuleFileName(NULL, size, KXmlGenBuffer)))
       
   154 	{
       
   155 		delete [] size;
       
   156 		return false;
       
   157 	}
       
   158 	String path(size);
       
   159 	delete [] size;
       
   160 	size_t last = path.rfind('\\');
       
   161 	if(last != String::npos)
       
   162 	{
       
   163 		aExePath = path.substr(0, last+1);
       
   164 		return true;
       
   165 	}
       
   166 #endif
       
   167 	return true ; // to avoid warning
       
   168 }
       
   169 
       
   170 
       
   171 /**
       
   172 Writes report header to xml file..
       
   173 
       
   174 Opens the xml file for output.
       
   175 Allocate the memory for xml report.
       
   176 Write the Dtd/Xslt info.
       
   177 Write the root element.
       
   178 
       
   179 @internalComponent
       
   180 @released
       
   181 */
       
   182 void XmlWriter::StartReport(void)
       
   183 {
       
   184 	iXmlFile.open(iXmlFileName.c_str());
       
   185 
       
   186 	if(!(iXmlFile.is_open()))
       
   187 	{
       
   188 		throw ExceptionReporter(FILEOPENFAIL, __FILE__, __LINE__, (char*)iXmlFileName.c_str());
       
   189 	}
       
   190 
       
   191 	if(!(CreateXslFile()))
       
   192 	{
       
   193 		ExceptionReporter(XSLCREATIONFAILED, __FILE__, __LINE__, (char*)KXslFileName.c_str()).Report();
       
   194 	}
       
   195 
       
   196 	iXmlBufPtr = xmlBufferCreate();
       
   197 	// xml writer pointer to buffer ( with no compression )
       
   198 	iXmlTextWriter = xmlNewTextWriterMemory(iXmlBufPtr,0);
       
   199 
       
   200 	if (!iXmlBufPtr || !iXmlTextWriter)
       
   201 	{
       
   202 		throw ExceptionReporter(NOMEMORY,__FILE__,__LINE__);
       
   203 	}
       
   204 
       
   205 	xmlTextWriterStartDocument(iXmlTextWriter, KXmlVersion.c_str(), KXmlEncoding.c_str(), KNull);
       
   206 	xmlTextWriterWriteRaw(iXmlTextWriter,(unsigned char*)KDtdXslInfo.c_str());
       
   207 	xmlTextWriterStartElement(iXmlTextWriter, BAD_CAST KXmlRootElement.c_str());
       
   208 	xmlTextWriterStartElement(iXmlTextWriter,BAD_CAST KXmlcomment.c_str());
       
   209 	xmlTextWriterWriteAttribute(iXmlTextWriter, BAD_CAST KXmlcomment.c_str(), BAD_CAST iInputCommnd.c_str());
       
   210 }
       
   211 
       
   212 
       
   213 /**
       
   214 Writes the report footer to Xml file..
       
   215 
       
   216 Write the xml end doc info..
       
   217 Release the writer pointer from the buffer.
       
   218 Writes the buffer content to xml file.
       
   219 Frees the xml buffer.
       
   220 
       
   221 @internalComponent
       
   222 @released
       
   223 */
       
   224 void XmlWriter::EndReport(void)
       
   225 {
       
   226 	xmlTextWriterEndElement(iXmlTextWriter);
       
   227 	xmlTextWriterEndElement(iXmlTextWriter);
       
   228 	xmlTextWriterEndDocument(iXmlTextWriter);
       
   229 	xmlFreeTextWriter(iXmlTextWriter);
       
   230 	
       
   231 	iXmlFile.clear(); 
       
   232 	iXmlFile.write((const char *)iXmlBufPtr->content,iXmlBufPtr->use);
       
   233 	 
       
   234 	if(iXmlFile.fail()){
       
   235 		xmlBufferFree(iXmlBufPtr);
       
   236 		throw ExceptionReporter(NODISKSPACE, (char*)iXmlFileName.c_str()); 
       
   237 	}
       
   238 	xmlBufferFree(iXmlBufPtr);
       
   239 }
       
   240 
       
   241 
       
   242 /**
       
   243 Writes the executable name element.
       
   244 
       
   245 @internalComponent
       
   246 @released
       
   247 
       
   248 @param aSerNo	  - Serial numebr of the executable specific to the image.
       
   249 @param aExeName	  - Reference to executable name.
       
   250 */
       
   251 void XmlWriter::StartExecutable(const unsigned int aSerNo, const String& aExeName)
       
   252 {
       
   253 	xmlTextWriterStartElement(iXmlTextWriter, BAD_CAST KXmlExeName.c_str());
       
   254 	xmlTextWriterWriteFormatAttribute(iXmlTextWriter, BAD_CAST KXmlExeAtt1.c_str(),
       
   255 									"%d",aSerNo);
       
   256 	xmlTextWriterWriteAttribute(iXmlTextWriter, BAD_CAST KXmlExeAtt2.c_str(),
       
   257 									BAD_CAST aExeName.c_str());
       
   258 }
       
   259 
       
   260 
       
   261 /**
       
   262 Writes the executable end element
       
   263 
       
   264 @internalComponent
       
   265 @released
       
   266 */
       
   267 void XmlWriter::EndExecutable(void)
       
   268 {
       
   269 	xmlTextWriterEndElement(iXmlTextWriter);
       
   270 }
       
   271 
       
   272 
       
   273 /**
       
   274 Writes the note about unknown dependency.
       
   275 
       
   276 @internalComponent
       
   277 @released
       
   278 */
       
   279 void XmlWriter::WriteNote(void)
       
   280 {
       
   281 	xmlTextWriterStartElement(iXmlTextWriter, BAD_CAST KNote.c_str());
       
   282 	xmlTextWriterWriteAttribute(iXmlTextWriter, BAD_CAST KXmlExeAtt2.c_str(), BAD_CAST KUnknownDependency.c_str());
       
   283 	xmlTextWriterWriteAttribute(iXmlTextWriter, BAD_CAST KNote.c_str(), BAD_CAST KNoteMesg.c_str());
       
   284 	xmlTextWriterEndElement(iXmlTextWriter);
       
   285 }
       
   286 
       
   287 
       
   288 /**
       
   289 Writes the attribute, their values and the status.
       
   290 
       
   291 @internalComponent
       
   292 @released
       
   293 
       
   294 @param aOneSetExeAtt - Reference to the attributes, their value and status
       
   295 */
       
   296 void XmlWriter::WriteExeAttribute(ExeAttribute& aOneSetExeAtt)
       
   297 {
       
   298 	xmlTextWriterStartElement(iXmlTextWriter, BAD_CAST aOneSetExeAtt.iAttName.c_str());
       
   299  	if (!(strcmp(KDepName.c_str(),aOneSetExeAtt.iAttName.c_str())) 
       
   300 		|| !(strcmp(KXMLDbgFlag.c_str(),aOneSetExeAtt.iAttName.c_str())))
       
   301 	{
       
   302 		xmlTextWriterWriteAttribute(iXmlTextWriter, BAD_CAST KDepAtt1.c_str(),
       
   303 								BAD_CAST aOneSetExeAtt.iAttValue.c_str());
       
   304 	}
       
   305 	else
       
   306 	{
       
   307 		xmlTextWriterWriteFormatAttribute(iXmlTextWriter, BAD_CAST KAtt2.c_str(),
       
   308 			"0x%X",(Common::StringToInt(aOneSetExeAtt.iAttValue)));
       
   309 	}
       
   310 	
       
   311 	xmlTextWriterWriteAttribute(iXmlTextWriter, BAD_CAST KAtt1.c_str(),
       
   312 								BAD_CAST aOneSetExeAtt.iAttStatus.c_str());
       
   313 	xmlTextWriterEndElement(iXmlTextWriter);
       
   314 }
       
   315 
       
   316 
       
   317 /**
       
   318 Writes the image name element.
       
   319 
       
   320 @internalComponent
       
   321 @released
       
   322 
       
   323 @param aImageName - Reference to the image name
       
   324 */
       
   325 void XmlWriter::StartImage(const String& aImageName)
       
   326 {
       
   327 	xmlTextWriterStartElement(iXmlTextWriter, BAD_CAST KXmlImageName.c_str());
       
   328 	xmlTextWriterWriteAttribute(iXmlTextWriter, BAD_CAST KXmlImageAtt1.c_str() ,
       
   329 								BAD_CAST aImageName.c_str());
       
   330 }
       
   331 
       
   332 /**
       
   333 Writes the image end element
       
   334 
       
   335 @internalComponent
       
   336 @released
       
   337 */
       
   338 void XmlWriter::EndImage(void)
       
   339 {
       
   340 	xmlTextWriterEndElement(iXmlTextWriter);
       
   341 }
       
   342 
       
   343 
       
   344 /**
       
   345 Returns the report type.
       
   346 
       
   347 @internalComponent
       
   348 @released
       
   349 */
       
   350 const String& XmlWriter::ReportType(void)
       
   351 {
       
   352 	return iRptType;
       
   353 }