diff -r 000000000000 -r 638b9c697799 apicompatanamdw/compatanalysercmd/libraryanalyser/inc/xmlsaxparser.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/apicompatanamdw/compatanalysercmd/libraryanalyser/inc/xmlsaxparser.hpp Tue Jan 12 14:52:39 2010 +0530 @@ -0,0 +1,161 @@ +/* +* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). +* All rights reserved. +* This component and the accompanying materials are made available +* under the terms of "Eclipse Public License v1.0" +* which accompanies this distribution, and is available +* at the URL "http://www.eclipse.org/legal/epl-v10.html". +* +* Initial Contributors: +* Nokia Corporation - initial contribution. +* +* Contributors: +* +* Description: +* +*/ +// XML Parsing includes +#include +#include + +#include +#include + +#if !defined XMLSAXPARSER + #define XMLSAXPARSER + +#include "XMLParserInterface.h" + +class XMLNode; + +// T - Document Handler +// U - ErrorHandler +template +class XMLSAXParser : public XMLParserInterface +{ + public: + XMLSAXParser(); + virtual ~XMLSAXParser(); + + virtual bool parse(); + virtual bool parse(const std::string&); + + T& getDocumentHandler() { return docHandler; } + XMLNode& getRootElement() { return docHandler.getRootElement(); } + + virtual void doValidation(bool v = true) { validate = v; } + virtual void doSchema(bool s = true) { schema = s; } + virtual void fullSchemaChecking(bool s = true) { schemaChecking = s; } + virtual void namespaces(bool n = true) { names = n; } + + private: + SAXParser* parser; + std::string xmlFile; + + T docHandler; + U errorHandler; + + bool validate; + bool schema; + bool schemaChecking; + bool names; + + bool getValidation() { return validate; } + bool getSchema() { return schema; } + bool getSchemaChecking() { return schemaChecking; } + bool getNamespaces() { return names; } + + void init(); + void deinit(); +}; + +template +XMLSAXParser::XMLSAXParser() +{ + parser = NULL; + this->init(); +} + +template +XMLSAXParser::~XMLSAXParser() +{ + this->deinit(); +} + +template +void XMLSAXParser::init() +{ + this->doValidation(); + this->doSchema(); + this->fullSchemaChecking(); + this->namespaces(); + + try + { + XMLPlatformUtils::Initialize(); + } + catch (const XMLException& toCatch) + { + cerr << "Error during initialization! Message:\n" + << XMLString::transcode(toCatch.getMessage()) << endl; + } +} + +template +void XMLSAXParser::deinit() +{ + // Delete the parser + if(parser) + delete parser; + + // And call the termination method + XMLPlatformUtils::Terminate(); +} + +template +bool XMLSAXParser::parse() +{ + return this->parse(xmlFile); +} + +template +bool XMLSAXParser::parse(const std::string& file) +{ + xmlFile = file; + bool errorOccurred = false; + + // Create a SAX parser object. Then, according to what we were told on + // the command line, set it to validate or not. + parser = new SAXParser; + if(!parser) + return false; + + parser->setDoValidation(this->getValidation()); + parser->setDoNamespaces(this->getNamespaces()); + parser->setDoSchema(this->getSchema()); + parser->setValidationSchemaFullChecking(this->getSchemaChecking()); + + parser->setDocumentHandler(&docHandler); + parser->setErrorHandler(&errorHandler); + + try + { + parser->parse(file.c_str()); + } + catch (const XMLException& e) + { + cerr << "\nError during parsing: '" << file << "'\n" + << "Exception message is: \n" + << XMLString::transcode(e.getMessage()) << "\n" << endl; + errorOccurred = true; + } + catch (...) + { + cerr << "\nUnexpected exception during parsing: '" << file << "'\n"; + errorOccurred = true; + } + + return (!errorOccurred); +} + +#endif // XMLSAXPARSER