apicompatanamdw/compatanalysercmd/libraryanalyser/inc/xmlsaxparser.hpp
changeset 0 638b9c697799
equal deleted inserted replaced
-1:000000000000 0:638b9c697799
       
     1 /*
       
     2 * Copyright (c) 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 "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 *
       
    16 */
       
    17 // XML Parsing includes
       
    18 #include <xercesc/util/PlatformUtils.hpp>
       
    19 #include <xercesc/parsers/SAXParser.hpp>
       
    20 
       
    21 #include <iostream>
       
    22 #include <string>
       
    23 
       
    24 #if !defined XMLSAXPARSER
       
    25 	#define  XMLSAXPARSER
       
    26 
       
    27 #include "XMLParserInterface.h"
       
    28 
       
    29 class XMLNode;
       
    30 
       
    31 // T - Document Handler
       
    32 // U - ErrorHandler
       
    33 template<typename T, typename U>
       
    34 class XMLSAXParser : public XMLParserInterface
       
    35 {
       
    36 	public:
       
    37     XMLSAXParser();
       
    38     virtual ~XMLSAXParser();
       
    39 
       
    40     virtual bool parse();
       
    41     virtual bool parse(const std::string&);
       
    42 
       
    43     T& getDocumentHandler() { return docHandler; }
       
    44 	XMLNode& getRootElement() { return docHandler.getRootElement(); }
       
    45 	
       
    46 	virtual void doValidation(bool v = true) { validate = v; }
       
    47 	virtual void doSchema(bool s = true) { schema = s; }
       
    48 	virtual void fullSchemaChecking(bool s = true) { schemaChecking = s; }
       
    49 	virtual void namespaces(bool n = true) { names = n; }
       
    50 	
       
    51  private:
       
    52 	SAXParser* 	parser;
       
    53 	std::string	xmlFile;
       
    54 
       
    55 	T		docHandler;
       
    56 	U		errorHandler;
       
    57 
       
    58 	bool validate;
       
    59 	bool schema;
       
    60 	bool schemaChecking;
       
    61 	bool names;
       
    62 	
       
    63 	bool getValidation() { return validate; }
       
    64 	bool getSchema() { return schema; }
       
    65 	bool getSchemaChecking() { return schemaChecking; }
       
    66 	bool getNamespaces() { return names; }
       
    67 	
       
    68 	void init();
       
    69 	void deinit();
       
    70 };
       
    71 
       
    72 template<typename T, typename U>
       
    73 XMLSAXParser<T, U>::XMLSAXParser()
       
    74 {
       
    75 	parser = NULL;
       
    76 	this->init();
       
    77 }
       
    78 
       
    79 template<typename T, typename U>
       
    80 XMLSAXParser<T, U>::~XMLSAXParser()
       
    81 {
       
    82 	this->deinit();
       
    83 }
       
    84 
       
    85 template<typename T, typename U>
       
    86 void XMLSAXParser<T, U>::init()
       
    87 {
       
    88 	this->doValidation();
       
    89 	this->doSchema();
       
    90 	this->fullSchemaChecking();
       
    91 	this->namespaces();
       
    92 	
       
    93 	try
       
    94 	{
       
    95 		XMLPlatformUtils::Initialize();
       
    96 	}
       
    97 	catch (const XMLException& toCatch)
       
    98 	{
       
    99 		cerr << "Error during initialization! Message:\n"
       
   100 			 << XMLString::transcode(toCatch.getMessage()) << endl;
       
   101 	}
       
   102 }
       
   103 
       
   104 template<typename T, typename U>
       
   105 void XMLSAXParser<T, U>::deinit()
       
   106 {
       
   107 	//  Delete the parser
       
   108 	if(parser)
       
   109 		delete parser;
       
   110 
       
   111 	// And call the termination method
       
   112 	XMLPlatformUtils::Terminate();
       
   113 }
       
   114 
       
   115 template<typename T, typename U>
       
   116 bool XMLSAXParser<T, U>::parse()
       
   117 {
       
   118 	return this->parse(xmlFile);
       
   119 }
       
   120 
       
   121 template<typename T, typename U>
       
   122 bool XMLSAXParser<T, U>::parse(const std::string& file)
       
   123 {
       
   124 	xmlFile = file;
       
   125 	bool errorOccurred = false;
       
   126 
       
   127 	//  Create a SAX parser object. Then, according to what we were told on
       
   128 	//  the command line, set it to validate or not.
       
   129 	parser = new SAXParser;
       
   130 	if(!parser)
       
   131 		return false;
       
   132 
       
   133 	parser->setDoValidation(this->getValidation());
       
   134 	parser->setDoNamespaces(this->getNamespaces());
       
   135 	parser->setDoSchema(this->getSchema());
       
   136 	parser->setValidationSchemaFullChecking(this->getSchemaChecking());
       
   137 
       
   138 	parser->setDocumentHandler(&docHandler);
       
   139 	parser->setErrorHandler(&errorHandler);
       
   140 
       
   141 	try
       
   142 	{
       
   143 		parser->parse(file.c_str());
       
   144 	}
       
   145 	catch (const XMLException& e)
       
   146 	{
       
   147 		cerr << "\nError during parsing: '" << file << "'\n"
       
   148 			<< "Exception message is:  \n"
       
   149 			<< XMLString::transcode(e.getMessage()) << "\n" << endl;
       
   150 		errorOccurred = true;
       
   151 	}
       
   152 	catch (...)
       
   153 	{
       
   154 		cerr << "\nUnexpected exception during parsing: '" << file << "'\n";
       
   155 		errorOccurred = true;
       
   156 	}
       
   157 
       
   158 	return (!errorOccurred);
       
   159 }
       
   160 
       
   161 #endif // XMLSAXPARSER