apicompatanamdw/compatanalysercmd/libraryanalyser/inc/xmlsaxparser.hpp
author noe\swadi
Mon, 03 May 2010 11:57:22 +0530
changeset 4 60ac11a5544a
parent 0 638b9c697799
permissions -rw-r--r--
Added tag Foundation test suite for Symbian v3 for changeset ebe3f8f03b59
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
     1
/*
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
     2
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
     3
* All rights reserved.
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
     4
* This component and the accompanying materials are made available
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
     5
* under the terms of "Eclipse Public License v1.0"
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
     6
* which accompanies this distribution, and is available
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
     8
*
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
     9
* Initial Contributors:
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    11
*
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    12
* Contributors:
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    13
*
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    14
* Description: 
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    15
*
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    16
*/
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    17
// XML Parsing includes
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    18
#include <xercesc/util/PlatformUtils.hpp>
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    19
#include <xercesc/parsers/SAXParser.hpp>
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    20
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    21
#include <iostream>
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    22
#include <string>
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    23
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    24
#if !defined XMLSAXPARSER
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    25
	#define  XMLSAXPARSER
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    26
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    27
#include "XMLParserInterface.h"
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    28
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    29
class XMLNode;
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    30
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    31
// T - Document Handler
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    32
// U - ErrorHandler
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    33
template<typename T, typename U>
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    34
class XMLSAXParser : public XMLParserInterface
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    35
{
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    36
	public:
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    37
    XMLSAXParser();
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    38
    virtual ~XMLSAXParser();
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    39
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    40
    virtual bool parse();
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    41
    virtual bool parse(const std::string&);
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    42
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    43
    T& getDocumentHandler() { return docHandler; }
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    44
	XMLNode& getRootElement() { return docHandler.getRootElement(); }
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    45
	
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    46
	virtual void doValidation(bool v = true) { validate = v; }
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    47
	virtual void doSchema(bool s = true) { schema = s; }
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    48
	virtual void fullSchemaChecking(bool s = true) { schemaChecking = s; }
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    49
	virtual void namespaces(bool n = true) { names = n; }
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    50
	
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    51
 private:
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    52
	SAXParser* 	parser;
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    53
	std::string	xmlFile;
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    54
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    55
	T		docHandler;
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    56
	U		errorHandler;
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    57
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    58
	bool validate;
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    59
	bool schema;
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    60
	bool schemaChecking;
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    61
	bool names;
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    62
	
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    63
	bool getValidation() { return validate; }
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    64
	bool getSchema() { return schema; }
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    65
	bool getSchemaChecking() { return schemaChecking; }
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    66
	bool getNamespaces() { return names; }
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    67
	
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    68
	void init();
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    69
	void deinit();
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    70
};
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    71
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    72
template<typename T, typename U>
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    73
XMLSAXParser<T, U>::XMLSAXParser()
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    74
{
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    75
	parser = NULL;
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    76
	this->init();
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    77
}
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    78
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    79
template<typename T, typename U>
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    80
XMLSAXParser<T, U>::~XMLSAXParser()
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    81
{
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    82
	this->deinit();
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    83
}
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    84
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    85
template<typename T, typename U>
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    86
void XMLSAXParser<T, U>::init()
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    87
{
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    88
	this->doValidation();
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    89
	this->doSchema();
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    90
	this->fullSchemaChecking();
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    91
	this->namespaces();
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    92
	
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    93
	try
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    94
	{
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    95
		XMLPlatformUtils::Initialize();
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    96
	}
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    97
	catch (const XMLException& toCatch)
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    98
	{
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
    99
		cerr << "Error during initialization! Message:\n"
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   100
			 << XMLString::transcode(toCatch.getMessage()) << endl;
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   101
	}
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   102
}
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   103
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   104
template<typename T, typename U>
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   105
void XMLSAXParser<T, U>::deinit()
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   106
{
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   107
	//  Delete the parser
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   108
	if(parser)
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   109
		delete parser;
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   110
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   111
	// And call the termination method
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   112
	XMLPlatformUtils::Terminate();
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   113
}
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   114
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   115
template<typename T, typename U>
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   116
bool XMLSAXParser<T, U>::parse()
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   117
{
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   118
	return this->parse(xmlFile);
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   119
}
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   120
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   121
template<typename T, typename U>
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   122
bool XMLSAXParser<T, U>::parse(const std::string& file)
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   123
{
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   124
	xmlFile = file;
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   125
	bool errorOccurred = false;
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   126
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   127
	//  Create a SAX parser object. Then, according to what we were told on
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   128
	//  the command line, set it to validate or not.
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   129
	parser = new SAXParser;
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   130
	if(!parser)
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   131
		return false;
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   132
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   133
	parser->setDoValidation(this->getValidation());
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   134
	parser->setDoNamespaces(this->getNamespaces());
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   135
	parser->setDoSchema(this->getSchema());
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   136
	parser->setValidationSchemaFullChecking(this->getSchemaChecking());
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   137
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   138
	parser->setDocumentHandler(&docHandler);
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   139
	parser->setErrorHandler(&errorHandler);
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   140
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   141
	try
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   142
	{
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   143
		parser->parse(file.c_str());
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   144
	}
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   145
	catch (const XMLException& e)
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   146
	{
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   147
		cerr << "\nError during parsing: '" << file << "'\n"
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   148
			<< "Exception message is:  \n"
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   149
			<< XMLString::transcode(e.getMessage()) << "\n" << endl;
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   150
		errorOccurred = true;
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   151
	}
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   152
	catch (...)
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   153
	{
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   154
		cerr << "\nUnexpected exception during parsing: '" << file << "'\n";
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   155
		errorOccurred = true;
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   156
	}
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   157
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   158
	return (!errorOccurred);
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   159
}
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   160
638b9c697799 1. Licenses updated to EPL.
noe\swadi
parents:
diff changeset
   161
#endif // XMLSAXPARSER